-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
[preset-env] Include / exclude module plugins properly #10218
[preset-env] Include / exclude module plugins properly #10218
Conversation
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/11348/ |
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/11132/ |
The I am wondering if we can reuse the let defaultIncludes = [];
if (modules !== false && moduleTransformations[modules]) {
// figure out an array of relatedPlugins
defaultIncludes.push(...)
})
const transformations = filterItems(
shippedProposals ? pluginList : pluginListWithoutProposals,
include.plugins,
exclude.plugins,
transformTargets,
defaultIncludes,
getOptionSpecificExcludesFor({ loose }),
pluginSyntaxMap,
); Note that We may also add a comment that |
@JLHwung Great points! Did struggle with this when doing the PR and was thinking about how I could do all of the filtering in |
Got a question, after looking into @JLHwung suggestions above. Preset-env is currently handling module plugins'/transformers' options differently compared to other plugins, just passing along the
and
From what I can tell from the docs there are no module plugins that are currently using any of the options passed to the other plugins ( |
I agree that we pass plugins.push([getPlugin("proposal-dynamic-import"), { loose }]); could be simplified to defaultIncludes.push("proposal-dynamic-import");
// generate transformations
transformations = filterItems( ..., defaultIncludes, ... )
// reuse the logic to construct plugins array from transformations.
// transformations => plugins Passing |
a9a6832
to
ae1f9fc
Compare
Created a new commit based on @JLHwung suggestions. I also did some refactoring in order to make the code easier to read and follow. The following changes were made:
Further notes: FYI. Screwed up when trying to merge changes from the upstream master. Git tree looks good now though. |
@@ -115,114 +216,65 @@ export default declare((api, opts) => { | |||
|
|||
const transformTargets = forceAllTransforms || hasUglifyTarget ? {} : targets; | |||
|
|||
const transformations = filterItems( | |||
const modulesPluginNames = getModulesPluginNames({ | |||
moduleTransformation: moduleTransformations[modules.toString()] || null, |
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.
nit: I am upset by false.toString()
can be passed into moduleTransformations
, though practically it does not change anything.
I prefer to pass moduleTransformation
and modules
instead and do the logic inside getModulesPluginNames
.
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.
Not ideal I know! This is one of those situations where using flow makes things less elegant (my own opinion ™).
Made the change you proposed in my latest commit. Used the variable name transformations
instead of moduleTransformations
inside of getModulesPluginNames
so that it not gets confused with the moduleTransformations
variable in the global scope.
it("throws when including module plugins", () => { | ||
expect(() => | ||
normalizeOptions.default({ include: ["proposal-dynamic-import"] }), | ||
).toThrow(); |
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.
Did this throw before this PR? If it didn't, I would prefer not to throw but to warn.
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.
Before the change it didn't throw, but index.js / filter-items.js could potentially have added a module plugin twice. For example the config that I specified above did that. Users were also able to specify an ambiguous config with for example modules
set to cjs
and includes
set to ['@babel/plugin-transform-modules-amd']
. Is it not better if preset-env throws in these cases in order to prevent wonky configs? I believe that there is otherwise a risk that users will overlook the warning. By throwing we are also preventing unnecessary support / issues further down the road for configs behaving weird due to inclusion of module plugins.
CI is failing after latest review commit. I need to update my branch with the latest commits from master ( |
It's ok to |
Co-Authored-By: Nicolò Ribaudo <[email protected]>
3200020
to
2d37d60
Compare
Thanks! And the comments are still there 🎉 |
This PR fixes how includes and excludes of module plugins (proposal-dynamic-import, transform-modules-commonjs, transform-modules-amd, transform-modules-commonjs, transform-modules-systemjs, transform-modules-umd) works. After this PR it is possible to exclude module plugins, but not include them.
Before this PR it wasn't possible to exclude any of the plugins listed above. This caused issues when you wanted to transform ES modules, but not dynamic imports (see this issue).
Another case that this PR is fixing is that it was possible to include a module plugin. However, that could potentially cause issues where a plugin was added twice, eg. with the following config:
Adding this PR will make it possible to set
modules: 'cjs'
andexclude: ['transform-modules-commonjs']
, which will resolve in that modules are not transformed to commonjs. A warning or an error might be preferable here, but I think that this behaviour is better then it was before (before it just transformed modules to commonjs). Furthermore, unless someones babel config is ambiguous (see cases above), this change should not break anyones' builds.