Skip to content

Commit

Permalink
fix: file access on esbuild-wasm
Browse files Browse the repository at this point in the history
This is a temporary fix for file loading on WASM. It will ultimately be
fixed by evanw/esbuild#2968.

This commit also improves the test suite to run all tests in both native
and wasm versions of esbuild.
  • Loading branch information
lucacasonato committed Mar 7, 2023
1 parent d27dfe3 commit e3fac11
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 46 deletions.
4 changes: 3 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ export function denoPlugin(options: DenoPluginOptions = {}): esbuild.Plugin {
return portableLoad(url, options);
}
}
build.onLoad({ filter: /.*\.json/, namespace: "file" }, onLoad);
// TODO(lucacasonato): once https://github.com/evanw/esbuild/pull/2968 is fixed, remove the catch all "file" handler
// build.onLoad({ filter: /.*\.json/, namespace: "file" }, onLoad);
build.onLoad({ filter: /.*/, namespace: "file" }, onLoad);
build.onLoad({ filter: /.*/, namespace: "http" }, onLoad);
build.onLoad({ filter: /.*/, namespace: "https" }, onLoad);
build.onLoad({ filter: /.*/, namespace: "data" }, onLoad);
Expand Down
104 changes: 61 additions & 43 deletions mod_test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,49 @@
import { denoPlugin } from "./mod.ts";
import { esbuild } from "./test_deps.ts";
import { esbuildNative, esbuildWasm } from "./test_deps.ts";
import { assert, assertEquals } from "./test_deps.ts";

const ALL = ["native", "portable"] as const;
const LOADERS = ["native", "portable"] as const;
const PLATFORMS = { "native": esbuildNative, "wasm": esbuildWasm };

const DEFAULT_OPTS = {
write: false,
format: "esm",
// TODO(lucacasonato): remove when https://github.com/evanw/esbuild/pull/2968 is fixed
absWorkingDir: Deno.cwd(),
} as const;

