Skip to content

Commit 02a043a

Browse files
committed
fix code
1 parent 361d6b4 commit 02a043a

File tree

7 files changed

+92
-90
lines changed

7 files changed

+92
-90
lines changed

.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
"airbnb-base",
99
"strict"
1010
],
11+
"globals": {
12+
"PARSER_NAME": true
13+
},
1114
"rules": {
1215
"comma-dangle": [
1316
2,

index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import Parser from './src/parser';
2-
import * as util from './src/util';
1+
import Parser from './src/parser'
2+
import * as util from './src/util'
33

44
export {
55
Parser,
66
util,
7-
};
7+
}
88

99
if (global && global.window) {
1010
global.window.NodeSQLParser = {
1111
Parser,
1212
util,
13-
};
13+
}
1414
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"start": "webpack --config webpack.config.js",
99
"build": "webpack --config webpack.config.js --prod",
1010
"test": "mochapack \"test/**/*.spec.js\"",
11-
"prepublishOnly": "npm run build",
11+
"prepublishOnly": "npm run lint && npm run build",
1212
"lint": "eslint src",
1313
"coverLocal": "cross-env NODE_ENV=coverage nyc --reporter=lcov --reporter=text npm run test",
1414
"cover:run": "cross-env NODE_ENV=coverage nyc --reporter=text-lcov npm run test",
@@ -33,6 +33,7 @@
3333
],
3434
"author": "taozhi8833998 <[email protected]>",
3535
"files": [
36+
"ast/",
3637
"index.js",
3738
"lib/",
3839
"index.d.ts",

src/parser.all.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ export default {
1414
mariadb,
1515
postgresql,
1616
transactsql,
17-
}
17+
}

src/parser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import parsers from './parser.all';
1+
import parsers from './parser.all'
22
import astToSQL from './sql'
33
import { DEFAULT_OPT, setParserOpt } from './util'
44

