Visitar URL original
[7.x backport] feat: read standalone targets from data-targets by JLHwung · Pull Request #17725 · babel/babel · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: read standalone targets from data-targets
  • Loading branch information
JLHwung committed Jan 23, 2026
commit b4f2c8a646b8bbf7ce6643e12e10840cfff4da92
31 changes: 22 additions & 9 deletions packages/babel-standalone/src/transformScriptTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import type { InputOptions } from "@babel/core";
let headEl: HTMLHeadElement;
let inlineScriptCount = 0;

type Browsers = string | ReadonlyArray<string>;

type CompilationResult = {
async: boolean;
type: string;
Expand All @@ -26,6 +28,7 @@ type CompilationResult = {
// todo: refine plugins/presets
plugins: InputOptions["plugins"];
presets: InputOptions["presets"];
targets: Browsers;
url: string | null;
};

Expand Down Expand Up @@ -54,7 +57,10 @@ function transformCode(
* Builds the Babel options for transforming the specified script, using some
* sensible default presets and plugins if none were explicitly provided.
*/
function buildBabelOptions(script: CompilationResult, filename: string) {
function buildBabelOptions(
script: CompilationResult,
filename: string,
): InputOptions {
let presets = script.presets;
if (!presets) {
if (script.type === "module") {
Expand All @@ -63,9 +69,6 @@ function buildBabelOptions(script: CompilationResult, filename: string) {
[
"env",
{
targets: {
esmodules: true,
},
modules: false,
},
],
Expand All @@ -84,12 +87,21 @@ function buildBabelOptions(script: CompilationResult, filename: string) {
];
}

const targets: InputOptions["targets"] = {
browsers: script.targets,
};

if (script.type === "module") {
targets.esmodules = "intersect";
}

return {
filename,
presets,
plugins,
sourceMaps: "inline" as const,
sourceFileName: filename,
targets,
};
}

Expand Down Expand Up @@ -141,9 +153,9 @@ function load(
/**
* Converts a comma-separated data attribute string into an array of values. If
* the string is empty, returns an empty array. If the string is not defined,
* returns null.
* returns undefined.
*/
function getPluginsOrPresetsFromScript(
function parseFromScriptAttribute(
script: HTMLScriptElement,
attributeName: string,
) {
Expand All @@ -155,7 +167,7 @@ function getPluginsOrPresetsFromScript(
if (!rawValue) {
// Any other falsy value (null, undefined) means we're not overriding this
// setting, and should use the default.
return null;
return undefined;
}
return rawValue.split(",").map(item => item.trim());
}
Expand Down Expand Up @@ -194,8 +206,9 @@ function loadScripts(
nonce: script.nonce,
error: false,
executed: false,
plugins: getPluginsOrPresetsFromScript(script, "data-plugins"),
presets: getPluginsOrPresetsFromScript(script, "data-presets"),
plugins: parseFromScriptAttribute(script, "data-plugins"),
presets: parseFromScriptAttribute(script, "data-presets"),
targets: parseFromScriptAttribute(script, "data-targets"),
loaded: false,
url: null,
content: null,
Expand Down
26 changes: 26 additions & 0 deletions packages/babel-standalone/test/transform-script-tags.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,30 @@ describeGte("16.0.0")("transformScriptTags", () => {
});
});
});
it("should support data-targets attribute", () => {
const input = `globalThis ?? window; /\\p{ASCII}/v`;
const targets = "chrome 84"; // Chrome 84 supports nullish coalescing but not the `v` flag in regexps.
const dom = new JSDOM(
`<!DOCTYPE html><head><script>${standaloneSource}</script><script type="text/babel" data-targets="${targets}">${input}</script></head>`,
{ runScripts: "dangerously" },
);
return new Promise((resolve, reject) => {
dom.window.addEventListener("DOMContentLoaded", () => {
try {
const transformedScriptElement =
dom.window.document.head.children.item(2);
expect(
transformedScriptElement.getAttribute("data-targets"),
).toBeNull();
expect(transformedScriptElement.innerHTML).toContain(
`globalThis ?? window;
/\\p{ASCII}/u;`,
);
resolve();
} catch (err) {
reject(err);
}
});
});
});
});
Loading