-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebpack.config.js
More file actions
58 lines (53 loc) · 1.74 KB
/
Copy pathwebpack.config.js
File metadata and controls
58 lines (53 loc) · 1.74 KB
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
const defaultConfig = require('@wordpress/scripts/config/webpack.config');
const path = require('path');
/**
* Custom webpack configuration that extends @wordpress/scripts
* Adds namespace transformation loader to process ES modules in memory
*
* Usage in your project's webpack.config.js:
* const grabDepsConfig = require('@kunoichi/grab-deps/webpack.config.js');
* module.exports = grabDepsConfig;
*
* Or to extend existing configuration:
* const grabDepsConfig = require('@kunoichi/grab-deps/webpack.config.js');
* module.exports = { ...grabDepsConfig, ...yourCustomConfig };
*/
/**
* Create grab-deps webpack configuration
* @param {object} baseConfig - Base webpack configuration (defaults to @wordpress/scripts config)
* @returns {object|object[]} - Modified webpack configuration
*/
function createGrabDepsConfig(baseConfig = defaultConfig) {
// If the config is an array (multiple configurations), we need to handle both
const configs = Array.isArray(baseConfig) ? baseConfig : [baseConfig];
const modifiedConfigs = configs.map(config => ({
...config,
resolve: {
...config.resolve,
alias: {
...config.resolve?.alias,
},
},
module: {
...config.module,
rules: [
// Add our custom loader before other loaders (including minification)
{
test: /\.m?(j|t)sx?$/,
exclude: /node_modules/,
use: [
{
loader: path.resolve(__dirname, 'lib/webpack-loaders/namespace-transform-loader.js'),
},
],
enforce: 'pre', // Run before other loaders
},
// Include all existing rules
...config.module.rules,
],
},
}));
return modifiedConfigs.length === 1 ? modifiedConfigs[0] : modifiedConfigs;
}
// Export the default configuration
module.exports = createGrabDepsConfig();