Skip to content

Commit eda99f2

Browse files
author
Joel Denning
authored
Support for json / wasm modules (#1)
1 parent 1d1246c commit eda99f2

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

lib/node-loader-http.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,25 @@ export function resolve(specifier, context, defaultResolve) {
2727

2828
export function getFormat(url, context, defaultGetFormat) {
2929
if (useLoader(url)) {
30-
let isModule;
30+
let format;
31+
// TODO: maybe change to content-type / mime type check rather than file extensions
3132
if (url.endsWith(".mjs")) {
32-
isModule = true;
33+
format = "module";
3334
} else if (url.endsWith(".cjs")) {
34-
isModule = false;
35+
format = "commonjs";
36+
} else if (url.endsWith(".wasm")) {
37+
format = "wasm";
38+
} else if (url.endsWith(".json")) {
39+
format = "json";
3540
} else {
3641
// default to true, since NodeJS loaders only are triggered by ESM code
3742
// Alternatively, we could consider looking up the nearest package.json to the process.cwd()
3843
// And seeing if it has `"type": "module"`
39-
isModule = true;
44+
format = "module";
4045
}
4146

4247
return {
43-
format: "module",
48+
format,
4449
};
4550
}
4651

test/basic.test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ describe(`basic http / https tests`, () => {
55
const ns = await import(
66
"http://unpkg.com/single-spa@5.5.5/lib/esm/single-spa.dev.js"
77
);
8-
console.log("ns", ns);
98
assert.ok(ns.start);
109
ns.start();
1110
});
@@ -14,8 +13,12 @@ describe(`basic http / https tests`, () => {
1413
const ns = await import(
1514
"https://unpkg.com/single-spa@5.5.5/lib/esm/single-spa.dev.js"
1615
);
17-
console.log("ns", ns);
1816
assert.ok(ns.start);
1917
ns.start();
2018
});
19+
20+
it(`can load a json module`, async () => {
21+
const ns = await import("https://unpkg.com/single-spa@5.5.5/package.json");
22+
assert.equal(ns.default.name, "single-spa");
23+
});
2124
});

0 commit comments

Comments
 (0)