function test(
name: string,
loaders: readonly ("native" | "portable")[],
fn: (loader: "native" | "portable") => Promise<void>,
fn: (
esbuild: typeof esbuildNative,
loader: "native" | "portable",
) => Promise<void>,
) {
for (const loader of loaders) {
Deno.test(`[${loader}] ${name}`, async () => {
try {
await esbuild.initialize({});
await fn(loader);
} finally {
esbuild.stop();
}
});
for (const [platform, esbuild] of Object.entries(PLATFORMS)) {
for (const loader of loaders) {
Deno.test({
name: `[${loader}, ${platform}] ${name}`,
sanitizeOps: platform === "wasm",
sanitizeResources: platform === "wasm",
ignore: platform === "wasm" && Deno.build.os === "windows",
fn: async () => {
try {
await esbuild.initialize({});
await fn(esbuild, loader);
} finally {
esbuild.stop();
}
},
});
}
}
}

test("remote ts", ALL, async (loader) => {
test("remote ts", LOADERS, async (esbuild, loader) => {
const res = await esbuild.build({
...DEFAULT_OPTS,
plugins: [denoPlugin({ loader })],
write: false,
entryPoints: ["https://deno.land/std@0.173.0/collections/without_all.ts"],
});
assertEquals(res.warnings, []);
Expand All @@ -36,10 +55,10 @@ test("remote ts", ALL, async (loader) => {
assertEquals(withoutAll([1, 2, 3], [2, 3, 4]), [1]);
});

test("local ts", ALL, async (loader) => {
test("local ts", LOADERS, async (esbuild, loader) => {
const res = await esbuild.build({
...DEFAULT_OPTS,
plugins: [denoPlugin({ loader })],
write: false,
entryPoints: ["./testdata/mod.ts"],
});
assertEquals(res.warnings, []);
Expand All @@ -51,10 +70,10 @@ test("local ts", ALL, async (loader) => {
assertEquals(bool, "asd2");
});

test("remote mts", ALL, async (loader) => {
test("remote mts", LOADERS, async (esbuild, loader) => {
const res = await esbuild.build({
...DEFAULT_OPTS,
plugins: [denoPlugin({ loader })],
write: false,
entryPoints: [
"https://gist.githubusercontent.com/lucacasonato/4ad57db57ee8d44e4ec08d6a912e93a7/raw/f33e698b4445a7243d72dbfe95afe2d004c7ffc6/mod.mts",
],
Expand All @@ -68,10 +87,10 @@ test("remote mts", ALL, async (loader) => {
assertEquals(bool, "asd2");
});

test("local mts", ALL, async (loader) => {
test("local mts", LOADERS, async (esbuild, loader) => {
const res = await esbuild.build({
...DEFAULT_OPTS,
plugins: [denoPlugin({ loader })],
write: false,
entryPoints: ["./testdata/mod.mts"],
});
assertEquals(res.warnings, []);
Expand All @@ -83,10 +102,10 @@ test("local mts", ALL, async (loader) => {
assertEquals(bool, "asd2");
});

test("remote js", ALL, async (loader) => {
test("remote js", LOADERS, async (esbuild, loader) => {
const res = await esbuild.build({
...DEFAULT_OPTS,
plugins: [denoPlugin({ loader })],
write: false,
entryPoints: ["https://crux.land/266TSp"],
});
assertEquals(res.warnings, []);
Expand All @@ -98,10 +117,10 @@ test("remote js", ALL, async (loader) => {
assertEquals(bool, "asd");
});

test("local js", ALL, async (loader) => {
test("local js", LOADERS, async (esbuild, loader) => {
const res = await esbuild.build({
...DEFAULT_OPTS,
plugins: [denoPlugin({ loader })],
write: false,
entryPoints: ["./testdata/mod.js"],
});
assertEquals(res.warnings, []);
Expand All @@ -113,10 +132,10 @@ test("local js", ALL, async (loader) => {
assertEquals(bool, "asd");
});

test("remote mjs", ALL, async (loader) => {
test("remote mjs", LOADERS, async (esbuild, loader) => {
const res = await esbuild.build({
...DEFAULT_OPTS,
plugins: [denoPlugin({ loader })],
write: false,
entryPoints: [
"https://gist.githubusercontent.com/lucacasonato/4ad57db57ee8d44e4ec08d6a912e93a7/raw/f33e698b4445a7243d72dbfe95afe2d004c7ffc6/mod.mjs",
],
Expand All @@ -130,10 +149,10 @@ test("remote mjs", ALL, async (loader) => {
assertEquals(bool, "asd");
});

test("local mjs", ALL, async (loader) => {
test("local mjs", LOADERS, async (esbuild, loader) => {
const res = await esbuild.build({
...DEFAULT_OPTS,
plugins: [denoPlugin({ loader })],
write: false,
entryPoints: ["./testdata/mod.mjs"],
});
assertEquals(res.warnings, []);
Expand All @@ -145,10 +164,10 @@ test("local mjs", ALL, async (loader) => {
assertEquals(bool, "asd");
});

test("remote jsx", ALL, async (loader) => {
test("remote jsx", LOADERS, async (esbuild, loader) => {
const res = await esbuild.build({
...DEFAULT_OPTS,
plugins: [denoPlugin({ loader })],
write: false,
entryPoints: ["https://crux.land/GeaWJ"],
});
assertEquals(res.warnings, []);
Expand All @@ -160,10 +179,10 @@ test("remote jsx", ALL, async (loader) => {
assertEquals(m.default, "foo");
});

test("local jsx", ALL, async (loader) => {
test("local jsx", LOADERS, async (esbuild, loader) => {
const res = await esbuild.build({
...DEFAULT_OPTS,
plugins: [denoPlugin({ loader })],
write: false,
entryPoints: ["./testdata/mod.jsx"],
});
assertEquals(res.warnings, []);
Expand All @@ -175,10 +194,10 @@ test("local jsx", ALL, async (loader) => {
assertEquals(m.default, "foo");
});

test("remote tsx", ALL, async (loader) => {
test("remote tsx", LOADERS, async (esbuild, loader) => {
const res = await esbuild.build({
...DEFAULT_OPTS,
plugins: [denoPlugin({ loader })],
write: false,
entryPoints: ["https://crux.land/2Qjyo7"],
});
assertEquals(res.warnings, []);
Expand All @@ -190,10 +209,10 @@ test("remote tsx", ALL, async (loader) => {
assertEquals(m.default, "foo");
});

test("local tsx", ALL, async (loader) => {
test("local tsx", LOADERS, async (esbuild, loader) => {
const res = await esbuild.build({
...DEFAULT_OPTS,
plugins: [denoPlugin({ loader })],
write: false,
entryPoints: ["./testdata/mod.tsx"],
});
assertEquals(res.warnings, []);
Expand All @@ -205,10 +224,10 @@ test("local tsx", ALL, async (loader) => {
assertEquals(m.default, "foo");
});

test("bundle remote imports", ALL, async (loader) => {
test("bundle remote imports", LOADERS, async (esbuild, loader) => {
const res = await esbuild.build({
...DEFAULT_OPTS,
plugins: [denoPlugin({ loader })],
write: false,
bundle: true,
platform: "neutral",
entryPoints: ["https://deno.land/std@0.173.0/uuid/mod.ts"],
Expand All @@ -224,12 +243,12 @@ test("bundle remote imports", ALL, async (loader) => {

const importMapURL = new URL("./testdata/importmap.json", import.meta.url);

test("bundle import map", ALL, async (loader) => {
test("bundle import map", LOADERS, async (esbuild, loader) => {
const res = await esbuild.build({
...DEFAULT_OPTS,
plugins: [
denoPlugin({ importMapURL, loader }),
],
write: false,
bundle: true,
platform: "neutral",
entryPoints: ["./testdata/importmap.js"],
Expand All @@ -243,11 +262,10 @@ test("bundle import map", ALL, async (loader) => {
assertEquals(bool, "asd2");
});

test("local json", ALL, async (loader) => {
test("local json", LOADERS, async (esbuild, loader) => {
const res = await esbuild.build({
...DEFAULT_OPTS,
plugins: [denoPlugin({ loader })],
write: false,
format: "esm",
entryPoints: ["./testdata/data.json"],
});
assertEquals(res.warnings, []);
Expand Down
5 changes: 3 additions & 2 deletions test_deps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as esbuild from "https://deno.land/x/esbuild@v0.17.11/mod.js";
export { esbuild };
import * as esbuildNative from "https://deno.land/x/esbuild@v0.17.11/mod.js";
import * as esbuildWasm from "https://deno.land/x/esbuild@v0.17.11/wasm.js";
export { esbuildNative, esbuildWasm };
export {
assert,
assertEquals,
Expand Down

0 comments on commit e3fac11

Please sign in to comment.