|
| 1 | +/** |
| 2 | + * External dependencies |
| 3 | + */ |
| 4 | +const { DefinePlugin } = require( 'webpack' ); |
| 5 | +const 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' ); |
| 11 | + |
| 12 | +/** |
| 13 | + * WordPress dependencies |
| 14 | + */ |
| 15 | +const DependencyExtractionPlugin = require( '@wordpress/dependency-extraction-webpack-plugin' ); |
| 16 | + |
| 17 | +const baseDir = join( __dirname, '../../' ); |
| 18 | + |
| 19 | +module.exports = function( env = { environment: 'production', watch: false, buildTarget: false } ) { |
| 20 | + const mode = env.environment; |
| 21 | + const suffix = mode === 'production' ? '.min' : ''; |
| 22 | + let buildTarget = env.buildTarget ? env.buildTarget : ( mode === 'production' ? 'build' : 'src' ); |
| 23 | + buildTarget = buildTarget + '/wp-includes'; |
| 24 | + |
| 25 | + const dynamicBlockFolders = [ |
| 26 | + 'archives', |
| 27 | + 'block', |
| 28 | + 'calendar', |
| 29 | + 'categories', |
| 30 | + 'file', |
| 31 | + 'latest-comments', |
| 32 | + 'latest-posts', |
| 33 | + 'loginout', |
| 34 | + 'page-list', |
| 35 | + 'post-content', |
| 36 | + 'post-date', |
| 37 | + 'post-excerpt', |
| 38 | + 'post-featured-image', |
| 39 | + 'post-terms', |
| 40 | + 'post-title', |
| 41 | + 'post-template', |
| 42 | + 'query', |
| 43 | + 'query-pagination', |
| 44 | + 'query-pagination-next', |
| 45 | + 'query-pagination-numbers', |
| 46 | + 'query-pagination-previous', |
| 47 | + 'query-title', |
| 48 | + 'rss', |
| 49 | + 'search', |
| 50 | + 'shortcode', |
| 51 | + 'site-logo', |
| 52 | + 'site-tagline', |
| 53 | + 'site-title', |
| 54 | + 'social-link', |
| 55 | + 'tag-cloud', |
| 56 | + ]; |
| 57 | + const blockFolders = [ |
| 58 | + 'audio', |
| 59 | + 'button', |
| 60 | + 'buttons', |
| 61 | + 'code', |
| 62 | + 'column', |
| 63 | + 'columns', |
| 64 | + 'cover', |
| 65 | + 'embed', |
| 66 | + 'freeform', |
| 67 | + 'gallery', |
| 68 | + 'group', |
| 69 | + 'heading', |
| 70 | + 'html', |
| 71 | + 'image', |
| 72 | + 'list', |
| 73 | + 'media-text', |
| 74 | + 'missing', |
| 75 | + 'more', |
| 76 | + 'nextpage', |
| 77 | + 'paragraph', |
| 78 | + 'preformatted', |
| 79 | + 'pullquote', |
| 80 | + 'quote', |
| 81 | + 'separator', |
| 82 | + 'social-links', |
| 83 | + 'spacer', |
| 84 | + 'table', |
| 85 | + 'text-columns', |
| 86 | + 'verse', |
| 87 | + 'video', |
| 88 | + ...dynamicBlockFolders, |
| 89 | + ]; |
| 90 | + const blockPHPFiles = { |
| 91 | + 'widgets/src/blocks/legacy-widget/index.php': 'wp-includes/blocks/legacy-widget.php', |
| 92 | + ...dynamicBlockFolders.reduce( ( files, blockName ) => { |
| 93 | + files[ `block-library/src/${ blockName }/index.php` ] = `wp-includes/blocks/${ blockName }.php`; |
| 94 | + return files; |
| 95 | + } , {} ), |
| 96 | + }; |
| 97 | + const blockMetadataFiles = { |
| 98 | + 'widgets/src/blocks/legacy-widget/block.json': 'wp-includes/blocks/legacy-widget/block.json', |
| 99 | + ...blockFolders.reduce( ( files, blockName ) => { |
| 100 | + files[ `block-library/src/${ blockName }/block.json` ] = `wp-includes/blocks/${ blockName }/block.json`; |
| 101 | + return files; |
| 102 | + } , {} ), |
| 103 | + }; |
| 104 | + |
| 105 | + const blockPHPCopies = Object.keys( blockPHPFiles ).map( ( filename ) => ( { |
| 106 | + from: join( baseDir, `node_modules/@wordpress/${ filename }` ), |
| 107 | + to: join( baseDir, `src/${ blockPHPFiles[ filename ] }` ), |
| 108 | + } ) ); |
| 109 | + |
| 110 | + const blockMetadataCopies = Object.keys( blockMetadataFiles ).map( ( filename ) => ( { |
| 111 | + from: join( baseDir, `node_modules/@wordpress/${ filename }` ), |
| 112 | + to: join( baseDir, `src/${ blockMetadataFiles[ filename ] }` ), |
| 113 | + } ) ); |
| 114 | + |
| 115 | + const blockStylesheetCopies = blockFolders.map( ( blockName ) => ( { |
| 116 | + from: join( baseDir, `node_modules/@wordpress/block-library/build-style/${ blockName }/*.css` ), |
| 117 | + to: join( baseDir, `${ buildTarget }/blocks/${ blockName }/` ), |
| 118 | + flatten: true, |
| 119 | + transform: ( content ) => { |
| 120 | + if ( mode === 'production' ) { |
| 121 | + return postcss( [ |
| 122 | + require( 'cssnano' )( { |
| 123 | + preset: 'default', |
| 124 | + } ), |
| 125 | + ] ) |
| 126 | + .process( content, { from: 'src/app.css', to: 'dest/app.css' } ) |
| 127 | + .then( ( result ) => result.css ); |
| 128 | + } |
| 129 | + |
| 130 | + return content; |
| 131 | + }, |
| 132 | + transformPath: ( targetPath, sourcePath ) => { |
| 133 | + if ( mode === 'production' ) { |
| 134 | + return targetPath.replace( /\.css$/, '.min.css' ); |
| 135 | + } |
| 136 | + |
| 137 | + return targetPath; |
| 138 | + } |
| 139 | + } ) ); |
| 140 | + |
| 141 | + const config = { |
| 142 | + mode, |
| 143 | + entry: { |
| 144 | + 'file/view': join( baseDir, `node_modules/@wordpress/block-library/build-module/file/view` ), |
| 145 | + }, |
| 146 | + output: { |
| 147 | + devtoolNamespace: 'wp', |
| 148 | + filename: `[name]${ suffix }.js`, |
| 149 | + path: join( baseDir, `${ buildTarget }/blocks` ), |
| 150 | + }, |
| 151 | + resolve: { |
| 152 | + modules: [ |
| 153 | + baseDir, |
| 154 | + 'node_modules', |
| 155 | + ], |
| 156 | + alias: { |
| 157 | + 'lodash-es': 'lodash', |
| 158 | + }, |
| 159 | + }, |
| 160 | + module: { |
| 161 | + rules: [ |
| 162 | + { |
| 163 | + test: /\.js$/, |
| 164 | + use: [ 'source-map-loader' ], |
| 165 | + enforce: 'pre', |
| 166 | + }, |
| 167 | + ], |
| 168 | + }, |
| 169 | + optimization: { |
| 170 | + moduleIds: mode === 'production' ? 'hashed' : 'named', |
| 171 | + }, |
| 172 | + plugins: [ |
| 173 | + new DefinePlugin( { |
| 174 | + // Inject the `GUTENBERG_PHASE` global, used for feature flagging. |
| 175 | + 'process.env.GUTENBERG_PHASE': 1, |
| 176 | + 'process.env.FORCE_REDUCED_MOTION': JSON.stringify( |
| 177 | + process.env.FORCE_REDUCED_MOTION |
| 178 | + ), |
| 179 | + } ), |
| 180 | + new DependencyExtractionPlugin( { |
| 181 | + injectPolyfill: true, |
| 182 | + } ), |
| 183 | + new CopyWebpackPlugin( |
| 184 | + [ |
| 185 | + ...blockPHPCopies, |
| 186 | + ...blockMetadataCopies, |
| 187 | + ...blockStylesheetCopies, |
| 188 | + ], |
| 189 | + ), |
| 190 | + ], |
| 191 | + stats: { |
| 192 | + children: false, |
| 193 | + }, |
| 194 | + |
| 195 | + watch: env.watch, |
| 196 | + }; |
| 197 | + |
| 198 | + if ( config.mode !== 'production' ) { |
| 199 | + config.devtool = process.env.SOURCEMAP || 'source-map'; |
| 200 | + } |
| 201 | + |
| 202 | + if ( mode === 'development' && env.buildTarget === 'build/' ) { |
| 203 | + delete config.devtool; |
| 204 | + config.mode = 'production'; |
| 205 | + config.optimization = { |
| 206 | + minimize: false, |
| 207 | + moduleIds: 'hashed', |
| 208 | + }; |
| 209 | + } |
| 210 | + |
| 211 | + return config; |
| 212 | +}; |
0 commit comments