Skip to content

Commit ee447c7

Browse files
authored
fix: maps overrides should take priority (#197)
1 parent 95c95b7 commit ee447c7

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

src/plugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export async function load({
6868
const config = await helpers.getDefaults(path);
6969

7070
const fetched = await fetchImportMaps([...config.map, ...pUrls]);
71-
const mappings = pMaps.concat(fetched);
71+
const mappings = fetched.concat(pMaps);
7272

7373
await importMapPlugin.load(mappings);
7474
}

tap-snapshots/test/plugin.js.test.cjs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,31 @@
55
* Make sure to inspect the output below. Do not ignore changes!
66
*/
77
'use strict'
8+
exports[`test/plugin.js > TAP > plugin() - direct definitions import should override fetched import maps > import maps from direct definition 1`] = `
9+
// fixtures/modules/file/main.js
10+
import { html } from "https://cdn.eik.dev/lit-html/v3";
11+
import { css } from "https://cdn.eik.dev/lit-html/v1";
12+
import { LitElement } from "https://cdn.eik.dev/lit-element/v2";
13+
var Inner = class extends LitElement {
14+
static get styles() {
15+
return [
16+
css\`
17+
:host {
18+
color: red;
19+
}
20+
\`
21+
];
22+
}
23+
render(world) {
24+
return html\`<p>Hello \${world}!</p>\`;
25+
}
26+
};
27+
export {
28+
Inner as default
29+
};
30+
31+
`
32+
833
exports[`test/plugin.js > TAP > plugin() - import map fetched from a URL > import maps from urls 1`] = `
934
// fixtures/modules/file/main.js
1035
import { html } from "https://cdn.eik.dev/lit-html/v2";

test/plugin.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,71 @@ const bufferToString = (buff) => {
2323
return str.join("");
2424
};
2525

26+
tap.test(
27+
"plugin() - direct definitions import should override fetched import maps",
28+
async (t) => {
29+
const app = fastify();
30+
app.server.keepAliveTimeout = 20;
31+
app.get("/one", (request, reply) => {
32+
reply.send({
33+
imports: {
34+
"lit-element": "https://cdn.eik.dev/lit-element/v2",
35+
},
36+
});
37+
});
38+
app.get("/two", (request, reply) => {
39+
reply.send({
40+
imports: {
41+
"lit-html": "https://cdn.eik.dev/lit-html/v1",
42+
"lit-html/lit-html": "https://cdn.eik.dev/lit-html/v2",
43+
},
44+
});
45+
});
46+
const address = await app.listen();
47+
48+
await fs.promises.writeFile(
49+
path.join(process.cwd(), "eik.json"),
50+
JSON.stringify({
51+
name: "test",
52+
server: "https://localhost",
53+
version: "1.0.0",
54+
files: "./dist",
55+
"import-map": `${address}/one`,
56+
}),
57+
);
58+
59+
await plugin.load({
60+
maps: [
61+
{
62+
imports: {
63+
"lit-html/lit-html": "https://cdn.eik.dev/lit-html/v3",
64+
},
65+
},
66+
],
67+
urls: [`${address}/one`, `${address}/two`],
68+
});
69+
70+
const result = await esbuild.build({
71+
entryPoints: [file],
72+
bundle: true,
73+
format: "esm",
74+
minify: false,
75+
sourcemap: false,
76+
target: ["esnext"],
77+
plugins: [plugin.plugin()],
78+
write: false,
79+
});
80+
81+
const code = bufferToString(result.outputFiles);
82+
t.matchSnapshot(clean(code), "import maps from direct definition");
83+
84+
plugin.clear();
85+
await app.close();
86+
await fs.promises.unlink(path.join(process.cwd(), "eik.json"));
87+
t.end();
88+
},
89+
);
90+
2691
tap.test("plugin() - import map fetched from a URL", async (t) => {
2792
const app = fastify();
2893
app.server.keepAliveTimeout = 20;

0 commit comments

Comments
 (0)