kawapress 0.0.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -5
- package/README.zh-CN.md +17 -5
- package/package.json +7 -3
- package/src/api/config.ts +12 -16
- package/src/api/plugin.ts +20 -3
- package/src/cli.ts +28 -2
- package/src/client/app.ts +36 -6
- package/src/client/components/KawaOutlet.vue +36 -0
- package/src/client/components/Root.vue +7 -0
- package/src/client/composables.ts +11 -10
- package/src/client/entries/entry-client.ts +8 -3
- package/src/client/entries/entry-server.ts +36 -6
- package/src/client/index.ts +8 -2
- package/src/client/outlet.ts +85 -0
- package/src/client/page-data.ts +23 -16
- package/src/client/page-table.ts +138 -0
- package/src/client/router/RouterLink.ts +34 -0
- package/src/client/router/RouterView.ts +25 -0
- package/src/client/router/app-router.ts +84 -0
- package/src/client/router/history.ts +53 -0
- package/src/client/router/injection.ts +29 -0
- package/src/client/router/link-interceptor.ts +67 -0
- package/src/client/router/reload.ts +34 -0
- package/src/client/router/route.ts +82 -0
- package/src/client/router/router.ts +160 -0
- package/src/client/testing/virtual-modules.ts +19 -0
- package/src/client/types/global-components.d.ts +1 -0
- package/src/client/types/virtual-modules.d.ts +3 -5
- package/src/compiler/frontmatter-compat.ts +12 -0
- package/src/compiler/link-registry.ts +29 -0
- package/src/compiler/markdown.ts +223 -21
- package/src/compiler/page-components.ts +144 -0
- package/src/compiler/page-loader.ts +15 -2
- package/src/compiler/page-summary.ts +96 -0
- package/src/core/dead-links.ts +234 -0
- package/src/core/extension-points.ts +64 -0
- package/src/core/head.ts +207 -0
- package/src/core/heading-title.ts +78 -0
- package/src/core/hydration-plan.ts +79 -0
- package/src/core/locale.ts +28 -18
- package/src/core/not-found.ts +22 -0
- package/src/core/outlet.ts +110 -0
- package/src/core/page-chunk.ts +50 -0
- package/src/core/page-manifest.ts +53 -0
- package/src/core/router.ts +46 -0
- package/src/core/site.ts +19 -19
- package/src/core/tokens.ts +59 -0
- package/src/generator/config.ts +93 -13
- package/src/generator/data/context.ts +1 -0
- package/src/generator/git.ts +79 -0
- package/src/generator/pages.ts +126 -0
- package/src/generator/runtime-dependencies.ts +280 -0
- package/src/generator/vite/app-chunk.ts +65 -0
- package/src/generator/vite/config.ts +98 -10
- package/src/generator/vite/page-inputs.ts +22 -0
- package/src/generator/vite/plugins/dev-dead-links.ts +72 -0
- package/src/generator/vite/plugins/pages.ts +162 -114
- package/src/generator.ts +13 -0
- package/src/index.ts +30 -2
- package/src/plugin/execution.ts +17 -13
- package/src/plugin/generator-runner.ts +8 -3
- package/src/plugin/outlet-collector.ts +69 -0
- package/src/plugin/runtime-runner.ts +7 -1
- package/src/server/build-node-env.ts +48 -0
- package/src/server/build.ts +50 -4
- package/src/server/check-dead-links.ts +88 -0
- package/src/server/page-assets.ts +138 -0
- package/src/server/prerender.ts +134 -27
- package/src/server/preview-routing.ts +64 -0
- package/src/server/preview.ts +78 -11
- package/src/server/request-handler.ts +36 -12
- package/src/server/template.ts +31 -0
- package/src/client/router.ts +0 -81
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import type { UserConfig } from 'vite'
|
|
2
|
+
import { existsSync, readFileSync, realpathSync } from 'node:fs'
|
|
3
|
+
import { dirname, join, resolve } from 'node:path'
|
|
4
|
+
import process from 'node:process'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Fields that can lead to a package when the search starts at the site itself.
|
|
8
|
+
* A site normally installs everything it imports, including in
|
|
9
|
+
* `devDependencies`, so all of them count as an entry point of the chain.
|
|
10
|
+
*/
|
|
11
|
+
const SITE_DEPENDENCY_FIELDS = [
|
|
12
|
+
'dependencies',
|
|
13
|
+
'devDependencies',
|
|
14
|
+
'optionalDependencies',
|
|
15
|
+
'peerDependencies',
|
|
16
|
+
] as const
|
|
17
|
+
|
|
18
|
+
/** Fields that can lead to a package from inside a dependency of the site. */
|
|
19
|
+
const PACKAGE_DEPENDENCY_FIELDS = [
|
|
20
|
+
'dependencies',
|
|
21
|
+
'optionalDependencies',
|
|
22
|
+
'peerDependencies',
|
|
23
|
+
] as const
|
|
24
|
+
|
|
25
|
+
/** Site, preset and plugin packages make chains of two or three hops long. */
|
|
26
|
+
const MAX_CHAIN_LENGTH = 4
|
|
27
|
+
|
|
28
|
+
/** Guard against a huge site dependency graph turning this into a slow walk. */
|
|
29
|
+
const MAX_VISITED_PACKAGES = 500
|
|
30
|
+
|
|
31
|
+
interface PackageManifest {
|
|
32
|
+
dependencies?: Record<string, string>
|
|
33
|
+
devDependencies?: Record<string, string>
|
|
34
|
+
optionalDependencies?: Record<string, string>
|
|
35
|
+
peerDependencies?: Record<string, string>
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface ChainCandidate {
|
|
39
|
+
directory: string
|
|
40
|
+
chain: string[]
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Declares the packages that a plugin imports from its runtime code, so both
|
|
45
|
+
* module graphs resolve them from the plugin's own installation instead of from
|
|
46
|
+
* the site root.
|
|
47
|
+
*
|
|
48
|
+
* Sites usually install a preset or `kawapress`, not every package a plugin
|
|
49
|
+
* uses, so a nested dependency cannot be imported by name from the site root.
|
|
50
|
+
* The declaration therefore names the package that owns the dependency and the
|
|
51
|
+
* helper derives the packages that lead from the site to it. Vite's optimizer
|
|
52
|
+
* expects that path as an `owner > dependency` chain.
|
|
53
|
+
*
|
|
54
|
+
* Call it from the `vite()` handler: the client dependency optimizer then
|
|
55
|
+
* pre-bundles these packages before the first request instead of discovering
|
|
56
|
+
* them after it and reloading the page, and the SSR build bundles them so Node
|
|
57
|
+
* never loads a second copy of Vue from `node_modules`.
|
|
58
|
+
*
|
|
59
|
+
* ```ts
|
|
60
|
+
* import { useRuntimeDependencies } from 'kawapress/generator'
|
|
61
|
+
*
|
|
62
|
+
* api.vite((config) => {
|
|
63
|
+
* useRuntimeDependencies(config, '@your-scope/plugin-name', ['some-browser-lib'])
|
|
64
|
+
* })
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export function useRuntimeDependencies(
|
|
68
|
+
config: UserConfig,
|
|
69
|
+
owner: string,
|
|
70
|
+
dependencies: readonly string[],
|
|
71
|
+
): void {
|
|
72
|
+
const root = resolve(config.root ?? process.cwd())
|
|
73
|
+
|
|
74
|
+
config.optimizeDeps ??= {}
|
|
75
|
+
config.optimizeDeps.include = mergeUnique(
|
|
76
|
+
config.optimizeDeps.include,
|
|
77
|
+
runtimeDependencySpecifiers(root, owner, dependencies),
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
// A dependency that imports Vue must be bundled by the SSR build, otherwise
|
|
81
|
+
// Node loads its own copy of Vue next to the bundled one.
|
|
82
|
+
const noExternal = config.ssr?.noExternal
|
|
83
|
+
if (noExternal !== true) {
|
|
84
|
+
const existing = noExternal === undefined
|
|
85
|
+
? []
|
|
86
|
+
: Array.isArray(noExternal) ? noExternal : [noExternal]
|
|
87
|
+
config.ssr ??= {}
|
|
88
|
+
config.ssr.noExternal = mergeUnique(existing, dependencies)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Specifiers for the client dependency optimizer: bare package names when the
|
|
94
|
+
* site root can already resolve them, otherwise the chain of packages that
|
|
95
|
+
* leads from the site to the owner.
|
|
96
|
+
*
|
|
97
|
+
* KawaPress uses this for the packages its own browser entry imports. Other
|
|
98
|
+
* plugins go through `useRuntimeDependencies()`, which also handles SSR.
|
|
99
|
+
*/
|
|
100
|
+
export function runtimeDependencySpecifiers(
|
|
101
|
+
root: string,
|
|
102
|
+
owner: string,
|
|
103
|
+
dependencies: readonly string[],
|
|
104
|
+
): string[] {
|
|
105
|
+
// The declared path is the most specific answer; the owner on its own and the
|
|
106
|
+
// bare package name are fallbacks for installations the search cannot see
|
|
107
|
+
// through.
|
|
108
|
+
const chains: string[][] = [findOwnerChain(root, owner) ?? [], [owner], []]
|
|
109
|
+
|
|
110
|
+
return dependencies.flatMap((dependency) => {
|
|
111
|
+
for (const chain of chains) {
|
|
112
|
+
const specifier = nestedSpecifier(root, chain, dependency)
|
|
113
|
+
if (specifier !== undefined) {
|
|
114
|
+
return [specifier]
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
// Nothing resolved: leave the dependency undeclared instead of handing the
|
|
118
|
+
// optimizer an entry it can only warn about.
|
|
119
|
+
return []
|
|
120
|
+
})
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Shortest declared path from the site to `owner`, for example
|
|
125
|
+
* `['kawapress', '@kawapress/preset-nagi']`.
|
|
126
|
+
*
|
|
127
|
+
* Only hops that the parent package declares are followed, so the result also
|
|
128
|
+
* works in a real installation where `node_modules` is not flat. A package that
|
|
129
|
+
* merely happens to be visible through a hoisted `node_modules` is never used as
|
|
130
|
+
* an intermediate.
|
|
131
|
+
*/
|
|
132
|
+
function findOwnerChain(root: string, owner: string): string[] | undefined {
|
|
133
|
+
const siteManifest = readPackageManifest(root)
|
|
134
|
+
if (siteManifest === undefined) {
|
|
135
|
+
return undefined
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const visited = new Set<string>()
|
|
139
|
+
let frontier: ChainCandidate[] = []
|
|
140
|
+
|
|
141
|
+
for (const name of declaredDependencies(siteManifest, SITE_DEPENDENCY_FIELDS)) {
|
|
142
|
+
const directory = findPackageDirectory(name, root)
|
|
143
|
+
if (directory !== undefined) {
|
|
144
|
+
frontier.push({ directory, chain: [name] })
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
for (let length = 1; length <= MAX_CHAIN_LENGTH; length++) {
|
|
149
|
+
const next: ChainCandidate[] = []
|
|
150
|
+
|
|
151
|
+
for (const candidate of frontier) {
|
|
152
|
+
if (visited.has(candidate.directory)) {
|
|
153
|
+
continue
|
|
154
|
+
}
|
|
155
|
+
visited.add(candidate.directory)
|
|
156
|
+
if (visited.size > MAX_VISITED_PACKAGES) {
|
|
157
|
+
return undefined
|
|
158
|
+
}
|
|
159
|
+
if (candidate.chain.at(-1) === owner) {
|
|
160
|
+
return candidate.chain
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const manifest = readPackageManifest(candidate.directory)
|
|
164
|
+
if (manifest === undefined) {
|
|
165
|
+
continue
|
|
166
|
+
}
|
|
167
|
+
for (const name of declaredDependencies(manifest, PACKAGE_DEPENDENCY_FIELDS)) {
|
|
168
|
+
const directory = findPackageDirectory(name, candidate.directory)
|
|
169
|
+
if (directory !== undefined) {
|
|
170
|
+
next.push({ directory, chain: [...candidate.chain, name] })
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
frontier = next
|
|
176
|
+
if (frontier.length === 0) {
|
|
177
|
+
return undefined
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return undefined
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Turns a chain into the specifier Vite wants, but only when every hop resolves
|
|
186
|
+
* from the previous one exactly the way Vite walks it.
|
|
187
|
+
*/
|
|
188
|
+
function nestedSpecifier(
|
|
189
|
+
root: string,
|
|
190
|
+
chain: readonly string[],
|
|
191
|
+
dependency: string,
|
|
192
|
+
): string | undefined {
|
|
193
|
+
let directory = root
|
|
194
|
+
for (const name of chain) {
|
|
195
|
+
const found = findPackageDirectory(name, directory)
|
|
196
|
+
if (found === undefined) {
|
|
197
|
+
return undefined
|
|
198
|
+
}
|
|
199
|
+
directory = found
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Subpaths such as `@unhead/vue/client` belong to their package directory.
|
|
203
|
+
if (findPackageDirectory(packageNameOf(dependency), directory) === undefined) {
|
|
204
|
+
return undefined
|
|
205
|
+
}
|
|
206
|
+
return [...chain, dependency].join(' > ')
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Directory of an installed package, resolved the way Vite resolves the nested
|
|
211
|
+
* `optimizeDeps.include` syntax: from `from`, then from each parent directory.
|
|
212
|
+
*/
|
|
213
|
+
function findPackageDirectory(name: string, from: string): string | undefined {
|
|
214
|
+
let directory = from
|
|
215
|
+
while (true) {
|
|
216
|
+
const candidate = join(directory, 'node_modules', name)
|
|
217
|
+
if (existsSync(join(candidate, 'package.json'))) {
|
|
218
|
+
return safeRealpath(candidate)
|
|
219
|
+
}
|
|
220
|
+
const parent = dirname(directory)
|
|
221
|
+
if (parent === directory) {
|
|
222
|
+
return undefined
|
|
223
|
+
}
|
|
224
|
+
directory = parent
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function readPackageManifest(directory: string): PackageManifest | undefined {
|
|
229
|
+
try {
|
|
230
|
+
return JSON.parse(
|
|
231
|
+
readFileSync(join(directory, 'package.json'), 'utf-8'),
|
|
232
|
+
) as PackageManifest
|
|
233
|
+
}
|
|
234
|
+
catch {
|
|
235
|
+
return undefined
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function declaredDependencies(
|
|
240
|
+
manifest: PackageManifest,
|
|
241
|
+
fields: readonly (keyof PackageManifest)[],
|
|
242
|
+
): string[] {
|
|
243
|
+
const names = new Set<string>()
|
|
244
|
+
for (const field of fields) {
|
|
245
|
+
const declared = manifest[field]
|
|
246
|
+
if (typeof declared === 'object' && declared !== null) {
|
|
247
|
+
for (const name of Object.keys(declared)) {
|
|
248
|
+
names.add(name)
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return [...names].sort()
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** `@scope/package/subpath` becomes `@scope/package`, `package/subpath` becomes `package`. */
|
|
256
|
+
function packageNameOf(specifier: string): string {
|
|
257
|
+
const segments = specifier.split('/')
|
|
258
|
+
return specifier.startsWith('@')
|
|
259
|
+
? segments.slice(0, 2).join('/')
|
|
260
|
+
: segments[0]
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function safeRealpath(path: string): string {
|
|
264
|
+
try {
|
|
265
|
+
return realpathSync(path)
|
|
266
|
+
}
|
|
267
|
+
catch {
|
|
268
|
+
return path
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function mergeUnique<T>(existing: readonly T[] | undefined, added: readonly T[]): T[] {
|
|
273
|
+
const merged = [...existing ?? []]
|
|
274
|
+
for (const value of added) {
|
|
275
|
+
if (!merged.includes(value)) {
|
|
276
|
+
merged.push(value)
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return merged
|
|
280
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/** Chunk holding the app: the runtime, the router, the theme and every module the entry imports. */
|
|
2
|
+
export const APP_CHUNK_NAME = 'app'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Virtual module `@vitejs/plugin-vue` adds to every SFC that exports a component
|
|
6
|
+
* besides its default export, so a Markdown page does not have to repeat the
|
|
7
|
+
* helper. It is shared by every page and by nothing else, so without this it
|
|
8
|
+
* becomes a chunk of its own and every page entry names it as an extra file.
|
|
9
|
+
*/
|
|
10
|
+
const VUE_EXPORT_HELPER_ID = '\0plugin-vue:export-helper'
|
|
11
|
+
|
|
12
|
+
/** The slice of Rolldown's module info this grouping reads. */
|
|
13
|
+
interface ChunkingModuleInfo {
|
|
14
|
+
/** Module ids this module imports statically. */
|
|
15
|
+
importedIds: string[]
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** The slice of Rolldown's chunking context this grouping reads. */
|
|
19
|
+
interface ChunkingContext {
|
|
20
|
+
getModuleInfo: (moduleId: string) => ChunkingModuleInfo | null
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Names the chunk of a module, or returns `null` to leave the choice to Rolldown. */
|
|
24
|
+
type ChunkNameFunction = (moduleId: string, context: ChunkingContext) => string | null
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Code-splitting group that collects everything `entryId` imports statically,
|
|
28
|
+
* directly or transitively.
|
|
29
|
+
*
|
|
30
|
+
* Rolldown's `$initial` tag cannot do this job once every Markdown page is an
|
|
31
|
+
* input of its own: a page entry makes the modules only that page uses part of
|
|
32
|
+
* an "initial" dependency chain as well, so page-only code would end up in the
|
|
33
|
+
* app chunk and every content edit would change its hash. Walking the graph from
|
|
34
|
+
* the entry keeps the app chunk to framework, router and theme code.
|
|
35
|
+
*/
|
|
36
|
+
export function appChunkGroup(entryId: string): ChunkNameFunction {
|
|
37
|
+
let reachable: Set<string> | undefined
|
|
38
|
+
|
|
39
|
+
return (moduleId, context) => {
|
|
40
|
+
if (moduleId === VUE_EXPORT_HELPER_ID) {
|
|
41
|
+
return APP_CHUNK_NAME
|
|
42
|
+
}
|
|
43
|
+
reachable ??= collectStaticImports(entryId, context)
|
|
44
|
+
return moduleId !== entryId && reachable.has(moduleId)
|
|
45
|
+
? APP_CHUNK_NAME
|
|
46
|
+
: null
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function collectStaticImports(
|
|
51
|
+
entryId: string,
|
|
52
|
+
context: ChunkingContext,
|
|
53
|
+
): Set<string> {
|
|
54
|
+
const reachable = new Set<string>()
|
|
55
|
+
const pending = [entryId]
|
|
56
|
+
while (pending.length > 0) {
|
|
57
|
+
const id = pending.pop()!
|
|
58
|
+
if (reachable.has(id)) {
|
|
59
|
+
continue
|
|
60
|
+
}
|
|
61
|
+
reachable.add(id)
|
|
62
|
+
pending.push(...context.getModuleInfo(id)?.importedIds ?? [])
|
|
63
|
+
}
|
|
64
|
+
return reachable
|
|
65
|
+
}
|
|
@@ -1,43 +1,128 @@
|
|
|
1
1
|
import type { InlineConfig, Plugin } from 'vite'
|
|
2
|
+
import type { MarkdownPageLoader } from '../../compiler/page-loader'
|
|
2
3
|
import type { ResolvedSiteConfig } from '../config'
|
|
3
4
|
import { createRequire } from 'node:module'
|
|
4
5
|
import { dirname, join, resolve } from 'node:path'
|
|
6
|
+
import process from 'node:process'
|
|
5
7
|
import vue from '@vitejs/plugin-vue'
|
|
6
8
|
import { createMarkdownPageLoader } from '../../compiler/page-loader'
|
|
7
9
|
import { createDataLoaderContext } from '../data/context'
|
|
10
|
+
import { runtimeDependencySpecifiers } from '../runtime-dependencies'
|
|
8
11
|
import { dataLoaderPlugin } from './plugins/data'
|
|
12
|
+
import { devDeadLinksPlugin } from './plugins/dev-dead-links'
|
|
9
13
|
import { markdownPlugin } from './plugins/markdown'
|
|
10
|
-
import { virtualPagesPlugin } from './plugins/pages'
|
|
14
|
+
import { createPageRouteIndex, virtualPagesPlugin } from './plugins/pages'
|
|
11
15
|
import { virtualRuntimePluginsPlugin } from './plugins/runtime-plugins'
|
|
12
16
|
import { virtualSitePlugin } from './plugins/site'
|
|
13
17
|
|
|
14
18
|
const require = createRequire(import.meta.url)
|
|
15
19
|
const VUE_PACKAGE_PATH = 'vue/package.json'
|
|
20
|
+
const KAWAPRESS_PACKAGE_NAME = 'kawapress'
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Packages the browser entry imports before it asks for a page: the head
|
|
24
|
+
* manager. A site installs `kawapress` and not its dependencies, so the site root
|
|
25
|
+
* cannot resolve them by name and they are declared through the chain of
|
|
26
|
+
* packages that leads to them.
|
|
27
|
+
*/
|
|
28
|
+
const CORE_RUNTIME_DEPENDENCIES = ['@unhead/vue', '@unhead/vue/client']
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Vue is declared by its bare name, not through that chain. `resolve.alias`
|
|
32
|
+
* already points every `vue` import at one file, including the imports in
|
|
33
|
+
* Markdown pages, and a page cannot resolve the package on its own: Vite finds
|
|
34
|
+
* the declared dependency for such an import by its bare name only.
|
|
35
|
+
*/
|
|
36
|
+
const VUE_DEPENDENCY = 'vue'
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Oldest browsers the shipped CSS is written for: the first releases with
|
|
40
|
+
* `light-dark()` (Chrome and Edge 123, Firefox 120, Safari 17.5). Every token,
|
|
41
|
+
* plugin and theme colour is a `light-dark()` value, so the minifier must leave
|
|
42
|
+
* it as written. With an older target lightningcss rewrites `light-dark()` inside
|
|
43
|
+
* a custom property into helper variables that only exist on rules declaring
|
|
44
|
+
* `color-scheme`, and a theme that overrides a token without one silently loses
|
|
45
|
+
* the colour in production. JavaScript keeps Vite's default target.
|
|
46
|
+
*/
|
|
47
|
+
export const CSS_TARGET = ['chrome123', 'edge123', 'firefox120', 'safari17.5', 'ios17.5']
|
|
48
|
+
|
|
49
|
+
export interface CreateBaseViteConfigOptions {
|
|
50
|
+
/** Reuse a page loader created by the caller, for example to read its link registry. */
|
|
51
|
+
pageLoader?: MarkdownPageLoader
|
|
52
|
+
}
|
|
16
53
|
|
|
17
54
|
export function createBaseViteConfig(
|
|
18
55
|
root: string,
|
|
19
56
|
siteConfig: ResolvedSiteConfig,
|
|
57
|
+
options: CreateBaseViteConfigOptions = {},
|
|
20
58
|
): InlineConfig {
|
|
59
|
+
const pageLoader = options.pageLoader ?? createSitePageLoader(root, siteConfig)
|
|
21
60
|
return {
|
|
22
61
|
root,
|
|
23
62
|
base: siteConfig.base,
|
|
24
63
|
publicDir: resolve(root, siteConfig.srcDir, siteConfig.publicDir),
|
|
25
64
|
clearScreen: false,
|
|
26
65
|
appType: 'custom',
|
|
66
|
+
build: {
|
|
67
|
+
cssTarget: CSS_TARGET,
|
|
68
|
+
},
|
|
69
|
+
...createDefineOverrides(),
|
|
27
70
|
resolve: {
|
|
28
71
|
alias: resolveVueAliases(root),
|
|
29
|
-
dedupe: ['vue'
|
|
72
|
+
dedupe: ['vue'],
|
|
30
73
|
},
|
|
31
74
|
ssr: {
|
|
32
|
-
noExternal: ['vue',
|
|
75
|
+
noExternal: ['vue', /^@vue\//],
|
|
33
76
|
resolve: {
|
|
34
77
|
conditions: ['module'],
|
|
35
78
|
},
|
|
36
79
|
},
|
|
37
|
-
|
|
80
|
+
// Without declared entries the optimizer starts empty: there is no
|
|
81
|
+
// `index.html` to scan, so every dependency would be discovered after the
|
|
82
|
+
// first page request and force a full reload. See useRuntimeDependencies().
|
|
83
|
+
optimizeDeps: {
|
|
84
|
+
include: [
|
|
85
|
+
VUE_DEPENDENCY,
|
|
86
|
+
...runtimeDependencySpecifiers(
|
|
87
|
+
root,
|
|
88
|
+
KAWAPRESS_PACKAGE_NAME,
|
|
89
|
+
CORE_RUNTIME_DEPENDENCIES,
|
|
90
|
+
),
|
|
91
|
+
],
|
|
92
|
+
},
|
|
93
|
+
plugins: createBasePlugins(root, siteConfig, pageLoader),
|
|
38
94
|
}
|
|
39
95
|
}
|
|
40
96
|
|
|
97
|
+
/**
|
|
98
|
+
* Compile-time flags for this site's bundles.
|
|
99
|
+
*
|
|
100
|
+
* The repository's E2E suite fails a test whenever Vue reports a hydration
|
|
101
|
+
* mismatch, and Vue only prints the detailed message of a production build when
|
|
102
|
+
* `__VUE_PROD_HYDRATION_MISMATCH_DETAILS__` is on. `KAWAPRESS_E2E=1` is set by
|
|
103
|
+
* the Playwright servers only, so this stays an internal switch: user builds
|
|
104
|
+
* keep Vue's default and behave exactly as before. Vite merges these entries
|
|
105
|
+
* with the `define` values that plugins add later.
|
|
106
|
+
*/
|
|
107
|
+
function createDefineOverrides(): Pick<InlineConfig, 'define'> {
|
|
108
|
+
return process.env.KAWAPRESS_E2E === '1'
|
|
109
|
+
? { define: { __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: 'true' } }
|
|
110
|
+
: {}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Page loader shared by the Vite plugins and, during a build, the link checker. */
|
|
114
|
+
export function createSitePageLoader(
|
|
115
|
+
root: string,
|
|
116
|
+
siteConfig: ResolvedSiteConfig,
|
|
117
|
+
): MarkdownPageLoader {
|
|
118
|
+
return createMarkdownPageLoader({
|
|
119
|
+
root,
|
|
120
|
+
base: siteConfig.base,
|
|
121
|
+
srcDir: siteConfig.srcDir,
|
|
122
|
+
pluginRunner: siteConfig.pluginRunner,
|
|
123
|
+
})
|
|
124
|
+
}
|
|
125
|
+
|
|
41
126
|
function resolveVueAliases(root: string) {
|
|
42
127
|
let packagePath: string
|
|
43
128
|
try {
|
|
@@ -60,14 +145,10 @@ function resolveVueAliases(root: string) {
|
|
|
60
145
|
function createBasePlugins(
|
|
61
146
|
root: string,
|
|
62
147
|
siteConfig: ResolvedSiteConfig,
|
|
148
|
+
pageLoader: MarkdownPageLoader,
|
|
63
149
|
): Plugin[] {
|
|
64
|
-
const pageLoader = createMarkdownPageLoader({
|
|
65
|
-
root,
|
|
66
|
-
base: siteConfig.base,
|
|
67
|
-
srcDir: siteConfig.srcDir,
|
|
68
|
-
pluginRunner: siteConfig.pluginRunner,
|
|
69
|
-
})
|
|
70
150
|
const dataLoaderContext = createDataLoaderContext(root, siteConfig)
|
|
151
|
+
const pageRoutes = createPageRouteIndex(pageLoader, siteConfig.publicDir)
|
|
71
152
|
|
|
72
153
|
return [
|
|
73
154
|
dataLoaderPlugin(dataLoaderContext),
|
|
@@ -85,10 +166,12 @@ function createBasePlugins(
|
|
|
85
166
|
srcDir: siteConfig.srcDir,
|
|
86
167
|
publicDir: siteConfig.publicDir,
|
|
87
168
|
pageLoader,
|
|
169
|
+
pageRoutes,
|
|
88
170
|
}),
|
|
89
171
|
virtualSitePlugin({
|
|
90
172
|
title: siteConfig.title,
|
|
91
173
|
base: siteConfig.base,
|
|
174
|
+
head: siteConfig.head,
|
|
92
175
|
locales: siteConfig.locales,
|
|
93
176
|
...(
|
|
94
177
|
siteConfig.themeConfig === undefined
|
|
@@ -96,5 +179,10 @@ function createBasePlugins(
|
|
|
96
179
|
: { themeConfig: siteConfig.themeConfig }
|
|
97
180
|
),
|
|
98
181
|
}),
|
|
182
|
+
devDeadLinksPlugin({
|
|
183
|
+
pageLoader,
|
|
184
|
+
pageRoutes,
|
|
185
|
+
deadLinks: siteConfig.deadLinks,
|
|
186
|
+
}),
|
|
99
187
|
]
|
|
100
188
|
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { MarkdownPageLoader } from '../../compiler/page-loader'
|
|
2
|
+
import { pageInputName } from '../../core/page-chunk'
|
|
3
|
+
import { listMarkdownPages } from '../pages'
|
|
4
|
+
import { pageScope } from './plugins/pages'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* One client build input per Markdown page, keyed by the entry name and holding
|
|
8
|
+
* the absolute module path.
|
|
9
|
+
*
|
|
10
|
+
* Pages are inputs of their own instead of being reached through a dynamic
|
|
11
|
+
* import, so each page becomes a chunk with a content hash without Vite writing
|
|
12
|
+
* a preload dependency table for every loader.
|
|
13
|
+
*/
|
|
14
|
+
export async function createPageInputs(
|
|
15
|
+
pageLoader: MarkdownPageLoader,
|
|
16
|
+
publicDir: string,
|
|
17
|
+
): Promise<Record<string, string>> {
|
|
18
|
+
const pages = await listMarkdownPages(pageScope(pageLoader, publicDir))
|
|
19
|
+
return Object.fromEntries(
|
|
20
|
+
pages.map(page => [pageInputName(page.routePath), page.file]),
|
|
21
|
+
)
|
|
22
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { Plugin, ViteDevServer } from 'vite'
|
|
2
|
+
import type { MarkdownPageLoader } from '../../../compiler/page-loader'
|
|
3
|
+
import type { DeadLink, ResolvedDeadLinkConfig } from '../../../core/dead-links'
|
|
4
|
+
import type { PageRouteIndex } from './pages'
|
|
5
|
+
import { markdownFileToSourcePath } from '../../../compiler/page-loader'
|
|
6
|
+
import { findPageDeadLinks, formatDeadLink } from '../../../core/dead-links'
|
|
7
|
+
import { markdownPagePathToRoutePath } from '../../../core/markdown-route'
|
|
8
|
+
|
|
9
|
+
export interface DevDeadLinksOptions {
|
|
10
|
+
pageLoader: MarkdownPageLoader
|
|
11
|
+
pageRoutes: PageRouteIndex
|
|
12
|
+
deadLinks: ResolvedDeadLinkConfig | false
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Warn about dead links while editing. Dev only checks that a page exists and
|
|
17
|
+
* that same-page anchors resolve, so a half-finished site keeps running.
|
|
18
|
+
*/
|
|
19
|
+
export function devDeadLinksPlugin(options: DevDeadLinksOptions): Plugin {
|
|
20
|
+
let server: ViteDevServer | undefined
|
|
21
|
+
const reported = new Map<string, Set<string>>()
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
name: 'kawapress:dev-dead-links',
|
|
25
|
+
apply: 'serve',
|
|
26
|
+
configureServer(devServer) {
|
|
27
|
+
server = devServer
|
|
28
|
+
},
|
|
29
|
+
async transform(_code, id) {
|
|
30
|
+
if (!server || options.deadLinks === false) {
|
|
31
|
+
return
|
|
32
|
+
}
|
|
33
|
+
const [file, query] = id.split('?', 2)
|
|
34
|
+
if (query || !file.endsWith('.md')) {
|
|
35
|
+
return
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const route = markdownPagePathToRoutePath(
|
|
39
|
+
markdownFileToSourcePath(file, options.pageLoader.sourceRoot),
|
|
40
|
+
)
|
|
41
|
+
const page = options.pageLoader.registry.get(route)
|
|
42
|
+
if (!page) {
|
|
43
|
+
return
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const deadLinks = findPageDeadLinks(
|
|
47
|
+
page,
|
|
48
|
+
{
|
|
49
|
+
routes: await options.pageRoutes.routes(),
|
|
50
|
+
headings: new Map([[route, page.headings]]),
|
|
51
|
+
publicFiles: [],
|
|
52
|
+
},
|
|
53
|
+
options.deadLinks.ignore,
|
|
54
|
+
).filter(isDevDeadLink)
|
|
55
|
+
|
|
56
|
+
const messages = new Set(deadLinks.map(formatDeadLink))
|
|
57
|
+
const previous = reported.get(id)
|
|
58
|
+
for (const message of messages) {
|
|
59
|
+
if (!previous?.has(message)) {
|
|
60
|
+
server.config.logger.warn(`KawaPress: dead link ${message}`)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
reported.set(id, messages)
|
|
64
|
+
},
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** `missing-file` and cross-page anchors are build-only checks. */
|
|
69
|
+
function isDevDeadLink(link: DeadLink): boolean {
|
|
70
|
+
return link.reason === 'missing-page'
|
|
71
|
+
|| (link.reason === 'missing-anchor' && link.target.startsWith('#'))
|
|
72
|
+
}
|