forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript-modules.js
More file actions
119 lines (105 loc) · 2.89 KB
/
script-modules.js
File metadata and controls
119 lines (105 loc) · 2.89 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* External dependencies
*/
const { createRequire } = require( 'node:module' );
const { dirname } = require( 'node:path' );
/**
* WordPress dependencies
*/
const DependencyExtractionPlugin = require( '@wordpress/dependency-extraction-webpack-plugin' );
/**
* Internal dependencies
*/
const {
baseDir,
getBaseConfig,
normalizeJoin,
MODULES,
SCRIPT_AND_MODULE_DUAL_PACKAGES,
WORDPRESS_NAMESPACE,
} = require( './shared' );
/** @type {Map<string, string>} */
const scriptModules = new Map();
for ( const packageName of MODULES.concat( SCRIPT_AND_MODULE_DUAL_PACKAGES ) ) {
const packageRequire = createRequire(
`${ dirname( require.resolve( `${ packageName }/package.json` ) ) }/`
);
const depPackageJson = packageRequire( './package.json' );
if ( ! Object.hasOwn( depPackageJson, 'wpScriptModuleExports' ) ) {
continue;
}
const moduleName = packageName.substring( WORDPRESS_NAMESPACE.length );
let { wpScriptModuleExports } = depPackageJson;
// Special handling for { "wpScriptModuleExports": "./build-module/index.js" }.
if ( typeof wpScriptModuleExports === 'string' ) {
wpScriptModuleExports = { '.': wpScriptModuleExports };
}
if ( Object.getPrototypeOf( wpScriptModuleExports ) !== Object.prototype ) {
throw new Error( 'wpScriptModuleExports must be an object' );
}
for ( const [ exportName, exportPath ] of Object.entries(
wpScriptModuleExports
) ) {
if ( typeof exportPath !== 'string' ) {
throw new Error( 'wpScriptModuleExports paths must be strings' );
}
if ( ! exportPath.startsWith( './' ) ) {
throw new Error(
'wpScriptModuleExports paths must start with "./"'
);
}
const name =
exportName === '.' ? 'index' : exportName.replace( /^\.\/?/, '' );
scriptModules.set(
`${ moduleName }/${ name }`,
packageRequire.resolve( exportPath )
);
}
}
module.exports = function (
env = { environment: 'production', watch: false, 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 baseConfig = getBaseConfig( env );
const config = {
...baseConfig,
entry: Object.fromEntries( scriptModules.entries() ),
experiments: {
outputModule: true,
},
output: {
devtoolNamespace: 'wp',
filename: `[name]${ suffix }.js`,
path: normalizeJoin(
baseDir,
`${ buildTarget }/js/dist/script-modules`
),
library: {
type: 'module',
},
environment: { module: true },
module: true,
chunkFormat: 'module',
asyncChunks: false,
},
plugins: [
...baseConfig.plugins,
new DependencyExtractionPlugin( {
injectPolyfill: false,
combineAssets: true,
combinedOutputFile: normalizeJoin(
baseDir,
`${ buildTarget }/assets/script-modules-packages${ suffix }.php`
),
} ),
],
};
return config;
};