Skip to content

Commit

Permalink
improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Dec 13, 2023
1 parent 842e7bb commit 01d6f28
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/rollup/plugins/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,28 +71,29 @@ export function wasmImport(): Plugin {
},
load(id) {
if (!id.startsWith(WASM_ID_PREFIX)) {
return null;
return;
}
const asset = wasmImports.get(id);
if (asset) {
return {
code: `export default "${asset.id}";`,
map: null,
};
if (!asset) {
return;
}
return {
code: `export default "${asset.id}";`,
map: null,
};
},
renderChunk(code, chunk, options) {
if (
!chunk.moduleIds.some((id) => id.startsWith(WASM_ID_PREFIX)) ||
!code.includes(WASM_ID_PREFIX)
) {
return null;
return;
}

const isIIFE = options.format === "iife" || options.format === "umd";

const s = new MagicString(code);
const ReplaceRE = new RegExp(`"(${WASM_ID_PREFIX}[^"]+)"`, "g");

const resolveImport = (id) => {
if (typeof id !== "string" || !id.startsWith(WASM_ID_PREFIX)) {
return null;
Expand All @@ -106,6 +107,8 @@ export function wasmImport(): Plugin {
(nestedLevel ? "../".repeat(nestedLevel) : "./") + asset.fileName
);
};

const ReplaceRE = new RegExp(`"(${WASM_ID_PREFIX}[^"]+)"`, "g");
for (const match of code.matchAll(ReplaceRE)) {
const resolved = resolveImport(match[1]);
if (!resolved) {
Expand Down

0 comments on commit 01d6f28

Please sign in to comment.