@octanejs/vite-plugin 0.4.3 → 0.6.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 CHANGED
@@ -7,7 +7,7 @@ client/server builds, and preview when an `octane.config.ts` declares routes.
7
7
  ## Install
8
8
 
9
9
  ```sh
10
- pnpm add octane @octanejs/vite-plugin @tsrx/oxc@0.20.0
10
+ pnpm add octane @octanejs/vite-plugin @tsrx/oxc@0.23.0
11
11
  pnpm add -D vite
12
12
  ```
13
13
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@octanejs/vite-plugin",
3
- "version": "0.4.3",
3
+ "version": "0.6.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "engines": {
@@ -43,20 +43,25 @@
43
43
  "types": "./types/node.d.ts",
44
44
  "import": "./src/server/node-http.js",
45
45
  "default": "./src/server/node-http.js"
46
+ },
47
+ "./seeds": {
48
+ "types": "./types/seeds.d.ts",
49
+ "import": "./src/seeds.js",
50
+ "default": "./src/seeds.js"
46
51
  }
47
52
  },
48
53
  "dependencies": {
49
- "@octanejs/app-core": "0.1.9",
54
+ "@octanejs/app-core": "0.3.0",
50
55
  "@ripple-ts/adapter": "^0.4.0"
51
56
  },
52
57
  "peerDependencies": {
53
- "octane": "^0.15.0",
58
+ "octane": "^0.17.0",
54
59
  "vite": "^8.0.16"
55
60
  },
