summaryrefslogtreecommitdiffstatshomepage
path: root/tools/webpack/development.js
blob: 1b871ee665e2ab687ba71294c1d116697a0bff71 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
 * External dependencies
 */
const { join } = require( 'path' );

/**
 * WordPress dependencies
 */
const DependencyExtractionWebpackPlugin = require( '@wordpress/dependency-extraction-webpack-plugin' );

const baseDir = join( __dirname, '../../' );

module.exports = function( env = { environment: 'production', buildTarget: false } ) {
	const mode = env.environment;
	const suffix = mode === 'production' ? '.min' : '';
	let buildTarget = env.buildTarget ? env.buildTarget : ( mode === 'production' ? 'build' : 'src' );
	buildTarget = buildTarget  + '/wp-includes';

	const sharedConfig = {
		mode: 'development',
		target: 'browserslist',
		output: {
			filename: `[name]${ suffix }.js`,
			path: join( baseDir, `${ buildTarget }/js/dist/development` ),
		},
	};

	// See https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/main/docs/TROUBLESHOOTING.md#externalising-react.
	return [
		{
			...sharedConfig,
			name: 'react-refresh-entry',
			entry: {
				'react-refresh-entry':
				'@pmmmwh/react-refresh-webpack-plugin/client/ReactRefreshEntry.js',
			},
			plugins: [ new DependencyExtractionWebpackPlugin( {
				outputFilename: '../../../assets/script-loader-[name].php',
			} ) ],
		},
		{
			...sharedConfig,
			name: 'react-refresh-runtime',
			entry: {
				'react-refresh-runtime': {
					import: 'react-refresh/runtime.js',
					library: {
						name: 'ReactRefreshRuntime',
						type: 'window',
					},
				},
			},
			plugins: [
				new DependencyExtractionWebpackPlugin( {
					useDefaults: false,
					outputFilename: '../../../assets/script-loader-[name].php'
				} ),
			],
		},
	];
};