diff --git a/packages/dependency-extraction-webpack-plugin/README.md b/packages/dependency-extraction-webpack-plugin/README.md index 91fb36e8ad09d3..6031a1d5f25681 100644 --- a/packages/dependency-extraction-webpack-plugin/README.md +++ b/packages/dependency-extraction-webpack-plugin/README.md @@ -267,14 +267,17 @@ module.exports = { * * @param {string} request Requested module * - * @return {(string|undefined)} Script global + * @return {(string|boolean|undefined)} Module ID */ function requestToExternalModule( request ) { // Handle imports like `import myModule from 'my-module'` if ( request === 'my-module' ) { - // Import should be ov the form `import { something } from "myModule";` in the final bundle. + // Import should be of the form `import { something } from "myModule";` in the final bundle. return 'myModule'; } + + // If the Module ID in source is the same as the external module, we can return `true`. + return request === 'external-module-id-no-change-required'; } module.exports = { diff --git a/packages/dependency-extraction-webpack-plugin/lib/index.js b/packages/dependency-extraction-webpack-plugin/lib/index.js index 9b04d42e7c919d..e8d9fc9bfee1d8 100644 --- a/packages/dependency-extraction-webpack-plugin/lib/index.js +++ b/packages/dependency-extraction-webpack-plugin/lib/index.js @@ -82,6 +82,10 @@ class DependencyExtractionWebpackPlugin { : defaultRequestToExternal( request ); } + if ( this.useModules && externalRequest === true ) { + externalRequest = request; + } + if ( externalRequest ) { this.externalizedDeps.add( request ); diff --git a/packages/dependency-extraction-webpack-plugin/lib/types.d.ts b/packages/dependency-extraction-webpack-plugin/lib/types.d.ts index c4a4af52b1b2fc..5a1bff90aa49ad 100644 --- a/packages/dependency-extraction-webpack-plugin/lib/types.d.ts +++ b/packages/dependency-extraction-webpack-plugin/lib/types.d.ts @@ -13,7 +13,9 @@ declare interface DependencyExtractionWebpackPluginOptions { outputFormat?: 'php' | 'json'; outputFilename?: string | Function; requestToExternal?: ( request: string ) => string | string[] | undefined; - requestToExternalModule?: ( request: string ) => string | undefined; + requestToExternalModule?: ( + request: string + ) => string | boolean | undefined; requestToHandle?: ( request: string ) => string | undefined; combinedOutputFile?: string | null; combineAssets?: boolean; diff --git a/packages/dependency-extraction-webpack-plugin/test/fixtures/combine-assets/webpack.config.js b/packages/dependency-extraction-webpack-plugin/test/fixtures/combine-assets/webpack.config.js index fb7ba94ca80998..420d5030ab1a69 100644 --- a/packages/dependency-extraction-webpack-plugin/test/fixtures/combine-assets/webpack.config.js +++ b/packages/dependency-extraction-webpack-plugin/test/fixtures/combine-assets/webpack.config.js @@ -12,9 +12,7 @@ module.exports = { new DependencyExtractionWebpackPlugin( { combineAssets: true, requestToExternalModule( request ) { - if ( request.startsWith( '@wordpress/' ) ) { - return request; - } + return request.startsWith( '@wordpress/' ); }, } ), ], diff --git a/packages/dependency-extraction-webpack-plugin/test/fixtures/dynamic-import/webpack.config.js b/packages/dependency-extraction-webpack-plugin/test/fixtures/dynamic-import/webpack.config.js index 6856d328ab7c68..59d4c5d2ead3ab 100644 --- a/packages/dependency-extraction-webpack-plugin/test/fixtures/dynamic-import/webpack.config.js +++ b/packages/dependency-extraction-webpack-plugin/test/fixtures/dynamic-import/webpack.config.js @@ -7,9 +7,7 @@ module.exports = { plugins: [ new DependencyExtractionWebpackPlugin( { requestToExternalModule( request ) { - if ( request.startsWith( '@wordpress/' ) ) { - return request; - } + return request.startsWith( '@wordpress/' ); }, } ), ], diff --git a/packages/dependency-extraction-webpack-plugin/test/fixtures/function-output-filename/webpack.config.js b/packages/dependency-extraction-webpack-plugin/test/fixtures/function-output-filename/webpack.config.js index f637a4087e3ca3..0aea733327a540 100644 --- a/packages/dependency-extraction-webpack-plugin/test/fixtures/function-output-filename/webpack.config.js +++ b/packages/dependency-extraction-webpack-plugin/test/fixtures/function-output-filename/webpack.config.js @@ -12,9 +12,7 @@ module.exports = { plugins: [ new DependencyExtractionWebpackPlugin( { requestToExternalModule( request ) { - if ( request.startsWith( '@wordpress/' ) ) { - return request; - } + return request.startsWith( '@wordpress/' ); }, } ), ], diff --git a/packages/dependency-extraction-webpack-plugin/test/fixtures/has-extension-suffix/webpack.config.js b/packages/dependency-extraction-webpack-plugin/test/fixtures/has-extension-suffix/webpack.config.js index ada40c8bf8e54e..4ef05f6986b9c4 100644 --- a/packages/dependency-extraction-webpack-plugin/test/fixtures/has-extension-suffix/webpack.config.js +++ b/packages/dependency-extraction-webpack-plugin/test/fixtures/has-extension-suffix/webpack.config.js @@ -10,9 +10,7 @@ module.exports = { plugins: [ new DependencyExtractionWebpackPlugin( { requestToExternalModule( request ) { - if ( request.startsWith( '@wordpress/' ) ) { - return request; - } + return request.startsWith( '@wordpress/' ); }, } ), ], diff --git a/packages/dependency-extraction-webpack-plugin/test/fixtures/option-function-output-filename/webpack.config.js b/packages/dependency-extraction-webpack-plugin/test/fixtures/option-function-output-filename/webpack.config.js index 5056f312c39992..40123021ae404c 100644 --- a/packages/dependency-extraction-webpack-plugin/test/fixtures/option-function-output-filename/webpack.config.js +++ b/packages/dependency-extraction-webpack-plugin/test/fixtures/option-function-output-filename/webpack.config.js @@ -10,9 +10,7 @@ module.exports = { return `chunk--${ chunkData.chunk.name }--[name].asset.php`; }, requestToExternalModule( request ) { - if ( request.startsWith( '@wordpress/' ) ) { - return request; - } + return request.startsWith( '@wordpress/' ); }, } ), ], diff --git a/packages/dependency-extraction-webpack-plugin/test/fixtures/option-output-filename/webpack.config.js b/packages/dependency-extraction-webpack-plugin/test/fixtures/option-output-filename/webpack.config.js index be52e661653868..fb02bc3c5bcd0b 100644 --- a/packages/dependency-extraction-webpack-plugin/test/fixtures/option-output-filename/webpack.config.js +++ b/packages/dependency-extraction-webpack-plugin/test/fixtures/option-output-filename/webpack.config.js @@ -8,9 +8,7 @@ module.exports = { new DependencyExtractionWebpackPlugin( { outputFilename: '[name]-foo.asset.php', requestToExternalModule( request ) { - if ( request.startsWith( '@wordpress/' ) ) { - return request; - } + return request.startsWith( '@wordpress/' ); }, } ), ], diff --git a/packages/dependency-extraction-webpack-plugin/test/fixtures/overrides/webpack.config.js b/packages/dependency-extraction-webpack-plugin/test/fixtures/overrides/webpack.config.js index 89eaf6ee4b2f53..5e28a677f1d5b0 100644 --- a/packages/dependency-extraction-webpack-plugin/test/fixtures/overrides/webpack.config.js +++ b/packages/dependency-extraction-webpack-plugin/test/fixtures/overrides/webpack.config.js @@ -23,9 +23,7 @@ module.exports = { if ( request === 'rxjs/operators' ) { return request; } - if ( request.startsWith( '@wordpress/' ) ) { - return request; - } + return request.startsWith( '@wordpress/' ); }, requestToHandle( request ) { if ( request === 'rxjs' || request === 'rxjs/operators' ) { diff --git a/packages/dependency-extraction-webpack-plugin/test/fixtures/runtime-chunk-single/webpack.config.js b/packages/dependency-extraction-webpack-plugin/test/fixtures/runtime-chunk-single/webpack.config.js index 1e0824563c52f0..b8fa673995e9be 100644 --- a/packages/dependency-extraction-webpack-plugin/test/fixtures/runtime-chunk-single/webpack.config.js +++ b/packages/dependency-extraction-webpack-plugin/test/fixtures/runtime-chunk-single/webpack.config.js @@ -11,9 +11,7 @@ module.exports = { plugins: [ new DependencyExtractionWebpackPlugin( { requestToExternalModule( request ) { - if ( request.startsWith( '@wordpress/' ) ) { - return request; - } + return request.startsWith( '@wordpress/' ); }, } ), ], diff --git a/packages/dependency-extraction-webpack-plugin/test/fixtures/style-imports/webpack.config.js b/packages/dependency-extraction-webpack-plugin/test/fixtures/style-imports/webpack.config.js index 332e182e34b042..bb412af5c61b89 100644 --- a/packages/dependency-extraction-webpack-plugin/test/fixtures/style-imports/webpack.config.js +++ b/packages/dependency-extraction-webpack-plugin/test/fixtures/style-imports/webpack.config.js @@ -12,9 +12,7 @@ module.exports = { plugins: [ new DependencyExtractionWebpackPlugin( { requestToExternalModule( request ) { - if ( request.startsWith( '@wordpress/' ) ) { - return request; - } + return request.startsWith( '@wordpress/' ); }, } ), new MiniCSSExtractPlugin(), diff --git a/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-require/webpack.config.js b/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-require/webpack.config.js index 6856d328ab7c68..59d4c5d2ead3ab 100644 --- a/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-require/webpack.config.js +++ b/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-require/webpack.config.js @@ -7,9 +7,7 @@ module.exports = { plugins: [ new DependencyExtractionWebpackPlugin( { requestToExternalModule( request ) { - if ( request.startsWith( '@wordpress/' ) ) { - return request; - } + return request.startsWith( '@wordpress/' ); }, } ), ], diff --git a/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress/webpack.config.js b/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress/webpack.config.js index 6856d328ab7c68..59d4c5d2ead3ab 100644 --- a/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress/webpack.config.js +++ b/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress/webpack.config.js @@ -7,9 +7,7 @@ module.exports = { plugins: [ new DependencyExtractionWebpackPlugin( { requestToExternalModule( request ) { - if ( request.startsWith( '@wordpress/' ) ) { - return request; - } + return request.startsWith( '@wordpress/' ); }, } ), ],