@@ -16,7 +16,7 @@ class Parser {
1616
parse(sql, opt = DEFAULT_OPT) {
1717
const { database = (PARSER_NAME || 'mysql') } = opt
1818
setParserOpt(opt)
19-
const typeCase = database.toLowerCase();
19+
const typeCase = database.toLowerCase()
2020
if (parsers[typeCase]) return parsers[typeCase](sql.trim())
2121
throw new Error(`${database} is not supported currently`)
2222
}

src/parser.single.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { parse } from '../pegjs/mysql.pegjs'
22

33
export default {
4-
[PARSER_NAME]: parse,
4+
[PARSER_NAME] : parse,
55
}

webpack.config.js

Lines changed: 79 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,98 @@
1-
const fs = require('fs');
2-
const webpack = require('webpack');
3-
const path = require('path');
4-
const nodeExternals = require('webpack-node-externals');
5-
const CopyPlugin = require('copy-webpack-plugin');
1+
const CopyPlugin = require('copy-webpack-plugin')
2+
const fs = require('fs')
3+
const path = require('path')
4+
const webpack = require('webpack')
5+
const nodeExternals = require('webpack-node-externals')
66

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

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

14-
if (isProd) {
15-
// generate ast types
16-
17-
require('./typegen');
16+
const moduleCfg = {
17+
rules: [
18+
{
19+
test: /\.m?js$/,
20+
exclude: /(node_modules|bower_components)/,
21+
use: isCoverage
22+
? {
23+
loader: 'istanbul-instrumenter-loader',
24+
options: { esModules: true },
25+
}
26+
: {
27+
loader: 'babel-loader',
28+
options: {
29+
presets: ['@babel/preset-env']
30+
}
31+
},
32+
enforce: 'post',
33+
},
34+
{
35+
test: /\.pegjs$/,
36+
loader: 'pegjs-loader'
37+
}
38+
],
1839
}
1940

41+
const getPlugins = (parserName, target, plugins) => [
42+
new webpack.DefinePlugin({
43+
PARSER_NAME: parserName ? JSON.stringify(parserName) : 'null',
44+
}),
45+
...(plugins || []),
46+
...(isProd
47+
? [
48+
new CopyPlugin({
49+
patterns: [
50+
'LICENSE',
51+
'lib',
52+
'README.md',
53+
'package.json',
54+
'types.d.ts',
55+
'ast/**',
56+
{
57+
from: 'index.d.ts',
58+
to: (parserName || 'index') + (target === 'web' ? '.umd' : '') + '.d.ts',
59+
}
60+
],
61+
}),
62+
] : [
63+
])
64+
]
65+
const getOutput = (target) => ({
66+
path: outputPath,
67+
library: '',
68+
libraryTarget: target === 'web' ? 'umd' : 'commonjs',
69+
// this ensures that source maps are mapped to actual files (not "webpack:" uris)
70+
devtoolModuleFilenameTemplate: info => path.resolve(__dirname, info.resourcePath),
71+
})
2072
function buildConfig(parserName, target, entry, plugins) {
21-
const watch = !isProd && !isTest && !isCoverage;
73+
const watch = !(isProd || isTest || isCoverage)
2274
return {
23-
entry,
24-
watch,
25-
target,
2675
devtool: 'source-map',
27-
mode: isProd ? 'production' : 'development',
28-
node: {
29-
__dirname: false
30-
},
3176
externals: target == 'web' ? [] : [
3277
nodeExternals({
3378
whitelist: ['webpack/hot/poll?100'],
3479
}),
3580
],
36-
module: {
37-
rules: [
38-
{
39-
test: /\.m?js$/,
40-
exclude: /(node_modules|bower_components)/,
41-
use: isCoverage
42-
? {
43-
loader: 'istanbul-instrumenter-loader',
44-
options: { esModules: true },
45-
}
46-
: {
47-
loader: 'babel-loader',
48-
options: {
49-
presets: ['@babel/preset-env']
50-
}
51-
},
52-
enforce: 'post',
53-
// exclude: /node_modules|\.spec\.js$/,
54-
},
55-
{
56-
test: /\.pegjs$/,
57-
loader: 'pegjs-loader'
58-
}
59-
],
60-
},
61-
resolve: {
62-
extensions: ['.js', '.pegjs'],
63-
},
64-
plugins: [
65-
new webpack.DefinePlugin({
66-
PARSER_NAME: parserName ? JSON.stringify(parserName) : 'null',
67-
}),
68-
...(plugins || []),
69-
...(isProd
70-
? [
71-
new CopyPlugin({
72-
patterns: [
73-
'LICENSE',
74-
'README.md',
75-
'package.json',
76-
'types.d.ts',
77-
'ast/**',
78-
{ from: 'index.d.ts', to: (parserName || 'index') + (target === 'web' ? '.umd' : '') + '.d.ts', }
79-
],
80-
}),
81-
] : [
82-
])],
83-
output: {
84-
path: outputPath,
85-
library: '',
86-
libraryTarget: target === 'web' ? 'umd' : 'commonjs',
87-
// this ensures that source maps are mapped to actual files (not "webpack:" uris)
88-
devtoolModuleFilenameTemplate: info => path.resolve(__dirname, info.resourcePath),
89-
},
90-
};
81+
entry,
82+
watch,
83+
target,
84+
mode: isProd ? 'production' : 'development',
85+
node: { __dirname: false },
86+
module: moduleCfg,
87+
resolve: { extensions: ['.js', '.pegjs'] },
88+
plugins: getPlugins(parserName, target, plugins),
89+
output: getOutput(target),
90+
}
9191
}
9292

93-
94-
9593
// =========== PROD CONFIG ================
9694
if (isProd) {
97-
const config = module.exports = [];
95+
const config = module.exports = []
9896

9997
for (const target of ['web', 'node']) {
10098
config.push(
@@ -131,5 +129,5 @@ if (isProd) {
131129
// test bundle (HMR)
132130
: buildConfig(null, 'node', {
133131
'tests': ['webpack/hot/poll?100', './tests-index.js'],
134-
}, [new webpack.HotModuleReplacementPlugin()]);
132+
}, [new webpack.HotModuleReplacementPlugin()])
135133
}

0 commit comments

Comments
 (0)