diff --git a/lib/node-loader-http.js b/lib/node-loader-http.js index 2936522..d5d13d8 100644 --- a/lib/node-loader-http.js +++ b/lib/node-loader-http.js @@ -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, }; } diff --git a/test/basic.test.js b/test/basic.test.js index 7be21c7..faf17b8 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -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(); }); @@ -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"); + }); });