Skip to content

Commit e3fac11

Browse files
committed
fix: file access on esbuild-wasm
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.
1 parent d27dfe3 commit e3fac11

File tree

3 files changed

+67
-46
lines changed

3 files changed

+67
-46
lines changed

mod.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ export function denoPlugin(options: DenoPluginOptions = {}): esbuild.Plugin {
9898
return portableLoad(url, options);
9999
}
100100
}
101-
build.onLoad({ filter: /.*\.json/, namespace: "file" }, onLoad);
101+
// TODO(lucacasonato): once https://github.com/evanw/esbuild/pull/2968 is fixed, remove the catch all "file" handler
102+
// build.onLoad({ filter: /.*\.json/, namespace: "file" }, onLoad);
103+
build.onLoad({ filter: /.*/, namespace: "file" }, onLoad);
102104
build.onLoad({ filter: /.*/, namespace: "http" }, onLoad);
103105
build.onLoad({ filter: /.*/, namespace: "https" }, onLoad);
104106
build.onLoad({ filter: /.*/, namespace: "data" }, onLoad);

mod_test.ts

Lines changed: 61 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,49 @@
11
import { denoPlugin } from "./mod.ts";
2-
import { esbuild } from "./test_deps.ts";
2+
import { esbuildNative, esbuildWasm } from "./test_deps.ts";
33
import { assert, assertEquals } from "./test_deps.ts";
44

5-
const ALL = ["native", "portable"] as const;
5+
const LOADERS = ["native", "portable"] as const;
6+
const PLATFORMS = { "native": esbuildNative, "wasm": esbuildWasm };
7+
8+
const DEFAULT_OPTS = {
9+
write: false,
10+
format: "esm",
11+
// TODO(lucacasonato): remove when https://github.com/evanw/esbuild/pull/2968 is fixed
12+
absWorkingDir: Deno.cwd(),
13+
} as const;
614

