@@ -9,6 +9,12 @@ const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
99const CopyPlugin = require ( 'copy-webpack-plugin' ) ;
1010const TerserPlugin = require ( 'terser-webpack-plugin' ) ;
1111
12+ const entry = {
13+ 'hello-editor' : path . resolve ( __dirname , './assets/dev/js/editor/hello-editor.js' ) ,
14+ 'hello-frontend' : path . resolve ( __dirname , './assets/dev/js/frontend/hello-frontend.js' ) ,
15+ 'hello-admin' : path . resolve ( __dirname , './assets/dev/js/admin/hello-admin.js' ) ,
16+ } ;
17+
1218const copyPluginConfig = new CopyPlugin ( {
1319 patterns : [
1420 {
@@ -19,6 +25,8 @@ const copyPluginConfig = new CopyPlugin( {
1925 info : { minimized : true } ,
2026 globOptions : {
2127 ignore : [
28+ // ignore minified php files
29+ ...Object . keys ( entry ) . map ( ( key ) => `**/assets/js/${ key } .min.asset.php` ) ,
2230 '**.zip' ,
2331 '**.css' ,
2432 '**/karma.conf.js' ,
@@ -68,36 +76,38 @@ const moduleRules = {
6876 ] ,
6977} ;
7078
71- const entry = {
72- 'hello-admin' : path . resolve ( __dirname , './assets/dev/js/admin/hello-admin.js' ) ,
73- 'hello-editor' : path . resolve ( __dirname , './assets/dev/js/editor/hello-editor.js' ) ,
74- 'hello-frontend' : path . resolve ( __dirname , './assets/dev/js/frontend/hello-frontend.js' ) ,
75- } ;
76-
77- const webpackConfig = {
79+ const commonConfig = {
7880 ...defaultConfig ,
7981 target : 'web' ,
8082 context : __dirname ,
8183 module : moduleRules ,
8284 entry,
83- mode : 'development' ,
8485 output : {
85- path : path . resolve ( __dirname , './build/assets/js' ) ,
86+ ...defaultConfig . output ,
87+ path : path . resolve ( __dirname , './assets/js' ) ,
8688 filename : '[name].js' ,
87- devtoolModuleFilenameTemplate : './[resource]' ,
8889 } ,
8990} ;
9091
91- const webpackProductionConfig = {
92- ...defaultConfig ,
93- target : 'web' ,
94- context : __dirname ,
95- module : moduleRules ,
92+ const webpackConfig = {
93+ ...commonConfig ,
94+ mode : 'development' ,
95+ output : {
96+ ...commonConfig . output ,
97+ devtoolModuleFilenameTemplate : './[resource]' ,
98+ } ,
9699 entry : {
97100 ...entry ,
98101 } ,
102+ devtool : 'source-map' ,
103+ } ;
104+
105+ const webpackProductionConfig = {
106+ ...commonConfig ,
107+ mode : 'production' ,
99108 optimization : {
100- minimize : true ,
109+ ...defaultConfig . optimization || { } ,
110+ minimize : false ,
101111 minimizer : [
102112 new TerserPlugin ( {
103113 terserOptions : {
@@ -107,46 +117,33 @@ const webpackProductionConfig = {
107117 } ) ,
108118 ] ,
109119 } ,
110- mode : 'production' ,
111- output : {
112- path : path . resolve ( __dirname , './build/assets/js' ) ,
113- filename : '[name].js' ,
114- } ,
115120 performance : { hints : false } ,
116121} ;
117122
118123// Add minified entry points
119124Object . entries ( webpackProductionConfig . entry ) . forEach ( ( [ wpEntry , value ] ) => {
120125 webpackProductionConfig . entry [ wpEntry + '.min' ] = value ;
121-
122- delete webpackProductionConfig . entry [ wpEntry ] ;
123126} ) ;
124127
125- const localOutputPath = { ...webpackProductionConfig . output , path : path . resolve ( __dirname , './assets/js' ) } ;
128+ // Override copyPluginConfig
129+ // we first remove the one supplied by @wordpress /scripts
130+ webpackProductionConfig . plugins = webpackProductionConfig . plugins . filter ( ( plugin ) => {
131+ return plugin . constructor . name !== 'CopyPlugin' ;
132+ } ) ;
133+ // then we add our own
134+ webpackProductionConfig . plugins = [ copyPluginConfig , ...defaultConfig . plugins ] ;
126135
127136module . exports = ( env ) => {
128- if ( env . developmentLocalWithWatch ) {
129- return { ...webpackConfig , watch : true , devtool : 'source-map' , output : localOutputPath } ;
130- }
131-
132- if ( env . productionLocalWithWatch ) {
133- return { ...webpackProductionConfig , watch : true , devtool : 'source-map' , output : localOutputPath } ;
134- }
135-
136- if ( env . productionLocal ) {
137- return { ...webpackProductionConfig , devtool : 'source-map' , output : localOutputPath } ;
138- }
139-
140137 if ( env . developmentLocal ) {
141- return { ...webpackConfig , devtool : 'source-map' , output : localOutputPath } ;
138+ return { ...webpackConfig , watch : true } ;
142139 }
143140
144141 if ( env . production ) {
145142 return webpackProductionConfig ;
146143 }
147144
148145 if ( env . development ) {
149- return { ... webpackConfig , plugins : [ copyPluginConfig , ... defaultConfig . plugins ] , output : localOutputPath } ;
146+ return webpackConfig ;
150147 }
151148
152149 throw new Error ( 'missing or invalid --env= development/production/developmentWithWatch/productionWithWatch' ) ;
0 commit comments