@wordpress/dependency-extraction-webpack-plugin 5.0.1-next.79a6196f.0 → 5.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -20
- package/lib/index.js +30 -20
- package/lib/util.js +27 -7
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -94,14 +94,14 @@ By default, the following module requests are handled:
|
|
|
94
94
|
|
|
95
95
|
This plugin is compatible with `externals`, but they may conflict. For example, adding `{ externals: { '@wordpress/blob': 'wp.blob' } }` to webpack configuration will effectively hide the `@wordpress/blob` module from the plugin and it will not be included in dependency lists.
|
|
96
96
|
|
|
97
|
-
### Behavior with modules
|
|
97
|
+
### Behavior with script modules
|
|
98
98
|
|
|
99
|
-
**Warning:**
|
|
99
|
+
**Warning:** Script modules support is considered experimental at this time.
|
|
100
100
|
|
|
101
101
|
This section describes the behavior of this package to bundle ECMAScript modules and generate asset
|
|
102
|
-
files suitable for use with the WordPress Modules API.
|
|
102
|
+
files suitable for use with the WordPress Script Modules API.
|
|
103
103
|
|
|
104
|
-
Some of this plugin's options change, and webpack requires configuration to output modules. Refer to
|
|
104
|
+
Some of this plugin's options change, and webpack requires configuration to output script modules. Refer to
|
|
105
105
|
[webpack's documentation](https://webpack.js.org/configuration/output/#outputmodule) for up-to-date details.
|
|
106
106
|
|
|
107
107
|
```js
|
|
@@ -118,7 +118,7 @@ const webpackConfig = {
|
|
|
118
118
|
plugin.constructor.name !== 'DependencyExtractionWebpackPlugin'
|
|
119
119
|
),
|
|
120
120
|
new DependencyExtractionWebpackPlugin( {
|
|
121
|
-
// With modules,
|
|
121
|
+
// With modules, use `requestToExternalModule`:
|
|
122
122
|
requestToExternalModule( request ) {
|
|
123
123
|
if ( request === 'my-registered-module' ) {
|
|
124
124
|
return request;
|
|
@@ -129,7 +129,7 @@ const webpackConfig = {
|
|
|
129
129
|
};
|
|
130
130
|
```
|
|
131
131
|
|
|
132
|
-
Each entry point in the webpack bundle will include an asset file that declares the WordPress module dependencies that should be enqueued. This file also contains the unique version hash calculated based on the file content.
|
|
132
|
+
Each entry point in the webpack bundle will include an asset file that declares the WordPress script module dependencies that should be enqueued. This file also contains the unique version hash calculated based on the file content.
|
|
133
133
|
|
|
134
134
|
For example:
|
|
135
135
|
|
|
@@ -144,15 +144,15 @@ import { store, getContext } from '@wordpress/interactivity';
|
|
|
144
144
|
<?php return array('dependencies' => array('@wordpress/interactivity'), 'version' => 'dd4c2dc50d046ed9d4c063a7ca95702f');
|
|
145
145
|
```
|
|
146
146
|
|
|
147
|
-
By default, the following module requests are handled:
|
|
147
|
+
By default, the following script module requests are handled:
|
|
148
148
|
|
|
149
149
|
| Request |
|
|
150
150
|
| ---------------------------- |
|
|
151
151
|
| `@wordpress/interactivity ` |
|
|
152
152
|
|
|
153
|
-
(`@wordpress/interactivity` is currently the only available WordPress module.)
|
|
153
|
+
(`@wordpress/interactivity` is currently the only available WordPress script module.)
|
|
154
154
|
|
|
155
|
-
**Note:** This plugin overlaps with the functionality provided by [webpack `externals`](https://webpack.js.org/configuration/externals). This plugin is intended to extract module
|
|
155
|
+
**Note:** This plugin overlaps with the functionality provided by [webpack `externals`](https://webpack.js.org/configuration/externals). This plugin is intended to extract script module identifiers from bundle compilation so that a list of script module dependencies does not need to be manually maintained. If you don't need to extract a list of script module dependencies, use the `externals` option directly.
|
|
156
156
|
|
|
157
157
|
This plugin is compatible with `externals`, but they may conflict. For example, adding `{ externals: { '@wordpress/blob': 'wp.blob' } }` to webpack configuration will effectively hide the `@wordpress/blob` module from the plugin and it will not be included in dependency lists.
|
|
158
158
|
|
|
@@ -210,7 +210,7 @@ Pass `useDefaults: false` to disable the default request handling.
|
|
|
210
210
|
|
|
211
211
|
Force `wp-polyfill` to be included in each entry point's dependency list. This would be the same as adding `import '@wordpress/polyfill';` to each entry point.
|
|
212
212
|
|
|
213
|
-
**Note**: This option is not available with modules.
|
|
213
|
+
**Note**: This option is not available with script modules.
|
|
214
214
|
|
|
215
215
|
##### `externalizedReport`
|
|
216
216
|
|
|
@@ -222,7 +222,7 @@ You can provide a filename, or set it to `true` to report to a default `external
|
|
|
222
222
|
|
|
223
223
|
##### `requestToExternal`
|
|
224
224
|
|
|
225
|
-
**Note**: This option is not available with modules. See [`requestToExternalModule`](#requestToExternalModule) for module usage.
|
|
225
|
+
**Note**: This option is not available with script modules. See [`requestToExternalModule`](#requestToExternalModule) for module usage.
|
|
226
226
|
|
|
227
227
|
- Type: function
|
|
228
228
|
|
|
@@ -253,11 +253,11 @@ module.exports = {
|
|
|
253
253
|
|
|
254
254
|
##### `requestToExternalModule`
|
|
255
255
|
|
|
256
|
-
**Note**: This option is only available with modules. See [`requestToExternal`](#requestToExternal) for script usage.
|
|
256
|
+
**Note**: This option is only available with script modules. See [`requestToExternal`](#requestToExternal) for script usage.
|
|
257
257
|
|
|
258
258
|
- Type: function
|
|
259
259
|
|
|
260
|
-
`requestToExternalModule` allows the module handling to be customized. The function should accept a module request string and may return a string representing the module to use. Often, the module will have the same name.
|
|
260
|
+
`requestToExternalModule` allows the script module handling to be customized. The function should accept a script module request string and may return a string representing the script module to use. Often, the script module will have the same name.
|
|
261
261
|
|
|
262
262
|
`requestToExternalModule` provided via configuration has precedence over default external handling. Unhandled requests will be handled by the default unless `useDefaults` is set to `false`.
|
|
263
263
|
|
|
@@ -265,9 +265,9 @@ module.exports = {
|
|
|
265
265
|
/**
|
|
266
266
|
* Externalize 'my-module'
|
|
267
267
|
*
|
|
268
|
-
* @param {string} request Requested module
|
|
268
|
+
* @param {string} request Requested script module
|
|
269
269
|
*
|
|
270
|
-
* @return {(string|boolean|undefined)}
|
|
270
|
+
* @return {(string|boolean|undefined)} Script module ID
|
|
271
271
|
*/
|
|
272
272
|
function requestToExternalModule( request ) {
|
|
273
273
|
// Handle imports like `import myModule from 'my-module'`
|
|
@@ -276,7 +276,7 @@ function requestToExternalModule( request ) {
|
|
|
276
276
|
return 'myModule';
|
|
277
277
|
}
|
|
278
278
|
|
|
279
|
-
|
|
279
|
+
// If the script module ID in source is the same as the external script module, `true` can be returned.
|
|
280
280
|
return request === 'external-module-id-no-change-required';
|
|
281
281
|
}
|
|
282
282
|
|
|
@@ -289,7 +289,7 @@ module.exports = {
|
|
|
289
289
|
|
|
290
290
|
##### `requestToHandle`
|
|
291
291
|
|
|
292
|
-
**Note**: This option is not available with modules. It has no corresponding module configuration.
|
|
292
|
+
**Note**: This option is not available with script modules. It has no corresponding module configuration.
|
|
293
293
|
|
|
294
294
|
- Type: function
|
|
295
295
|
|
|
@@ -343,7 +343,7 @@ $script_url = plugins_url( $script_path, __FILE__ );
|
|
|
343
343
|
wp_enqueue_script( 'script', $script_url, $script_asset['dependencies'], $script_asset['version'] );
|
|
344
344
|
```
|
|
345
345
|
|
|
346
|
-
Or with modules (the Module API is
|
|
346
|
+
Or with modules (the Script Module API is only available in WordPress > 6.5):
|
|
347
347
|
|
|
348
348
|
```php
|
|
349
349
|
$module_path = 'path/to/module.js';
|
|
@@ -352,8 +352,8 @@ $module_asset = file_exists( $module_asset_path )
|
|
|
352
352
|
? require( $module_asset_path )
|
|
353
353
|
: array( 'dependencies' => array(), 'version' => filemtime( $module_path ) );
|
|
354
354
|
$module_url = plugins_url( $module_path, __FILE__ );
|
|
355
|
-
|
|
356
|
-
|
|
355
|
+
wp_register_script_module( 'my-module', $module_url, $module_asset['dependencies'], $module_asset['version'] );
|
|
356
|
+
wp_enqueue_script_module( 'my-module' );
|
|
357
357
|
```
|
|
358
358
|
|
|
359
359
|
## Contributing to this package
|
package/lib/index.js
CHANGED
|
@@ -62,28 +62,38 @@ class DependencyExtractionWebpackPlugin {
|
|
|
62
62
|
externalizeWpDeps( { request }, callback ) {
|
|
63
63
|
let externalRequest;
|
|
64
64
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
if (
|
|
68
|
-
|
|
69
|
-
this.options.requestToExternalModule
|
|
65
|
+
try {
|
|
66
|
+
// Handle via options.requestToExternal(Module) first.
|
|
67
|
+
if ( this.useModules ) {
|
|
68
|
+
if (
|
|
69
|
+
typeof this.options.requestToExternalModule === 'function'
|
|
70
|
+
) {
|
|
71
|
+
externalRequest =
|
|
72
|
+
this.options.requestToExternalModule( request );
|
|
73
|
+
|
|
74
|
+
// requestToExternalModule allows a boolean shorthand
|
|
75
|
+
if ( externalRequest === false ) {
|
|
76
|
+
externalRequest = undefined;
|
|
77
|
+
}
|
|
78
|
+
if ( externalRequest === true ) {
|
|
79
|
+
externalRequest = request;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
} else if ( typeof this.options.requestToExternal === 'function' ) {
|
|
83
|
+
externalRequest = this.options.requestToExternal( request );
|
|
70
84
|
}
|
|
71
|
-
} else if ( typeof this.options.requestToExternal === 'function' ) {
|
|
72
|
-
externalRequest = this.options.requestToExternal( request );
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// Cascade to default if unhandled and enabled.
|
|
76
|
-
if (
|
|
77
|
-
typeof externalRequest === 'undefined' &&
|
|
78
|
-
this.options.useDefaults
|
|
79
|
-
) {
|
|
80
|
-
externalRequest = this.useModules
|
|
81
|
-
? defaultRequestToExternalModule( request )
|
|
82
|
-
: defaultRequestToExternal( request );
|
|
83
|
-
}
|
|
84
85
|
|
|
85
|
-
|
|
86
|
-
|
|
86
|
+
// Cascade to default if unhandled and enabled.
|
|
87
|
+
if (
|
|
88
|
+
typeof externalRequest === 'undefined' &&
|
|
89
|
+
this.options.useDefaults
|
|
90
|
+
) {
|
|
91
|
+
externalRequest = this.useModules
|
|
92
|
+
? defaultRequestToExternalModule( request )
|
|
93
|
+
: defaultRequestToExternal( request );
|
|
94
|
+
}
|
|
95
|
+
} catch ( err ) {
|
|
96
|
+
return callback( err );
|
|
87
97
|
}
|
|
88
98
|
|
|
89
99
|
if ( externalRequest ) {
|
package/lib/util.js
CHANGED
|
@@ -59,20 +59,40 @@ function defaultRequestToExternal( request ) {
|
|
|
59
59
|
/**
|
|
60
60
|
* Default request to external module transformation
|
|
61
61
|
*
|
|
62
|
-
* Currently only @wordpress/interactivity
|
|
62
|
+
* Currently only @wordpress/interactivity and `@wordpress/interactivity-router`
|
|
63
|
+
* are supported.
|
|
64
|
+
*
|
|
65
|
+
* Do not use the boolean shorthand here, it's only handled for the
|
|
66
|
+
* `requestToExternalModule` option.
|
|
63
67
|
*
|
|
64
68
|
* @param {string} request Module request (the module name in `import from`) to be transformed
|
|
65
|
-
* @return {string|undefined} The resulting external definition.
|
|
66
|
-
*
|
|
69
|
+
* @return {string|Error|undefined} The resulting external definition.
|
|
70
|
+
* - Return `undefined` to ignore the request (do not externalize).
|
|
71
|
+
* - Return `string` to map the request to an external.
|
|
72
|
+
* - Return `Error` to emit an error.
|
|
67
73
|
*/
|
|
68
74
|
function defaultRequestToExternalModule( request ) {
|
|
69
75
|
if ( request === '@wordpress/interactivity' ) {
|
|
70
|
-
// This is a special case. Interactivity does not support dynamic imports at
|
|
71
|
-
// time. We add the external "module" type to indicate that webpack
|
|
72
|
-
// externalize this as a module (instead of our default `import()`
|
|
73
|
-
// which forces @wordpress/interactivity imports to be
|
|
76
|
+
// This is a special case. Interactivity does not support dynamic imports at
|
|
77
|
+
// this time. We add the external "module" type to indicate that webpack
|
|
78
|
+
// should externalize this as a module (instead of our default `import()`
|
|
79
|
+
// external type) which forces @wordpress/interactivity imports to be
|
|
80
|
+
// hoisted to static imports.
|
|
74
81
|
return `module ${ request }`;
|
|
75
82
|
}
|
|
83
|
+
|
|
84
|
+
if ( request === '@wordpress/interactivity-router' ) {
|
|
85
|
+
// Assumes this is usually going to be used as a dynamic import.
|
|
86
|
+
return `import ${ request }`;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const isWordPressScript = Boolean( defaultRequestToExternal( request ) );
|
|
90
|
+
|
|
91
|
+
if ( isWordPressScript ) {
|
|
92
|
+
throw new Error(
|
|
93
|
+
`Attempted to use WordPress script in a module: ${ request }, which is not supported yet.`
|
|
94
|
+
);
|
|
95
|
+
}
|
|
76
96
|
}
|
|
77
97
|
|
|
78
98
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordpress/dependency-extraction-webpack-plugin",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.1.0",
|
|
4
4
|
"description": "Extract WordPress script dependencies from webpack bundles.",
|
|
5
5
|
"author": "The WordPress Contributors",
|
|
6
6
|
"license": "GPL-2.0-or-later",
|
|
@@ -37,5 +37,5 @@
|
|
|
37
37
|
"publishConfig": {
|
|
38
38
|
"access": "public"
|
|
39
39
|
},
|
|
40
|
-
"gitHead": "
|
|
40
|
+
"gitHead": "45de2cb4212fed7f2763e95f10300d1ff9d0ec08"
|
|
41
41
|
}
|