Skip to content

Commit

Permalink
fix(plugin): handle escaped \0 char for import rewrites
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jan 8, 2024
1 parent 35ce726 commit 3b6ae8e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
sha1,
UMWASM_HELPERS_ID,
UNWASM_EXTERNAL_PREFIX,
UNWASM_EXTERNAL_RE,
UnwasmPluginOptions,
WasmAsset,
} from "./shared";
Expand Down Expand Up @@ -124,8 +125,7 @@ const unplugin = createUnplugin<UnwasmPluginOptions>((opts) => {
!(
chunk.moduleIds.some((id) => id.endsWith(".wasm")) ||
chunk.imports.some((id) => id.endsWith(".wasm"))
) ||
!code.includes(UNWASM_EXTERNAL_PREFIX)
)
) {
return;
}
Expand All @@ -146,9 +146,8 @@ const unplugin = createUnplugin<UnwasmPluginOptions>((opts) => {
asset,
};
};
const ReplaceRE = new RegExp(`${UNWASM_EXTERNAL_PREFIX}([^"']+)`, "g");
for (const match of code.matchAll(ReplaceRE)) {
const resolved = resolveImport(match[1]);
for (const match of code.matchAll(UNWASM_EXTERNAL_RE)) {
const resolved = resolveImport(match[2]);
const index = match.index as number;
const len = match[0].length;
if (!resolved || !index) {
Expand Down
1 change: 1 addition & 0 deletions src/plugin/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type WasmAsset = {
};

export const UNWASM_EXTERNAL_PREFIX = "\0unwasm:external:";
export const UNWASM_EXTERNAL_RE = /(\0|\\0)unwasm:external:([^"']+)/gu;
export const UMWASM_HELPERS_ID = "\0unwasm:helpers";

export function sha1(source: Buffer) {
Expand Down

0 comments on commit 3b6ae8e

Please sign in to comment.