Skip to content

Commit

Permalink
fix code
Browse files Browse the repository at this point in the history
  • Loading branch information
taozhi8833998 committed Jul 21, 2020
1 parent 361d6b4 commit 02a043a
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 90 deletions.
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
"airbnb-base",
"strict"
],
"globals": {
"PARSER_NAME": true
},
"rules": {
"comma-dangle": [
2,
Expand Down
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import Parser from './src/parser';
import * as util from './src/util';
import Parser from './src/parser'
import * as util from './src/util'

export {
Parser,
util,
};
}

if (global && global.window) {
global.window.NodeSQLParser = {
Parser,
util,
};
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"start": "webpack --config webpack.config.js",
"build": "webpack --config webpack.config.js --prod",
"test": "mochapack \"test/**/*.spec.js\"",
"prepublishOnly": "npm run build",
"prepublishOnly": "npm run lint && npm run build",
"lint": "eslint src",
"coverLocal": "cross-env NODE_ENV=coverage nyc --reporter=lcov --reporter=text npm run test",
"cover:run": "cross-env NODE_ENV=coverage nyc --reporter=text-lcov npm run test",
Expand All @@ -33,6 +33,7 @@
],
"author": "taozhi8833998 <[email protected]>",
"files": [
"ast/",
"index.js",
"lib/",
"index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/parser.all.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ export default {
mariadb,
postgresql,
transactsql,
}
}
4 changes: 2 additions & 2 deletions src/parser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import parsers from './parser.all';
import parsers from './parser.all'
import astToSQL from './sql'
import { DEFAULT_OPT, setParserOpt } from './util'

Expand All @@ -16,7 +16,7 @@ class Parser {
parse(sql, opt = DEFAULT_OPT) {
const { database = (PARSER_NAME || 'mysql') } = opt
setParserOpt(opt)
const typeCase = database.toLowerCase();
const typeCase = database.toLowerCase()
if (parsers[typeCase]) return parsers[typeCase](sql.trim())
throw new Error(`${database} is not supported currently`)
}
Expand Down
2 changes: 1 addition & 1 deletion src/parser.single.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parse } from '../pegjs/mysql.pegjs'

export default {
[PARSER_NAME]: parse,
[PARSER_NAME] : parse,
}
160 changes: 79 additions & 81 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,100 +1,98 @@
const fs = require('fs');
const webpack = require('webpack');
const path = require('path');
const nodeExternals = require('webpack-node-externals');
const CopyPlugin = require('copy-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin')
const fs = require('fs')
const path = require('path')
const webpack = require('webpack')
const nodeExternals = require('webpack-node-externals')

const isCoverage = process.env.NODE_ENV === 'coverage'
const isProd = process.argv.includes('--prod')
const isTest = isCoverage || process.argv.includes('--test')
const subDir = isProd ? 'output/prod' : isTest ? 'output/test' : 'output/dev'
const outputPath = path.join(__dirname, subDir)
require('rimraf').sync(outputPath)

var isCoverage = process.env.NODE_ENV === 'coverage';
const isProd = process.argv.includes('--prod');
const isTest = isCoverage || process.argv.includes('--test');
const outputPath = path.join(__dirname, isProd ? 'output/prod' : isTest ? 'output/test' : 'output/dev');
require('rimraf').sync(outputPath);
if (isProd) require('./typegen')

if (isProd) {
// generate ast types

require('./typegen');
const moduleCfg = {
rules: [
{
test: /\.m?js$/,
exclude: /(node_modules|bower_components)/,
use: isCoverage
? {
loader: 'istanbul-instrumenter-loader',
options: { esModules: true },
}
: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
},
enforce: 'post',
},
{
test: /\.pegjs$/,
loader: 'pegjs-loader'
}
],
}

const getPlugins = (parserName, target, plugins) => [
new webpack.DefinePlugin({
PARSER_NAME: parserName ? JSON.stringify(parserName) : 'null',
}),
...(plugins || []),
...(isProd
? [
new CopyPlugin({
patterns: [
'LICENSE',
'lib',
'README.md',
'package.json',
'types.d.ts',
'ast/**',
{
from: 'index.d.ts',
to: (parserName || 'index') + (target === 'web' ? '.umd' : '') + '.d.ts',
}
],
}),
] : [
])
]
const getOutput = (target) => ({
path: outputPath,
library: '',
libraryTarget: target === 'web' ? 'umd' : 'commonjs',
// this ensures that source maps are mapped to actual files (not "webpack:" uris)
devtoolModuleFilenameTemplate: info => path.resolve(__dirname, info.resourcePath),
})
function buildConfig(parserName, target, entry, plugins) {
const watch = !isProd && !isTest && !isCoverage;
const watch = !(isProd || isTest || isCoverage)
return {
entry,
watch,
target,
devtool: 'source-map',
mode: isProd ? 'production' : 'development',
node: {
__dirname: false
},
externals: target == 'web' ? [] : [
nodeExternals({
whitelist: ['webpack/hot/poll?100'],
}),
],
module: {
rules: [
{
test: /\.m?js$/,
exclude: /(node_modules|bower_components)/,
use: isCoverage
? {
loader: 'istanbul-instrumenter-loader',
options: { esModules: true },
}
: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
},
enforce: 'post',
// exclude: /node_modules|\.spec\.js$/,
},
{
test: /\.pegjs$/,
loader: 'pegjs-loader'
}
],
},
resolve: {
extensions: ['.js', '.pegjs'],
},
plugins: [
new webpack.DefinePlugin({
PARSER_NAME: parserName ? JSON.stringify(parserName) : 'null',
}),
...(plugins || []),
...(isProd
? [
new CopyPlugin({
patterns: [
'LICENSE',
'README.md',
'package.json',
'types.d.ts',
'ast/**',
{ from: 'index.d.ts', to: (parserName || 'index') + (target === 'web' ? '.umd' : '') + '.d.ts', }
],
}),
] : [
])],
output: {
path: outputPath,
library: '',
libraryTarget: target === 'web' ? 'umd' : 'commonjs',
// this ensures that source maps are mapped to actual files (not "webpack:" uris)
devtoolModuleFilenameTemplate: info => path.resolve(__dirname, info.resourcePath),
},
};
entry,
watch,
target,
mode: isProd ? 'production' : 'development',
node: { __dirname: false },
module: moduleCfg,
resolve: { extensions: ['.js', '.pegjs'] },
plugins: getPlugins(parserName, target, plugins),
output: getOutput(target),
}
}



// =========== PROD CONFIG ================
if (isProd) {
const config = module.exports = [];
const config = module.exports = []

for (const target of ['web', 'node']) {
config.push(
Expand Down Expand Up @@ -131,5 +129,5 @@ if (isProd) {
// test bundle (HMR)
: buildConfig(null, 'node', {
'tests': ['webpack/hot/poll?100', './tests-index.js'],
}, [new webpack.HotModuleReplacementPlugin()]);
}, [new webpack.HotModuleReplacementPlugin()])
}

0 comments on commit 02a043a

Please sign in to comment.