Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions lib/node-loader-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,25 @@ export function resolve(specifier, context, defaultResolve) {

export function getFormat(url, context, defaultGetFormat) {
if (useLoader(url)) {
let isModule;
let format;
// TODO: maybe change to content-type / mime type check rather than file extensions
if (url.endsWith(".mjs")) {
isModule = true;
format = "module";
} else if (url.endsWith(".cjs")) {
isModule = false;
format = "commonjs";
} else if (url.endsWith(".wasm")) {
format = "wasm";
} else if (url.endsWith(".json")) {
format = "json";
} else {
// default to true, since NodeJS loaders only are triggered by ESM code
// Alternatively, we could consider looking up the nearest package.json to the process.cwd()
// And seeing if it has `"type": "module"`
isModule = true;
format = "module";
}

return {
format: "module",
format,
};
}

Expand Down
7 changes: 5 additions & 2 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ describe(`basic http / https tests`, () => {
const ns = await import(
"http://unpkg.com/single-spa@5.5.5/lib/esm/single-spa.dev.js"
);
console.log("ns", ns);
assert.ok(ns.start);
ns.start();
});
Expand All @@ -14,8 +13,12 @@ describe(`basic http / https tests`, () => {
const ns = await import(
"https://unpkg.com/single-spa@5.5.5/lib/esm/single-spa.dev.js"
);
console.log("ns", ns);
assert.ok(ns.start);
ns.start();
});

it(`can load a json module`, async () => {
const ns = await import("https://unpkg.com/single-spa@5.5.5/package.json");
assert.equal(ns.default.name, "single-spa");
});
});