Skip to content

Commit 91f9b8f

Browse files
committed
fix: more reliable chunk hash augmentation
Closes #143
1 parent 0976e8f commit 91f9b8f

File tree

2 files changed

+78
-66
lines changed

2 files changed

+78
-66
lines changed

__tests__/__snapshots__/index.test.ts.snap

Lines changed: 58 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -577,13 +577,13 @@ exports[`code-splitting manual-chunks: js 3`] = `
577577
`;
578578

579579
exports[`code-splitting manual-chunks: js 4`] = `
580-
"import { m as modules_d55e0e9b } from './fourts-309ca5ed.js';
581-
import './index-bb3ba0d0.js';
580+
"import { m as modules_d55e0e9b } from './fourts-ee6fab52.js';
581+
import './index-01e5c533.js';
582582
583583
(async () => {
584-
const first = await import('./first-5ef3cedc.js');
585-
const second = await import('./second-da297dc5.js');
586-
const otherScript = await import('./other-script-dfcd0135.js');
584+
const first = await import('./first-5f8383da.js');
585+
const second = await import('./second-43e90718.js');
586+
const otherScript = await import('./other-script-9f836635.js');
587587
console.log(first, second, otherScript);
588588
})();
589589
@@ -592,13 +592,13 @@ console.log(modules_d55e0e9b);
592592
`;
593593

594594
exports[`code-splitting manual-chunks: js 5`] = `
595-
"import { a as modules_3ede29d3 } from './fourts-309ca5ed.js';
596-
import './index-bb3ba0d0.js';
595+
"import { a as modules_3ede29d3 } from './fourts-ee6fab52.js';
596+
import './index-01e5c533.js';
597597
598598
(async () => {
599-
const first = await import('./first-5ef3cedc.js');
600-
const second = await import('./second-da297dc5.js');
601-
const otherScript = await import('./other-script-dfcd0135.js');
599+
const first = await import('./first-5f8383da.js');
600+
const second = await import('./second-43e90718.js');
601+
const otherScript = await import('./other-script-9f836635.js');
602602
console.log(first, second, otherScript);
603603
})();
604604
@@ -607,7 +607,7 @@ console.log(modules_3ede29d3);
607607
`;
608608

609609
exports[`code-splitting manual-chunks: js 6`] = `
610-
"import './nondynamics-36d7f0a7.js';
610+
"import './nondynamics-6e84e0d1.js';
611611
"
612612
`;
613613

@@ -626,7 +626,7 @@ console.log(modules_2539eb5b);
626626
exports[`code-splitting manual-chunks: js 9`] = `
627627
"(async () => {
628628
await import('./noncss-f3feb5ac.js');
629-
const nestedScript = await import('./nested-script-a2367435.js');
629+
const nestedScript = await import('./nested-script-c58d3205.js');
630630
console.log(nestedScript);
631631
})();
632632
"
@@ -724,8 +724,8 @@ export { modules_3ede29d3 as a, modules_d55e0e9b as m };
724724
`;
725725

726726
exports[`code-splitting manual-chunks-only: js 2`] = `
727-
"import { m as modules_d55e0e9b, a as modules_3ede29d3 } from './fourts-309ca5ed.js';
728-
import './nondynamics-36d7f0a7.js';
727+
"import { m as modules_d55e0e9b, a as modules_3ede29d3 } from './fourts-ee6fab52.js';
728+
import './nondynamics-6e84e0d1.js';
729729
730730
console.log(\\"sub\\");
731731
@@ -788,9 +788,9 @@ var nestedScript = /*#__PURE__*/Object.freeze({
788788
`;
789789

790790
exports[`code-splitting manual-chunks-only: js 3`] = `
791-
"import './fourts-309ca5ed.js';
791+
"import './fourts-ee6fab52.js';
792792
import './index.js';
793-
import './nondynamics-36d7f0a7.js';
793+
import './nondynamics-6e84e0d1.js';
794794
"
795795
`;
796796

