Skip to content

Commit 09af948

Browse files
committed
feat(wasm): support build multiple files with same name
1 parent 57fa93a commit 09af948

File tree

2 files changed

+46
-18
lines changed

2 files changed

+46
-18
lines changed

biome.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
"rules": {
2222
"recommended": true,
2323
"suspicious": {
24-
"noControlCharactersInRegex": "off"
24+
"noControlCharactersInRegex": "off",
25+
"noRedeclare": "warn",
26+
"noShadowRestrictedNames": "warn"
2527
}
2628
}
2729
},

src/wasm/plugin.ts

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
WASM_MAP_EXTENSION,
1616
WASM_TEXT_EXTENSION,
1717
} from "./constants";
18+
import { createHash } from "node:crypto";
1819

1920
interface AssemblyScriptOptions {
2021
optimize?: boolean;
@@ -62,7 +63,7 @@ function getCompilerFlags(options: AssemblyScriptOptions): string[] {
6263
}
6364

6465
export default function webAssemblySupport(
65-
options: AssemblyScriptOptions = {},
66+
options: AssemblyScriptOptions = {}
6667
) {
6768
return {
6869
name: "wasm-support",
@@ -96,40 +97,40 @@ export default function webAssemblySupport(
9697
const wasmFileName = fileName.replace(TS_EXTENSION, WASM_EXTENSION);
9798
const wasmTextFileName = fileName.replace(
9899
TS_EXTENSION,
99-
WASM_TEXT_EXTENSION,
100+
WASM_TEXT_EXTENSION
100101
);
101102
const jsBindingsFileName = fileName.replace(TS_EXTENSION, JS_EXTENSION);
102103
const dTsFileName = fileName.replace(TS_EXTENSION, D_TS_EXTENSION);
103104
const sourceMapFileName = fileName.replace(
104105
TS_EXTENSION,
105-
WASM_MAP_EXTENSION,
106+
WASM_MAP_EXTENSION
106107
);
107108

108109
const standaloneEnvironment = new StandaloneEnvironment(cwd);
109110
const tempCodeFileName = path.join(
110111
standaloneEnvironment.standaloneOutputPath,
111-
fileName,
112+
fileName
112113
);
113114
await standaloneEnvironment.setup();
114115
const outFilePath = path.join(
115116
standaloneEnvironment.standaloneOutputPath,
116-
wasmFileName,
117+
wasmFileName
117118
);
118119
const textFilePath = path.join(
119120
standaloneEnvironment.standaloneOutputPath,
120-
wasmTextFileName,
121+
wasmTextFileName
121122
);
122123
const jsBindingsPath = path.join(
123124
standaloneEnvironment.standaloneOutputPath,
124-
jsBindingsFileName,
125+
jsBindingsFileName
125126
);
126127
const dTsPath = path.join(
127128
standaloneEnvironment.standaloneOutputPath,
128-
dTsFileName,
129+
dTsFileName
129130
);
130131
const sourceMapPath = path.join(
131132
standaloneEnvironment.standaloneOutputPath,
132-
sourceMapFileName,
133+
sourceMapFileName
133134
);
134135

135136
await writeFile(tempCodeFileName, cleanCode);
@@ -140,20 +141,31 @@ export default function webAssemblySupport(
140141
outFilePath,
141142
"--textFile",
142143
textFilePath,
144+
"--sourceMap",
145+
sourceMapPath,
143146
"--bindings",
144147
"esm",
145-
"--sourceMap",
148+
"--optimize",
149+
"--optimizeLevel",
150+
"3",
151+
"--shrinkLevel",
152+
"0",
153+
"--noAssert",
154+
"--converge",
155+
"--exportRuntime",
146156
...getCompilerFlags(options),
147157
];
148158

149159
try {
150-
const { error, stderr } = await asc.main(compilerOptions, {
160+
const { error } = await asc.main(compilerOptions, {
151161
stdout: process.stdout,
152162
stderr: process.stderr,
153163
});
154164

155165
if (error) {
156-
throw new Error(`AssemblyScript compilation failed: ${stderr}`);
166+
throw new Error(
167+
`AssemblyScript compilation failed: ${error.message}`
168+
);
157169
}
158170

159171
const [
@@ -170,27 +182,41 @@ export default function webAssemblySupport(
170182
readJson(sourceMapPath, "utf-8"),
171183
]);
172184

185+
const idUniqueHash = createHash("sha1")
186+
.update(id)
187+
.digest("hex")
188+
.slice(0, 8);
189+
const wasmDistPath = path.join(
190+
"wasm",
191+
`${idUniqueHash}-${wasmFileName}`
192+
);
193+
const wasmTextDistPath = path.join(
194+
"wasm",
195+
`${idUniqueHash}-${wasmTextFileName}`
196+
);
197+
const dTsDistPath = path.join("wasm", `${idUniqueHash}-${dTsFileName}`);
198+
173199
const referenceId = (this as any).emitFile({
174200
type: "asset",
175-
fileName: path.join("wasm", wasmFileName),
201+
fileName: wasmDistPath,
176202
source: wasmBinaryContent,
177203
});
178204
(this as any).emitFile({
179205
type: "asset",
180-
fileName: path.join("wasm", wasmTextFileName),
206+
fileName: wasmTextDistPath,
181207
source: wasmTextContent,
182208
});
183209
(this as any).emitFile({
184210
type: "asset",
185-
fileName: path.join("wasm", dTsFileName),
211+
fileName: dTsDistPath,
186212
source: dTsContent,
187213
});
188214

189215
await standaloneEnvironment.clean();
190216

191217
const resolvedBindings = generatedBindings.replace(
192218
BINDINGS_DEFAULT_WASM_URL_REGEX,
193-
`new URL(import.meta.ROLLUP_FILE_URL_${referenceId})`,
219+
`new URL(import.meta.ROLLUP_FILE_URL_${referenceId})`
194220
);
195221
return {
196222
code: resolvedBindings,
@@ -199,7 +225,7 @@ export default function webAssemblySupport(
199225
} catch (error) {
200226
if (error instanceof Error) {
201227
throw new Error(
202-
`AssemblyScript compilation failed: ${error.message}`,
228+
`AssemblyScript compilation failed: ${error.message}`
203229
);
204230
}
205231
}

0 commit comments

Comments
 (0)