Skip to content

Commit 10b9a9c

Browse files
Add support for with { type: "bytes" }
1 parent e135e27 commit 10b9a9c

108 files changed

Lines changed: 571 additions & 5 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Makefile.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile.source.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,14 +518,14 @@ target["bootstrap-flow"] = function () {
518518

519519
target["new-version-checklist"] = function () {
520520
// eslint-disable-next-line no-constant-condition
521-
if (0) {
521+
if (1) {
522522
console.log(
523523
`
524524
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
525525
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
526526
!!!!!! !!!!!!
527-
!!!!!! Write any important message here, and change the !!!!!!
528-
!!!!!! if (0) above to if (1) !!!!!!
527+
!!!!!! Update the minVersion for the immutableUint8Array !!!!!!
528+
!!!!!! helper !!!!!!
529529
!!!!!! !!!!!!
530530
!!!!!! !!!!!!
531531
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

packages/babel-helpers/src/helpers-generated.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,19 @@ const helpers: Record<string, Helper> = {
660660
dependencies: {},
661661
internal: false,
662662
}),
663+
// size: 119, gzip size: 105
664+
immutableUint8Array: helper(
665+
"8.0.0-rc.2",
666+
"function _immutableUint8Array(r){var t=r.buffer;return new Uint8Array(t.transferToImmutable?t.transferToImmutable():t)}",
667+
{
668+
globals: ["Uint8Array"],
669+
locals: { _immutableUint8Array: ["body.0.id"] },
670+
exportBindingAssignments: [],
671+
exportName: "_immutableUint8Array",
672+
dependencies: {},
673+
internal: false,
674+
},
675+
),
663676
// size: 537, gzip size: 258
664677
importDeferProxy: helper(
665678
"7.23.0",
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* @minVersion 8.0.0-rc.2 */
2+
3+
declare global {
4+
interface ArrayBuffer {
5+
transferToImmutable?(): ArrayBuffer;
6+
}
7+
}
8+
9+
export default function _immutableUint8Array(
10+
u8orNodeBuffer: Uint8Array<ArrayBuffer>,
11+
) {
12+
var buf = u8orNodeBuffer.buffer;
13+
return new Uint8Array(
14+
buf.transferToImmutable ? buf.transferToImmutable() : buf,
15+
);
16+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
src
2+
test
3+
*.log
4+
tsconfig.json
5+
tsconfig.tsbuildinfo
Lines changed: 19 additions & 0 deletions
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"name": "@babel/plugin-proposal-bytes-modules",
3+
"version": "8.0.0-rc.2",
4+
"description": "Transform bytes modules imports (`import '...' with { type: 'bytes' }`) to work in browsers and Node.js.",
5+
"repository": {
6+
"type": "git",
7+
"url": "https://github.com/babel/babel.git",
8+
"directory": "packages/babel-plugin-proposal-bytes-modules"
9+
},
10+
"homepage": "https://babel.dev/docs/en/next/babel-plugin-proposal-bytes-modules",
11+
"license": "MIT",
12+
"publishConfig": {
13+
"access": "public"
14+
},
15+
"main": "./lib/index.js",
16+
"exports": {
17+
".": {
18+
"types": "./lib/index.d.ts",
19+
"default": "./lib/index.js"
20+
},
21+
"./package.json": "./package.json"
22+
},
23+
"keywords": [
24+
"babel-plugin",
25+
"import",
26+
"source",
27+
"phase",
28+
"wasm",
29+
"WebAssembly",
30+
"assertions",
31+
"proposal",
32+
"stage-3"
33+
],
34+
"dependencies": {
35+
"@babel/helper-import-to-platform-api": "workspace:^",
36+
"@babel/helper-plugin-utils": "workspace:^"
37+
},
38+
"peerDependencies": {
39+
"@babel/core": "workspace:^"
40+
},
41+
"devDependencies": {
42+
"@babel/core": "workspace:^",
43+
"@babel/helper-plugin-test-runner": "workspace:^",
44+
"babel-plugin-polyfill-corejs3": "^0.14.0"
45+
},
46+
"engines": {
47+
"node": "^20.19.0 || >=22.12.0"
48+
},
49+
"author": "The Babel Team (https://babel.dev/team)",
50+
"type": "module"
51+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import { declare } from "@babel/helper-plugin-utils";
2+
import type { types as t, File } from "@babel/core";
3+
import {
4+
importToPlatformApi,
5+
buildParallelStaticImports,
6+
type Pieces,
7+
type Builders,
8+
} from "@babel/helper-import-to-platform-api";
9+
10+
export default declare(api => {
11+
const { types: t, template } = api;
12+
api.assertVersion(REQUIRED_VERSION("^8.0.0"));
13+
14+
const targets = api.targets();
15+
16+
let helperESM: Builders;
17+
let helperCJS: Builders;
18+
19+
const transformers: Pieces = {
20+
webFetch: (fetch, file) =>
21+
template.expression.ast`
22+
${fetch}.then(r => r.bytes()).then(${file.addHelper("immutableUint8Array")})
23+
`,
24+
nodeFsSync: (buf, file) =>
25+
t.callExpression(file.addHelper("immutableUint8Array"), [buf]),
26+
nodeFsAsync: file => file.addHelper("immutableUint8Array"),
27+
};
28+
29+
const getHelper = (file: File) => {
30+
const modules = file.get("@babel/plugin-transform-modules-*");
31+
if (modules === "commonjs") {
32+
return (helperCJS ??= importToPlatformApi(targets, transformers, true));
33+
}
34+
if (modules == null) {
35+
return (helperESM ??= importToPlatformApi(targets, transformers, false));
36+
}
37+
throw new Error(
38+
`@babel/plugin-proposal-bytes-modules can only be used when not ` +
39+
`compiling modules, or when compiling them to CommonJS.`,
40+
);
41+
};
42+
43+
function getAttributeKey({ key }: t.ImportAttribute): string {
44+
return t.isIdentifier(key) ? key.name : key.value;
45+
}
46+
47+
function hasTypeBytes(attributes: t.ImportAttribute[]) {
48+
return !!attributes?.some(
49+
attr => getAttributeKey(attr) === "type" && attr.value.value === "bytes",
50+
);
51+
}
52+
53+
return {
54+
name: "proposal-bytes-modules",
55+
56+
visitor: {
57+
Program(path) {
58+
if (path.node.sourceType !== "module") return;
59+
60+
const helper = getHelper(this.file);
61+
62+
const data = [];
63+
for (const decl of path.get("body")) {
64+
if (!decl.isImportDeclaration()) continue;
65+
const attributes = decl.node.attributes || undefined;
66+
if (!hasTypeBytes(attributes)) continue;
67+
68+
if (decl.node.phase != null) {
69+
throw decl.buildCodeFrameError(
70+
"Bytes modules do not support phase modifiers.",
71+
);
72+
}
73+
if (attributes.length > 1) {
74+
const paths = decl.get("attributes");
75+
const index = getAttributeKey(attributes[0]) === "type" ? 1 : 0;
76+
throw paths[index].buildCodeFrameError(
77+
"Unknown attribute for bytes modules.",
78+
);
79+
}
80+
81+
let id: t.Identifier;
82+
let needsNS = false;
83+
for (const specifier of decl.get("specifiers")) {
84+
if (specifier.isImportSpecifier()) {
85+
throw specifier.buildCodeFrameError(
86+
"Bytes modules do not support named imports.",
87+
);
88+
}
89+
90+
id = specifier.node.local;
91+
needsNS = specifier.isImportNamespaceSpecifier();
92+
}
93+
id ??= path.scope.generateUidIdentifier("_");
94+
95+
let fetch = helper.buildFetch(decl.node.source, this.file);
96+
97+
if (needsNS) {
98+
if (helper.needsAwait) {
99+
fetch = template.expression.ast`
100+
${fetch}.then(j => ({ default: j }))
101+
`;
102+
} else {
103+
fetch = template.expression.ast`{ default: ${fetch} }`;
104+
}
105+
}
106+
107+
data.push({ id, fetch });
108+
decl.remove();
109+
}
110+
if (data.length === 0) return;
111+
112+
const decl = buildParallelStaticImports(data, helper.needsAwait);
113+
if (decl) path.unshiftContainer("body", decl);
114+
},
115+
},
116+
};
117+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import j from "./x" with { type: "bytes" };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"sourceType": "module",
3+
"targets": []
4+
}

0 commit comments

Comments
 (0)