Repository navigation
Expand file tree
/
Copy pathoptimizeCSSOutput.ts
More file actions
125 lines (96 loc) · 3.06 KB
/
Copy pathoptimizeCSSOutput.ts
File metadata and controls
125 lines (96 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import type { CSSOutput, UsedIdCache } from "@navita/webpack-plugin";
import type { Chunk } from "webpack";
const merge = (...merge: UsedIdCache[]) => {
const result: UsedIdCache = {};
for (const usedIds of merge) {
for (const key in usedIds) {
result[key] = [...(result[key] || []), ...(usedIds[key] || [])];
}
}
for (const key in result) {
result[key] = [...new Set(result[key])];
}
return result;
};
const intersect = (...intersect: UsedIdCache[]) => {
const result: UsedIdCache = {};
for (const usedIds of intersect) {
for (const key in usedIds) {
result[key] = result[key] ? result[key].filter(id => usedIds[key].includes(id)) : usedIds[key];
}
}
return result;
}
// structuredClone would be better, but since Next.js version 13 is at node 16.14.0,
// we can't use it.
const copy = (source: UsedIdCache): UsedIdCache => JSON.parse(JSON.stringify(source));
const removeParentFromCurrent = (usedIds: UsedIdCache, parentUsedIds: UsedIdCache) => {
for (const key in parentUsedIds) {
if (usedIds[key] && parentUsedIds[key]) {
usedIds[key] = usedIds[key].filter(id => !parentUsedIds[key].includes(id));
}
}
};
export function optimizeCSSOutput(output: CSSOutput) {
const nameToChunk = Object.fromEntries(
Array.from(output.keys()).filter((x) => x.name).map((x) => [x.name, x])
);
const getAllParentUsedIds = (chunk: Chunk) => {
const { name: route } = chunk;
if (!route) {
// If we don't have a name, it's a dynamic chunk.
const value = output.get(chunk);
if (value.parents.length === 0) {
return [];
}
return [
intersect(
...value.parents.map((parent) => merge(
...getAllParentUsedIds(parent),
output.get(parent).usedIds
)
)
)
];
}
if (route.startsWith('pages/')) {
const routes = [
'pages/_document',
'pages/_app',
route,
];
const currentRouteIndex = routes.indexOf(route);
return routes
.filter((_, index) => currentRouteIndex > index)
.map((x) => output.get(nameToChunk[x]))
.filter(Boolean).map((x) => copy(x.usedIds));
}
const parts = route.split('/');
const parents: UsedIdCache[] = [];
let currentPart = '';
for (const part of parts) {
currentPart = [currentPart, part].filter(Boolean).join('/');
const possibleParent = `${currentPart}/layout`;
if (route === possibleParent) {
continue;
}
const parentChunk = nameToChunk[possibleParent];
if (output.has(parentChunk)) {
parents.push(copy(output.get(parentChunk).usedIds));
}
}
return parents;
};
const newOutput: CSSOutput = new Map();
for (const chunk of output.keys()) {
const currentUsedIds = copy(output.get(chunk).usedIds);
for (const parent of getAllParentUsedIds(chunk)) {
removeParentFromCurrent(currentUsedIds, parent);
}
newOutput.set(chunk, {
...output.get(chunk),
usedIds: currentUsedIds,
});
}
return newOutput;
}