@@ -864,8 +864,8 @@ export { modules_3ede29d3 as a, modules_d55e0e9b as m };
864864
`;
865865

866866
exports[`code-splitting manual-chunks-only-single: js 2`] = `
867-
"import { m as modules_d55e0e9b, a as modules_3ede29d3 } from './fourts-309ca5ed.js';
868-
import './nondynamics-36d7f0a7.js';
867+
"import { m as modules_d55e0e9b, a as modules_3ede29d3 } from './fourts-ee6fab52.js';
868+
import './nondynamics-6e84e0d1.js';
869869
870870
console.log(\\"sub\\");
871871
@@ -928,9 +928,9 @@ var nestedScript = /*#__PURE__*/Object.freeze({
928928
`;
929929

930930
exports[`code-splitting manual-chunks-only-single: js 3`] = `
931-
"import './fourts-309ca5ed.js';
931+
"import './fourts-ee6fab52.js';
932932
import './index.js';
933-
import './nondynamics-36d7f0a7.js';
933+
import './nondynamics-6e84e0d1.js';
934934
"
935935
`;
936936

@@ -1012,13 +1012,13 @@ exports[`code-splitting manual-chunks-single: js 3`] = `
10121012
`;
10131013

10141014
exports[`code-splitting manual-chunks-single: js 4`] = `
1015-
"import { m as modules_d55e0e9b } from './fourts-309ca5ed.js';
1016-
import './index-bb3ba0d0.js';
1015+
"import { m as modules_d55e0e9b } from './fourts-ee6fab52.js';
1016+
import './index-01e5c533.js';
10171017
10181018
(async () => {
1019-
const first = await import('./first-5ef3cedc.js');
1020-
const second = await import('./second-da297dc5.js');
1021-
const otherScript = await import('./other-script-dfcd0135.js');
1019+
const first = await import('./first-5f8383da.js');
1020+
const second = await import('./second-43e90718.js');
1021+
const otherScript = await import('./other-script-9f836635.js');
10221022
console.log(first, second, otherScript);
10231023
})();
10241024
@@ -1027,13 +1027,13 @@ console.log(modules_d55e0e9b);
10271027
`;
10281028

10291029
exports[`code-splitting manual-chunks-single: js 5`] = `
1030-
"import { a as modules_3ede29d3 } from './fourts-309ca5ed.js';
1031-
import './index-bb3ba0d0.js';
1030+
"import { a as modules_3ede29d3 } from './fourts-ee6fab52.js';
1031+
import './index-01e5c533.js';
10321032
10331033
(async () => {
1034-
const first = await import('./first-5ef3cedc.js');
1035-
const second = await import('./second-da297dc5.js');
1036-
const otherScript = await import('./other-script-dfcd0135.js');
1034+
const first = await import('./first-5f8383da.js');
1035+
const second = await import('./second-43e90718.js');
1036+
const otherScript = await import('./other-script-9f836635.js');
10371037
console.log(first, second, otherScript);
10381038
})();
10391039
@@ -1042,7 +1042,7 @@ console.log(modules_3ede29d3);
10421042
`;
10431043

10441044
exports[`code-splitting manual-chunks-single: js 6`] = `
1045-
"import './nondynamics-36d7f0a7.js';
1045+
"import './nondynamics-6e84e0d1.js';
10461046
"
10471047
`;
10481048

@@ -1061,7 +1061,7 @@ console.log(modules_2539eb5b);
10611061
exports[`code-splitting manual-chunks-single: js 9`] = `
10621062
"(async () => {
10631063
await import('./noncss-f3feb5ac.js');
1064-
const nestedScript = await import('./nested-script-a2367435.js');
1064+
const nestedScript = await import('./nested-script-c58d3205.js');
10651065
console.log(nestedScript);
10661066
})();
10671067
"
@@ -1162,14 +1162,14 @@ exports[`code-splitting multi-entry: js 2`] = `
11621162
`;
11631163

