prunelle 0.6.0 → 0.7.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/LICENSE +21 -0
- package/llms.txt +48 -43
- package/package.json +4 -1
- package/src/a11y/declaration.ts +106 -72
- package/src/a11y/index.ts +59 -39
- package/src/a11y/rgaa-map.ts +95 -49
- package/src/build/bundle.ts +8 -4
- package/src/build/byte-size.ts +11 -0
- package/src/build/client-plugin.ts +3 -1
- package/src/build/compile.ts +2 -1
- package/src/build/ecoindex.ts +15 -14
- package/src/build/files.ts +6 -0
- package/src/build/first-load.ts +83 -11
- package/src/build/graph.ts +29 -39
- package/src/build/index.ts +57 -18
- package/src/build/island-promote.ts +229 -0
- package/src/build/islands.ts +9 -201
- package/src/build/manifest.ts +4 -125
- package/src/build/plugin-host.ts +21 -8
- package/src/build/precompress.ts +1 -1
- package/src/build/prerender.ts +97 -44
- package/src/build/reach.ts +136 -0
- package/src/build/render-rules.ts +2 -2
- package/src/build/report.ts +19 -3
- package/src/build/static-headers.ts +93 -0
- package/src/build/static.ts +2 -0
- package/src/cli/check-forms.ts +97 -48
- package/src/cli/check-imports.ts +25 -0
- package/src/cli/check-links.ts +2 -2
- package/src/cli/check-security.ts +64 -0
- package/src/cli/check.ts +33 -7
- package/src/cli/commands.ts +35 -7
- package/src/cli/docs.ts +4 -3
- package/src/cli/inspect.ts +2 -1
- package/src/cli/mcp-tools.ts +7 -5
- package/src/cli/mcp.ts +1 -1
- package/src/cli/new.ts +14 -3
- package/src/cli/render.ts +14 -19
- package/src/cli/upgrade.ts +22 -5
- package/src/compiler/analyze.ts +26 -5
- package/src/compiler/cacheability.ts +2 -2
- package/src/compiler/compliance-eco.ts +71 -0
- package/src/compiler/compliance-rgaa.ts +110 -4
- package/src/compiler/compliance-urls.ts +145 -0
- package/src/compiler/compliance.ts +168 -116
- package/src/compiler/foreign-imports.ts +88 -0
- package/src/compiler/mistakes.ts +19 -1
- package/src/compiler/props-body.ts +52 -0
- package/src/compiler/shared.ts +2 -2
- package/src/compiler/ssr.ts +2 -2
- package/src/context.ts +34 -0
- package/src/dev/error-log.ts +90 -64
- package/src/dev/host-check.ts +26 -0
- package/src/dev/index.ts +12 -5
- package/src/dev/open-editor.ts +15 -11
- package/src/dev/protocol.ts +2 -0
- package/src/dev/supervisor.ts +8 -0
- package/src/dev/watchers.ts +27 -2
- package/src/dom/action.ts +4 -1
- package/src/dom/extras.ts +1 -2
- package/src/dom/index.ts +13 -2
- package/src/dom/island.ts +22 -10
- package/src/dom/loop.ts +25 -16
- package/src/eco/declaration.ts +257 -0
- package/src/eco/index.ts +65 -0
- package/src/index.ts +2 -2
- package/src/jsx-runtime.ts +2 -2
- package/src/plugins/config.ts +1 -1
- package/src/plugins/context.ts +16 -1
- package/src/plugins/load.ts +4 -3
- package/src/plugins/public.ts +2 -2
- package/src/plugins/services.ts +3 -0
- package/src/plugins/types.ts +17 -2
- package/src/server/action-flash.ts +158 -0
- package/src/server/action-protect.ts +38 -37
- package/src/server/action.ts +11 -127
- package/src/server/app-secret.ts +63 -0
- package/src/server/compress.ts +5 -1
- package/src/server/consent.ts +59 -22
- package/src/server/cookies.ts +16 -2
- package/src/server/env.ts +14 -0
- package/src/server/errors.ts +3 -1
- package/src/server/form-schema.ts +1 -1
- package/src/server/health.ts +46 -15
- package/src/server/http.ts +47 -7
- package/src/server/index.ts +52 -30
- package/src/server/loader.ts +6 -2
- package/src/server/plugin-host.ts +3 -1
- package/src/server/public.ts +4 -3
- package/src/server/rate-limit.ts +52 -9
- package/src/server/route-cache.ts +26 -4
- package/src/server/route-table.ts +7 -3
- package/src/server/routing.ts +3 -87
- package/src/server/sanitize-html.ts +309 -0
- package/src/server/security.ts +71 -20
- package/src/server/serve-handlers.ts +14 -4
- package/src/server/start.ts +1 -1
- package/src/server/static-dir.ts +27 -4
- package/src/server/stream.ts +28 -6
- package/src/ssr/consent-banner.ts +58 -0
- package/src/ssr/context.ts +20 -4
- package/src/ssr/index.ts +18 -0
- package/src/ssr/navigation.ts +2 -2
- package/src/style.ts +32 -1
- package/templates/_shared/gitignore +5 -0
- package/templates/_shared/src/routes/legal.tsx +10 -1
- package/templates/app/.env.example +4 -0
- package/templates/app/package.json +2 -1
- package/templates/app/prunelle.config.ts +2 -0
- package/templates/app/src/auth.ts +4 -6
- package/templates/app/src/mail.ts +4 -0
- package/templates/app/src/routes/_layout.tsx +9 -1
- package/templates/app/src/routes/dashboard/index.tsx +42 -8
- package/templates/app/src/routes/login.tsx +49 -30
- package/templates/app/src/routes/privacy.tsx +25 -0
- package/templates/app/src/styles/app.css +8 -1
- package/templates/app/tests/app.test.ts +44 -3
- package/templates/blog/package.json +1 -1
- package/templates/blog/src/routes/_layout.tsx +6 -1
- package/templates/blog/src/routes/index.tsx +1 -1
- package/templates/blog/src/styles/app.css +6 -1
- package/templates/fullstack/package.json +1 -1
- package/templates/fullstack/src/components/GreetForm.tsx +2 -1
- package/templates/fullstack/src/routes/_layout.tsx +4 -0
- package/templates/fullstack/src/styles/app.css +11 -1
- package/templates/minimal/package.json +1 -1
- package/templates/minimal/src/routes/_layout.tsx +4 -0
- package/templates/minimal/src/styles/app.css +11 -1
package/src/build/graph.ts
CHANGED
|
@@ -2,10 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
import type { ModuleAnalysis } from '../compiler/index'
|
|
4
4
|
import { existsSync, readFileSync, statSync } from 'node:fs'
|
|
5
|
+
import { parseTsx } from '../compiler/analyze'
|
|
6
|
+
import { computeJsxNodes } from '../compiler/ast'
|
|
5
7
|
import { analyzeModule } from '../compiler/index'
|
|
6
8
|
import { PrunelleMistake } from '../compiler/mistakes'
|
|
7
9
|
import { formatCodeFrame, formatDiagnostic, formatLoc } from '../compiler/shared'
|
|
8
10
|
import { isTsFile, relPosix } from './files'
|
|
11
|
+
import { resolveExportTarget } from './island-promote'
|
|
9
12
|
import { resolveImportPath } from './resolve'
|
|
10
13
|
|
|
11
14
|
export interface FileStamp {
|
|
@@ -41,8 +44,28 @@ export interface BuildCacheStats {
|
|
|
41
44
|
serverSkips: number
|
|
42
45
|
}
|
|
43
46
|
|
|
47
|
+
/** An analysis without the AST and what is keyed by its nodes: the cache outlives a build, the AST (many times the size of the source) does not need to. */
|
|
48
|
+
export type SlimAnalysis = Omit<ModuleAnalysis, 'program' | 'jsxNodes' | 'exprContext'>
|
|
49
|
+
|
|
50
|
+
export function slimAnalysis(analysis: ModuleAnalysis): SlimAnalysis {
|
|
51
|
+
const { program: _program, jsxNodes: _jsxNodes, exprContext: _exprContext, ...slim } = analysis
|
|
52
|
+
return slim
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The analysis again, its program and JSX node set parsed together on first use: the node set only matches the program it was computed from. */
|
|
56
|
+
export function reviveAnalysis(slim: SlimAnalysis, code: string): ModuleAnalysis {
|
|
57
|
+
let program: ModuleAnalysis['program'] | undefined
|
|
58
|
+
let jsxNodes: ModuleAnalysis['jsxNodes'] | undefined
|
|
59
|
+
const parsed = (): ModuleAnalysis['program'] => program ??= parseTsx(slim.filename, code)
|
|
60
|
+
return Object.defineProperties({ ...slim } as ModuleAnalysis, {
|
|
61
|
+
program: { get: parsed, enumerable: true, configurable: true },
|
|
62
|
+
jsxNodes: { get: () => jsxNodes ??= slim.hasJsx ? computeJsxNodes(parsed()) : new WeakSet(), enumerable: true, configurable: true },
|
|
63
|
+
exprContext: { value: undefined, writable: true, enumerable: true, configurable: true },
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
|
|
44
67
|
export interface BuildCache {
|
|
45
|
-
analyses: Map<string, FileStamp & { code: string, analysis:
|
|
68
|
+
analyses: Map<string, FileStamp & { code: string, analysis: SlimAnalysis }>
|
|
46
69
|
domCompiled: Map<string, FileStamp & { key?: string | undefined, code: string }>
|
|
47
70
|
ssrCompiled: Map<string, FileStamp & { key: string, code: string }>
|
|
48
71
|
lastClient?: CachedClientBuild | undefined
|
|
@@ -101,7 +124,6 @@ export function stampsMatch(inputStamps: ReadonlyMap<string, FileStamp>): boolea
|
|
|
101
124
|
return true
|
|
102
125
|
}
|
|
103
126
|
|
|
104
|
-
const MAX_REEXPORT_DEPTH = 8
|
|
105
127
|
|
|
106
128
|
export class ModuleGraph {
|
|
107
129
|
readonly analyses = new Map<string, ModuleAnalysis>()
|
|
@@ -175,14 +197,14 @@ export class ModuleGraph {
|
|
|
175
197
|
const cached = stamp === undefined ? undefined : this.cache?.analyses.get(file)
|
|
176
198
|
let analysis: ModuleAnalysis
|
|
177
199
|
if (cached !== undefined && cached.mtimeMs === stamp!.mtimeMs && cached.size === stamp!.size) {
|
|
178
|
-
analysis = cached.analysis
|
|
200
|
+
analysis = reviveAnalysis(cached.analysis, cached.code)
|
|
179
201
|
this.sources.set(file, cached.code)
|
|
180
202
|
}
|
|
181
203
|
else {
|
|
182
204
|
const code = this.source(file)
|
|
183
205
|
analysis = this.analyze(file, code)
|
|
184
206
|
if (this.cache !== undefined && stamp !== undefined)
|
|
185
|
-
this.cache.analyses.set(file, { mtimeMs: stamp.mtimeMs, size: stamp.size, code, analysis })
|
|
207
|
+
this.cache.analyses.set(file, { mtimeMs: stamp.mtimeMs, size: stamp.size, code, analysis: slimAnalysis(analysis) })
|
|
186
208
|
}
|
|
187
209
|
this.analyses.set(file, analysis)
|
|
188
210
|
for (const dep of this.localDeps(file))
|
|
@@ -208,40 +230,8 @@ export class ModuleGraph {
|
|
|
208
230
|
}
|
|
209
231
|
}
|
|
210
232
|
|
|
211
|
-
/**
|
|
212
|
-
exportTarget(file: string, exportName: string
|
|
213
|
-
|
|
214
|
-
return { file, exportName }
|
|
215
|
-
const key = `${file}\0${exportName}`
|
|
216
|
-
if (seen.has(key))
|
|
217
|
-
return { file, exportName }
|
|
218
|
-
seen.add(key)
|
|
219
|
-
|
|
220
|
-
const analysis = this.load(file)
|
|
221
|
-
const direct = analysis.reexports.find(r => r.exported === exportName)
|
|
222
|
-
if (direct !== undefined) {
|
|
223
|
-
const next = this.resolve(file, direct.source)
|
|
224
|
-
if (next !== undefined && isTsFile(next))
|
|
225
|
-
return this.exportTarget(next, direct.local, depth + 1, seen)
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
if (analysis.exportedNames.includes(exportName))
|
|
229
|
-
return { file, exportName }
|
|
230
|
-
|
|
231
|
-
// Follow `export * from './y'` barrel re-exports (named exports only, per ESM spec `export *` never re-exports `default`).
|
|
232
|
-
if (exportName !== 'default') {
|
|
233
|
-
for (const re of analysis.reexports) {
|
|
234
|
-
if (re.exported !== '*')
|
|
235
|
-
continue
|
|
236
|
-
const next = this.resolve(file, re.source)
|
|
237
|
-
if (next === undefined || !isTsFile(next))
|
|
238
|
-
continue
|
|
239
|
-
const candidate = this.exportTarget(next, exportName, depth + 1, seen)
|
|
240
|
-
if (this.load(candidate.file).exportedNames.includes(candidate.exportName))
|
|
241
|
-
return candidate
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
return { file, exportName }
|
|
233
|
+
/** The module that declares `exportName`, following re-export chains and loading each file (parse errors are never swallowed). */
|
|
234
|
+
exportTarget(file: string, exportName: string): { file: string, exportName: string } {
|
|
235
|
+
return resolveExportTarget(this, f => this.load(f), file, exportName) ?? { file, exportName }
|
|
246
236
|
}
|
|
247
237
|
}
|
package/src/build/index.ts
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
import type { ComplianceConfig } from '../compiler/compliance'
|
|
7
7
|
import type { AppRuntimeContext } from '../plugins/context'
|
|
8
8
|
import type { PrunellePlugin } from '../plugins/index'
|
|
9
|
+
import type { BudgetConfig } from '../plugins/types'
|
|
9
10
|
import type { BuildCache } from './graph'
|
|
10
11
|
import type { ActionsByFile } from './islands'
|
|
11
12
|
import type { BuildManifest } from './manifest'
|
|
@@ -14,13 +15,14 @@ import { copyFileSync, existsSync, mkdirSync, readdirSync, realpathSync, rmSync,
|
|
|
14
15
|
import { basename, dirname, join, relative, resolve } from 'node:path'
|
|
15
16
|
import { checkAppForms } from '../cli/check-forms'
|
|
16
17
|
import { findCachedRoutePrivateReads } from '../compiler/cacheability'
|
|
17
|
-
import { collectProjectComplianceWarnings, shouldEmitWarning } from '../compiler/compliance'
|
|
18
|
+
import { collectProjectComplianceWarnings, pluginSetsLang, shouldEmitWarning } from '../compiler/compliance'
|
|
18
19
|
import { formatCodeFrame, formatDiagnostic, formatLoc } from '../compiler/shared'
|
|
19
20
|
import { createAppContext, loadConfig, resolvePlugins, runWithAppContext } from '../plugins/index'
|
|
20
21
|
import { actionHash } from '../server/action'
|
|
22
|
+
import { devFromEnv } from '../server/env'
|
|
21
23
|
import { buildClient, buildServer, linkOrCopyFile, pluginBunPlugins, setPrunedClientCss, standaloneCssFiles } from './bundle'
|
|
22
24
|
import { compileExecutable } from './compile'
|
|
23
|
-
import { assertSafeOutDir, fileSize, isInNodeModules, relPosix, walkAllFiles, walkRoutes } from './files'
|
|
25
|
+
import { assertSafeOutDir, fileSize, isHiddenPublicPath, isInNodeModules, relPosix, walkAllFiles, walkRoutes } from './files'
|
|
24
26
|
import { isInside } from './is-inside'
|
|
25
27
|
import { ModuleGraph } from './graph'
|
|
26
28
|
import { nodeEnvWarning, renderProblems, serverNodeEnvReads } from './render-rules'
|
|
@@ -30,7 +32,8 @@ import { createPluginHost, createPublicFiles } from './plugin-host'
|
|
|
30
32
|
import { carryPreviousAssets } from './carry'
|
|
31
33
|
import { assertOwnOutDir, processSuffix, removeDeadProcessDirs, removeStaleStages, stageDirFor, swapStage } from './output'
|
|
32
34
|
import { prerender } from './prerender'
|
|
33
|
-
import { analyzeBundleWeights, checkJsBudget, formatBundleReport, formatBundleReportJson, islandOrigins } from './report'
|
|
35
|
+
import { analyzeBundleWeights, checkFirstLoadBudget, checkJsBudget, formatBundleReport, formatBundleReportJson, islandOrigins } from './report'
|
|
36
|
+
import { parseByteSize } from './byte-size'
|
|
34
37
|
import { SERVER_ENTRY_FILE, serverEntrySource } from './server-entry'
|
|
35
38
|
import { PRUNELLE_SRC_ROOT, resolvePrunelleSpecifier } from './resolve'
|
|
36
39
|
import { ancestorFiles, assertNoRouteConflicts, discoverRouteMatchers, fileToRoutePath, hasRouteHandlerExport, missingPathsProblem, normalizeRoutePattern, routeExportProblem, routeFileProblem, SPECIAL_ROUTE_FILES } from './routes'
|
|
@@ -53,6 +56,8 @@ export interface BuildOptions {
|
|
|
53
56
|
minify?: boolean | undefined
|
|
54
57
|
assetPrefix?: string | undefined
|
|
55
58
|
site?: string | undefined
|
|
59
|
+
/** `<html lang>` of every page; defaults to `lang` in `prunelle.config.ts`. */
|
|
60
|
+
lang?: string | undefined
|
|
56
61
|
plugins?: PrunellePlugin[] | undefined
|
|
57
62
|
/** Reusable incremental cache for `prunelle dev` (skips re-parsing/re-compiling unchanged files). */
|
|
58
63
|
cache?: BuildCache | undefined
|
|
@@ -135,8 +140,12 @@ interface BuildRun {
|
|
|
135
140
|
stageSuffix: string
|
|
136
141
|
assetPrefix: string
|
|
137
142
|
site: string | undefined
|
|
143
|
+
lang: string | undefined
|
|
144
|
+
/** Byte caps from `budget` in `prunelle.config.ts`, `--max-js` taking over `js`. */
|
|
145
|
+
budget: { js: number | undefined, jsSource: string, firstLoad: number | undefined }
|
|
138
146
|
defaultPort: number | undefined
|
|
139
147
|
compliance: ComplianceConfig | undefined
|
|
148
|
+
consentMaxAge: number | undefined
|
|
140
149
|
autoPrerender: boolean
|
|
141
150
|
minify: boolean
|
|
142
151
|
isDev: boolean
|
|
@@ -151,7 +160,7 @@ export async function build(options: BuildOptions): Promise<BuildManifest> {
|
|
|
151
160
|
}
|
|
152
161
|
|
|
153
162
|
const root = resolve(options.root)
|
|
154
|
-
const isDev = options.dev ?? (
|
|
163
|
+
const isDev = options.dev ?? devFromEnv()
|
|
155
164
|
const effectiveNodeEnv = options.nodeEnv ?? (isDev ? undefined : (process.env.NODE_ENV ?? 'production'))
|
|
156
165
|
const fileConfig = await loadConfig(root, { dev: isDev, nodeEnv: effectiveNodeEnv })
|
|
157
166
|
const routesDir = resolve(root, options.routesDir ?? fileConfig.routesDir ?? 'src/routes')
|
|
@@ -195,8 +204,11 @@ export async function build(options: BuildOptions): Promise<BuildManifest> {
|
|
|
195
204
|
stageSuffix,
|
|
196
205
|
assetPrefix,
|
|
197
206
|
site,
|
|
207
|
+
lang: options.lang ?? fileConfig.lang,
|
|
208
|
+
budget: resolveBudget(options.maxJs, fileConfig.budget),
|
|
198
209
|
defaultPort: fileConfig.port,
|
|
199
210
|
compliance: fileConfig.compliance,
|
|
211
|
+
consentMaxAge: fileConfig.consent?.maxAge,
|
|
200
212
|
autoPrerender: options.autoPrerender ?? fileConfig.autoPrerender ?? false,
|
|
201
213
|
minify: options.minify ?? true,
|
|
202
214
|
isDev,
|
|
@@ -233,7 +245,10 @@ function emitBuildWarnings(run: BuildRun, graph: ModuleGraph, routeFiles: readon
|
|
|
233
245
|
routeFiles,
|
|
234
246
|
analyses: graph.analyses,
|
|
235
247
|
compliance: run.compliance,
|
|
248
|
+
consentMaxAge: run.consentMaxAge,
|
|
236
249
|
site: run.site,
|
|
250
|
+
lang: run.lang,
|
|
251
|
+
langFromPlugin: pluginSetsLang(run.plugins),
|
|
237
252
|
})
|
|
238
253
|
for (const warning of projectWarnings)
|
|
239
254
|
console.warn(warning)
|
|
@@ -349,7 +364,10 @@ function collectActionsByFile(graph: ModuleGraph, root: string, assetPrefix: str
|
|
|
349
364
|
|
|
350
365
|
function copyPublicFiles(publicDir: string, outPublicDir: string, publicFiles: PublicFiles, copyPublic: boolean): void {
|
|
351
366
|
for (const abs of walkAllFiles(publicDir).sort()) {
|
|
352
|
-
const rel =
|
|
367
|
+
const rel = relative(publicDir, abs)
|
|
368
|
+
if (isHiddenPublicPath(rel))
|
|
369
|
+
continue
|
|
370
|
+
publicFiles.register(rel)
|
|
353
371
|
if (copyPublic) {
|
|
354
372
|
mkdirSync(dirname(join(outPublicDir, rel)), { recursive: true })
|
|
355
373
|
copyFileSync(abs, join(outPublicDir, rel))
|
|
@@ -370,11 +388,11 @@ function collectNonEmptyClientAssets(clientDir: string, assetFiles: readonly str
|
|
|
370
388
|
return { assets, builtClientAssets: new Set(assets) }
|
|
371
389
|
}
|
|
372
390
|
|
|
373
|
-
async function validateAppForms(
|
|
374
|
-
const formCheck = await checkAppForms({ root, routesDir, routeFiles, graph })
|
|
391
|
+
async function validateAppForms(run: BuildRun, routeFiles: readonly string[], graph: ModuleGraph): Promise<void> {
|
|
392
|
+
const formCheck = await checkAppForms({ root: run.root, routesDir: run.routesDir, routeFiles, graph, compliance: run.compliance })
|
|
375
393
|
if (formCheck.errors.length > 0)
|
|
376
394
|
throw new Error(formCheck.errors.join('\n'))
|
|
377
|
-
if (!isDev) {
|
|
395
|
+
if (!run.isDev) {
|
|
378
396
|
for (const warning of formCheck.warnings)
|
|
379
397
|
console.warn(warning)
|
|
380
398
|
}
|
|
@@ -410,7 +428,7 @@ async function runBuild(run: BuildRun): Promise<BuildManifest> {
|
|
|
410
428
|
|
|
411
429
|
const actionsByFile = collectActionsByFile(graph, root, assetPrefix)
|
|
412
430
|
const islands = planCheckedIslands(root, routesDir, graph, pageFiles, specialFiles, isStatic)
|
|
413
|
-
await validateAppForms(
|
|
431
|
+
await validateAppForms(run, allRouteFiles, graph)
|
|
414
432
|
|
|
415
433
|
mkdirSync(clientDir, { recursive: true })
|
|
416
434
|
mkdirSync(serverDir, { recursive: true })
|
|
@@ -418,8 +436,8 @@ async function runBuild(run: BuildRun): Promise<BuildManifest> {
|
|
|
418
436
|
.flatMap(p => p.css ?? [])
|
|
419
437
|
.map(c => resolve(root, c))
|
|
420
438
|
.filter(c => existsSync(c))
|
|
421
|
-
const wantsAnalyze = wantsBundleAnalysis(
|
|
422
|
-
const env = { root, outDir, clientDir, serverDir, assetPrefix, minify: run.minify, isDev, nodeEnv: run.nodeEnv, analyze: wantsAnalyze, graph, cache: options.cache, plugins, actionsByFile, islands }
|
|
439
|
+
const wantsAnalyze = wantsBundleAnalysis(run)
|
|
440
|
+
const env = { root, outDir, clientDir, serverDir, assetPrefix, lang: run.lang, minify: run.minify, isDev, nodeEnv: run.nodeEnv, analyze: wantsAnalyze, graph, cache: options.cache, plugins, actionsByFile, islands }
|
|
423
441
|
|
|
424
442
|
const routeEntryFiles = new Set([...specialFiles, ...pageFiles])
|
|
425
443
|
const standaloneActionFiles = [...actionsByFile.keys()].filter(f => !routeEntryFiles.has(f))
|
|
@@ -527,13 +545,32 @@ interface BuiltOutput {
|
|
|
527
545
|
outPublicDir: string
|
|
528
546
|
}
|
|
529
547
|
|
|
530
|
-
function
|
|
531
|
-
|
|
548
|
+
function budgetBytes(value: number | string | undefined, key: string): number | undefined {
|
|
549
|
+
if (value === undefined)
|
|
550
|
+
return undefined
|
|
551
|
+
const bytes = parseByteSize(value)
|
|
552
|
+
if (bytes === undefined)
|
|
553
|
+
throw new Error(`prunelle: ${key} in prunelle.config.ts is ${JSON.stringify(value)}, not a size: give bytes or KB, e.g. ${key.endsWith('js') ? '\'40kb\'' : '\'500kb\''}.`)
|
|
554
|
+
return bytes
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
function resolveBudget(maxJs: number | undefined, budget: BudgetConfig | undefined): BuildRun['budget'] {
|
|
558
|
+
const js = budgetBytes(budget?.js, 'budget.js')
|
|
559
|
+
return {
|
|
560
|
+
js: maxJs ?? js,
|
|
561
|
+
jsSource: maxJs !== undefined ? '--max-js' : 'budget.js',
|
|
562
|
+
firstLoad: budgetBytes(budget?.firstLoad, 'budget.firstLoad'),
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
function wantsBundleAnalysis(run: BuildRun): boolean {
|
|
567
|
+
const { options, budget } = run
|
|
568
|
+
return options.analyze === true || options.analyzeJson === true || budget.js !== undefined || budget.firstLoad !== undefined || options.onAnalyze !== undefined
|
|
532
569
|
}
|
|
533
570
|
|
|
534
571
|
function emitBundleAnalysis(run: BuildRun, built: BuiltOutput, clientDir: string, prerenderBaseDir: string | undefined): void {
|
|
535
|
-
const { options, assetPrefix } = run
|
|
536
|
-
if (!wantsBundleAnalysis(
|
|
572
|
+
const { options, assetPrefix, budget } = run
|
|
573
|
+
if (!wantsBundleAnalysis(run))
|
|
537
574
|
return
|
|
538
575
|
const report = analyzeBundleWeights(built.manifest, clientDir, assetPrefix, built.metafile, built.analyzeOrigins, [built.outPublicDir, run.publicDir], prerenderBaseDir)
|
|
539
576
|
options.onAnalyze?.(report)
|
|
@@ -541,8 +578,10 @@ function emitBundleAnalysis(run: BuildRun, built: BuiltOutput, clientDir: string
|
|
|
541
578
|
console.log(formatBundleReportJson(report))
|
|
542
579
|
else if (options.analyze === true)
|
|
543
580
|
console.log(formatBundleReport(report))
|
|
544
|
-
if (
|
|
545
|
-
checkJsBudget(report,
|
|
581
|
+
if (budget.js !== undefined)
|
|
582
|
+
checkJsBudget(report, budget.js, budget.jsSource)
|
|
583
|
+
if (budget.firstLoad !== undefined)
|
|
584
|
+
checkFirstLoadBudget(report, budget.firstLoad)
|
|
546
585
|
}
|
|
547
586
|
|
|
548
587
|
/** Prerenders, runs the plugins' after-prerender hooks, exports or carries the previous build's dropped files, compiles, then swaps the stage in. */
|
|
@@ -556,7 +595,7 @@ async function landBuild(run: BuildRun, built: BuiltOutput): Promise<void> {
|
|
|
556
595
|
const prerenderRoutes = options.prerender !== false || isStatic ? manifest.routes.filter(r => r.prerender) : []
|
|
557
596
|
const prerenderDir = isStatic ? run.stageDir : join(outDir, 'prerendered')
|
|
558
597
|
const prerendered = prerenderRoutes.length > 0 || isStatic
|
|
559
|
-
? await prerender({ root, outDir, assetPrefix, site: run.site, plugins, pluginOverride: options.plugins, isDev, nodeEnv: run.nodeEnv, isStatic, prerenderDir, routes: prerenderRoutes })
|
|
598
|
+
? await prerender({ root, outDir, assetPrefix, site: run.site, plugins, pluginOverride: options.plugins, isDev, nodeEnv: run.nodeEnv, isStatic, prerenderDir, routes: prerenderRoutes, routesDir: run.routesDir })
|
|
560
599
|
: undefined
|
|
561
600
|
|
|
562
601
|
try {
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
// Which modules run in the browser: following re-exports to the binding they name, and promoting what reads an interactive import.
|
|
2
|
+
|
|
3
|
+
import type { ModuleAnalysis } from '../compiler/analyze'
|
|
4
|
+
import type { IslandIssue } from './islands'
|
|
5
|
+
import type { ModuleGraph } from './graph'
|
|
6
|
+
import { withStateReads } from '../compiler/interactivity'
|
|
7
|
+
import { isInNodeModules, isTsFile } from './files'
|
|
8
|
+
|
|
9
|
+
export type LoadModule = (file: string) => ModuleAnalysis | undefined
|
|
10
|
+
|
|
11
|
+
export const MAX_REEXPORT_DEPTH = 8
|
|
12
|
+
|
|
13
|
+
/** Follows `export { x } from './y'` and `export * from './y'` chains using `load` so parse errors are never swallowed. */
|
|
14
|
+
export function resolveExportTarget(
|
|
15
|
+
graph: ModuleGraph,
|
|
16
|
+
load: LoadModule,
|
|
17
|
+
file: string,
|
|
18
|
+
exportName: string,
|
|
19
|
+
depth = 0,
|
|
20
|
+
seen = new Set<string>(),
|
|
21
|
+
): { file: string, exportName: string } | undefined {
|
|
22
|
+
if (depth > MAX_REEXPORT_DEPTH)
|
|
23
|
+
return { file, exportName }
|
|
24
|
+
const key = `${file}\0${exportName}`
|
|
25
|
+
if (seen.has(key))
|
|
26
|
+
return { file, exportName }
|
|
27
|
+
seen.add(key)
|
|
28
|
+
|
|
29
|
+
const analysis = load(file)
|
|
30
|
+
if (analysis === undefined)
|
|
31
|
+
return undefined
|
|
32
|
+
const direct = analysis.reexports.find(r => r.exported === exportName)
|
|
33
|
+
if (direct !== undefined) {
|
|
34
|
+
const next = graph.resolve(file, direct.source)
|
|
35
|
+
if (next !== undefined && isTsFile(next))
|
|
36
|
+
return resolveExportTarget(graph, load, next, direct.local, depth + 1, seen)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (analysis.exportedNames.includes(exportName))
|
|
40
|
+
return { file, exportName }
|
|
41
|
+
|
|
42
|
+
// Follow `export * from './y'` barrel re-exports (named exports only, per ESM spec `export *` never re-exports `default`).
|
|
43
|
+
if (exportName !== 'default') {
|
|
44
|
+
for (const re of analysis.reexports) {
|
|
45
|
+
if (re.exported !== '*')
|
|
46
|
+
continue
|
|
47
|
+
const next = graph.resolve(file, re.source)
|
|
48
|
+
if (next === undefined || !isTsFile(next))
|
|
49
|
+
continue
|
|
50
|
+
const candidate = resolveExportTarget(graph, load, next, exportName, depth + 1, seen)
|
|
51
|
+
if (candidate !== undefined && load(candidate.file)?.exportedNames.includes(candidate.exportName))
|
|
52
|
+
return candidate
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return { file, exportName }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function moduleHasInteractiveExport(graph: ModuleGraph, load: LoadModule, file: string, seen = new Set<string>()): boolean {
|
|
60
|
+
if (seen.has(file))
|
|
61
|
+
return false
|
|
62
|
+
seen.add(file)
|
|
63
|
+
const analysis = graph.analyses.get(file) ?? load(file)
|
|
64
|
+
if (analysis === undefined)
|
|
65
|
+
return false
|
|
66
|
+
if (analysis.stateExports.length > 0 || [...analysis.exports.values()].some(e => e.interactive))
|
|
67
|
+
return true
|
|
68
|
+
for (const re of analysis.reexports) {
|
|
69
|
+
const next = graph.resolve(file, re.source)
|
|
70
|
+
if (next === undefined || !isTsFile(next))
|
|
71
|
+
continue
|
|
72
|
+
if (re.exported === '*') {
|
|
73
|
+
if (moduleHasInteractiveExport(graph, load, next, seen))
|
|
74
|
+
return true
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
const target = resolveExportTarget(graph, load, next, re.local)
|
|
78
|
+
const targetAnalysis = target === undefined ? undefined : (graph.analyses.get(target.file) ?? load(target.file))
|
|
79
|
+
if (target !== undefined && (targetAnalysis?.stateExports.includes(target.exportName) === true || targetAnalysis?.exports.get(target.exportName)?.interactive === true))
|
|
80
|
+
return true
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return false
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function collectExportedSignals(graph: ModuleGraph, load: LoadModule, file: string, out: Set<string>, seen = new Set<string>()): void {
|
|
87
|
+
if (seen.has(file))
|
|
88
|
+
return
|
|
89
|
+
seen.add(file)
|
|
90
|
+
const analysis = graph.analyses.get(file) ?? load(file)
|
|
91
|
+
if (analysis === undefined)
|
|
92
|
+
return
|
|
93
|
+
for (const name of analysis.signalExports)
|
|
94
|
+
out.add(name)
|
|
95
|
+
for (const re of analysis.reexports) {
|
|
96
|
+
const next = graph.resolve(file, re.source)
|
|
97
|
+
if (next === undefined || !isTsFile(next))
|
|
98
|
+
continue
|
|
99
|
+
if (re.exported === '*') {
|
|
100
|
+
collectExportedSignals(graph, load, next, out, seen)
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
const target = resolveExportTarget(graph, load, next, re.local)
|
|
104
|
+
if (target !== undefined && (graph.analyses.get(target.file) ?? load(target.file))?.signalExports.includes(target.exportName) === true)
|
|
105
|
+
out.add(re.exported)
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function interactivityChanged(prev: ModuleAnalysis, next: { isInteractive: boolean, exports: Map<string, { interactive: boolean }>, localIslands: Map<string, unknown> }): boolean {
|
|
111
|
+
if (prev.isInteractive !== next.isInteractive)
|
|
112
|
+
return true
|
|
113
|
+
for (const [name, fact] of next.exports) {
|
|
114
|
+
if (prev.exports.get(name)?.interactive !== fact.interactive)
|
|
115
|
+
return true
|
|
116
|
+
}
|
|
117
|
+
return next.localIslands.size !== prev.localIslands.size || [...next.localIslands.keys()].some(name => !prev.localIslands.has(name))
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** An imported island a module reads as a value (`const D = Counter`, `{ C: Counter }`, `c={Counter}`), which makes what holds it an island. */
|
|
121
|
+
export interface IslandValueUse { file: string, offset: number, name: string }
|
|
122
|
+
|
|
123
|
+
const COMPONENT_NAME_RE = /^[A-Z]/
|
|
124
|
+
|
|
125
|
+
// The first place `analysis` reads `local` as a value, its bindings being in source order.
|
|
126
|
+
const firstValueRead = (analysis: ModuleAnalysis, local: string): number | undefined => analysis.bindings.map(b => b.valueRefs.get(local)).find(offset => offset !== undefined)
|
|
127
|
+
|
|
128
|
+
export function createPromoter(graph: ModuleGraph, load: LoadModule, issues: IslandIssue[], valueUses: IslandValueUse[] = []): { promoteAll: () => void, loadPromoted: LoadModule } {
|
|
129
|
+
const initialAsyncInteractive = new Set<string>()
|
|
130
|
+
const initialServerOnly = new Set<string>()
|
|
131
|
+
const reportedAsync = new Set<string>()
|
|
132
|
+
const reportedServerOnly = new Set<string>()
|
|
133
|
+
|
|
134
|
+
const recordInitial = (file: string, a: ModuleAnalysis): void => {
|
|
135
|
+
if (a.hasAsyncDefault && a.exports.get('default')?.interactive === true)
|
|
136
|
+
initialAsyncInteractive.add(file)
|
|
137
|
+
if (a.serverOnly !== undefined)
|
|
138
|
+
initialServerOnly.add(file)
|
|
139
|
+
}
|
|
140
|
+
for (const [file, a] of graph.analyses)
|
|
141
|
+
recordInitial(file, a)
|
|
142
|
+
|
|
143
|
+
// What an import names: reactive state, an interactive export (a component or a hook), or neither.
|
|
144
|
+
const interactiveKind = (file: string, source: string, imported: string): 'state' | 'export' | undefined => {
|
|
145
|
+
const resolved = graph.resolve(file, source)
|
|
146
|
+
if (resolved === undefined || !isTsFile(resolved))
|
|
147
|
+
return undefined
|
|
148
|
+
if (imported === '*')
|
|
149
|
+
return moduleHasInteractiveExport(graph, load, resolved) ? 'export' : undefined
|
|
150
|
+
const target = resolveExportTarget(graph, load, resolved, imported)
|
|
151
|
+
if (target === undefined)
|
|
152
|
+
return undefined
|
|
153
|
+
const targetAnalysis = graph.analyses.get(target.file) ?? load(target.file)
|
|
154
|
+
if (targetAnalysis?.stateExports.includes(target.exportName) === true)
|
|
155
|
+
return 'state'
|
|
156
|
+
return targetAnalysis?.exports.get(target.exportName)?.interactive === true ? 'export' : undefined
|
|
157
|
+
}
|
|
158
|
+
const isInteractiveTarget = (file: string, source: string, imported: string): boolean => interactiveKind(file, source, imported) !== undefined
|
|
159
|
+
|
|
160
|
+
const reportedValueUses = new Set<string>()
|
|
161
|
+
const recordValueUses = (file: string, analysis: ModuleAnalysis): void => {
|
|
162
|
+
for (const imp of analysis.imports) {
|
|
163
|
+
for (const spec of imp.specifiers) {
|
|
164
|
+
const offset = spec.imported === '*' || !COMPONENT_NAME_RE.test(spec.local) ? undefined : firstValueRead(analysis, spec.local)
|
|
165
|
+
if (offset === undefined || reportedValueUses.has(`${file}#${spec.local}`) || interactiveKind(file, imp.source, spec.imported) !== 'export')
|
|
166
|
+
continue
|
|
167
|
+
reportedValueUses.add(`${file}#${spec.local}`)
|
|
168
|
+
valueUses.push({ file, offset, name: spec.local })
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const promoteOne = (file: string): boolean => {
|
|
174
|
+
if (isInNodeModules(file) || !isTsFile(file))
|
|
175
|
+
return false
|
|
176
|
+
const analysis = graph.analyses.get(file)
|
|
177
|
+
if (analysis === undefined)
|
|
178
|
+
return false
|
|
179
|
+
const promoted = withStateReads(analysis, (imp, imported) => isInteractiveTarget(file, imp.source, imported))
|
|
180
|
+
if (promoted === undefined)
|
|
181
|
+
return false
|
|
182
|
+
const page = promoted.exports.get('default')
|
|
183
|
+
if (analysis.hasAsyncDefault && page?.interactive === true) {
|
|
184
|
+
if (!initialAsyncInteractive.has(file) && !reportedAsync.has(file)) {
|
|
185
|
+
reportedAsync.add(file)
|
|
186
|
+
issues.push({ kind: 'async-interactive', file, offset: page.offset ?? 0, reason: page.reason })
|
|
187
|
+
}
|
|
188
|
+
return false
|
|
189
|
+
}
|
|
190
|
+
if (promoted.serverOnly !== undefined) {
|
|
191
|
+
if (!initialServerOnly.has(file) && !reportedServerOnly.has(file)) {
|
|
192
|
+
reportedServerOnly.add(file)
|
|
193
|
+
issues.push({ kind: 'server-only-tag', file, serverOnly: promoted.serverOnly })
|
|
194
|
+
}
|
|
195
|
+
return false
|
|
196
|
+
}
|
|
197
|
+
if (!interactivityChanged(analysis, promoted))
|
|
198
|
+
return false
|
|
199
|
+
recordValueUses(file, analysis)
|
|
200
|
+
graph.analyses.set(file, { ...analysis, ...promoted })
|
|
201
|
+
return true
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const promoteAll = (): void => {
|
|
205
|
+
for (let pass = 0; pass < MAX_REEXPORT_DEPTH; pass++) {
|
|
206
|
+
let changed = false
|
|
207
|
+
for (const file of [...graph.analyses.keys()]) {
|
|
208
|
+
if (promoteOne(file))
|
|
209
|
+
changed = true
|
|
210
|
+
}
|
|
211
|
+
if (!changed)
|
|
212
|
+
break
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const loadPromoted: LoadModule = (file: string): ModuleAnalysis | undefined => {
|
|
217
|
+
const existed = graph.analyses.has(file)
|
|
218
|
+
const analysis = graph.analyses.get(file) ?? load(file)
|
|
219
|
+
if (analysis === undefined)
|
|
220
|
+
return undefined
|
|
221
|
+
if (!existed) {
|
|
222
|
+
recordInitial(file, analysis)
|
|
223
|
+
promoteAll()
|
|
224
|
+
}
|
|
225
|
+
return graph.analyses.get(file) ?? analysis
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return { promoteAll, loadPromoted }
|
|
229
|
+
}
|