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

Avoid unnecessary code for unused lazy imports #15449

Merged
merged 1 commit into from Feb 22, 2023

Conversation

nicolo-ribaudo
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo commented Feb 21, 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

Thanks to #15446, I noticed that out released code include this:

function _module() {
  const data = require("module");
  _module = function () {
    return data;
  };
  return data;
}

even if that _module() function is then not used anywhere.

Babel generates that code when there is a import Module from "module" import, and it's then unused because all the usages of Module are stripped away at build time. With this PR, we avoid injecting any code for lazy imports that are not actually used, since it's code that would never be executed at runtime anyway.

As before, we continue emitting eager imports for import "x" since these import statements are explicitly used for side effects.

I have a feeling that #15446 is caused by trying to bundle @babel/core with a webpack version that throws by default when trying to include some node-specific modules. That issue is not a bug with Babel, but once we merge this PR and we release a Babel version compiled with this PR (so in at least two releases), that issue will be fixed because the dead require call will be removed.

@nicolo-ribaudo nicolo-ribaudo added area: modules PR: Output optimization 🔬 A type of pull request used for our changelog categories labels Feb 21, 2023
@nicolo-ribaudo nicolo-ribaudo changed the title Avoid unneessary code for unused lazy imports Avoid unnecessary code for unused lazy imports Feb 21, 2023
@babel-bot
Copy link
Collaborator

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

@JLHwung JLHwung merged commit 4d42879 into babel:main Feb 22, 2023
@nicolo-ribaudo nicolo-ribaudo deleted the avoid-unnecessary-lazy-require branch February 23, 2023 09:15
@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 May 26, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: modules outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Output optimization 🔬 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

4 participants