11641164
exports[`code-splitting multi-entry: js 3`] = `
1165-
"import './index-bb3ba0d0.js';
1165+
"import './index-01e5c533.js';
11661166
11671167
var modules_d55e0e9b = {\\"fourth\\":\\"fourth_fourth\\"};
11681168
11691169
(async () => {
1170-
const first = await import('./first-5ef3cedc.js');
1171-
const second = await import('./second-da297dc5.js');
1172-
const otherScript = await import('./other-script-98094377.js');
1170+
const first = await import('./first-5f8383da.js');
1171+
const second = await import('./second-43e90718.js');
1172+
const otherScript = await import('./other-script-a19f2750.js');
11731173
console.log(first, second, otherScript);
11741174
})();
11751175
@@ -1178,14 +1178,14 @@ console.log(modules_d55e0e9b);
11781178
`;
11791179

11801180
exports[`code-splitting multi-entry: js 4`] = `
1181-
"import './index-bb3ba0d0.js';
1181+
"import './index-01e5c533.js';
11821182
11831183
var modules_3ede29d3 = {\\"fourthtwo\\":\\"fourthtwo_fourthtwo\\"};
11841184
11851185
(async () => {
1186-
const first = await import('./first-5ef3cedc.js');
1187-
const second = await import('./second-da297dc5.js');
1188-
const otherScript = await import('./other-script-98094377.js');
1186+
const first = await import('./first-5f8383da.js');
1187+
const second = await import('./second-43e90718.js');
1188+
const otherScript = await import('./other-script-a19f2750.js');
11891189
console.log(first, second, otherScript);
11901190
})();
11911191
@@ -1208,7 +1208,7 @@ exports[`code-splitting multi-entry: js 6`] = `
12081208
exports[`code-splitting multi-entry: js 7`] = `
12091209
"(async () => {
12101210
await import('./noncss-f3feb5ac.js');
1211-
const nestedScript = await import('./nested-script-0cc66e75.js');
1211+
const nestedScript = await import('./nested-script-bdf44697.js');
12121212
console.log(nestedScript);
12131213
})();
12141214
"
@@ -1292,14 +1292,14 @@ exports[`code-splitting multi-entry-single: js 2`] = `
12921292
`;
12931293

12941294
exports[`code-splitting multi-entry-single: js 3`] = `
1295-
"import './index-bb3ba0d0.js';
1295+
"import './index-01e5c533.js';
12961296
12971297
var modules_d55e0e9b = {\\"fourth\\":\\"fourth_fourth\\"};
12981298
12991299
(async () => {
1300-
const first = await import('./first-5ef3cedc.js');
1301-
const second = await import('./second-da297dc5.js');
1302-
const otherScript = await import('./other-script-98094377.js');
1300+
const first = await import('./first-5f8383da.js');
1301+
const second = await import('./second-43e90718.js');
1302+
const otherScript = await import('./other-script-a19f2750.js');
13031303
console.log(first, second, otherScript);
13041304
})();
13051305
@@ -1308,14 +1308,14 @@ console.log(modules_d55e0e9b);
13081308
`;
13091309

13101310
exports[`code-splitting multi-entry-single: js 4`] = `
1311-
"import './index-bb3ba0d0.js';
1311+
"import './index-01e5c533.js';
13121312
13131313
var modules_3ede29d3 = {\\"fourthtwo\\":\\"fourthtwo_fourthtwo\\"};
13141314
13151315
(async () => {
1316-
const first = await import('./first-5ef3cedc.js');
1317-
const second = await import('./second-da297dc5.js');
1318-
const otherScript = await import('./other-script-98094377.js');
1316+
const first = await import('./first-5f8383da.js');
1317+
const second = await import('./second-43e90718.js');
1318+
const otherScript = await import('./other-script-a19f2750.js');
13191319
console.log(first, second, otherScript);
13201320
})();
13211321
@@ -1338,7 +1338,7 @@ exports[`code-splitting multi-entry-single: js 6`] = `
13381338
exports[`code-splitting multi-entry-single: js 7`] = `
13391339
"(async () => {
13401340
await import('./noncss-f3feb5ac.js');
1341-
const nestedScript = await import('./nested-script-0cc66e75.js');
1341+
const nestedScript = await import('./nested-script-bdf44697.js');
13421342
console.log(nestedScript);
13431343
})();
13441344
"
@@ -1918,9 +1918,9 @@ exports[`code-splitting single: js 2`] = `
19181918
console.log(\\"sub\\");
19191919
19201920
(async () => {
1921-
const first = await import('./first-5ef3cedc.js');
1922-
const second = await import('./second-da297dc5.js');
1923-
const otherScript = await import('./other-script-98094377.js');
1921+
const first = await import('./first-5f8383da.js');
1922+
const second = await import('./second-43e90718.js');
1923+
const otherScript = await import('./other-script-a19f2750.js');
19241924
console.log(first, second, otherScript);
19251925
})();
19261926
@@ -1943,7 +1943,7 @@ exports[`code-splitting single: js 4`] = `
19431943
exports[`code-splitting single: js 5`] = `
19441944
"(async () => {
19451945
await import('./noncss-f3feb5ac.js');
1946-
const nestedScript = await import('./nested-script-0cc66e75.js');
1946+
const nestedScript = await import('./nested-script-bdf44697.js');
19471947
console.log(nestedScript);
19481948
})();
19491949
"
@@ -2025,9 +2025,9 @@ exports[`code-splitting true: js 2`] = `
20252025
console.log(\\"sub\\");
20262026
20272027
(async () => {
2028-
const first = await import('./first-5ef3cedc.js');
2029-
const second = await import('./second-da297dc5.js');
2030-
const otherScript = await import('./other-script-98094377.js');
2028+
const first = await import('./first-5f8383da.js');
2029+
const second = await import('./second-43e90718.js');
2030+
const otherScript = await import('./other-script-a19f2750.js');
20312031
console.log(first, second, otherScript);
20322032
})();
20332033
@@ -2050,7 +2050,7 @@ exports[`code-splitting true: js 4`] = `
20502050
exports[`code-splitting true: js 5`] = `
20512051
"(async () => {
20522052
await import('./noncss-f3feb5ac.js');
2053-
const nestedScript = await import('./nested-script-0cc66e75.js');
2053+
const nestedScript = await import('./nested-script-bdf44697.js');
20542054
console.log(nestedScript);
20552055
})();
20562056
"

