feat: support using map as the externals root#18784
feat: support using map as the externals root#18784fi3ework wants to merge 1 commit intowebpack:mainfrom
Conversation
|
For maintainers only:
|
a07d3c8 to
b578a20
Compare
| ? esmType | ||
| : /** @type {ExternalsCategory} */ (dependencyType); | ||
|
|
||
| const resolved = externalType[externalCategory] || externalType.fallback; |
There was a problem hiding this comment.
Should we have a default value for fallback?
There was a problem hiding this comment.
I think it's hard to decide for user which type should be used as fallback, and it might introduce more implicit settings.
| externalsType: { | ||
| "static-import": "commonjs", | ||
| "dynamic-import": "import", | ||
| fallback: "window" |
There was a problem hiding this comment.
Can we add a test case with amd, also we support system and other
To be honestly we can use https://github.com/webpack/webpack/blob/main/lib/dependencies/CommonJsFullRequireDependency.js#L93 and https://github.com/webpack/webpack/blob/main/lib/dependencies/ImportDependency.js#L40
Do I understand correctly that we are trying to provide the opportunity to use one or another type external depending by dependency category?
Maybe we can introduce new function to our dependencies
There was a problem hiding this comment.
Can we add a test case with amd, also we support system and other
Sure.
Do I understand correctly that we are trying to provide the opportunity to use one or another type external depending by dependency category?
The original requirement is that when we build for NPM package, we need to keep import() as import() and convert import to commonjs (similar to #16272). Considering that we previously added the type of 'module-import', supporting map types for externalsTypes would be more scalable and more convenient for users. We had thought about exposing some fields of dependency in a function before, but this would expose internal abstractions, so we need to categorize possible categories for users in advance. The finer the category judgment here, the better it can prevent us from having to split existing categories in the future.
Maybe we can introduce new functions into our dependencies.
I need to review all possible external types again, I previously thought that category covered all enumerations, but now we need to consider the field type.
There was a problem hiding this comment.
Can we add a test case with amd, also we support system and other
I added a test case for amd. But I am not clear of the system here, does Systemjs got a dedicated import method?
lib/ExternalModuleFactoryPlugin.js
Outdated
| ? "static-import" | ||
| : dependency instanceof ImportDependency | ||
| ? "dynamic-import" | ||
| : undefined; |
There was a problem hiding this comment.
Ideally in future we should refactor and move it to dependency property/method, I can refactor and show you
There was a problem hiding this comment.
IIUC, move to a new field, and the field will represent for all available keys in externals type map?
There was a problem hiding this comment.
Yeah, because we can have external url's (CSS for examle), external new URL(...), external @import's (yeah, I don't think developers often using it, but will be great to solve it in universal way), that is why I think we need getExternalCategory or something like that
There was a problem hiding this comment.
I added a new getExternalCategory to ModuleDependency, and some of the subclass implemented it. As of now:
AMDRequireItemDependency: "amd"CommonJsRequireDependency: "commonjs"HarmonyImportDependency: "static-import"ImportDependency: "dynamic-import"
Please take a look, and I think there are more dependencies to add but I'm not sure what they are. Any suggestion please.
And the name of "static-import" and "dynamic-import" looks a bit weird for me as it hasn't been present in webpack before, maybe import and import() are better as mentioned above.
df41278 to
ceccc39
Compare
ceccc39 to
35828a7
Compare
35828a7 to
6ae4cfe
Compare
|
Friendly ping @alexander-akait, could you please review the comments so we can improve the PR one more step. Thanks! 😊 |
|
@alexander-akait Friendly ping 😊. If this PR changes too many scopes and may not be merged, we could consider adding a new The most essential demand is to reserve dynamic import for CJS as it's supported since 13.2.0. Dynamic import could allow user to import an ESM module, whereas require can't. I think this is commonly used in Node.js targeted scenes. esbuild support emit |
What kind of change does this PR introduce?
Related discussion: #16272.
Support fine-grained control of external types using an external category in a map format.
While the current external function type can achieve this to somehow, it has a major drawback: the string and regex checks must be overridden in the function type to perform these judgments.
The external type of map is added here to prevent the ongoing spread of subsequent types like
'module-import'We can imagine that the'module-import'type no longer exists. Instead, we use the new external type map, which we can implement easily as follows.In the future, when there are more similar requirements of
'A-B', we can use map to solve it.Overview changes:
ExternalModuleDid you add tests for your changes?
Yes.
Does this PR introduce a breaking change?
I believe not, 'externalsType' just adds a new non-string type.
What needs to be documented once your changes are merged?
Yes, if this could be merged.