X Tutup
Skip to content

Use package.json#imports for browser-specific code#17756

Merged
nicolo-ribaudo merged 4 commits intobabel:mainfrom
nicolo-ribaudo:use-pkg-json-imports
Jan 30, 2026
Merged

Use package.json#imports for browser-specific code#17756
nicolo-ribaudo merged 4 commits intobabel:mainfrom
nicolo-ribaudo:use-pkg-json-imports

Conversation

@nicolo-ribaudo
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo commented Jan 29, 2026

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

Rather than our custom setup. This PR now also allows bundling packages with multiple entrypoints, or packages that have browser overrides. For example, for @babel/core, it generates:

./packages/babel-core/lib
├── caching-2iA7hK9v.js
├── config
│   ├── caching.js
│   ├── files
│   │   ├── index-browser.js
│   │   └── index.js
│   ├── resolve-targets-browser.js
│   └── resolve-targets.js
├── errors
│   └── rewrite-stack-trace.js
├── index-CeIvb_5d.js
├── index.js
├── transform-file-browser.js
├── transform-file-BWC-srJZ.js
└── transform-file.js

@nicolo-ribaudo nicolo-ribaudo added the PR: Internal (next major) 🏠 A type of pull request used for our changelog categories for next major release label Jan 29, 2026
@nicolo-ribaudo nicolo-ribaudo force-pushed the use-pkg-json-imports branch 2 times, most recently from e0c8c4d to c9c7b90 Compare January 29, 2026 23:05
@babel-bot
Copy link
Collaborator

babel-bot commented Jan 29, 2026

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

@nicolo-ribaudo nicolo-ribaudo force-pushed the use-pkg-json-imports branch 2 times, most recently from 3b9b1b9 to ea4c555 Compare January 29, 2026 23:23
@pkg-pr-new
Copy link

pkg-pr-new bot commented Jan 30, 2026

Open in StackBlitz

commit: d1a76a3

@nicolo-ribaudo nicolo-ribaudo force-pushed the use-pkg-json-imports branch 2 times, most recently from e34fc7e to 963e15e Compare January 30, 2026 01:38
"default": "./lib/transform-file.js"
},
"#hack-for-rollup-inlining-order-1": "./lib/errors/rewrite-stack-trace.js",
"#hack-for-rollup-inlining-order-2": "./lib/config/caching.js"
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 is Rollup to force to create two separate files for these, otherwise it bundles them in an order that causes TDZ errors.

"./src/config/files/index.ts": "./src/config/files/index-browser.ts",
"./src/config/resolve-targets.ts": "./src/config/resolve-targets-browser.ts",
"./src/transform-file.ts": "./src/transform-file-browser.ts"
},
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a breaking change for downstream tools/frameworks that bundle babel-core or the transform-runtime package, if they still uses legacy webpack / rollup versions that does not recognize the imports field.

Copy link
Member Author

@nicolo-ribaudo nicolo-ribaudo Jan 30, 2026

Choose a reason for hiding this comment

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

Yes, however package.json#imports have been introduced in 2019. Webpack and Rollup have both supported it since 2020 (webpack/enhanced-resolve@effa185, rollup/plugins@0e4f014).

I think it is reasonable to expect for people that are updating to Babel 8 and want to bundle it to use a bundler released in the past 6 years. It's very likely that at this point we are shipping syntax that is not compatible with those bundlers too.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's very likely that at this point we are shipping syntax that is not compatible with those bundlers too.

You are right. To clarify, I don't object the change itself, but would rather point out that since it will break old bundlers anyway, at least we should mention this change in the migration note.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done :)

@nicolo-ribaudo nicolo-ribaudo added PR: Breaking Change 💥 A type of pull request used for our changelog categories for next major release and removed PR: Internal (next major) 🏠 A type of pull request used for our changelog categories for next major release labels Jan 30, 2026
@liuxingbaoyu
Copy link
Member

liuxingbaoyu commented Jan 30, 2026

I like this PR!
Because standalone package has been compiled to ES5 and will be injected into the global environment.

@nicolo-ribaudo nicolo-ribaudo merged commit 237db3e into babel:main Jan 30, 2026
55 checks passed
@nicolo-ribaudo nicolo-ribaudo deleted the use-pkg-json-imports branch January 30, 2026 22:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: Breaking Change 💥 A type of pull request used for our changelog categories for next major release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

X Tutup