prunelle 0.8.0 → 0.10.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/llms.txt +68 -51
- package/package.json +7 -2
- package/src/a11y/declaration.ts +10 -17
- package/src/a11y/index.ts +69 -125
- package/src/animate.ts +127 -0
- package/src/build/asides.ts +51 -0
- package/src/build/bundle.ts +94 -136
- package/src/build/client-entries.ts +6 -6
- package/src/build/client-outputs.ts +75 -0
- package/src/build/client-plugin.ts +9 -4
- package/src/build/ecoindex.ts +4 -2
- package/src/build/files.ts +18 -0
- package/src/build/graph.ts +14 -6
- package/src/build/index.ts +23 -11
- package/src/build/island-promote.ts +42 -2
- package/src/build/islands.ts +63 -9
- package/src/build/key-types.ts +31 -0
- package/src/build/manifest.ts +117 -1
- package/src/build/plugin-host.ts +13 -1
- package/src/build/prerender.ts +8 -3
- package/src/build/reach.ts +18 -4
- package/src/build/report.ts +1 -1
- package/src/build/resolve.ts +5 -1
- package/src/build/route-types.ts +16 -0
- package/src/build/routes.ts +42 -15
- package/src/build/seed-ids.ts +65 -0
- package/src/build/seed-writes.ts +111 -0
- package/src/build/static-headers.ts +2 -2
- package/src/build/static.ts +1 -1
- package/src/cli/check-cache.ts +51 -0
- package/src/cli/check-consent.ts +95 -0
- package/src/cli/check-forms.ts +10 -2
- package/src/cli/check-links-patterns.ts +101 -0
- package/src/cli/check-links.ts +30 -105
- package/src/cli/check-regions.ts +185 -0
- package/src/cli/check-security.ts +50 -6
- package/src/cli/check.ts +49 -21
- package/src/cli/commands.ts +138 -16
- package/src/cli/docs.ts +21 -2
- package/src/cli/form-opaque-children.ts +40 -0
- package/src/cli/index.ts +1 -1
- package/src/cli/inspect.ts +2 -0
- package/src/cli/mcp-tools.ts +2 -2
- package/src/cli/new.ts +9 -5
- package/src/cli/upgrade-names.ts +14 -2
- package/src/cli/upgrade.ts +3 -0
- package/src/compiler/analyze.ts +22 -16
- package/src/compiler/cacheability-private.ts +13 -3
- package/src/compiler/cacheability.ts +24 -2
- package/src/compiler/compliance-contrast.ts +322 -0
- package/src/compiler/compliance-empty.ts +24 -0
- package/src/compiler/compliance-rgaa-content.ts +95 -0
- package/src/compiler/compliance-rgaa-criteria.ts +45 -0
- package/src/compiler/compliance-rgaa-pages.ts +140 -0
- package/src/compiler/compliance-rgaa.ts +19 -0
- package/src/compiler/compliance-rules.ts +72 -0
- package/src/compiler/compliance-third-party.ts +59 -0
- package/src/compiler/compliance-urls.ts +52 -0
- package/src/compiler/compliance.ts +61 -93
- package/src/compiler/cookie-inventory.ts +119 -0
- package/src/compiler/dom-attrs.ts +178 -22
- package/src/compiler/dom.ts +31 -44
- package/src/compiler/emit.ts +17 -6
- package/src/compiler/expr-context.ts +260 -207
- package/src/compiler/foreign-imports.ts +5 -3
- package/src/compiler/mistakes.ts +18 -9
- package/src/compiler/props-body.ts +263 -21
- package/src/compiler/props-pattern.ts +211 -0
- package/src/compiler/props.ts +324 -343
- package/src/compiler/signal-reads.ts +142 -29
- package/src/compiler/spread-handlers.ts +25 -13
- package/src/compiler/ssr-emit.ts +510 -0
- package/src/compiler/ssr-helpers.ts +165 -121
- package/src/compiler/ssr.ts +28 -441
- package/src/context.ts +14 -1
- package/src/dev/child.ts +1 -1
- package/src/dev/host-check.ts +2 -1
- package/src/dev/hot-islands.ts +71 -0
- package/src/dev/index.ts +31 -18
- package/src/dev/protocol.ts +1 -1
- package/src/dev/queue.ts +3 -2
- package/src/dev/reload.ts +5 -2
- package/src/dev/supervisor.ts +3 -1
- package/src/dev/watchers.ts +6 -2
- package/src/dom/action.ts +97 -14
- package/src/dom/attrs.ts +93 -25
- package/src/dom/extras.ts +91 -50
- package/src/dom/field-marks.ts +32 -0
- package/src/dom/hot.ts +9 -0
- package/src/dom/index.ts +44 -18
- package/src/dom/island-hot.ts +29 -0
- package/src/dom/island.ts +13 -2
- package/src/dom/loop.ts +259 -188
- package/src/dom/prefetch.ts +135 -0
- package/src/dom/regions.ts +515 -0
- package/src/dom/swap.ts +246 -0
- package/src/dom/trusted.ts +22 -0
- package/src/dom/upload.ts +44 -0
- package/src/eco/declaration.ts +44 -41
- package/src/eco/index.ts +8 -5
- package/src/href.ts +31 -0
- package/src/index.ts +12 -5
- package/src/plugins/config.ts +1 -1
- package/src/plugins/context.ts +7 -1
- package/src/plugins/daily.ts +27 -0
- package/src/plugins/index.ts +3 -3
- package/src/plugins/order.ts +1 -1
- package/src/plugins/public.ts +12 -5
- package/src/plugins/services.ts +88 -14
- package/src/plugins/types.ts +128 -10
- package/src/reactivity/graph.ts +244 -41
- package/src/reactivity/hmr.ts +101 -0
- package/src/reactivity/index.ts +243 -239
- package/src/reactivity/resource.ts +129 -0
- package/src/reactivity/seed.ts +141 -0
- package/src/reactivity/selector.ts +85 -47
- package/src/reactivity/server-seed.ts +202 -0
- package/src/reactivity/store.ts +331 -97
- package/src/rgpd/draft.ts +171 -0
- package/src/rgpd/forms.ts +85 -0
- package/src/rgpd/index.ts +57 -0
- package/src/server/action-flash.ts +1 -0
- package/src/server/action-protect.ts +3 -0
- package/src/server/action-registry.ts +11 -9
- package/src/server/action.ts +51 -19
- package/src/server/app-secret.ts +14 -8
- package/src/server/cache-invalidation.ts +97 -0
- package/src/server/compress.ts +55 -18
- package/src/server/consent.ts +21 -5
- package/src/server/cookie-inventory.ts +78 -0
- package/src/server/cookies.ts +77 -8
- package/src/server/cors.ts +98 -0
- package/src/server/csp-report.ts +90 -0
- package/src/server/discard.ts +13 -0
- package/src/server/document.ts +2 -2
- package/src/server/env.ts +1 -1
- package/src/server/errors.ts +9 -2
- package/src/server/field-errors.ts +91 -0
- package/src/server/form-schema.ts +16 -11
- package/src/server/health.ts +41 -11
- package/src/server/http.ts +28 -11
- package/src/server/index.ts +122 -307
- package/src/server/loader.ts +2 -0
- package/src/server/page.ts +3 -9
- package/src/server/plugin-host.ts +66 -16
- package/src/server/plugin-route-shadow.ts +24 -0
- package/src/server/public.ts +14 -3
- package/src/server/rate-limit.ts +49 -10
- package/src/server/region.ts +22 -2
- package/src/server/request-handler.ts +406 -0
- package/src/server/request-log.ts +94 -0
- package/src/server/route-cache.ts +55 -8
- package/src/server/route-input.ts +54 -0
- package/src/server/route-table.ts +53 -27
- package/src/server/router.ts +2 -1
- package/src/server/routing.ts +104 -45
- package/src/server/sanitize-html.ts +4 -2
- package/src/server/security.ts +38 -8
- package/src/server/serve-handlers.ts +16 -6
- package/src/server/served-file.ts +19 -14
- package/src/server/shared-store.ts +323 -0
- package/src/server/start.ts +12 -5
- package/src/server/static-dir.ts +34 -12
- package/src/server/stream.ts +2 -1
- package/src/server/surface.ts +39 -0
- package/src/server/webhook.ts +50 -0
- package/src/server/workers.ts +168 -0
- package/src/ssr/consent-banner.ts +20 -16
- package/src/ssr/consent-texts.ts +40 -0
- package/src/ssr/context.ts +174 -53
- package/src/ssr/cookie-table.ts +75 -0
- package/src/ssr/index.ts +63 -58
- package/src/ssr/navigation.ts +161 -14
- package/src/ssr/region.ts +9 -3
- package/src/style.ts +12 -5
- package/src/test/browser.ts +53 -23
- package/src/test/build-reuse.ts +85 -0
- package/src/test/index.ts +51 -8
- package/src/test/mount.ts +53 -0
- package/src/test/outside.ts +94 -0
- package/src/ts-plugin/diagnostics.ts +56 -18
- package/templates/_shared/AGENTS.md +1 -1
- package/templates/_shared/Dockerfile +22 -0
- package/templates/_shared/dockerignore +9 -0
- package/templates/_shared/gitignore +2 -0
- package/templates/app/.env.example +3 -1
- package/templates/app/compose.yaml +32 -0
- package/templates/app/db/migrations/001_notes.sql +7 -0
- package/templates/app/db/schema.ts +11 -0
- package/templates/app/package.json +1 -0
- package/templates/app/prunelle.config.ts +6 -0
- package/templates/app/src/actions/notes.ts +26 -0
- package/templates/app/src/auth.ts +4 -2
- package/templates/app/src/db.ts +5 -0
- package/templates/app/src/routes/dashboard/index.tsx +26 -1
- package/templates/app/src/routes/privacy.tsx +6 -2
- package/templates/app/tests/app.test.ts +14 -3
- package/templates/plugin/AGENTS.md +11 -0
- package/templates/plugin/CLAUDE.md +1 -0
- package/templates/plugin/README.md +27 -0
- package/templates/plugin/gitignore +2 -0
- package/templates/plugin/llms.txt +19 -0
- package/templates/plugin/package.json +33 -0
- package/templates/plugin/src/index.ts +40 -0
- package/templates/plugin/tests/fixture/prunelle.config.ts +4 -0
- package/templates/plugin/tests/fixture/src/routes/index.tsx +11 -0
- package/templates/plugin/tests/plugin.test.ts +42 -0
- package/templates/plugin/tsconfig.json +19 -0
- package/ts-plugin.cjs +4058 -2246
package/src/build/bundle.ts
CHANGED
|
@@ -3,22 +3,27 @@
|
|
|
3
3
|
import type { BunPlugin, PluginBuilder } from 'bun'
|
|
4
4
|
import type { IslandBinding } from '../compiler/index'
|
|
5
5
|
import type { BuildPluginTarget, PrunellePlugin } from '../plugins/index'
|
|
6
|
+
import type { Asides } from './asides'
|
|
6
7
|
import type { ClientTransforms } from './client-plugin'
|
|
7
8
|
import type { BuildCache, ModuleGraph } from './graph'
|
|
8
9
|
import type { ActionsByFile, IslandPlan } from './islands'
|
|
9
10
|
import { copyFileSync, existsSync, linkSync, mkdirSync, readdirSync, realpathSync, writeFileSync } from 'node:fs'
|
|
10
11
|
import { dirname, isAbsolute, join, relative, resolve } from 'node:path'
|
|
12
|
+
import { pluginSetsLang } from '../compiler/compliance'
|
|
11
13
|
import { compileSsr } from '../compiler/index'
|
|
12
|
-
import { readsPrivateEnv } from '../compiler/server-only'
|
|
13
14
|
import { clientPlugin, PRUNELLE_SPECIFIER_FILTER, localIslandExports, TS_FILTER } from './client-plugin'
|
|
14
|
-
import {
|
|
15
|
+
import { buildAsides } from './asides'
|
|
16
|
+
import { isInNodeModules, isTsFile, relPosix, runBunBuild } from './files'
|
|
15
17
|
import { isInside } from './is-inside'
|
|
16
18
|
import { stampFiles, stampsMatch } from './graph'
|
|
17
19
|
import { entryStaticImports, hydrateStrategies, writeClientEntries } from './client-entries'
|
|
20
|
+
import { recordMetafileOutputs, scanEnvLeaks } from './client-outputs'
|
|
18
21
|
import { clientLeakError, islandKey } from './islands'
|
|
19
22
|
import { pluginHookError } from './plugin-host'
|
|
20
23
|
import { ensureCacheGitignore } from './precompress'
|
|
24
|
+
import { graphUsesRegion } from './reach'
|
|
21
25
|
import { PRUNELLE_SRC_ROOT, resolvePrunelleSpecifier } from './resolve'
|
|
26
|
+
import { nameModuleState } from './seed-ids'
|
|
22
27
|
|
|
23
28
|
export interface BundleEnv {
|
|
24
29
|
root: string
|
|
@@ -30,6 +35,8 @@ export interface BundleEnv {
|
|
|
30
35
|
lang?: string | undefined
|
|
31
36
|
minify: boolean
|
|
32
37
|
isDev: boolean
|
|
38
|
+
/** Split the server bundle by route so `prunelle dev` can keep unchanged chunks loaded; single-entry builds isolate every reload/test cleanly. */
|
|
39
|
+
splitServer?: boolean | undefined
|
|
33
40
|
nodeEnv?: string | undefined
|
|
34
41
|
analyze?: boolean | undefined
|
|
35
42
|
graph: ModuleGraph
|
|
@@ -93,14 +100,14 @@ function reuseDirFiles(srcDir: string, dstDir: string, relFiles?: readonly strin
|
|
|
93
100
|
}
|
|
94
101
|
|
|
95
102
|
const PRUNELLE_SCOPE_RE = /^@prunellejs\//
|
|
96
|
-
const LEADING_DOT_SLASH_RE = /^\.\//
|
|
97
103
|
const EXTERNAL_PRUNELLE_SPECIFIERS = new Set(['prunelle/build', 'prunelle/test'])
|
|
98
104
|
|
|
99
105
|
export function pluginBunPlugins(env: Pick<BundleEnv, 'plugins' | 'root' | 'assetPrefix' | 'minify' | 'lang'>, target: BuildPluginTarget): BunPlugin[] {
|
|
100
106
|
const { root, assetPrefix, minify, lang } = env
|
|
107
|
+
const multiLocale = pluginSetsLang(env.plugins)
|
|
101
108
|
return env.plugins.flatMap((p) => {
|
|
102
109
|
const bp = p.bunPlugins
|
|
103
|
-
return typeof bp === 'function' ? bp({ root, assetPrefix, minify, target, lang }) : bp ?? []
|
|
110
|
+
return typeof bp === 'function' ? bp({ root, assetPrefix, minify, target, lang, multiLocale }) : bp ?? []
|
|
104
111
|
})
|
|
105
112
|
}
|
|
106
113
|
|
|
@@ -131,23 +138,6 @@ function pluginTransforms(plugins: PrunellePlugin[]): ClientTransforms {
|
|
|
131
138
|
return transforms
|
|
132
139
|
}
|
|
133
140
|
|
|
134
|
-
/** Runs Bun.build, failing first with what `ownFailure` throws (a leak, a plugin hook), then with Bun's own logs. */
|
|
135
|
-
async function runBunBuild(side: 'client' | 'server', root: string, ownFailure: () => void, config: Parameters<typeof Bun.build>[0]): Promise<Awaited<ReturnType<typeof Bun.build>>> {
|
|
136
|
-
let result: Awaited<ReturnType<typeof Bun.build>>
|
|
137
|
-
try {
|
|
138
|
-
result = await Bun.build(config)
|
|
139
|
-
}
|
|
140
|
-
catch (err) {
|
|
141
|
-
ownFailure()
|
|
142
|
-
if (err instanceof AggregateError && err.errors.length > 0)
|
|
143
|
-
throw new Error(`prunelle build (${side}) failed:\n${formatBunBuildLogs(err.errors, root)}`)
|
|
144
|
-
throw err
|
|
145
|
-
}
|
|
146
|
-
ownFailure()
|
|
147
|
-
if (!result.success)
|
|
148
|
-
throw new Error(`prunelle build (${side}) failed:\n${formatBunBuildLogs(result.logs, root)}`)
|
|
149
|
-
return result
|
|
150
|
-
}
|
|
151
141
|
|
|
152
142
|
/** CSS files imported outside islands (plus the plugins' own), built as standalone stylesheets. */
|
|
153
143
|
export function standaloneCssFiles(graph: ModuleGraph, pluginCss: string[], islandCss: Set<string>): string[] {
|
|
@@ -180,19 +170,6 @@ export interface ClientBuild {
|
|
|
180
170
|
metafile: Bun.BuildOutput['metafile']
|
|
181
171
|
}
|
|
182
172
|
|
|
183
|
-
/** Builds the prop revivers on their own, so the loader names the file through `PRUNELLE_REVIVE` and imports it only for the props that need it. */
|
|
184
|
-
async function buildReviver(env: BundleEnv): Promise<{ url: string, file: string }> {
|
|
185
|
-
const built = await runBunBuild('client', env.root, () => {}, {
|
|
186
|
-
entrypoints: [join(PRUNELLE_SRC_ROOT, 'dom/revive.ts')],
|
|
187
|
-
outdir: env.clientDir,
|
|
188
|
-
target: 'browser',
|
|
189
|
-
format: 'esm',
|
|
190
|
-
minify: env.minify,
|
|
191
|
-
naming: '[name]-[hash].[ext]',
|
|
192
|
-
})
|
|
193
|
-
const file = relPosix(env.clientDir, built.outputs[0]!.path)
|
|
194
|
-
return { url: `${env.assetPrefix}/${file}`, file }
|
|
195
|
-
}
|
|
196
173
|
|
|
197
174
|
function publicEnvDefines(): Record<string, string> {
|
|
198
175
|
const defines: Record<string, string> = {}
|
|
@@ -208,7 +185,7 @@ function buildClientSignature(
|
|
|
208
185
|
env: BundleEnv,
|
|
209
186
|
standaloneCss: readonly string[],
|
|
210
187
|
pluginClientEntries: readonly string[],
|
|
211
|
-
|
|
188
|
+
hasRegions: boolean,
|
|
212
189
|
transformsKey: string,
|
|
213
190
|
strategies: ReturnType<typeof hydrateStrategies>['strategies'],
|
|
214
191
|
hasClientOnlyHydrate: boolean,
|
|
@@ -218,14 +195,15 @@ function buildClientSignature(
|
|
|
218
195
|
return ''
|
|
219
196
|
return JSON.stringify({
|
|
220
197
|
minify: env.minify,
|
|
221
|
-
assetPrefix: env.assetPrefix, lang: env.lang,
|
|
198
|
+
assetPrefix: env.assetPrefix, lang: env.lang, multiLocale: pluginSetsLang(env.plugins),
|
|
222
199
|
isDev: env.isDev,
|
|
200
|
+
seeds: env.islands.seedsModuleState,
|
|
223
201
|
nodeEnv: env.nodeEnv ?? (env.isDev ? 'development' : 'production'),
|
|
224
202
|
transforms: transformsKey,
|
|
225
203
|
strategies,
|
|
226
204
|
hasClientOnlyHydrate,
|
|
227
205
|
pluginClientEntries,
|
|
228
|
-
|
|
206
|
+
hasRegions,
|
|
229
207
|
standaloneCss: [...standaloneCss].sort(),
|
|
230
208
|
islands: [...env.islands.targets.entries()]
|
|
231
209
|
.sort(([a], [b]) => a.localeCompare(b))
|
|
@@ -235,11 +213,14 @@ function buildClientSignature(
|
|
|
235
213
|
.map(([f, list]) => [f, list.map(a => `${a.exportName}=${a.url}`)]),
|
|
236
214
|
envDefines: Object.entries(envDefines).sort(([a], [b]) => a.localeCompare(b)),
|
|
237
215
|
bunPlugins: env.plugins.filter(p => p.bunPlugins !== undefined).map(p => p.name),
|
|
216
|
+
cssSources: env.plugins.some(p => p.affectsClientCss === true)
|
|
217
|
+
? [...env.graph.analyses.keys()].filter(f => !isInNodeModules(f)).sort()
|
|
218
|
+
: [],
|
|
238
219
|
})
|
|
239
220
|
}
|
|
240
221
|
|
|
241
222
|
export async function buildClient(env: BundleEnv, standaloneCss: string[]): Promise<ClientBuild> {
|
|
242
|
-
const { root, clientDir,
|
|
223
|
+
const { root, clientDir, graph, cache, actionsByFile, islands } = env
|
|
243
224
|
const result: ClientBuild = {
|
|
244
225
|
islandChunks: new Map(),
|
|
245
226
|
srcCssToUrl: new Map(),
|
|
@@ -253,8 +234,8 @@ export async function buildClient(env: BundleEnv, standaloneCss: string[]): Prom
|
|
|
253
234
|
metafile: undefined,
|
|
254
235
|
}
|
|
255
236
|
const pluginClientEntries = env.plugins.map(p => p.clientEntry).filter((e): e is string => Boolean(e))
|
|
256
|
-
const
|
|
257
|
-
if (islands.targets.size === 0 && pluginClientEntries.length === 0 &&
|
|
237
|
+
const hasRegions = graphUsesRegion(graph)
|
|
238
|
+
if (islands.targets.size === 0 && pluginClientEntries.length === 0 && !hasRegions && standaloneCss.length === 0) {
|
|
258
239
|
if (cache !== undefined) {
|
|
259
240
|
cache.lastClient = undefined
|
|
260
241
|
prunedClientCss.delete(cache)
|
|
@@ -266,42 +247,22 @@ export async function buildClient(env: BundleEnv, standaloneCss: string[]): Prom
|
|
|
266
247
|
const { strategies, hasClientOnlyHydrate } = hydrateStrategies(graph.analyses.values())
|
|
267
248
|
|
|
268
249
|
const envDefines = publicEnvDefines()
|
|
269
|
-
const clientSignature = buildClientSignature(env, standaloneCss, pluginClientEntries,
|
|
250
|
+
const clientSignature = buildClientSignature(env, standaloneCss, pluginClientEntries, hasRegions, transforms.key, strategies, hasClientOnlyHydrate, envDefines)
|
|
270
251
|
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
&& cache.lastClient !== undefined
|
|
275
|
-
&& cache.lastClient.signature === clientSignature
|
|
276
|
-
&& stampsMatch(cache.lastClient.inputStamps)
|
|
277
|
-
&& reuseDirFiles(cache.lastClient.clientDir, clientDir, cache.lastClient.assetFiles, prunedClientCss.get(cache))
|
|
278
|
-
) {
|
|
279
|
-
cache.stats.clientSkips++
|
|
280
|
-
cache.lastClient.clientDir = clientDir
|
|
281
|
-
return {
|
|
282
|
-
islandChunks: new Map(cache.lastClient.islandChunks),
|
|
283
|
-
srcCssToUrl: new Map(cache.lastClient.srcCssToUrl),
|
|
284
|
-
islandCssUrl: new Map(cache.lastClient.islandCssUrl),
|
|
285
|
-
assetFiles: [...cache.lastClient.assetFiles],
|
|
286
|
-
styleUrls: [...cache.lastClient.styleUrls],
|
|
287
|
-
runtimeScript: cache.lastClient.runtimeScript,
|
|
288
|
-
regionScript: cache.lastClient.regionScript,
|
|
289
|
-
reviveScript: cache.lastClient.reviveScript,
|
|
290
|
-
chunkImports: cache.lastClient.chunkImports,
|
|
291
|
-
metafile: undefined,
|
|
292
|
-
}
|
|
293
|
-
}
|
|
252
|
+
const reused = reuseClientBuild(env, clientSignature)
|
|
253
|
+
if (reused !== undefined)
|
|
254
|
+
return reused
|
|
294
255
|
|
|
295
256
|
// Bun hashes an entry's path into its chunk name, so the entries sit at one path per app: a dev rebuild or a static stage then keeps every unchanged chunk name.
|
|
296
257
|
const entriesDir = join(root, '.prunelle-cache/entries')
|
|
297
258
|
mkdirSync(entriesDir, { recursive: true })
|
|
298
259
|
ensureCacheGitignore(dirname(entriesDir))
|
|
299
|
-
const { entryPoints, islandEntryToKey, runtimeEntryPath, regionEntryPath } = writeClientEntries(env, entriesDir, { pluginClientEntries,
|
|
260
|
+
const { entryPoints, islandEntryToKey, runtimeEntryPath, regionEntryPath } = writeClientEntries(env, entriesDir, { pluginClientEntries, hasRegions, hasClientOnlyHydrate, standaloneCss })
|
|
300
261
|
|
|
301
262
|
const clientLeaks = new Set<string>()
|
|
302
263
|
const inlinedEnvKey = (key: string): boolean => key === 'NODE_ENV' || `process.env.${key}` in envDefines
|
|
303
|
-
const plugin = clientPlugin({ root, isDev: env.isDev, graph, cache, actionsByFile, transforms, leaks: clientLeaks, localExports: localIslandExports(islands.targets.values()), clientKeep: islands.clientKeep })
|
|
304
|
-
const
|
|
264
|
+
const plugin = clientPlugin({ root, isDev: env.isDev, graph, cache, actionsByFile, transforms, leaks: clientLeaks, localExports: localIslandExports(islands.targets.values()), clientKeep: islands.clientKeep, seedsModuleState: islands.seedsModuleState })
|
|
265
|
+
const asides = await buildAsides(env, islands.targets.size > 0)
|
|
305
266
|
|
|
306
267
|
const clientResult = await runBunBuild('client', env.root, () => {
|
|
307
268
|
if (clientLeaks.size > 0)
|
|
@@ -317,9 +278,9 @@ export async function buildClient(env: BundleEnv, standaloneCss: string[]): Prom
|
|
|
317
278
|
minify: env.minify,
|
|
318
279
|
metafile: true,
|
|
319
280
|
naming: '[name]-[hash].[ext]',
|
|
320
|
-
external:
|
|
281
|
+
external: asides.external,
|
|
321
282
|
define: {
|
|
322
|
-
...
|
|
283
|
+
...asides.define,
|
|
323
284
|
PRUNELLE_VISIBLE: String(strategies.visible),
|
|
324
285
|
PRUNELLE_IDLE: String(strategies.idle),
|
|
325
286
|
PRUNELLE_INTERACTION: String(strategies.interaction),
|
|
@@ -345,62 +306,53 @@ export async function buildClient(env: BundleEnv, standaloneCss: string[]): Prom
|
|
|
345
306
|
return abs
|
|
346
307
|
}
|
|
347
308
|
|
|
348
|
-
|
|
349
|
-
for (const output of clientResult.outputs) {
|
|
350
|
-
if (!output.path.endsWith('.js'))
|
|
351
|
-
continue
|
|
352
|
-
const js = await output.text()
|
|
353
|
-
if (!js.includes('process.env') && !js.includes('Bun.env'))
|
|
354
|
-
continue
|
|
355
|
-
const relFile = relPosix(clientDir, output.path)
|
|
356
|
-
const info = metafile?.outputs[relFile] ?? metafile?.outputs[`./${relFile}`]
|
|
357
|
-
for (const [inKey, input] of Object.entries(info?.inputs ?? {})) {
|
|
358
|
-
if (input.bytesInOutput <= 0)
|
|
359
|
-
continue
|
|
360
|
-
const abs = resolveMetaPath(inKey)
|
|
361
|
-
let program = graph.analyses.get(abs)?.program
|
|
362
|
-
if (program === undefined && existsSync(abs) && isTsFile(abs) && !isInNodeModules(abs))
|
|
363
|
-
program = graph.load(abs).program
|
|
364
|
-
if (program !== undefined && readsPrivateEnv(program, inlinedEnvKey))
|
|
365
|
-
clientLeaks.add(`${relPosix(root, abs)} reads process.env or Bun.env (only a process.env.PUBLIC_* set at build time is inlined)`)
|
|
366
|
-
}
|
|
367
|
-
}
|
|
309
|
+
await scanEnvLeaks(clientResult.outputs, metafile, env, resolveMetaPath, inlinedEnvKey, clientLeaks)
|
|
368
310
|
if (clientLeaks.size > 0)
|
|
369
311
|
throw clientLeakError(clientLeaks)
|
|
370
312
|
|
|
371
|
-
|
|
372
|
-
const relOut = outKey.replace(LEADING_DOT_SLASH_RE, '')
|
|
373
|
-
const url = `${assetPrefix}/${relOut}`
|
|
374
|
-
const entry = info.entryPoint === undefined ? undefined : resolveMetaPath(info.entryPoint)
|
|
375
|
-
if (entry !== undefined && entry === runtimeEntryPath && relOut.endsWith('.js'))
|
|
376
|
-
result.runtimeScript = url
|
|
377
|
-
if (entry !== undefined && entry === regionEntryPath && relOut.endsWith('.js'))
|
|
378
|
-
result.regionScript = url
|
|
379
|
-
const entryKey = entry === undefined ? undefined : islandEntryToKey.get(entry)
|
|
380
|
-
if (entryKey !== undefined && relOut.endsWith('.js'))
|
|
381
|
-
result.islandChunks.set(entryKey, url)
|
|
382
|
-
if (relOut.endsWith('.css')) {
|
|
383
|
-
if (entry?.endsWith('.css'))
|
|
384
|
-
result.srcCssToUrl.set(entry, url)
|
|
385
|
-
for (const inKey of Object.keys(info.inputs ?? {})) {
|
|
386
|
-
if (inKey.endsWith('.css'))
|
|
387
|
-
result.srcCssToUrl.set(resolveMetaPath(inKey), url)
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
const cssBundle = (info as { cssBundle?: string }).cssBundle
|
|
391
|
-
if (cssBundle !== undefined && entryKey !== undefined)
|
|
392
|
-
result.islandCssUrl.set(entryKey, `${assetPrefix}/${cssBundle.replace(LEADING_DOT_SLASH_RE, '')}`)
|
|
393
|
-
}
|
|
313
|
+
recordMetafileOutputs(result, metafile, env, islandEntryToKey, runtimeEntryPath, regionEntryPath, resolveMetaPath)
|
|
394
314
|
|
|
395
|
-
recordClientFiles(result, clientResult.outputs, env,
|
|
315
|
+
recordClientFiles(result, clientResult.outputs, env, asides)
|
|
396
316
|
|
|
397
|
-
if (cache !== undefined)
|
|
398
|
-
|
|
317
|
+
if (cache !== undefined) {
|
|
318
|
+
const affectsClientCss = env.plugins.some(p => p.affectsClientCss === true)
|
|
319
|
+
const cssSourceFiles = affectsClientCss ? [...graph.analyses.keys()].filter(f => !isInNodeModules(f)) : []
|
|
320
|
+
rememberClientBuild(cache, result, { signature: clientSignature, clientDir, inputFiles: [...standaloneCss, ...pluginClientEntries, ...cssSourceFiles], entriesDir, actionsByFile, graph, resolveMetaPath })
|
|
321
|
+
}
|
|
399
322
|
return result
|
|
400
323
|
}
|
|
401
324
|
|
|
402
|
-
|
|
403
|
-
function
|
|
325
|
+
// The last client build, when nothing it read changed: its files are linked into the new dir and its result handed back.
|
|
326
|
+
function reuseClientBuild(env: BundleEnv, clientSignature: string): ClientBuild | undefined {
|
|
327
|
+
const { clientDir, cache } = env
|
|
328
|
+
if (
|
|
329
|
+
cache === undefined
|
|
330
|
+
|| env.analyze
|
|
331
|
+
|| cache.lastClient === undefined
|
|
332
|
+
|| cache.lastClient.signature !== clientSignature
|
|
333
|
+
|| !stampsMatch(cache.lastClient.inputStamps)
|
|
334
|
+
|| !reuseDirFiles(cache.lastClient.clientDir, clientDir, cache.lastClient.assetFiles, prunedClientCss.get(cache))
|
|
335
|
+
) {
|
|
336
|
+
return undefined
|
|
337
|
+
}
|
|
338
|
+
cache.stats.clientSkips++
|
|
339
|
+
cache.lastClient.clientDir = clientDir
|
|
340
|
+
return {
|
|
341
|
+
islandChunks: new Map(cache.lastClient.islandChunks),
|
|
342
|
+
srcCssToUrl: new Map(cache.lastClient.srcCssToUrl),
|
|
343
|
+
islandCssUrl: new Map(cache.lastClient.islandCssUrl),
|
|
344
|
+
assetFiles: [...cache.lastClient.assetFiles],
|
|
345
|
+
styleUrls: [...cache.lastClient.styleUrls],
|
|
346
|
+
runtimeScript: cache.lastClient.runtimeScript,
|
|
347
|
+
regionScript: cache.lastClient.regionScript,
|
|
348
|
+
reviveScript: cache.lastClient.reviveScript,
|
|
349
|
+
chunkImports: cache.lastClient.chunkImports,
|
|
350
|
+
metafile: undefined,
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/** The emitted files, the stylesheets, each entry's static imports and the modules built aside, into the client build's result. */
|
|
355
|
+
function recordClientFiles(result: ClientBuild, outputs: readonly Bun.BuildArtifact[], env: BundleEnv, asides: Asides): void {
|
|
404
356
|
for (const output of outputs) {
|
|
405
357
|
const relFile = relPosix(env.clientDir, output.path)
|
|
406
358
|
result.assetFiles.push(relFile)
|
|
@@ -408,10 +360,8 @@ function recordClientFiles(result: ClientBuild, outputs: readonly Bun.BuildArtif
|
|
|
408
360
|
result.styleUrls.push(`${env.assetPrefix}/${relFile}`)
|
|
409
361
|
}
|
|
410
362
|
result.chunkImports = entryStaticImports(result.metafile, env.assetPrefix)
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
result.assetFiles.push(reviver.file)
|
|
414
|
-
}
|
|
363
|
+
result.reviveScript = asides.reviveScript
|
|
364
|
+
result.assetFiles.push(...asides.files)
|
|
415
365
|
}
|
|
416
366
|
|
|
417
367
|
/** Keeps what a client build produced, so the next build with the same signature and unchanged inputs reuses its files. */
|
|
@@ -449,6 +399,16 @@ function rememberClientBuild(
|
|
|
449
399
|
}
|
|
450
400
|
}
|
|
451
401
|
|
|
402
|
+
// A path under node_modules that a link leads to app sources, such as a workspace package.
|
|
403
|
+
function isLinkedSource(resolved: string): boolean {
|
|
404
|
+
try {
|
|
405
|
+
return !isInNodeModules(realpathSync(resolved))
|
|
406
|
+
}
|
|
407
|
+
catch {
|
|
408
|
+
return false
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
452
412
|
// Where the server build takes each import from: `prunelle` from its source, packages that render components bundled for the SSR compiler, the rest external.
|
|
453
413
|
// In dev prunelle's own source stays external too: Bun never unloads a module, so a bundled copy of prunelle's runtime was evaluated again and kept on every save.
|
|
454
414
|
function resolveServerImports(b: PluginBuilder, isDev: boolean, root: string, reachesTsx: (file: string) => boolean, externalOutDir: boolean): void {
|
|
@@ -491,14 +451,8 @@ function resolveServerImports(b: PluginBuilder, isDev: boolean, root: string, re
|
|
|
491
451
|
if (isTsFile(resolved)) {
|
|
492
452
|
if (!isInNodeModules(resolved))
|
|
493
453
|
return { path: resolved }
|
|
494
|
-
if (isDev)
|
|
495
|
-
|
|
496
|
-
const real = realpathSync(resolved)
|
|
497
|
-
if (!isInNodeModules(real))
|
|
498
|
-
return { path: resolved }
|
|
499
|
-
}
|
|
500
|
-
catch {}
|
|
501
|
-
}
|
|
454
|
+
if (isDev && isLinkedSource(resolved))
|
|
455
|
+
return { path: resolved }
|
|
502
456
|
}
|
|
503
457
|
if (externalOutDir)
|
|
504
458
|
return { path: resolved, external: true }
|
|
@@ -509,14 +463,17 @@ function resolveServerImports(b: PluginBuilder, isDev: boolean, root: string, re
|
|
|
509
463
|
/** `entry` is the generated `_entry.js`, an in-memory file bundled beside `entries` so it shares their chunks. */
|
|
510
464
|
export async function buildServer(env: BundleEnv, entries: string[], serverRoot: string, islandChunks: Map<string, string>, entry: { file: string, source: string }): Promise<void> {
|
|
511
465
|
const { root, outDir, serverDir, graph, cache, islands } = env
|
|
466
|
+
const splitServer = env.splitServer ?? env.isDev
|
|
512
467
|
const transforms = pluginTransforms(env.plugins)
|
|
513
468
|
const externalOutDir = !isInside(root, outDir, { orSame: true })
|
|
514
469
|
|
|
515
470
|
const serverSignature = cache !== undefined
|
|
516
471
|
? JSON.stringify({
|
|
517
472
|
serverRoot,
|
|
518
|
-
assetPrefix: env.assetPrefix, lang: env.lang,
|
|
473
|
+
assetPrefix: env.assetPrefix, lang: env.lang, multiLocale: pluginSetsLang(env.plugins),
|
|
519
474
|
isDev: env.isDev,
|
|
475
|
+
splitServer,
|
|
476
|
+
seeds: islands.seedsModuleState,
|
|
520
477
|
transforms: transforms.key,
|
|
521
478
|
entries: [...entries].sort(),
|
|
522
479
|
islandChunks: [...islandChunks.entries()].sort(([a], [b]) => a.localeCompare(b)),
|
|
@@ -575,10 +532,11 @@ export async function buildServer(env: BundleEnv, entries: string[], serverRoot:
|
|
|
575
532
|
resolveServerImports(b, env.isDev, root, reachesTsx, externalOutDir)
|
|
576
533
|
b.onLoad({ filter: TS_FILTER }, (args) => {
|
|
577
534
|
if (!args.path.endsWith('.tsx')) {
|
|
578
|
-
if (
|
|
535
|
+
if (isInNodeModules(args.path))
|
|
579
536
|
return undefined
|
|
580
537
|
const raw = graph.source(args.path)
|
|
581
|
-
const
|
|
538
|
+
const named = islands.seedsModuleState ? nameModuleState(raw, args.path, relPosix(root, args.path), 'server') : raw
|
|
539
|
+
const transformed = transforms.active ? transforms.apply(named, args.path, 'server') : named
|
|
582
540
|
return transformed !== raw ? { contents: transformed, loader: 'ts' } : undefined
|
|
583
541
|
}
|
|
584
542
|
const analysis = graph.analyses.get(args.path)
|
|
@@ -587,7 +545,7 @@ export async function buildServer(env: BundleEnv, entries: string[], serverRoot:
|
|
|
587
545
|
// Island code of another module may render any component of a module it imports; in its own module, only the code an island reaches.
|
|
588
546
|
const emitMarkers = islands.reach.has(args.path) || (isInteractive && analysis.browserSpans)
|
|
589
547
|
let islandMap: Map<string, IslandBinding> | undefined
|
|
590
|
-
let key = `${env.isDev ? 'd:' : ''}${isInteractive ? '1' : '0'}:${emitMarkers === true ? '1' : emitMarkers === false ? '0' : 's'}|t=${transforms.key}`
|
|
548
|
+
let key = `${env.isDev ? 'd:' : ''}${islands.seedsModuleState ? 'n' : ''}${isInteractive ? '1' : '0'}:${emitMarkers === true ? '1' : emitMarkers === false ? '0' : 's'}|t=${transforms.key}`
|
|
591
549
|
if (rawBindings !== undefined) {
|
|
592
550
|
islandMap = new Map()
|
|
593
551
|
for (const [localName, target] of rawBindings) {
|
|
@@ -609,7 +567,7 @@ export async function buildServer(env: BundleEnv, entries: string[], serverRoot:
|
|
|
609
567
|
emitMarkers,
|
|
610
568
|
devSourceRoot: env.isDev ? root : undefined,
|
|
611
569
|
} as Parameters<typeof compileSsr>[2])
|
|
612
|
-
const code = transforms.apply(compiled.code, args.path, 'server')
|
|
570
|
+
const code = transforms.apply(islands.seedsModuleState ? nameModuleState(compiled.code, args.path, relPosix(root, args.path), 'server') : compiled.code, args.path, 'server')
|
|
613
571
|
if (cache !== undefined && stamp !== undefined)
|
|
614
572
|
cache.ssrCompiled.set(args.path, { mtimeMs: stamp.mtimeMs, size: stamp.size, key, code })
|
|
615
573
|
return { contents: code, loader: 'tsx' }
|
|
@@ -621,13 +579,13 @@ export async function buildServer(env: BundleEnv, entries: string[], serverRoot:
|
|
|
621
579
|
if (transforms.failure !== undefined)
|
|
622
580
|
throw transforms.failure
|
|
623
581
|
}, {
|
|
624
|
-
entrypoints: [...entries, entry.file],
|
|
582
|
+
entrypoints: splitServer ? [...entries, entry.file] : [entry.file],
|
|
625
583
|
files: { [entry.file]: entry.source },
|
|
626
584
|
outdir: serverDir,
|
|
627
585
|
root: serverRoot,
|
|
628
586
|
target: 'bun',
|
|
629
587
|
format: 'esm',
|
|
630
|
-
splitting:
|
|
588
|
+
splitting: splitServer,
|
|
631
589
|
packages: 'external',
|
|
632
590
|
minify: false,
|
|
633
591
|
// A stack trace names the source file and line: Bun reads the map beside each chunk.
|
|
@@ -36,12 +36,12 @@ export interface ClientEntryFiles {
|
|
|
36
36
|
entryPoints: string[]
|
|
37
37
|
islandEntryToKey: Map<string, string>
|
|
38
38
|
runtimeEntryPath: string | undefined
|
|
39
|
-
/** The
|
|
39
|
+
/** The core `<Region>` client bundle, built apart so only the routes that render a `<Region>` load it. */
|
|
40
40
|
regionEntryPath: string | undefined
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
export function writeClientEntries(env: BundleEnv, entriesDir: string, options: { pluginClientEntries: string[],
|
|
44
|
-
const { pluginClientEntries,
|
|
43
|
+
export function writeClientEntries(env: BundleEnv, entriesDir: string, options: { pluginClientEntries: string[], hasRegions: boolean, hasClientOnlyHydrate: boolean, standaloneCss: string[] }): ClientEntryFiles {
|
|
44
|
+
const { pluginClientEntries, hasRegions, hasClientOnlyHydrate } = options
|
|
45
45
|
const out: ClientEntryFiles = { entryPoints: [], islandEntryToKey: new Map(), runtimeEntryPath: undefined, regionEntryPath: undefined }
|
|
46
46
|
if (env.islands.targets.size > 0 || pluginClientEntries.length > 0) {
|
|
47
47
|
const runtimeLines: string[] = []
|
|
@@ -55,16 +55,16 @@ export function writeClientEntries(env: BundleEnv, entriesDir: string, options:
|
|
|
55
55
|
writeEntry(out.runtimeEntryPath, `${runtimeLines.join('\n')}\n`)
|
|
56
56
|
out.entryPoints.push(out.runtimeEntryPath)
|
|
57
57
|
}
|
|
58
|
-
if (
|
|
58
|
+
if (hasRegions) {
|
|
59
59
|
out.regionEntryPath = join(entriesDir, 'regions.ts')
|
|
60
|
-
writeEntry(out.regionEntryPath,
|
|
60
|
+
writeEntry(out.regionEntryPath, `import { initRegions } from 'prunelle/internal/regions';\ninitRegions();\n`)
|
|
61
61
|
out.entryPoints.push(out.regionEntryPath)
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
out.entryPoints.push(...options.standaloneCss)
|
|
65
65
|
|
|
66
66
|
// Each island hands the loader the DOM runtime its chunk imports anyway, so a page whose islands all wait downloads the loader alone.
|
|
67
|
-
const domExports = `export { hydrate${hasClientOnlyHydrate ? ', mount' : ''} } from 'prunelle/internal/dom';\n`
|
|
67
|
+
const domExports = `export { hydrate${hasClientOnlyHydrate ? ', mount' : ''} } from 'prunelle/internal/dom';\n${env.isDev ? `export { hotMount } from 'prunelle/internal/hot';\n` : ''}`
|
|
68
68
|
for (const [key, target] of env.islands.targets) {
|
|
69
69
|
const entryPath = join(entriesDir, `${target.id}.ts`)
|
|
70
70
|
const exportStmt = target.exportName === 'default'
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// What the client build's metafile and chunks are read for: env keys left un-inlined, and the files each entry emitted.
|
|
2
|
+
|
|
3
|
+
import type { BundleEnv, ClientBuild } from './bundle'
|
|
4
|
+
import { existsSync } from 'node:fs'
|
|
5
|
+
import { readsPrivateEnv } from '../compiler/server-only'
|
|
6
|
+
import { isInNodeModules, isTsFile, relPosix } from './files'
|
|
7
|
+
|
|
8
|
+
const LEADING_DOT_SLASH_RE = /^\.\//
|
|
9
|
+
|
|
10
|
+
// A process.env or Bun.env left in a chunk is a key the build did not inline: name the app module that put it there.
|
|
11
|
+
export async function scanEnvLeaks(
|
|
12
|
+
outputs: readonly Bun.BuildArtifact[],
|
|
13
|
+
metafile: ClientBuild['metafile'],
|
|
14
|
+
env: BundleEnv,
|
|
15
|
+
resolveMetaPath: (p: string) => string,
|
|
16
|
+
inlinedEnvKey: (key: string) => boolean,
|
|
17
|
+
clientLeaks: Set<string>,
|
|
18
|
+
): Promise<void> {
|
|
19
|
+
const { root, clientDir, graph } = env
|
|
20
|
+
for (const output of outputs) {
|
|
21
|
+
if (!output.path.endsWith('.js'))
|
|
22
|
+
continue
|
|
23
|
+
const js = await output.text()
|
|
24
|
+
if (!js.includes('process.env') && !js.includes('Bun.env'))
|
|
25
|
+
continue
|
|
26
|
+
const relFile = relPosix(clientDir, output.path)
|
|
27
|
+
const info = metafile?.outputs[relFile] ?? metafile?.outputs[`./${relFile}`]
|
|
28
|
+
for (const [inKey, input] of Object.entries(info?.inputs ?? {})) {
|
|
29
|
+
if (input.bytesInOutput <= 0)
|
|
30
|
+
continue
|
|
31
|
+
const abs = resolveMetaPath(inKey)
|
|
32
|
+
let program = graph.analyses.get(abs)?.program
|
|
33
|
+
if (program === undefined && existsSync(abs) && isTsFile(abs) && !isInNodeModules(abs))
|
|
34
|
+
program = graph.load(abs).program
|
|
35
|
+
if (program !== undefined && readsPrivateEnv(program, inlinedEnvKey))
|
|
36
|
+
clientLeaks.add(`${relPosix(root, abs)} reads process.env or Bun.env (only a process.env.PUBLIC_* set at build time is inlined)`)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Each emitted file of the metafile into the result: the runtime and region scripts, the island chunks and the stylesheets.
|
|
42
|
+
export function recordMetafileOutputs(
|
|
43
|
+
result: ClientBuild,
|
|
44
|
+
metafile: ClientBuild['metafile'],
|
|
45
|
+
env: BundleEnv,
|
|
46
|
+
islandEntryToKey: ReadonlyMap<string, string>,
|
|
47
|
+
runtimeEntryPath: string | undefined,
|
|
48
|
+
regionEntryPath: string | undefined,
|
|
49
|
+
resolveMetaPath: (p: string) => string,
|
|
50
|
+
): void {
|
|
51
|
+
const { assetPrefix } = env
|
|
52
|
+
for (const [outKey, info] of Object.entries(metafile?.outputs ?? {})) {
|
|
53
|
+
const relOut = outKey.replace(LEADING_DOT_SLASH_RE, '')
|
|
54
|
+
const url = `${assetPrefix}/${relOut}`
|
|
55
|
+
const entry = info.entryPoint === undefined ? undefined : resolveMetaPath(info.entryPoint)
|
|
56
|
+
if (entry !== undefined && entry === runtimeEntryPath && relOut.endsWith('.js'))
|
|
57
|
+
result.runtimeScript = url
|
|
58
|
+
if (entry !== undefined && entry === regionEntryPath && relOut.endsWith('.js'))
|
|
59
|
+
result.regionScript = url
|
|
60
|
+
const entryKey = entry === undefined ? undefined : islandEntryToKey.get(entry)
|
|
61
|
+
if (entryKey !== undefined && relOut.endsWith('.js'))
|
|
62
|
+
result.islandChunks.set(entryKey, url)
|
|
63
|
+
if (relOut.endsWith('.css')) {
|
|
64
|
+
if (entry?.endsWith('.css'))
|
|
65
|
+
result.srcCssToUrl.set(entry, url)
|
|
66
|
+
for (const inKey of Object.keys(info.inputs ?? {})) {
|
|
67
|
+
if (inKey.endsWith('.css'))
|
|
68
|
+
result.srcCssToUrl.set(resolveMetaPath(inKey), url)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const cssBundle = (info as { cssBundle?: string }).cssBundle
|
|
72
|
+
if (cssBundle !== undefined && entryKey !== undefined)
|
|
73
|
+
result.islandCssUrl.set(entryKey, `${assetPrefix}/${cssBundle.replace(LEADING_DOT_SLASH_RE, '')}`)
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -9,6 +9,7 @@ import { SERVER_ONLY_FILTER } from '../compiler/server-only'
|
|
|
9
9
|
import { pruneForClient } from './client-prune'
|
|
10
10
|
import { isInNodeModules, relPosix } from './files'
|
|
11
11
|
import { resolvePrunelleSpecifier } from './resolve'
|
|
12
|
+
import { nameModuleState } from './seed-ids'
|
|
12
13
|
|
|
13
14
|
export const PRUNELLE_SPECIFIER_FILTER = /^prunelle(?:\/.*)?$/
|
|
14
15
|
export const TS_FILTER = /\.(?:ts|tsx)$/
|
|
@@ -36,6 +37,8 @@ export interface ClientPluginEnv {
|
|
|
36
37
|
localExports: ReadonlyMap<string, string>
|
|
37
38
|
/** Per file the browser loads only for its own islands, the top-level names its client copy keeps. */
|
|
38
39
|
clientKeep: ReadonlyMap<string, ReadonlySet<string>>
|
|
40
|
+
/** Some module could write module state while an island renders, so the module's signals and stores are named for the page to seed. */
|
|
41
|
+
seedsModuleState: boolean
|
|
39
42
|
}
|
|
40
43
|
|
|
41
44
|
/** The `export { … }` statement each file needs so the client entry can import the islands the file does not export. */
|
|
@@ -117,7 +120,7 @@ export function directPrunelleImports(code: string, graph: ModuleGraph, filename
|
|
|
117
120
|
}
|
|
118
121
|
|
|
119
122
|
export function clientPlugin(env: ClientPluginEnv): BunPlugin {
|
|
120
|
-
const { root, isDev, graph, cache, actionsByFile, transforms, leaks, localExports, clientKeep } = env
|
|
123
|
+
const { root, isDev, graph, cache, actionsByFile, transforms, leaks, localExports, clientKeep, seedsModuleState } = env
|
|
121
124
|
return {
|
|
122
125
|
name: 'prunelle-client',
|
|
123
126
|
setup(b) {
|
|
@@ -146,7 +149,8 @@ export function clientPlugin(env: ClientPluginEnv): BunPlugin {
|
|
|
146
149
|
|
|
147
150
|
if (!args.path.endsWith('.tsx')) {
|
|
148
151
|
const raw = graph.source(args.path)
|
|
149
|
-
const
|
|
152
|
+
const named = isInNodeModules(args.path) || !seedsModuleState ? raw : nameModuleState(raw, args.path, relPosix(root, args.path), 'client')
|
|
153
|
+
const own = transforms.active && !isInNodeModules(args.path) ? transforms.apply(named, args.path, 'client') : named
|
|
150
154
|
const direct = directPrunelleImports(own, graph, args.path)
|
|
151
155
|
return direct !== raw ? { contents: direct, loader: 'ts' } : undefined
|
|
152
156
|
}
|
|
@@ -154,7 +158,7 @@ export function clientPlugin(env: ClientPluginEnv): BunPlugin {
|
|
|
154
158
|
const stamp = graph.stamp(args.path)
|
|
155
159
|
const extraExports = localExports.get(args.path) ?? ''
|
|
156
160
|
const keep = clientKeep.get(args.path)
|
|
157
|
-
const key = `${isDev ? 'd:' : ''}${transforms.key}${extraExports}${keep === undefined ? '' : `|keep=${[...keep].sort().join(',')}`}`
|
|
161
|
+
const key = `${isDev ? 'd:' : ''}${seedsModuleState ? 'n:' : ''}${transforms.key}${extraExports}${keep === undefined ? '' : `|keep=${[...keep].sort().join(',')}`}`
|
|
158
162
|
const cachedDom = stamp !== undefined ? cache?.domCompiled.get(args.path) : undefined
|
|
159
163
|
if (cachedDom !== undefined && cachedDom.mtimeMs === stamp!.mtimeMs && cachedDom.size === stamp!.size && (cachedDom.key ?? '') === key)
|
|
160
164
|
return { contents: cachedDom.code, loader: 'tsx' }
|
|
@@ -165,7 +169,8 @@ export function clientPlugin(env: ClientPluginEnv): BunPlugin {
|
|
|
165
169
|
...(clientSource === source ? { analysis } : {}),
|
|
166
170
|
devSourceRoot: isDev ? root : undefined,
|
|
167
171
|
})
|
|
168
|
-
const
|
|
172
|
+
const named = isInNodeModules(args.path) || !seedsModuleState ? dom.code : nameModuleState(dom.code, args.path, relPosix(root, args.path), 'client')
|
|
173
|
+
const compiled = directPrunelleImports(transforms.apply(named, args.path, 'client'), graph, args.path) + extraExports
|
|
169
174
|
if (cache !== undefined && stamp !== undefined)
|
|
170
175
|
cache.domCompiled.set(args.path, { mtimeMs: stamp.mtimeMs, size: stamp.size, key, code: compiled })
|
|
171
176
|
return { contents: compiled, loader: 'tsx' }
|
package/src/build/ecoindex.ts
CHANGED
|
@@ -175,10 +175,12 @@ export function computeEcoIndex(inputs: EcoIndexInputs): Omit<RouteEcoIndex, 'ur
|
|
|
175
175
|
const qReq = computeQuantile(QUANTILES_REQ, requests)
|
|
176
176
|
const qSize = computeQuantile(QUANTILES_SIZE, weightBytes / 1000)
|
|
177
177
|
const raw = 100 - (5 * (3 * qDom + 2 * qReq + qSize)) / 6
|
|
178
|
-
const
|
|
178
|
+
const exact = Math.min(100, Math.max(0, raw))
|
|
179
|
+
const score = Math.round(exact)
|
|
179
180
|
const ghgGrams = Math.round((2 + (2 * (50 - score)) / 100) * 100) / 100
|
|
180
181
|
const waterCl = Math.round((3 + (3 * (50 - score)) / 100) * 100) / 100
|
|
181
|
-
|
|
182
|
+
// The reference grades the unrounded score: 80.2 shows as 80 and is still an A.
|
|
183
|
+
return { grade: gradeForScore(exact), score, dom, requests, weightBytes, ghgGrams, waterCl }
|
|
182
184
|
}
|
|
183
185
|
|
|
184
186
|
function htmlServedSize(abs: string, raw: Uint8Array): number {
|
package/src/build/files.ts
CHANGED
|
@@ -169,3 +169,21 @@ export function isHiddenPublicPath(rel: string): boolean {
|
|
|
169
169
|
const segments = rel.split(/[\\/]/).filter(s => s !== '')
|
|
170
170
|
return segments.some((s, i) => s.startsWith('.') && !(i === 0 && s === '.well-known'))
|
|
171
171
|
}
|
|
172
|
+
|
|
173
|
+
/** Runs Bun.build, failing first with what `ownFailure` throws (a leak, a plugin hook), then with Bun's own logs. */
|
|
174
|
+
export async function runBunBuild(side: 'client' | 'server', root: string, ownFailure: () => void, config: Parameters<typeof Bun.build>[0]): Promise<Awaited<ReturnType<typeof Bun.build>>> {
|
|
175
|
+
let result: Awaited<ReturnType<typeof Bun.build>>
|
|
176
|
+
try {
|
|
177
|
+
result = await Bun.build(config)
|
|
178
|
+
}
|
|
179
|
+
catch (err) {
|
|
180
|
+
ownFailure()
|
|
181
|
+
if (err instanceof AggregateError && err.errors.length > 0)
|
|
182
|
+
throw new Error(`prunelle build (${side}) failed:\n${formatBunBuildLogs(err.errors, root)}`)
|
|
183
|
+
throw err
|
|
184
|
+
}
|
|
185
|
+
ownFailure()
|
|
186
|
+
if (!result.success)
|
|
187
|
+
throw new Error(`prunelle build (${side}) failed:\n${formatBunBuildLogs(result.logs, root)}`)
|
|
188
|
+
return result
|
|
189
|
+
}
|