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

Refactor handling of modules plugins in preset-env #15988

Merged
merged 6 commits into from Oct 20, 2023

Conversation

nicolo-ribaudo
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo commented Sep 19, 2023

Q                       A
Fixed Issues?
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

I was working on preserving ESM by default in Babel 8 (unless the caller explicitly says that it does not support ESM), and I noticed that transform-export-namespace-from had some useless interactions with target checks. While removing it, I also noticed that we don't add the dynamic import syntax plugin in some cases when we are not transpiling it (we should include it for old @babel/parser versions).

@nicolo-ribaudo nicolo-ribaudo added the PR: Internal 🏠 A type of pull request used for our changelog categories label Sep 19, 2023
}));
const modules =
optionsModules === "auto"
? api.caller(supportsStaticESM)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?. removed from api.caller because every Babel 7 version provides api.caller. This was only needed during the beta period.

@babel-bot
Copy link
Collaborator

babel-bot commented Sep 19, 2023

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/55665/

@@ -160,15 +157,12 @@ export const getModulesPluginNames = ({
if (!shouldTransformDynamicImport) {
modulesPluginNames.push("syntax-dynamic-import");
}
if (!shouldTransformExportNamespaceFrom) {
modulesPluginNames.push("syntax-export-namespace-from");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not need to be included, because this plugin also goes through the normal process for including transform/syntax plugins. The only reason this plugin has special handling is that we must forcefully enable the transform if the targets support it but the caller does not.

Maybe we should get rid of this behavior in Babel 8, similarly to how the caller does not affect any other plugin.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add transform-export-namespace-from to the pluginSyntaxObject in packages/babel-preset-env/src/shipped-proposals.ts? So that we can enable the syntax plugin if compiling to Chrome 117 on Babel 7.0.0.

const shouldSkipExportNamespaceFrom =
(modules === "auto" && api.caller?.(supportsExportNamespaceFrom)) ||
(modules === false &&
!isRequired("transform-export-namespace-from", transformTargets, {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to check isRequired here, given that it will be also re-checked as for any other plugin if the transform is not forcefully included.

@@ -71,7 +71,6 @@ describe("babel-preset-env", () => {
}),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getModulesPluginNames is a public interface, it may be a breaking change to remove the syntax plugin here. In Babel 7 we have to unconditionally enable syntax-export-namespace-from anyway, I think it is fine to include it in getModulesPluginNames, we can remove those plugins in Babel 8.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Babel 8 let's also make this function not public anymore


// TODO(Babel 8): This is only here for backward compatibility. Remove it.
export { getModulesPluginNamesBackwardCompat as getModulesPluginNames };
const getModulesPluginNamesBackwardCompat = ({
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is not changed, I just moved it down here and exported it for backward compat. I'll open a PR removing it for Babel 8.

!exclude.plugins.has("transform-export-namespace-from")
) {
include.plugins.add("transform-export-namespace-from");
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JLHwung Now transform-export-namespace-from is 100% handled as a normal plugin, with this if statement being the only exception.

The other two modules plugins are still "special" because it does make no sense to do targets-based compilation for them given that they completely change the underlying module system, so they are still handled by the separate function.

If you look at the diffs, now the only actual output difference is that we enable syntax-dynamic-import in some cases where we weren't doing so previously.

bugfix/transform-v8-spread-parameters-in-optional-chaining { chrome < 91 }
transform-modules-commonjs
transform-dynamic-import
transform-export-namespace-from { }
syntax-import-meta
syntax-import-attributes
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These in the debug folder are Babel 8 tests, I'm not sure about why they included the syntax plugins but 🤷 the new output is correct

@nicolo-ribaudo nicolo-ribaudo merged commit c46f000 into babel:main Oct 20, 2023
47 checks passed
@nicolo-ribaudo nicolo-ribaudo deleted the env-modules-refactor branch October 20, 2023 09:01
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jan 20, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Internal 🏠 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants