Skip to content

Commit

Permalink
Improve requestToExternalModule shorthand handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sirreal committed Jan 12, 2024
1 parent e018681 commit e4f147e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
17 changes: 12 additions & 5 deletions packages/dependency-extraction-webpack-plugin/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,29 @@ class DependencyExtractionWebpackPlugin {
if ( typeof this.options.requestToExternalModule === 'function' ) {
externalRequest =
this.options.requestToExternalModule( request );

// requestToExternalModule allows a boolean shorthand
if ( externalRequest === false ) {
externalRequest = undefined;
}
if ( externalRequest === true ) {
externalRequest = request;
}
}
} else if ( typeof this.options.requestToExternal === 'function' ) {
externalRequest = this.options.requestToExternal( request );
}

// Cascade to default if unhandled and enabled.
if ( ! externalRequest && this.options.useDefaults ) {
if (
typeof externalRequest === 'undefined' &&
this.options.useDefaults
) {
externalRequest = this.useModules
? defaultRequestToExternalModule( request )
: defaultRequestToExternal( request );
}

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

if ( externalRequest instanceof Error ) {
return callback( externalRequest );
}
Expand Down
5 changes: 3 additions & 2 deletions packages/dependency-extraction-webpack-plugin/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ function defaultRequestToExternal( request ) {
*
* Currently only @wordpress/interactivity
*
* Do not use the boolean shorthand here, it's only handled for the `requestToExternalModule` option.
*
* @param {string} request Module request (the module name in `import from`) to be transformed
* @return {string|boolean|Error|undefined} The resulting external definition.
* @return {string|Error|undefined} The resulting external definition.
* - Return `undefined` to ignore the request (do not externalize).
* - Return `true` to externalize the request with the same Module ID
* - Return `string` to map the request to an external.
* - Return `Error` to emit an error.
*/
Expand Down

0 comments on commit e4f147e

Please sign in to comment.