Skip to content

Allow sync loading of ESM when --experimental-require-module#16820

Merged
nicolo-ribaudo merged 6 commits intobabel:mainfrom
nicolo-ribaudo:require-esm
Sep 26, 2024
Merged

Allow sync loading of ESM when --experimental-require-module#16820
nicolo-ribaudo merged 6 commits intobabel:mainfrom
nicolo-ribaudo:require-esm

Conversation

@nicolo-ribaudo
Copy link
Member

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

Node.js now has this flag to allow require() of ESM files, which means we can support ESM plugins/presets in transformSync&co. This will be useful for the Babel 8 migration.

This PR simply makes us respect that Node.js flag rather than just refusing to require() esm files ending in .mjs. I'm not sure if I should consider it as a bugfix or as internal. Note that require("<something>.js") already worked for ESM under that flag, because we try/catch to see if the .js file can be loaded.

@babel-bot
Copy link
Collaborator

babel-bot commented Sep 8, 2024

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

@nicolo-ribaudo nicolo-ribaudo force-pushed the require-esm branch 2 times, most recently from b90eb37 to 2980b50 Compare September 16, 2024 11:57
@nicolo-ribaudo
Copy link
Member Author

This is not breaking but I'd like to get it merged before the 8.0.0-beta, since it significantly simplify using ESM plugins (i.e. you wouldn't be forced to use Babel asynchronously anymore).