56
61
  "devDependencies": {
57
62
  "@types/jsdom": "30.0.0",
58
63
  "@types/node": "^24.13.3",
59
- "octane": "0.15.0",
64
+ "octane": "0.17.0",
60
65
  "playwright": "^1.61.1",
61
66
  "playwright-webkit": "1.63.0",
62
67
  "vite": "^8.1.5"
@@ -30,6 +30,9 @@ export function createClientBuildState(onIncompatibleDiscovery) {
30
30
  get hasStatefulModules() {
31
31
  return sources.size !== 0 || signalSources.size !== 0;
32
32
  },
33
+ get hasStreamedSignals() {
34
+ return signalSources.size !== 0;
35
+ },
33
36
  begin() {
34
37
  buildId = PENDING_CLIENT_BUILD_ID;
35
38
  hadHotUpdates = false;
package/src/index.js CHANGED
@@ -67,6 +67,7 @@ export {
67
67
  handleRpcRequest,
68
68
  handleServerRoute,
69
69
  is_rpc_request,
70
+ routeSeed,
70
71
  runMiddlewareChain,
71
72
  } from '@octanejs/app-core';
72
73
  export {
@@ -381,7 +382,7 @@ function createIslandsShellWarnings(server, projectRoot) {
381
382
 
382
383
  /**
383
384
  * Every module path the server can name in #__octane_data — page entries,
384
- * layouts, the preHydrate hook, and root boundaries. The generated hydrate
385
+ * layouts, the preHydrate hook, the data module, and root boundaries. The generated hydrate
385
386
  * entry maps each as a LITERAL `() => import('/src/…')`. Production needs the
386
387
  * map so Rollup chunks and hashes the modules; dev needs it so the imports go
387
388
  * through Vite's import analysis and share URL identity with every other
@@ -397,6 +398,7 @@ function collect_hydrate_module_paths(config) {
397
398
  .filter((r) => r.type === 'render')
398
399
  .flatMap((r) => [get_route_entry_path(/** @type {RenderRoute} */ (r).entry), r.layout]);
399
400
  if (cfg.router.preHydrate) entries.push(cfg.router.preHydrate);
401
+ if (cfg.data) entries.push(cfg.data);
400
402
  entries.push(
401
403
  get_route_entry_path(cfg.rootBoundary.pending),
402
404
  get_route_entry_path(cfg.rootBoundary.catch),
@@ -404,6 +406,61 @@ function collect_hydrate_module_paths(config) {
404
406
  return [...new Set(entries.filter((e) => typeof e === 'string'))];
405
407
  }
406
408
 
409
+ /**
410
+ * Narrow the production hydrate entry to the features its client graph uses.
411
+ * Loading that graph first runs every compiled transform, so independent
412
+ * islands and streamed signals are recorded before the entry's own lazy
413
+ * imports reach chunking. A void root needs every page and layout the entry can
414
+ * render proven a void `@{}` body, by the compiler's own final-code proof.
415
+ *
416
+ * @param {import('vite').Rollup.PluginContext} context
417
+ * @param {ResolvedOctaneConfig} config
418
+ * @param {string[]} entries
419
+ * @param {ReturnType<typeof createClientBuildState>} clientBuild
420
+ * @param {(context: any, importer: string, imports: Array<{ request: string, imported: string }>) => Promise<unknown[]>} provenVoidImports
421
+ */
422
+ async function hydrate_entry_features(context, config, entries, clientBuild, provenVoidImports) {
423
+ const seen = new Set();
424
+ // Rolldown's ModuleInfo cannot tell an external import apart, and loading
425
+ // one never settles, so each import is resolved again from its importer.
426
+ /** @param {string} request @param {string} importer @returns {Promise<void>} */
427
+ const visit = async (request, importer) => {
428
+ const resolved = await context.resolve(request, importer);
429
+ if (!resolved || resolved.external || seen.has(resolved.id)) return;
430
+ seen.add(resolved.id);
431
+ const info = await context.load({ id: resolved.id, resolveDependencies: true });
432
+ await Promise.all(
433
+ [...info.importedIds, ...info.dynamicallyImportedIds].map((id) => visit(id, resolved.id)),
434
+ );
435
+ };
436
+ await Promise.all(entries.map((entry) => visit(entry, RESOLVED_VIRTUAL_HYDRATE_ID)));
437
+ const rootBoundary = !!(config.rootBoundary.pending || config.rootBoundary.catch);
438
+ const imports = config.router.routes
439
+ .filter(
440
+ /** @returns {route is RenderRoute} */
441
+ (route) => route.type === 'render' && route.hydrate !== 'islands',
442
+ )
443
+ .flatMap((route) => [
444
+ {
445
+ request: /** @type {string} */ (get_route_entry_path(route.entry)),
446
+ imported: get_route_entry_export_name(route.entry) ?? 'default',
447
+ },
448
+ ...(route.layout ? [{ request: route.layout, imported: 'default' }] : []),
449
+ ]);
450
+ return {
451
+ rootBoundary,
452
+ independentHydration: clientBuild.metadata().capabilities.independentHydration,
453
+ streamedSignals: clientBuild.hasStreamedSignals,
454
+ // A root boundary or the data module's DataRoot wraps the route in a body no proof covers.
455
+ voidRoot:
456
+ !rootBoundary &&
457
+ !config.data &&
458
+ imports.length > 0 &&
459
+ (await provenVoidImports(context, RESOLVED_VIRTUAL_HYDRATE_ID, imports)).length ===
460
+ imports.length,
461
+ };
462
+ }
463
+
407
464
  /**
408
465
  * The recommended Octane Vite integration. With no octane.config.ts it behaves
409
466
  * as a compiler plugin inside a normal Vite SPA; configured routes activate the
@@ -833,10 +890,21 @@ export function octane(inlineOptions = {}) {
833
890
  // restarts. Literal `import('/src/…')` entries go through import
834
891
  // analysis and share url identity with every other importer.
835
892
  let entries = staticEntries;
836
- if (!isBuild) {
837
- const loaded = octaneConfig ?? (await loadStartupConfig(root))?.config ?? null;
838
- entries = collect_hydrate_module_paths(loaded);
839
- }
893
+ // A watch rebuild can add a feature after this entry has loaded.
894
+ const features =
895
+ isBuild && config.build.watch == null && buildOctaneConfig
896
+ ? await hydrate_entry_features(
897
+ this,
898
+ buildOctaneConfig,
899
+ entries,
900
+ clientBuild,
901
+ compilerPlugin.api.provenVoidImports,
902
+ )
903
+ : null;
904
+ const loaded = isBuild
905
+ ? buildOctaneConfig
906
+ : (octaneConfig ?? (await loadStartupConfig(root))?.config ?? null);
907
+ if (!isBuild) entries = collect_hydrate_module_paths(loaded);
840
908
  const file = write_project_generated_file(
841
909
  config,
842
910
  'client-entry.js',
@@ -845,6 +913,8 @@ export function octane(inlineOptions = {}) {
845
913
  staticEntries: entries,
846
914
  clientBuildId: clientBuild.buildId,
847
915
  devClientBuild: !isBuild,
916
+ ...features,
917
+ data: Boolean(loaded?.data),
848
918
  }),
849
919
  );
850
920
  return fs.readFileSync(file, 'utf-8');
@@ -873,12 +943,13 @@ export function octane(inlineOptions = {}) {
873
943
 
874
944
  /**
875
945
  * Observe the compiler's client output after the pre-transform. A generated
876
- * __serverRpc call is an exact, syntax-level signal that this source owns a
877
- * `module server`; production uses the collected paths as static SSR imports.
946
+ * __serverRpc or __serverRpcStream call is an exact, syntax-level signal that
947
+ * this source owns a `module server`; production uses the collected paths as
948
+ * static SSR imports.
878
949
  */
879
950
  transform(code, id, options) {
880
951
  if (!isBuild || options?.ssr || !is_octane_module_path(id.split('?')[0])) return null;
881
- if (!code.includes('_$__serverRpc(')) return null;
952
+ if (!code.includes('_$__serverRpc')) return null;
882
953
  const file = id.split('?')[0];
883
954
  const relative = path.relative(root, file);
884
955
  const isWithinRoot =
@@ -1258,6 +1329,8 @@ export function octane(inlineOptions = {}) {
1258
1329
  routes: cfg.router.routes,
1259
1330
  octaneConfigPath: getOctaneConfigPath(root),
1260
1331
  rootBoundary: cfg.rootBoundary,
1332
+ data: cfg.data,
1333
+ dataRootModuleId: requireFromPlugin.resolve('@octanejs/app-core/data-root'),
1261
1334
  rpcModulePaths: [...serverModuleModules],
1262
1335
  clientAssetMap,
1263
1336
  clientBuild: completedClientBuild,
@@ -1396,6 +1469,7 @@ export function octane(inlineOptions = {}) {
1396
1469
  * output?: 'js' | 'ts',
1397
1470
  * profile?: boolean | 'auto',
1398
1471
  * strong?: boolean,
1472
+ * strongDependencies?: import('octane/compiler/vite').OctaneVitePluginOptions['strongDependencies'],
1399
1473
  * textTypes?: import('octane/compiler/vite').OctaneVitePluginOptions['textTypes'],
1400
1474
  * knownAttributeSpreads?: import('octane/compiler/vite').OctaneVitePluginOptions['knownAttributeSpreads'],
1401
1475
  * domBindingFixedProps?: import('octane/compiler/vite').OctaneVitePluginOptions['domBindingFixedProps'],
@@ -1442,17 +1516,13 @@ export function octane(inlineOptions = {}) {
1442
1516
  if (typeof compilerConfigHook === 'function') {
1443
1517
  compilerPlugin.config = function compilerConfigWithAppOptions(userConfig, env) {
1444
1518
  const projectRoot = userConfig.root ? path.resolve(userConfig.root) : process.cwd();
1445
- // Both inline compiler options override app config independently. Preserve
1446
- // the synchronous path when neither needs to read octane.config.ts.
1447
- if (inlineOptions.renderers !== undefined && inlineOptions.strong !== undefined) {
1448
- compilerConfigWatchFiles.clear();
1449
- return compilerConfigHook.call(this, userConfig, env);
1450
- }
1451
-
1519
+ // Inline `renderers` and `strong` override app config independently.
1520
+ // `strongDependencies` comes only from octane.config.ts, so it is always read.
1452
1521
  const configPath = getOctaneConfigPath(projectRoot);
1453
1522
  if (!octaneConfigExists(projectRoot)) {
1454
1523
  if (inlineOptions.renderers === undefined) delete compilerOptions.renderers;
1455
1524
  if (inlineOptions.strong === undefined) delete compilerOptions.strong;
1525
+ delete compilerOptions.strongDependencies;
1456
1526
  compilerConfigWatchFiles.clear();
1457
1527
  // A new app config can introduce Strong mode or renderer rules.
1458
1528
  compilerConfigWatchFiles.add(path.resolve(configPath));
@@ -1467,6 +1537,7 @@ export function octane(inlineOptions = {}) {
1467
1537
  if (inlineOptions.strong === undefined) {
1468
1538
  compilerOptions.strong = config.config.compiler.strong;
1469
1539
  }
1540
+ compilerOptions.strongDependencies = config.config.compiler.strongDependencies;
1470
1541
  compilerConfigWatchFiles.clear();
1471
1542
  for (const file of [...config.dependencies, ...config.missingDependencies]) {
1472
1543
  compilerConfigWatchFiles.add(path.resolve(file));
package/src/seeds.js ADDED
@@ -0,0 +1 @@
1
+ export { readRouteSeed, routeSeed } from '@octanejs/app-core/seeds';
@@ -2,4 +2,5 @@ export {
2
2
  createLayoutWrapper,
3
3
  createPropsWrapper,
4
4
  createRootBoundaryWrapper,
5
+ provideRouteData,
5
6
  } from '@octanejs/app-core/production';
@@ -1 +1 @@
1
- export { composeHtmlStream } from '@octanejs/app-core/html';
1
+ export { composeHtmlStream, endRoute, routeEnding } from '@octanejs/app-core/html';
@@ -3,7 +3,8 @@ import { readFile } from 'node:fs/promises';
3
3
  import { randomUUID } from 'node:crypto';
4
4
  import { join } from 'node:path';
5
5
  import { createRpcRegistry } from '@octanejs/app-core';
6
- import { composeHtmlStream } from './html-stream.js';
6
+ import { createRequire } from 'node:module';
7
+ import { composeHtmlStream, endRoute, routeEnding } from './html-stream.js';
7
8
  import {
8
9
  getContextNonce,
9
10
  injectHydrationEntry,
@@ -16,6 +17,7 @@ import {
16
17
  createLayoutWrapper,
17
18
  createPropsWrapper,
18
19
  createRootBoundaryWrapper,
20
+ provideRouteData,
19
21
  } from './component-wrappers.js';
20
22
  import {
21
23
  get_component_export,
@@ -23,6 +25,9 @@ import {
23
25
  get_route_entry_path,
24
26
  } from '../routes.js';
25
27
 
28
+ /** App-core's `DataRoot`, compiled for the server through Vite's SSR loader. */
29
+ const DATA_ROOT_MODULE = createRequire(import.meta.url).resolve('@octanejs/app-core/data-root');
30
+
26
31
  /**
27
32
  * @typedef {import('@octanejs/vite-plugin').Context} Context
28
33
  * @typedef {import('@octanejs/vite-plugin').RenderRoute} RenderRoute
@@ -208,6 +213,16 @@ export async function handleRenderRoute(
208
213
  { pending: PendingComponent, catch: CatchComponent },
209
214
  /** @type {any} */ (serverRuntime),
210
215
  );
216
+ const dataPath = octaneConfig?.data;
217
+ if (dataPath) {
218
+ const dataModule = await vite.ssrLoadModule(dataPath);
219
+ const { DataRoot } = await vite.ssrLoadModule(DATA_ROOT_MODULE);
220
+ RootComponent = provideRouteData(
221
+ context,
222
+ { path: dataPath, open: dataModule.default, Root: DataRoot },
223
+ RootComponent,
224
+ );
225
+ }
211
226
  // Discovery runs while loading the page/layout above. Snapshot capability
212
227
  // afterwards; its live registry also resolves widgets compiled lazily later.
213
228
  const clientBuild = getClientBuild?.();
@@ -232,6 +247,7 @@ export async function handleRenderRoute(
232
247
  params: context.params,
233
248
  url: requestUrl,
234
249
  preHydrate: octaneConfig?.router.preHydrate ?? null,
250
+ data: octaneConfig?.data ?? null,
235
251
  rootBoundary: {
236
252
  pending: serializeComponentEntry(pendingEntry),
237
253
  catch: serializeComponentEntry(catchEntry),
@@ -314,10 +330,17 @@ export async function handleRenderRoute(
314
330
 
315
331
  // Feature-bearing documents activate after the complete shell and its
316
332
  // selection authority; open result channels must not hold input hostage.
317
- const body = composeHtmlStream(prefix, renderStream, suffix, preparedTemplate.afterShell);
333
+ const body = composeHtmlStream(
334
+ prefix,
335
+ renderStream,
336
+ suffix,
337
+ preparedTemplate.afterShell,
338
+ routeEnding(context, nonceAttribute(nonce)),
339
+ );
318
340
 
319
341
  return new Response(body, { status, headers });
320
342
  } catch (error) {
343
+ endRoute(context);
321
344
  console.error('[octane] SSR render error:', error);
322
345
 
323
346
  const errorHtml = generateErrorHtml(error, route);
@@ -0,0 +1 @@
1
+ export * from '@octanejs/app-core/seeds';