33 */
44const { DefinePlugin } = require ( 'webpack' ) ;
55const CopyWebpackPlugin = require ( 'copy-webpack-plugin' ) ;
6- const postcss = require ( 'postcss' ) ;
7- const UglifyJS = require ( 'uglify-js' ) ;
8-
9- const { join, basename } = require ( 'path' ) ;
10- const { get } = require ( 'lodash' ) ;
6+ const { join } = require ( 'path' ) ;
117
128/**
139 * WordPress dependencies
1410 */
1511const DependencyExtractionPlugin = require ( '@wordpress/dependency-extraction-webpack-plugin' ) ;
1612
17- const baseDir = join ( __dirname , '../../' ) ;
13+ /**
14+ * Internal dependencies
15+ */
16+ const { stylesTransform, baseConfig, baseDir } = require ( './shared' ) ;
1817
1918module . exports = function ( env = { environment : 'production' , watch : false , buildTarget : false } ) {
2019 const mode = env . environment ;
@@ -125,32 +124,13 @@ module.exports = function( env = { environment: 'production', watch: false, buil
125124
126125 const blockStylesheetCopies = blockFolders . map ( ( blockName ) => ( {
127126 from : join ( baseDir , `node_modules/@wordpress/block-library/build-style/${ blockName } /*.css` ) ,
128- to : join ( baseDir , `${ buildTarget } /blocks/${ blockName } /` ) ,
129- flatten : true ,
130- transform : ( content ) => {
131- if ( mode === 'production' ) {
132- return postcss ( [
133- require ( 'cssnano' ) ( {
134- preset : 'default' ,
135- } ) ,
136- ] )
137- . process ( content , { from : 'src/app.css' , to : 'dest/app.css' } )
138- . then ( ( result ) => result . css ) ;
139- }
140-
141- return content ;
142- } ,
143- transformPath : ( targetPath , sourcePath ) => {
144- if ( mode === 'production' ) {
145- return targetPath . replace ( / \. c s s $ / , '.min.css' ) ;
146- }
147-
148- return targetPath ;
149- }
127+ to : join ( baseDir , `${ buildTarget } /blocks/${ blockName } /[name]${ suffix } .css` ) ,
128+ transform : stylesTransform ( mode ) ,
129+ noErrorOnMissing : true ,
150130 } ) ) ;
151131
152132 const config = {
153- mode ,
133+ ... baseConfig ( env ) ,
154134 entry : {
155135 'file/view' : join ( baseDir , `node_modules/@wordpress/block-library/build-module/file/view` ) ,
156136 'navigation/view' : join ( baseDir , `node_modules/@wordpress/block-library/build-module/navigation/view` ) ,
@@ -160,27 +140,6 @@ module.exports = function( env = { environment: 'production', watch: false, buil
160140 filename : `[name]${ suffix } .js` ,
161141 path : join ( baseDir , `${ buildTarget } /blocks` ) ,
162142 } ,
163- resolve : {
164- modules : [
165- baseDir ,
166- 'node_modules' ,
167- ] ,
168- alias : {
169- 'lodash-es' : 'lodash' ,
170- } ,
171- } ,
172- module : {
173- rules : [
174- {
175- test : / \. j s $ / ,
176- use : [ 'source-map-loader' ] ,
177- enforce : 'pre' ,
178- } ,
179- ] ,
180- } ,
181- optimization : {
182- moduleIds : mode === 'production' ? 'hashed' : 'named' ,
183- } ,
184143 plugins : [
185144 new DefinePlugin ( {
186145 // Inject the `GUTENBERG_PHASE` global, used for feature flagging.
@@ -192,33 +151,15 @@ module.exports = function( env = { environment: 'production', watch: false, buil
192151 new DependencyExtractionPlugin ( {
193152 injectPolyfill : false ,
194153 } ) ,
195- new CopyWebpackPlugin (
196- [
154+ new CopyWebpackPlugin ( {
155+ patterns : [
197156 ...blockPHPCopies ,
198157 ...blockMetadataCopies ,
199158 ...blockStylesheetCopies ,
200159 ] ,
201- ) ,
160+ } ) ,
202161 ] ,
203- stats : {
204- children : false ,
205- } ,
206-
207- watch : env . watch ,
208162 } ;
209163
210- if ( config . mode !== 'production' ) {
211- config . devtool = process . env . SOURCEMAP || 'source-map' ;
212- }
213-
214- if ( mode === 'development' && env . buildTarget === 'build/' ) {
215- delete config . devtool ;
216- config . mode = 'production' ;
217- config . optimization = {
218- minimize : false ,
219- moduleIds : 'hashed' ,
220- } ;
221- }
222-
223164 return config ;
224165} ;
0 commit comments