@nicolo-ribaudo nicolo-ribaudo added this to the v8.0.0-beta milestone Sep 17, 2024
@nicolo-ribaudo nicolo-ribaudo added the PR: Internal 🏠 A type of pull request used for our changelog categories label Sep 17, 2024
export default function* loadCodeDefault(
filepath: string,
asyncError: string,
loader: "require" | "import" | "auto",
Copy link
Member

@liuxingbaoyu liuxingbaoyu Sep 17, 2024

Choose a reason for hiding this comment

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

auto doesn't seem to be used?

@nicolo-ribaudo
Copy link
Member Author

This flag will be unflagged in Node.js 23.

Copy link
Member

@liuxingbaoyu liuxingbaoyu left a comment

Choose a reason for hiding this comment

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

Amazing!

@nicolo-ribaudo nicolo-ribaudo merged commit b2ba04c into babel:main Sep 26, 2024
@nicolo-ribaudo nicolo-ribaudo deleted the require-esm branch September 26, 2024 17:48
zemnmez-renovate-bot added a commit to zemn-me/monorepo that referenced this pull request Oct 2, 2024
##### [`v7.25.7](https://github.com/babel/babel/blob/HEAD/CHANGELOG.md#v7257-2024-10-02)

##### 🐛 Bug Fix

-   `babel-helper-validator-identifier`
    -   [#16825](babel/babel#16825) fix: update identifier to unicode 16 ([@JLHwung](https://github.com/JLHwung))
-   `babel-traverse`
    -   [#16814](babel/babel#16814) fix: issue with node path keys updated on unrelated paths ([@DylanPiercey](https://github.com/DylanPiercey))
-   `babel-plugin-transform-classes`
    -   [#16797](babel/babel#16797) Use an inclusion rather than exclusion list for `super()` check ([@nicolo-ribaudo](https://github.com/nicolo-ribaudo))
-   `babel-generator`
    -   [#16788](babel/babel#16788) Fix printing of TS `infer` in compact mode ([@nicolo-ribaudo](https://github.com/nicolo-ribaudo))
    -   [#16785](babel/babel#16785) Print TS type annotations for destructuring in assignment pattern ([@nicolo-ribaudo](https://github.com/nicolo-ribaudo))
    -   [#16778](babel/babel#16778) Respect `[no LineTerminator here]` after nodes ([@nicolo-ribaudo](https://github.com/nicolo-ribaudo))

##### 💅 Polish

-   `babel-types`
    -   [#16852](babel/babel#16852) Add deprecated JSDOC for fields ([@liuxingbaoyu](https://github.com/liuxingbaoyu))

##### 🏠 Internal

-   `babel-core`
    -   [#16820](babel/babel#16820) Allow sync loading of ESM when `--experimental-require-module` ([@nicolo-ribaudo](https://github.com/nicolo-ribaudo))
-   `babel-helper-compilation-targets`, `babel-helper-plugin-utils`, `babel-preset-env`
    -   [#16858](babel/babel#16858) Add browserslist config to external dependency ([@JLHwung](https://github.com/JLHwung))
-   `babel-plugin-proposal-destructuring-private`, `babel-plugin-syntax-decimal`, `babel-plugin-syntax-import-reflection`, `babel-standalone`
    -   [#16809](babel/babel#16809) Archive syntax-import-reflection and syntax-decimal ([@nicolo-ribaudo](https://github.com/nicolo-ribaudo))
-   `babel-generator`
    -   [#16779](babel/babel#16779) Simplify logic for `[no LineTerminator here]` before nodes ([@nicolo-ribaudo](https://github.com/nicolo-ribaudo))

##### 🏃‍♀️ Performance

-   `babel-plugin-transform-typescript`
    -   [#16875](babel/babel#16875) perf: Avoid extra cloning of namespaces ([@liuxingbaoyu](https://github.com/liuxingbaoyu))
-   `babel-types`
    -   [#16842](babel/babel#16842) perf: Improve [@babel/types](https://github.com/babel/types) builders ([@liuxingbaoyu](https://github.com/liuxingbaoyu))
    -   [#16828](babel/babel#16828) Only access `BABEL_TYPES_8_BREAKING` at startup ([@nicolo-ribaudo](https://github.com/nicolo-ribaudo))
github-merge-queue bot pushed a commit to zemn-me/monorepo that referenced this pull request Oct 2, 2024
##### [`v7.25.7](https://github.com/babel/babel/blob/HEAD/CHANGELOG.md#v7257-2024-10-02)

##### 🐛 Bug Fix

-   `babel-helper-validator-identifier`
    -   [#16825](babel/babel#16825) fix: update identifier to unicode 16 ([@JLHwung](https://github.com/JLHwung))
-   `babel-traverse`
    -   [#16814](babel/babel#16814) fix: issue with node path keys updated on unrelated paths ([@DylanPiercey](https://github.com/DylanPiercey))
-   `babel-plugin-transform-classes`
    -   [#16797](babel/babel#16797) Use an inclusion rather than exclusion list for `super()` check ([@nicolo-ribaudo](https://github.com/nicolo-ribaudo))
-   `babel-generator`
    -   [#16788](babel/babel#16788) Fix printing of TS `infer` in compact mode ([@nicolo-ribaudo](https://github.com/nicolo-ribaudo))
    -   [#16785](babel/babel#16785) Print TS type annotations for destructuring in assignment pattern ([@nicolo-ribaudo](https://github.com/nicolo-ribaudo))
    -   [#16778](babel/babel#16778) Respect `[no LineTerminator here]` after nodes ([@nicolo-ribaudo](https://github.com/nicolo-ribaudo))

##### 💅 Polish

-   `babel-types`
    -   [#16852](babel/babel#16852) Add deprecated JSDOC for fields ([@liuxingbaoyu](https://github.com/liuxingbaoyu))

##### 🏠 Internal

-   `babel-core`
    -   [#16820](babel/babel#16820) Allow sync loading of ESM when `--experimental-require-module` ([@nicolo-ribaudo](https://github.com/nicolo-ribaudo))
-   `babel-helper-compilation-targets`, `babel-helper-plugin-utils`, `babel-preset-env`
    -   [#16858](babel/babel#16858) Add browserslist config to external dependency ([@JLHwung](https://github.com/JLHwung))
-   `babel-plugin-proposal-destructuring-private`, `babel-plugin-syntax-decimal`, `babel-plugin-syntax-import-reflection`, `babel-standalone`
    -   [#16809](babel/babel#16809) Archive syntax-import-reflection and syntax-decimal ([@nicolo-ribaudo](https://github.com/nicolo-ribaudo))
-   `babel-generator`
    -   [#16779](babel/babel#16779) Simplify logic for `[no LineTerminator here]` before nodes ([@nicolo-ribaudo](https://github.com/nicolo-ribaudo))

##### 🏃‍♀️ Performance

-   `babel-plugin-transform-typescript`
    -   [#16875](babel/babel#16875) perf: Avoid extra cloning of namespaces ([@liuxingbaoyu](https://github.com/liuxingbaoyu))
-   `babel-types`
    -   [#16842](babel/babel#16842) perf: Improve [@babel/types](https://github.com/babel/types) builders ([@liuxingbaoyu](https://github.com/liuxingbaoyu))
    -   [#16828](babel/babel#16828) Only access `BABEL_TYPES_8_BREAKING` at startup ([@nicolo-ribaudo](https://github.com/nicolo-ribaudo))
@victordidenko
Copy link

I'm not sure, but looks like this change breaks something, I'm receiving error

[!] (plugin wyw-in-js) Error: [BABEL] src/index.ts: .loader is not a valid Plugin property
src/index.ts
    at .../node_modules/.pnpm/@babel+core@7.25.7/node_modules/@babel/core/src/config/validation/plugins.ts:122:41
    at Array.forEach (<anonymous>)
    at validatePluginObject (.../node_modules/.pnpm/@babel+core@7.25.7/node_modules/@babel/core/src/config/validation/plugins.ts:108:20)

Or maybe this is an issue with Anber/wyw-in-js#104

@nicolo-ribaudo
Copy link
Member Author

Could you open a new issue? This is indeed a regression, I didn't realize that that API is public.

@victordidenko
Copy link

opened 👌

@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 8, 2025
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 8, 2025
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 pkg: core 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.

5 participants