Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DependencyExtractionWebpackPlugin: Add true shorthand for requestToExternalModule #57593

Merged
merged 4 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading