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 ? j s $ / ,
20+ exclude : / ( n o d e _ m o d u l e s | b o w e r _ c o m p o n e n t s ) / ,
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 : / \. p e g j s $ / ,
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+ } )
2072function 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 ? j s $ / ,
40- exclude : / ( n o d e _ m o d u l e s | b o w e r _ c o m p o n e n t s ) / ,
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 : / \. p e g j s $ / ,
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 ================
9694if ( 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