Skip to content

Commit 993ebe7

Browse files
authored
test(svelte): Add test for svelte <style> + rollup chunking (#639)
It works in this test, but based on stuff I've seen working on the website I'm not convinced that there isn't still a bug.
1 parent e560455 commit 993ebe7

File tree

10 files changed

+118
-5
lines changed

10 files changed

+118
-5
lines changed

jest.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module.exports = {
2222
"<rootDir>/packages/test-utils/expect/toMatchDiffSnapshot.js",
2323
"<rootDir>/packages/test-utils/expect/toMatchRollupSnapshot.js",
2424
"<rootDir>/packages/test-utils/expect/toMatchRollupCodeSnapshot.js",
25+
"<rootDir>/packages/test-utils/expect/toMatchRollupAssetSnapshot.js",
2526
],
2627

2728
snapshotSerializers : [

packages/svelte/test/__snapshots__/rollup.test.js.snap

+31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,36 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`/svelte.js rollup chunking should correctly chunk svelte files using inline <style> 1`] = `
4+
Object {
5+
"a.css": "
6+
/* packages/svelte/test/specimens/inline-chunking/a-dep.css */
7+
.mc6ca450b0_adep {
8+
margin: 5px;
9+
}
10+
/* packages/svelte/test/specimens/inline-chunking/a.html */
11+
.mc80aa5044_a {
12+
color: aqua;
13+
}
14+
",
15+
"b.css": "
16+
/* packages/svelte/test/specimens/inline-chunking/b-dep.css */
17+
.mcf56d52f6_bdep {
18+
padding: 5px;
19+
}
20+
/* packages/svelte/test/specimens/inline-chunking/b.html */
21+
.mcbb945b2f_b {
22+
color: blue;
23+
}
24+
",
25+
"shared.css": "
26+
/* packages/svelte/test/specimens/inline-chunking/shared.css */
27+
.mcfef68931_shared {
28+
background: black;
29+
}
30+
",
31+
}
32+
`;
33+
334
exports[`/svelte.js rollup watching should generate updated output 1`] = `
435
Snapshot Diff:
536
- First value

packages/svelte/test/rollup.test.js

+32-2
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ describe("/svelte.js", () => {
196196
import Component from "./error-${type}.html";
197197
198198
console.log(Component);
199-
`
199+
`,
200200
},
201201

202202
allowFallthrough : true,
@@ -207,12 +207,42 @@ describe("/svelte.js", () => {
207207
require("@modular-css/rollup")({
208208
processor,
209209
}),
210-
]
210+
],
211211
});
212212
} catch(e) {
213213
expect(e.toString()).toMatch(/\.wooga/);
214214
}
215215
}
216216
);
217217
});
218+
219+
describe("rollup chunking", () => {
220+
it("should correctly chunk svelte files using inline <style>", async () => {
221+
const { preprocess, processor } = plugin();
222+
223+
const bundle = await rollup({
224+
input : [
225+
require.resolve("./specimens/inline-chunking/a.html"),
226+
require.resolve("./specimens/inline-chunking/b.html"),
227+
],
228+
229+
plugins : [
230+
require("rollup-plugin-svelte")({
231+
preprocess,
232+
}),
233+
require("@modular-css/rollup")({
234+
processor,
235+
}),
236+
],
237+
});
238+
239+
const out = await bundle.generate({
240+
format : "esm",
241+
assetFileNames : "[name][extname]",
242+
chunkFileNames : "[name]",
243+
});
244+
245+
expect(out).toMatchRollupAssetSnapshot();
246+
});
247+
});
218248
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.adep {
2+
margin: 5px;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<div class="{css.a}">A.HTML</div>
2+
3+
<style>
4+
.a {
5+
composes: shared from "./shared.css";
6+
composes: adep from "./a-dep.css";
7+
8+
color: aqua;
9+
}
10+
</style>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.bdep {
2+
padding: 5px;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<div class="{css.b}">B.HTML</div>
2+
3+
<style>
4+
.b {
5+
composes: shared from "./shared.css";
6+
composes: bdep from "./b-dep.css";
7+
8+
color: blue;
9+
}
10+
</style>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.shared {
2+
background: black;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"use strict";
2+
3+
const { toMatchSnapshot } = require("jest-snapshot");
4+
5+
expect.extend({
6+
toMatchRollupAssetSnapshot({ output }) {
7+
const out = Object.create(null);
8+
9+
output.forEach(({ isAsset, fileName, source }) => {
10+
if(!isAsset) {
11+
return;
12+
}
13+
14+
out[fileName] = `\n${source}`;
15+
});
16+
17+
return toMatchSnapshot.call(
18+
this,
19+
out,
20+
);
21+
},
22+
});

packages/test-utils/expect/toMatchRollupCodeSnapshot.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ expect.extend({
66
toMatchRollupCodeSnapshot({ output }) {
77
const out = Object.create(null);
88

9-
output.forEach((chunk) => {
10-
if(chunk.isAsset) {
9+
output.forEach(({ isAsset, name, code }) => {
10+
if(isAsset) {
1111
return;
1212
}
1313

14-
out[chunk.name] = chunk.code;
14+
out[name] = code;
1515
});
1616

1717
return toMatchSnapshot.call(

0 commit comments

Comments
 (0)