X Tutup
Skip to content

feat: read standalone targets from data-targets#17482

Merged
nicolo-ribaudo merged 2 commits intobabel:mainfrom
JLHwung:add-targets-standalone-script-tag
Jan 27, 2026
Merged

feat: read standalone targets from data-targets#17482
nicolo-ribaudo merged 2 commits intobabel:mainfrom
JLHwung:add-targets-standalone-script-tag

Conversation

@JLHwung
Copy link
Contributor

@JLHwung JLHwung commented Aug 18, 2025

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

In this PR babel-standalone will read browsers targets from the data-targets attribute. E.g. to transform the script to chrome 84 or above:

<script type="text/babel" data-targets="chrome 84">...</script>

In Babel 7, although there is no easy way to customize the targets option in the script tag, the output will run on most browsers anyway, aligning with the goal of standalone: It works out of the box.

However, in Babel 8, we have changed the default targets to the browserslist's defaults. In the migration docs we suggest users to specify the targets option directly, which should be feasible for babel-core users or downstream tool users. But for standalone users, they don't have an easy access to most Babel options. Hence this is what this PR is for.

@JLHwung JLHwung added PR: New Feature 🚀 A type of pull request used for our changelog categories pkg: standalone labels Aug 18, 2025
@babel-bot
Copy link
Collaborator

babel-bot commented Aug 18, 2025

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

@pkg-pr-new
Copy link

pkg-pr-new bot commented Aug 18, 2025

Open in StackBlitz

commit: c112fa2

@liuxingbaoyu
Copy link
Member

I'm afraid this still won't work out of the box.
While this isn't recommended for production use, it might not work if users share the link with others.
I'd prefer to stick with the old defaults when transforming script.

@nicolo-ribaudo
Copy link
Member

I wonder if we could do some basic user agent sniffing, defaulting to "current browser"?

@JLHwung
Copy link
Contributor Author

JLHwung commented Aug 26, 2025

I'm afraid this still won't work out of the box.
While this isn't recommended for production use, it might not work if users share the link with others.
I'd prefer to stick with the old defaults when transforming script.

In Babel 8 we bumped the default targets to defaults, it would be very confusing if Babel standalone changed the default targets in contrast with Babel core, since they share the same goal: convert codes for legacy browsers. If the new defaults target does not work for some users, they can always specify older targets such as ie: 7. After all, the whole targets option thing informs users that they chose the compilation targets, not us.

In the migration docs, we can put that if you want the old Babel 7 standalone behaviour, you can specify data-targets="ie 7", which is also the targets of how babel-standalone is built. If users ever pause like "wait, do I really need the IE 7 support?", I believe they will do the research and come up with a better targets than the Babel 7 defaults.

I wonder if we could do some basic user agent sniffing, defaulting to "current browser"?

I thought about it before, but the MDN docs (source) explicitly disapproves this approach, as browsers could pretend to be other browsers.

@JLHwung
Copy link
Contributor Author

JLHwung commented Sep 15, 2025

Docs PR is ready, PTAL: babel/website#3130

@JLHwung JLHwung force-pushed the add-targets-standalone-script-tag branch from ffed727 to 3383f4e Compare January 19, 2026 20:46
@nicolo-ribaudo nicolo-ribaudo added PR: New Feature (next major) 🚀 A type of pull request used for our changelog categories for next major release and removed PR: New Feature 🚀 A type of pull request used for our changelog categories labels Jan 23, 2026
@nicolo-ribaudo
Copy link
Member

@JLHwung could you already prepare the backport PR?

@JLHwung
Copy link
Contributor Author

JLHwung commented Jan 23, 2026

@JLHwung could you already prepare the backport PR?

Thanks for the heads up, I do not recall that I have prepared a backport, will do it now.

@nicolo-ribaudo nicolo-ribaudo merged commit 1c09c85 into babel:main Jan 27, 2026
54 checks passed
@nicolo-ribaudo nicolo-ribaudo deleted the add-targets-standalone-script-tag branch January 27, 2026 15:59
@nicolo-ribaudo nicolo-ribaudo added the PR: New Feature 🚀 A type of pull request used for our changelog categories label Mar 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

7.x: needs backport pkg: standalone PR: New Feature (next major) 🚀 A type of pull request used for our changelog categories for next major release PR: New Feature 🚀 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

X Tutup