forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-css-inlined-link-tags.tsx
63 lines (58 loc) · 2.03 KB
/
get-css-inlined-link-tags.tsx
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
import {
ClientCSSReferenceManifest,
ClientReferenceManifest,
} from '../../build/webpack/plugins/flight-manifest-plugin'
import { getClientReferenceModuleKey } from '../../lib/client-reference'
/**
* Get inline <link> tags based on server CSS manifest. Only used when rendering to HTML.
*/
export function getCssInlinedLinkTags(
clientReferenceManifest: ClientReferenceManifest,
serverCSSManifest: ClientCSSReferenceManifest,
filePath: string,
serverCSSForEntries: string[],
injectedCSS: Set<string>,
collectNewCSSImports?: boolean
): string[] {
const layoutOrPageCssModules = serverCSSManifest.cssImports[filePath]
const filePathWithoutExt = filePath.replace(/(\.[A-Za-z0-9]+)+$/, '')
const cssFilesForEntry = new Set(
clientReferenceManifest.cssFiles?.[filePathWithoutExt] || []
)
if (!layoutOrPageCssModules || !cssFilesForEntry.size) {
return []
}
const chunks = new Set<string>()
for (const mod of layoutOrPageCssModules) {
// We only include the CSS if it's a global CSS, or it is used by this
// entrypoint.
if (
serverCSSForEntries.includes(mod) ||
!/\.module\.(css|sass|scss)$/.test(mod)
) {
// If the CSS is already injected by a parent layer, we don't need
// to inject it again.
if (!injectedCSS.has(mod)) {
const modData =
clientReferenceManifest.clientModules[
getClientReferenceModuleKey(mod, '')
]
if (modData) {
for (const chunk of modData.chunks) {
// If the current entry in the final tree-shaked bundle has that CSS
// chunk, it means that it's actually used. We should include it.
if (cssFilesForEntry.has(chunk)) {
chunks.add(chunk)
// This might be a new layout, and to make it more efficient and
// not introducing another loop, we mutate the set directly.
if (collectNewCSSImports) {
injectedCSS.add(mod)
}
}
}
}
}
}
}
return [...chunks]
}