Skip to content

Support for json / wasm modules #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 3, 2021
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");
});
});