-
Notifications
You must be signed in to change notification settings - Fork 2
/
nuxt-module.ts
132 lines (99 loc) · 3.98 KB
/
nuxt-module.ts
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
126
127
128
129
130
131
132
import path from "node:path"
import { defineNuxtModule } from "@nuxt/kit"
import type { NuxtModule } from "@nuxt/schema"
import { LIB_NAME } from "../config"
import { file } from "../utils/fs"
import type { Loader } from "../models/Loader"
import { getBundleFiles } from "../functions/get-bundle-files"
import { writeHTMLFiles } from "../functions/write-html-files"
import { stringifyReport } from "../functions/report"
import { extractAssets, rewriteAssets } from "../functions/nuxt-assets"
import processCssMediaSplitter from "../core"
interface Options {
mediaFileMinSize?: number
}
export default defineNuxtModule<Options>({
meta: {
name: `nuxt-module-${LIB_NAME}`,
configKey: "nuxtModuleCssMediaSplitter",
},
async setup(options, nuxt) {
if (nuxt.options._prepare)
return
const IS_GENERATE = nuxt.options._generate
const IS_BUILD = !nuxt.options._generate && !nuxt.options._prepare && !nuxt.options._start && !nuxt.options.dev
if (IS_GENERATE) {
await nuxt.hook("close", async () => {
const distDir = path.resolve(nuxt.options.rootDir, ".output", "public")
const result = await processCssMediaSplitter({
distDir,
mediaFileMinSize: options.mediaFileMinSize,
})
if (result === null)
return
const { htmlFiles } = await getBundleFiles({ distDir })
await writeHTMLFiles({
files: htmlFiles,
html: result.loader.html,
})
})
}
if (IS_BUILD) {
const MANIFEST_REPLACER = { id: `${LIB_NAME}--nuxt-manifest` }
const LOADER_REPLACER = { id: `${LIB_NAME}--nuxt-loader` }
if (nuxt.options.app.head.script)
nuxt.options.app.head.script.push(MANIFEST_REPLACER, LOADER_REPLACER)
else
nuxt.options.app.head.script = [MANIFEST_REPLACER, LOADER_REPLACER]
let loader: Loader | null = null
await nuxt.hook("build:done", async () => {
const distDir = path.resolve(nuxt.options.buildDir, "dist", "client")
const result = await processCssMediaSplitter({
distDir,
mediaFileMinSize: options.mediaFileMinSize,
})
if (result === null)
return
loader = result.loader
// eslint-disable-next-line no-console
console.log(stringifyReport(result.report, "nuxt"))
})
await nuxt.hook("close", async () => {
if (loader === null)
return
const SERVER_CHUNKS_DIR = path.resolve(nuxt.options.rootDir, ".output", "server", "chunks")
const distDir = path.resolve(nuxt.options.rootDir, ".output", "public")
const rendererPath = path.join(SERVER_CHUNKS_DIR, "handlers", "renderer.mjs")
let renderer = await file.read.plain(rendererPath)
renderer = renderer.replace(
JSON.stringify(MANIFEST_REPLACER),
JSON.stringify({ innerHTML: loader.manifest.content, id: "<POST_BUILD: INSERT TEMPLATE MANIFEST ID>", type: "application/json" }),
)
renderer = renderer.replace(
JSON.stringify(LOADER_REPLACER),
JSON.stringify({ innerHTML: loader.script.content }),
)
await file.write.plain(rendererPath, renderer)
const { htmlFiles } = await getBundleFiles({ distDir })
if (htmlFiles.length === 0)
return
const scripts = [MANIFEST_REPLACER.id, LOADER_REPLACER.id].map(id => new RegExp(`<script id="${id}"></script>\n?`))
await writeHTMLFiles({
files: htmlFiles,
html: loader.html,
replace: scripts.map(s => [s, ""] as const),
})
const assetsFilePath = path.join(SERVER_CHUNKS_DIR, "nitro", "node-server.mjs")
const assetsFileContent = await file.read.plain(assetsFilePath)
const result = extractAssets(assetsFileContent)
await rewriteAssets({
assets: result.assets,
distDir,
assetsFilePath,
assetsFileContent,
assetsPosition: result.pos,
})
})
}
},
}) as NuxtModule<Options>