Skip to content

Commit

Permalink
DependencyExtractionWebpackPlugin: Add boolean shorthand for requestT…
Browse files Browse the repository at this point in the history
…oExternalModule (#57593)

The most common usage of the `requestToExternalModule` option is
to return the same requested module ID that was passed in. A shorthand
boolean return value is added support this behavior without returning
the original requested module ID.
  • Loading branch information
sirreal authored Jan 8, 2024
1 parent f57bcc8 commit 36fa07f
Show file tree
Hide file tree
Showing 14 changed files with 23 additions and 36 deletions.
7 changes: 5 additions & 2 deletions packages/dependency-extraction-webpack-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
4 changes: 4 additions & 0 deletions packages/dependency-extraction-webpack-plugin/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ class DependencyExtractionWebpackPlugin {
: defaultRequestToExternal( request );
}

if ( this.useModules && externalRequest === true ) {
externalRequest = request;
}

if ( externalRequest ) {
this.externalizedDeps.add( request );

Expand Down
4 changes: 3 additions & 1 deletion packages/dependency-extraction-webpack-plugin/lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ module.exports = {
new DependencyExtractionWebpackPlugin( {
combineAssets: true,
requestToExternalModule( request ) {
if ( request.startsWith( '@wordpress/' ) ) {
return request;
}
return request.startsWith( '@wordpress/' );
},
} ),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ module.exports = {
plugins: [
new DependencyExtractionWebpackPlugin( {
requestToExternalModule( request ) {
if ( request.startsWith( '@wordpress/' ) ) {
return request;
}
return request.startsWith( '@wordpress/' );
},
} ),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ module.exports = {
plugins: [
new DependencyExtractionWebpackPlugin( {
requestToExternalModule( request ) {
if ( request.startsWith( '@wordpress/' ) ) {
return request;
}
return request.startsWith( '@wordpress/' );
},
} ),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ module.exports = {
plugins: [
new DependencyExtractionWebpackPlugin( {
requestToExternalModule( request ) {
if ( request.startsWith( '@wordpress/' ) ) {
return request;
}
return request.startsWith( '@wordpress/' );
},
} ),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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/' );
},
} ),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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/' );
},
} ),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ module.exports = {
plugins: [
new DependencyExtractionWebpackPlugin( {
requestToExternalModule( request ) {
if ( request.startsWith( '@wordpress/' ) ) {
return request;
}
return request.startsWith( '@wordpress/' );
},
} ),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ module.exports = {
plugins: [
new DependencyExtractionWebpackPlugin( {
requestToExternalModule( request ) {
if ( request.startsWith( '@wordpress/' ) ) {
return request;
}
return request.startsWith( '@wordpress/' );
},
} ),
new MiniCSSExtractPlugin(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ module.exports = {
plugins: [
new DependencyExtractionWebpackPlugin( {
requestToExternalModule( request ) {
if ( request.startsWith( '@wordpress/' ) ) {
return request;
}
return request.startsWith( '@wordpress/' );
},
} ),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ module.exports = {
plugins: [
new DependencyExtractionWebpackPlugin( {
requestToExternalModule( request ) {
if ( request.startsWith( '@wordpress/' ) ) {
return request;
}
return request.startsWith( '@wordpress/' );
},
} ),
],
Expand Down

0 comments on commit 36fa07f

Please sign in to comment.