715
function test(
816
name: string,
917
loaders: readonly ("native" | "portable")[],
10-
fn: (loader: "native" | "portable") => Promise<void>,
18+
fn: (
19+
esbuild: typeof esbuildNative,
20+
loader: "native" | "portable",
21+
) => Promise<void>,
1122
) {
12-
for (const loader of loaders) {
13-
Deno.test(`[${loader}] ${name}`, async () => {
14-
try {
15-
await esbuild.initialize({});
16-
await fn(loader);
17-
} finally {
18-
esbuild.stop();
19-
}
20-
});
23+
for (const [platform, esbuild] of Object.entries(PLATFORMS)) {
24+
for (const loader of loaders) {
25+
Deno.test({
26+
name: `[${loader}, ${platform}] ${name}`,
27+
sanitizeOps: platform === "wasm",
28+
sanitizeResources: platform === "wasm",
29+
ignore: platform === "wasm" && Deno.build.os === "windows",
30+
fn: async () => {
31+
try {
32+
await esbuild.initialize({});
33+
await fn(esbuild, loader);
34+
} finally {
35+
esbuild.stop();
36+
}
37+
},
38+
});
39+
}
2140
}
2241
}
2342

24-
test("remote ts", ALL, async (loader) => {
43+
test("remote ts", LOADERS, async (esbuild, loader) => {
2544
const res = await esbuild.build({
45+
...DEFAULT_OPTS,
2646
plugins: [denoPlugin({ loader })],
27-
write: false,
2847
entryPoints: ["https://deno.land/std@0.173.0/collections/without_all.ts"],
2948
});
3049
assertEquals(res.warnings, []);
@@ -36,10 +55,10 @@ test("remote ts", ALL, async (loader) => {
3655
assertEquals(withoutAll([1, 2, 3], [2, 3, 4]), [1]);
3756
});
3857

39-
test("local ts", ALL, async (loader) => {
58+
test("local ts", LOADERS, async (esbuild, loader) => {
4059
const res = await esbuild.build({
60+
...DEFAULT_OPTS,
4161
plugins: [denoPlugin({ loader })],
42-
write: false,
4362
entryPoints: ["./testdata/mod.ts"],
4463
});
4564
assertEquals(res.warnings, []);
@@ -51,10 +70,10 @@ test("local ts", ALL, async (loader) => {
5170
assertEquals(bool, "asd2");
5271
});
5372

54-
test("remote mts", ALL, async (loader) => {
73+
test("remote mts", LOADERS, async (esbuild, loader) => {
5574
const res = await esbuild.build({
75+
...DEFAULT_OPTS,
5676
plugins: [denoPlugin({ loader })],
57-
write: false,
5877
entryPoints: [
5978
"https://gist.githubusercontent.com/lucacasonato/4ad57db57ee8d44e4ec08d6a912e93a7/raw/f33e698b4445a7243d72dbfe95afe2d004c7ffc6/mod.mts",
6079
],
@@ -68,10 +87,10 @@ test("remote mts", ALL, async (loader) => {
6887
assertEquals(bool, "asd2");
6988
});
7089

71-
test("local mts", ALL, async (loader) => {
90+
test("local mts", LOADERS, async (esbuild, loader) => {
7291
const res = await esbuild.build({
92+
...DEFAULT_OPTS,
7393
plugins: [denoPlugin({ loader })],
74-
write: false,
7594
entryPoints: ["./testdata/mod.mts"],
7695
});
7796
assertEquals(res.warnings, []);
@@ -83,10 +102,10 @@ test("local mts", ALL, async (loader) => {
83102
assertEquals(bool, "asd2");
84103
});
85104

86-
test("remote js", ALL, async (loader) => {
105+
test("remote js", LOADERS, async (esbuild, loader) => {
87106
const res = await esbuild.build({
107+
...DEFAULT_OPTS,
88108
plugins: [denoPlugin({ loader })],
89-
write: false,
90109
entryPoints: ["https://crux.land/266TSp"],
91110
});
92111
assertEquals(res.warnings, []);
@@ -98,10 +117,10 @@ test("remote js", ALL, async (loader) => {
98117
assertEquals(bool, "asd");
99118
});
100119

101-
test("local js", ALL, async (loader) => {
120+
test("local js", LOADERS, async (esbuild, loader) => {
102121
const res = await esbuild.build({
122+
...DEFAULT_OPTS,
103123
plugins: [denoPlugin({ loader })],
104-
write: false,
105124
entryPoints: ["./testdata/mod.js"],
106125
});
107126
assertEquals(res.warnings, []);
@@ -113,10 +132,10 @@ test("local js", ALL, async (loader) => {
113132
assertEquals(bool, "asd");
114133
});
115134

116-
test("remote mjs", ALL, async (loader) => {
135+
test("remote mjs", LOADERS, async (esbuild, loader) => {
117136
const res = await esbuild.build({
137+
...DEFAULT_OPTS,
118138
plugins: [denoPlugin({ loader })],
119-
write: false,
120139
entryPoints: [
121140
"https://gist.githubusercontent.com/lucacasonato/4ad57db57ee8d44e4ec08d6a912e93a7/raw/f33e698b4445a7243d72dbfe95afe2d004c7ffc6/mod.mjs",
122141
],
@@ -130,10 +149,10 @@ test("remote mjs", ALL, async (loader) => {
130149
assertEquals(bool, "asd");
131150
});
132151

133-
test("local mjs", ALL, async (loader) => {
152+
test("local mjs", LOADERS, async (esbuild, loader) => {
134153
const res = await esbuild.build({
154+
...DEFAULT_OPTS,
135155
plugins: [denoPlugin({ loader })],
136-
write: false,
137156
entryPoints: ["./testdata/mod.mjs"],
138157
});
139158
assertEquals(res.warnings, []);
@@ -145,10 +164,10 @@ test("local mjs", ALL, async (loader) => {
145164
assertEquals(bool, "asd");
146165
});
147166

148-
test("remote jsx", ALL, async (loader) => {
167+
test("remote jsx", LOADERS, async (esbuild, loader) => {
149168
const res = await esbuild.build({
169+
...DEFAULT_OPTS,
150170
plugins: [denoPlugin({ loader })],
151-
write: false,
152171
entryPoints: ["https://crux.land/GeaWJ"],
153172
});
154173
assertEquals(res.warnings, []);
@@ -160,10 +179,10 @@ test("remote jsx", ALL, async (loader) => {
160179
assertEquals(m.default, "foo");
161180
});
162181

163-
test("local jsx", ALL, async (loader) => {
182+
test("local jsx", LOADERS, async (esbuild, loader) => {
164183
const res = await esbuild.build({
184+
...DEFAULT_OPTS,
165185
plugins: [denoPlugin({ loader })],
166-
write: false,
167186
entryPoints: ["./testdata/mod.jsx"],
168187
});
169188
assertEquals(res.warnings, []);
@@ -175,10 +194,10 @@ test("local jsx", ALL, async (loader) => {
175194
assertEquals(m.default, "foo");
176195
});
177196

178-
test("remote tsx", ALL, async (loader) => {
197+
test("remote tsx", LOADERS, async (esbuild, loader) => {
179198
const res = await esbuild.build({
199+
...DEFAULT_OPTS,
180200
plugins: [denoPlugin({ loader })],
181-
write: false,
182201
entryPoints: ["https://crux.land/2Qjyo7"],
183202
});
184203
assertEquals(res.warnings, []);
@@ -190,10 +209,10 @@ test("remote tsx", ALL, async (loader) => {
190209
assertEquals(m.default, "foo");
191210
});
192211

193-
test("local tsx", ALL, async (loader) => {
212+
test("local tsx", LOADERS, async (esbuild, loader) => {
194213
const res = await esbuild.build({
214+
...DEFAULT_OPTS,
195215
plugins: [denoPlugin({ loader })],
196-
write: false,
197216
entryPoints: ["./testdata/mod.tsx"],
198217
});
199218
assertEquals(res.warnings, []);
@@ -205,10 +224,10 @@ test("local tsx", ALL, async (loader) => {
205224
assertEquals(m.default, "foo");
206225
});
207226

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

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

227-
test("bundle import map", ALL, async (loader) => {
246+
test("bundle import map", LOADERS, async (esbuild, loader) => {
228247
const res = await esbuild.build({
248+
...DEFAULT_OPTS,
229249
plugins: [
230250
denoPlugin({ importMapURL, loader }),
231251
],
232-
write: false,
233252
bundle: true,
234253
platform: "neutral",
235254
entryPoints: ["./testdata/importmap.js"],
@@ -243,11 +262,10 @@ test("bundle import map", ALL, async (loader) => {
243262
assertEquals(bool, "asd2");
244263
});
245264

246-
test("local json", ALL, async (loader) => {
265+
test("local json", LOADERS, async (esbuild, loader) => {
247266
const res = await esbuild.build({
267+
...DEFAULT_OPTS,
248268
plugins: [denoPlugin({ loader })],
249-
write: false,
250-
format: "esm",
251269
entryPoints: ["./testdata/data.json"],
252270
});
253271
assertEquals(res.warnings, []);

test_deps.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import * as esbuild from "https://deno.land/x/esbuild@v0.17.11/mod.js";
2-
export { esbuild };
1+
import * as esbuildNative from "https://deno.land/x/esbuild@v0.17.11/mod.js";
2+
import * as esbuildWasm from "https://deno.land/x/esbuild@v0.17.11/wasm.js";
3+
export { esbuildNative, esbuildWasm };
34
export {
45
assert,
56
assertEquals,

0 commit comments

Comments
 (0)