src/index.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,22 +124,34 @@ export default (options: Options = {}): Plugin => {
124124
if (extracted.length === 0) return;
125125

126126
const ids: string[] = [];
127-
for (const id of Object.keys(chunk.modules)) {
128-
const i = this.getModuleInfo(id);
129-
i && ids.push(i.id, ...i.importedIds);
127+
for (const module of Object.keys(chunk.modules)) {
128+
const traversed: string[] = [];
129+
let current = [module];
130+
do {
131+
const imports: string[] = [];
132+
for (const id of current) {
133+
if (traversed.includes(id)) continue;
134+
if (loaders.isSupported(id)) {
135+
if (isIncluded(id)) imports.push(id);
136+
continue;
137+
}
138+
traversed.push(id);
139+
const i = this.getModuleInfo(id);
140+
i && imports.push(...i.importedIds);
141+
}
142+
current = imports;
143+
} while (current.some(id => !loaders.isSupported(id)));
144+
ids.push(...current);
130145
}
131146

132147
const hashable = extracted
133148
.filter(e => ids.includes(e.id))
134149
.sort((a, b) => ids.lastIndexOf(a.id) - ids.lastIndexOf(b.id))
135-
.map(e => {
136-
const { base, dir } = path.parse(e.id);
137-
return { ...e, id: base, map: mm(e.map).relative(dir).toString() };
138-
});
150+
.map(e => `${path.basename(e.id)}:${e.css}`);
139151

140152
if (hashable.length === 0) return;
141153

142-
return JSON.stringify(hashable);
154+
return hashable.join(":");
143155
},
144156

145157
async generateBundle(opts, bundle) {

0 commit comments

Comments
 (0)