|
| 1 | +/** |
| 2 | + * External dependencies |
| 3 | + */ |
| 4 | +const TerserPlugin = require( 'terser-webpack-plugin' ); |
| 5 | + |
| 6 | +/** |
| 7 | + * Internal dependencies |
| 8 | + */ |
| 9 | +const { baseDir } = require( './shared' ); |
| 10 | + |
| 11 | +/** |
| 12 | + * Webpack configuration for development scripts (React Refresh). |
| 13 | + * |
| 14 | + * These scripts enable hot module replacement for block development |
| 15 | + * when using `@wordpress/scripts` with the `--hot` flag. |
| 16 | + * |
| 17 | + * @param {Object} env Environment options. |
| 18 | + * @param {string} env.buildTarget Build target directory. |
| 19 | + * @param {boolean} env.watch Whether to watch for changes. |
| 20 | + * @return {Object} Webpack configuration object. |
| 21 | + */ |
| 22 | +module.exports = function( env = { buildTarget: 'src/', watch: false } ) { |
| 23 | + const buildTarget = env.buildTarget || 'src/'; |
| 24 | + |
| 25 | + const entry = { |
| 26 | + // React Refresh runtime - exposes ReactRefreshRuntime global. |
| 27 | + [ buildTarget + 'wp-includes/js/dist/development/react-refresh-runtime.js' ]: { |
| 28 | + import: 'react-refresh/runtime', |
| 29 | + library: { |
| 30 | + name: 'ReactRefreshRuntime', |
| 31 | + type: 'window', |
| 32 | + }, |
| 33 | + }, |
| 34 | + [ buildTarget + 'wp-includes/js/dist/development/react-refresh-runtime.min.js' ]: { |
| 35 | + import: 'react-refresh/runtime', |
| 36 | + library: { |
| 37 | + name: 'ReactRefreshRuntime', |
| 38 | + type: 'window', |
| 39 | + }, |
| 40 | + }, |
| 41 | + // React Refresh entry - injects runtime into global hook before React loads. |
| 42 | + [ buildTarget + 'wp-includes/js/dist/development/react-refresh-entry.js' ]: |
| 43 | + '@pmmmwh/react-refresh-webpack-plugin/client/ReactRefreshEntry.js', |
| 44 | + [ buildTarget + 'wp-includes/js/dist/development/react-refresh-entry.min.js' ]: |
| 45 | + '@pmmmwh/react-refresh-webpack-plugin/client/ReactRefreshEntry.js', |
| 46 | + }; |
| 47 | + |
| 48 | + return { |
| 49 | + target: 'browserslist', |
| 50 | + // Must use development mode to preserve process.env.NODE_ENV checks |
| 51 | + // in the source files. These scripts are only used during development. |
| 52 | + mode: 'development', |
| 53 | + devtool: false, |
| 54 | + cache: true, |
| 55 | + entry, |
| 56 | + output: { |
| 57 | + path: baseDir, |
| 58 | + filename: '[name]', |
| 59 | + }, |
| 60 | + optimization: { |
| 61 | + minimize: true, |
| 62 | + moduleIds: 'deterministic', |
| 63 | + minimizer: [ |
| 64 | + new TerserPlugin( { |
| 65 | + include: /\.min\.js$/, |
| 66 | + extractComments: false, |
| 67 | + } ), |
| 68 | + ], |
| 69 | + }, |
| 70 | + watch: env.watch, |
| 71 | + }; |
| 72 | +}; |
0 commit comments