Skip to content

Commit

Permalink
Stringify specifier synchronously in case of dynamic specifier
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Sep 21, 2023
1 parent c48d8ca commit 03e32f7
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
21 changes: 17 additions & 4 deletions packages/babel-plugin-proposal-import-wasm-source/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,23 @@ export default declare(api => {
}

buildFetchAsync ??= buildFetchSync;
const buildFetchAsyncWrapped: typeof buildFetchAsync = (expression, path) =>
template.expression.ast`
Promise.resolve().then(() => ${buildFetchAsync(expression, path)})
`;
const buildFetchAsyncWrapped: typeof buildFetchAsync = (
expression,
path,
) => {
if (t.isStringLiteral(expression)) {
return template.expression.ast`
Promise.resolve().then(() => ${buildFetchAsync(expression, path)})
`;
} else {
return template.expression.ast`
Promise.resolve(\`\${${expression}}\`).then((s) => ${buildFetchAsync(
t.identifier("s"),
path,
)})
`;
}
};

return {
buildFetch: buildFetchSync || buildFetchAsync,
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
let promise = Promise.resolve().then(() => typeof process === "object" && process.versions.node ? import("fs").then(fs => fs.promises.readFile(new URL(import.meta.resolve(getSpecifier())))).then(WebAssembly.compile) : WebAssembly.compileStreaming(fetch(import.meta.resolve(getSpecifier()))));
let promise = Promise.resolve(`${getSpecifier()}`).then(s => typeof process === "object" && process.versions.node ? import("fs").then(fs => fs.promises.readFile(new URL(import.meta.resolve(s)))).then(WebAssembly.compile) : WebAssembly.compileStreaming(fetch(import.meta.resolve(s))));
Original file line number Diff line number Diff line change
@@ -1 +1 @@
let promise = Promise.resolve().then(() => WebAssembly.compileStreaming(fetch(import.meta.resolve(getSpecifier()))));
let promise = Promise.resolve(`${getSpecifier()}`).then(s => WebAssembly.compileStreaming(fetch(import.meta.resolve(s))));
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"use strict";

let promise = Promise.resolve().then(() => require("fs").promises.readFile(require.resolve(getSpecifier())).then(WebAssembly.compile));
let promise = Promise.resolve(`${getSpecifier()}`).then(s => require("fs").promises.readFile(require.resolve(s)).then(WebAssembly.compile));
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import { promises as _promises } from "fs";
let promise = Promise.resolve().then(() => _promises.readFile(new URL(import.meta.resolve(getSpecifier()))).then(WebAssembly.compile));
let promise = Promise.resolve(`${getSpecifier()}`).then(s => _promises.readFile(new URL(import.meta.resolve(s))).then(WebAssembly.compile));

0 comments on commit 03e32f7

Please sign in to comment.