-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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: Throw when using scripts from modules #57795
Changes from 3 commits
4ba4632
e018681
e4f147e
602162b
5860e6a
6d26ec1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,14 @@ 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 ); | ||
|
@@ -82,8 +90,8 @@ class DependencyExtractionWebpackPlugin { | |
: defaultRequestToExternal( request ); | ||
} | ||
|
||
if ( this.useModules && externalRequest === true ) { | ||
externalRequest = request; | ||
if ( externalRequest instanceof Error ) { | ||
return callback( externalRequest ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's best not to throw the error directly. If we throw it directly webpack doesn't know where the error is coming from and shows a general error.
Another option would be to throw the error in |
||
} | ||
|
||
if ( externalRequest ) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,9 +61,13 @@ 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|undefined} The resulting external definition. Return `undefined` | ||
* to ignore the request. Return `string` to map the request to an external. This may simply be returning the request, e.g. `@wordpress/interactivity` maps to the external `@wordpress/interactivity`. | ||
* @return {string|Error|undefined} The resulting external definition. | ||
* - Return `undefined` to ignore the request (do not externalize). | ||
* - Return `string` to map the request to an external. | ||
* - Return `Error` to emit an error. | ||
*/ | ||
function defaultRequestToExternalModule( request ) { | ||
if ( request === '@wordpress/interactivity' ) { | ||
|
@@ -73,6 +77,14 @@ function defaultRequestToExternalModule( request ) { | |
// which forces @wordpress/interactivity imports to be hoisted to static imports. | ||
return `module ${ request }`; | ||
} | ||
|
||
const isWordPressScript = Boolean( defaultRequestToExternal( request ) ); | ||
|
||
if ( isWordPressScript ) { | ||
return new Error( | ||
`Attempted to use WordPress script in a module: ${ request }` | ||
sirreal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we want to prevent bundling That's an approach identical to scripts, only instead of ensuring that scripts are There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we really decide to treat There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think we're not there yet, as there are no There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My understanding is that we can't use scripts from modules at all at this time. Adding them to the import map wouldn't help because they don't export anything, they're not modules. We can throw the error here, that's fine. We'll need to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
But even without this PR we will never use any actual WP script anyway. We'll either bundle a local If we keep this, it feels intuitively wrong to me to piggy-pack on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I share some of your reservations about this change. But I think that it's a good idea to provide this kind of feedback because during this scripts<->modules transition there's likely to be a lot of confusion and mistakes. This kind of change is intended to bring some clarity at an ideal time. It would help a lot of there were a post we could link to in the error message about the plans for this phase. @luisherranz is there anything public about scripts-modules compatibility we could link to? This plugin is specific to WordPress. It can be used outside of WordPress, but that's not its primary purpose. If we do use it outside of WordPress, we should set I piggy-backed on Some of those other modules probably work - There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alright, please go forward then 🙂 I don't have any other feedback. |
||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,9 @@ module.exports = { | |
new DependencyExtractionWebpackPlugin( { | ||
combineAssets: true, | ||
requestToExternalModule( request ) { | ||
return request.startsWith( '@wordpress/' ); | ||
return ( | ||
request.startsWith( '@wordpress/' ) || request === 'lodash' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the module tests There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed in the original PR that |
||
); | ||
}, | ||
} ), | ||
], | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* eslint-disable eslint-comments/disable-enable-pair */ | ||
/* eslint-disable eslint-comments/no-unlimited-disable */ | ||
/* eslint-disable */ | ||
|
||
import $ from 'jquery'; | ||
const apiFetch = await import( '@wordpress/api-fetch' ); | ||
|
||
$( () => { | ||
apiFetch( { path: '/' } ); | ||
} ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
const DependencyExtractionWebpackPlugin = require( '../../..' ); | ||
|
||
module.exports = { | ||
plugins: [ | ||
new DependencyExtractionWebpackPlugin( { | ||
// eslint-disable-next-line no-unused-vars | ||
requestToExternal( request ) { | ||
return new Error( 'Ensure error in script build.' ); | ||
}, | ||
} ), | ||
], | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fixes a small bug where we wouldn't run our default if we returned
false
(only available with modules).