@reckona/mreact-router 0.0.89 → 0.0.91

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/src/render.ts CHANGED
@@ -1062,6 +1062,8 @@ async function renderAppRequestInternal(options: RenderAppRequestOptions): Promi
1062
1062
  options.serverModules,
1063
1063
  options.serverModuleCacheVersion,
1064
1064
  options.vitePlugins,
1065
+ undefined,
1066
+ options.importPolicy,
1065
1067
  ),
1066
1068
  );
1067
1069
  const pageHtml = renderedPage.html;
@@ -1102,6 +1104,7 @@ async function renderAppRequestInternal(options: RenderAppRequestOptions): Promi
1102
1104
  clientRouteInferenceCache,
1103
1105
  timing,
1104
1106
  vitePlugins: options.vitePlugins,
1107
+ importPolicy: options.importPolicy,
1105
1108
  }),
1106
1109
  );
1107
1110
  const metadata = await loadComposedRouteMetadata({
@@ -1202,6 +1205,7 @@ async function renderAppRequestInternal(options: RenderAppRequestOptions): Promi
1202
1205
  serverSourceFiles: options.serverSourceFiles,
1203
1206
  vitePlugins: options.vitePlugins,
1204
1207
  clientRouteInferenceCache,
1208
+ importPolicy: options.importPolicy,
1205
1209
  script: clientScript,
1206
1210
  clientReferenceManifest: output.metadata.clientReferenceManifest,
1207
1211
  });
@@ -1238,6 +1242,7 @@ async function renderAppRequestInternal(options: RenderAppRequestOptions): Promi
1238
1242
  serverSourceFiles: options.serverSourceFiles,
1239
1243
  vitePlugins: options.vitePlugins,
1240
1244
  clientRouteInferenceCache,
1245
+ importPolicy: options.importPolicy,
1241
1246
  clientRoute,
1242
1247
  script: clientScript,
1243
1248
  clientReferenceManifest: output.metadata.clientReferenceManifest,
@@ -1301,6 +1306,7 @@ async function renderAppRequestInternal(options: RenderAppRequestOptions): Promi
1301
1306
  options.serverModuleCacheVersion,
1302
1307
  options.vitePlugins,
1303
1308
  timing,
1309
+ options.importPolicy,
1304
1310
  ),
1305
1311
  );
1306
1312
  finishRenderTimingPhase(timing, phaseStartedAt, "pageRenderMs");
@@ -2584,6 +2590,7 @@ async function runServerModuleWithSlots(
2584
2590
  serverModuleCacheVersion: string | undefined,
2585
2591
  vitePlugins?: readonly PluginOption[] | undefined,
2586
2592
  timing?: RenderTiming | undefined,
2593
+ importPolicy?: AppRouterImportPolicy | undefined,
2587
2594
  ): Promise<{ html: string; slots: Record<string, string> }> {
2588
2595
  const moduleLoadStartedAt = renderTimingPhaseStartedAt(timing);
2589
2596
  const module = await loadServerModule(
@@ -2592,6 +2599,7 @@ async function runServerModuleWithSlots(
2592
2599
  serverModules,
2593
2600
  serverModuleCacheVersion,
2594
2601
  vitePlugins,
2602
+ importPolicy,
2595
2603
  );
2596
2604
  finishRenderTimingPhase(timing, moduleLoadStartedAt, "pageModuleLoadMs");
2597
2605
  const component = selectServerComponent(module);
@@ -2614,6 +2622,7 @@ async function loadServerModule(
2614
2622
  serverModules: ReadonlyMap<string, BuiltServerModuleArtifact> | undefined,
2615
2623
  serverModuleCacheVersion: string | undefined,
2616
2624
  vitePlugins?: readonly PluginOption[] | undefined,
2625
+ importPolicy?: AppRouterImportPolicy | undefined,
2617
2626
  ): Promise<ServerModuleExports> {
2618
2627
  const artifact = serverModules?.get(sourcefile)?.string;
2619
2628
  const codeHash = memoizedHashText(code);
@@ -2635,11 +2644,21 @@ async function loadServerModule(
2635
2644
  ? undefined
2636
2645
  : `server-component:${serverModuleCacheVersion}:${sourcefile}:${
2637
2646
  moduleCode === code ? codeHash : memoizedHashText(moduleCode)
2638
- }:${vitePluginsCacheKey(vitePlugins)}`;
2647
+ }:${importPolicyCacheKey(importPolicy)}:${vitePluginsCacheKey(vitePlugins)}`;
2639
2648
  return await importAppRouterSourceModule<ServerModuleExports>({
2640
2649
  cacheKey,
2641
2650
  code: moduleCode,
2642
2651
  label: `server-component:${sourcefile}`,
2652
+ plugins:
2653
+ importPolicy === undefined
2654
+ ? undefined
2655
+ : [
2656
+ createAppRouterImportPolicyPlugin({
2657
+ appDir: dirname(sourcefile),
2658
+ importPolicy,
2659
+ label: "Server component",
2660
+ }),
2661
+ ],
2643
2662
  ...(prebuiltCode === undefined
2644
2663
  ? {
2645
2664
  resolveDir: dirname(sourcefile),
@@ -2677,6 +2696,7 @@ async function loadServerComponent(
2677
2696
  serverModules: ReadonlyMap<string, BuiltServerModuleArtifact> | undefined,
2678
2697
  serverModuleCacheVersion: string | undefined,
2679
2698
  vitePlugins?: readonly PluginOption[] | undefined,
2699
+ importPolicy?: AppRouterImportPolicy | undefined,
2680
2700
  ): Promise<ServerComponent> {
2681
2701
  const module = await loadServerModule(
2682
2702
  code,
@@ -2684,6 +2704,7 @@ async function loadServerComponent(
2684
2704
  serverModules,
2685
2705
  serverModuleCacheVersion,
2686
2706
  vitePlugins,
2707
+ importPolicy,
2687
2708
  );
2688
2709
  return selectServerComponent(module);
2689
2710
  }
@@ -2732,6 +2753,7 @@ function runServerStreamModule(
2732
2753
  serverSourceFiles?: ReadonlyMap<string, string> | undefined;
2733
2754
  script?: string | undefined;
2734
2755
  vitePlugins?: readonly PluginOption[] | undefined;
2756
+ importPolicy?: AppRouterImportPolicy | undefined;
2735
2757
  },
2736
2758
  ): ReadableStream<Uint8Array> {
2737
2759
  return renderToReadableStream(async (sink) => {
@@ -2741,6 +2763,7 @@ function runServerStreamModule(
2741
2763
  serverModules: options.serverModules,
2742
2764
  serverModuleCacheVersion: options.serverModuleCacheVersion,
2743
2765
  vitePlugins: options.vitePlugins,
2766
+ importPolicy: options.importPolicy,
2744
2767
  });
2745
2768
  const layoutShells = await layoutShellsForPage(
2746
2769
  options.appDir,
@@ -2752,6 +2775,7 @@ function runServerStreamModule(
2752
2775
  options.serverSourceFiles,
2753
2776
  options.clientRouteInferenceCache,
2754
2777
  options.vitePlugins,
2778
+ options.importPolicy,
2755
2779
  );
2756
2780
  const marker = options.clientRoute
2757
2781
  ? hydrationMarkerParts({
@@ -2790,6 +2814,7 @@ function runServerStreamModule(
2790
2814
  options.serverModules,
2791
2815
  options.serverModuleCacheVersion,
2792
2816
  options.vitePlugins,
2817
+ options.importPolicy,
2793
2818
  );
2794
2819
 
2795
2820
  sink.append(marker?.suffix ?? "");
@@ -2991,6 +3016,7 @@ async function runServerStreamModuleWithLoading(
2991
3016
  serverSourceFiles?: ReadonlyMap<string, string> | undefined;
2992
3017
  script?: string | undefined;
2993
3018
  vitePlugins?: readonly PluginOption[] | undefined;
3019
+ importPolicy?: AppRouterImportPolicy | undefined;
2994
3020
  },
2995
3021
  ): Promise<ReadableStream<Uint8Array>> {
2996
3022
  const loadingProps = {
@@ -3009,6 +3035,7 @@ async function runServerStreamModuleWithLoading(
3009
3035
  options.serverSourceFiles,
3010
3036
  options.clientRouteInferenceCache,
3011
3037
  options.vitePlugins,
3038
+ options.importPolicy,
3012
3039
  );
3013
3040
  const loadingHtml = await renderServerFileToHtml(
3014
3041
  options.loadingFile,
@@ -3065,6 +3092,7 @@ async function runServerStreamModuleWithLoading(
3065
3092
  options.serverModules,
3066
3093
  options.serverModuleCacheVersion,
3067
3094
  options.vitePlugins,
3095
+ options.importPolicy,
3068
3096
  );
3069
3097
  },
3070
3098
  {
@@ -3154,6 +3182,7 @@ async function appendServerStreamModule(
3154
3182
  serverModules: ReadonlyMap<string, BuiltServerModuleArtifact> | undefined,
3155
3183
  serverModuleCacheVersion: string | undefined,
3156
3184
  vitePlugins?: readonly PluginOption[] | undefined,
3185
+ importPolicy?: AppRouterImportPolicy | undefined,
3157
3186
  ): Promise<void> {
3158
3187
  const module = await loadServerStreamModule(
3159
3188
  code,
@@ -3161,6 +3190,7 @@ async function appendServerStreamModule(
3161
3190
  serverModules,
3162
3191
  serverModuleCacheVersion,
3163
3192
  vitePlugins,
3193
+ importPolicy,
3164
3194
  );
3165
3195
  const component = selectStreamComponent(module);
3166
3196
 
@@ -3175,6 +3205,7 @@ async function renderServerStreamSlots(
3175
3205
  serverModules?: ReadonlyMap<string, BuiltServerModuleArtifact> | undefined;
3176
3206
  serverModuleCacheVersion?: string | undefined;
3177
3207
  vitePlugins?: readonly PluginOption[] | undefined;
3208
+ importPolicy?: AppRouterImportPolicy | undefined;
3178
3209
  },
3179
3210
  ): Promise<Record<string, string>> {
3180
3211
  if (!hasRouteSlotsExport(code)) {
@@ -3187,6 +3218,7 @@ async function renderServerStreamSlots(
3187
3218
  options.serverModules,
3188
3219
  options.serverModuleCacheVersion,
3189
3220
  options.vitePlugins,
3221
+ options.importPolicy,
3190
3222
  );
3191
3223
 
3192
3224
  if (module.slots === undefined) {
@@ -3220,6 +3252,7 @@ async function loadServerStreamModule(
3220
3252
  serverModules: ReadonlyMap<string, BuiltServerModuleArtifact> | undefined,
3221
3253
  serverModuleCacheVersion: string | undefined,
3222
3254
  vitePlugins?: readonly PluginOption[] | undefined,
3255
+ importPolicy?: AppRouterImportPolicy | undefined,
3223
3256
  ): Promise<StreamModuleExports> {
3224
3257
  const artifactCode = serverModules?.get(sourcefile)?.stream;
3225
3258
  const codeHash = memoizedHashText(code);
@@ -3241,11 +3274,21 @@ async function loadServerStreamModule(
3241
3274
  ? undefined
3242
3275
  : `server-stream-component:${serverModuleCacheVersion}:${sourcefile}:${
3243
3276
  moduleCode === code ? codeHash : memoizedHashText(moduleCode)
3244
- }:${vitePluginsCacheKey(vitePlugins)}`;
3277
+ }:${importPolicyCacheKey(importPolicy)}:${vitePluginsCacheKey(vitePlugins)}`;
3245
3278
  return await importAppRouterSourceModule<StreamModuleExports>({
3246
3279
  cacheKey,
3247
3280
  code: moduleCode,
3248
3281
  label: `server-stream-component:${sourcefile}`,
3282
+ plugins:
3283
+ importPolicy === undefined
3284
+ ? undefined
3285
+ : [
3286
+ createAppRouterImportPolicyPlugin({
3287
+ appDir: dirname(sourcefile),
3288
+ importPolicy,
3289
+ label: "Server stream component",
3290
+ }),
3291
+ ],
3249
3292
  ...(prebuiltCode === undefined
3250
3293
  ? {
3251
3294
  resolveDir: dirname(sourcefile),
@@ -3284,6 +3327,7 @@ async function applyLayouts(options: {
3284
3327
  serverSourceFiles?: ReadonlyMap<string, string> | undefined;
3285
3328
  timing?: RenderTiming | undefined;
3286
3329
  vitePlugins?: readonly PluginOption[] | undefined;
3330
+ importPolicy?: AppRouterImportPolicy | undefined;
3287
3331
  }): Promise<string> {
3288
3332
  const layoutFiles = await shellFilesForPage(
3289
3333
  options.appDir,
@@ -3306,6 +3350,7 @@ async function applyLayouts(options: {
3306
3350
  options.clientRouteInferenceCache,
3307
3351
  options.timing,
3308
3352
  options.vitePlugins,
3353
+ options.importPolicy,
3309
3354
  );
3310
3355
  shellHasOutOfOrderBoundary ||= rendered.hasOutOfOrderBoundary;
3311
3356
  html = `${rendered.prefix}${html}${rendered.suffix}`;
@@ -3339,6 +3384,7 @@ async function layoutShellsForPage(
3339
3384
  serverSourceFiles: ReadonlyMap<string, string> | undefined,
3340
3385
  clientRouteInferenceCache: ClientRouteInferenceCache | undefined,
3341
3386
  vitePlugins?: readonly PluginOption[] | undefined,
3387
+ importPolicy?: AppRouterImportPolicy | undefined,
3342
3388
  ): Promise<RenderedShell[]> {
3343
3389
  const layoutFiles = await shellFilesForPage(appDir, pageFile, serverModuleCacheVersion);
3344
3390
  const shells: RenderedShell[] = [];
@@ -3357,6 +3403,7 @@ async function layoutShellsForPage(
3357
3403
  clientRouteInferenceCache,
3358
3404
  undefined,
3359
3405
  vitePlugins,
3406
+ importPolicy,
3360
3407
  ),
3361
3408
  );
3362
3409
  }
@@ -3382,12 +3429,13 @@ async function renderShellPrefixSuffix(
3382
3429
  clientRouteInferenceCache?: ClientRouteInferenceCache | undefined,
3383
3430
  timing?: RenderTiming | undefined,
3384
3431
  vitePlugins?: readonly PluginOption[] | undefined,
3432
+ importPolicy?: AppRouterImportPolicy | undefined,
3385
3433
  ): Promise<RenderedShell> {
3386
3434
  const hasNamedSlots = Object.keys(slotContext.namedSlots).length > 0;
3387
3435
  const cacheKey =
3388
3436
  serverModuleCacheVersion === undefined || hasNamedSlots || shell.kind === "template"
3389
3437
  ? undefined
3390
- : `${appDir}\0${shell.file}\0${serverModuleCacheVersion}\0${vitePluginsCacheKey(vitePlugins)}`;
3438
+ : `${appDir}\0${shell.file}\0${serverModuleCacheVersion}\0${importPolicyCacheKey(importPolicy)}\0${vitePluginsCacheKey(vitePlugins)}`;
3391
3439
  if (cacheKey !== undefined) {
3392
3440
  const cached = readRouterRuntimeCacheEntry(
3393
3441
  renderedShellCache,
@@ -3446,6 +3494,7 @@ async function renderShellPrefixSuffix(
3446
3494
  serverModules,
3447
3495
  serverModuleCacheVersion,
3448
3496
  vitePlugins,
3497
+ importPolicy,
3449
3498
  ),
3450
3499
  )
3451
3500
  : await loadServerComponent(
@@ -3454,6 +3503,7 @@ async function renderShellPrefixSuffix(
3454
3503
  serverModules,
3455
3504
  serverModuleCacheVersion,
3456
3505
  vitePlugins,
3506
+ importPolicy,
3457
3507
  );
3458
3508
  addRenderTimingPhaseDuration(timing, phaseStartedAt, "layoutModuleLoadMs");
3459
3509
  phaseStartedAt = renderTimingPhaseStartedAt(timing);
@@ -1,8 +1,10 @@
1
1
  import { loadConfigFromFile, type ConfigEnv, type PluginOption, type UserConfig } from "vite";
2
2
  import type { ResolvedAppRouterProject } from "./config.js";
3
+ import type { AppRouterImportPolicy } from "./import-policy.js";
3
4
  import { mreactRouterConfigFromPlugins } from "./vite.js";
4
5
 
5
6
  export interface LoadedMreactRouterViteConfig {
7
+ importPolicy?: AppRouterImportPolicy | undefined;
6
8
  project: ResolvedAppRouterProject;
7
9
  serverPort?: number | undefined;
8
10
  viteConfig?: UserConfig | undefined;
@@ -41,8 +43,12 @@ export async function loadMreactRouterViteConfigDetails(options: {
41
43
  }
42
44
 
43
45
  const serverPort = loaded.config.server?.port;
46
+ const importPolicy = (config as ResolvedAppRouterProject & {
47
+ importPolicy?: AppRouterImportPolicy | undefined;
48
+ }).importPolicy;
44
49
 
45
50
  return {
51
+ ...(importPolicy === undefined ? {} : { importPolicy }),
46
52
  project: config,
47
53
  ...(typeof serverPort === "number" ? { serverPort } : {}),
48
54
  viteConfig: {
package/src/vite.ts CHANGED
@@ -71,8 +71,12 @@ const virtualReactiveCoreId = "\0mreact-router-reactive-core";
71
71
  const virtualReactiveDevtoolsId = "\0mreact-router-reactive-devtools";
72
72
  const mreactRouterConfigKey = "__mreactRouterConfig";
73
73
 
74
+ type MreactRouterPluginConfig = ResolvedAppRouterProject & {
75
+ importPolicy?: AppRouterImportPolicy | undefined;
76
+ };
77
+
74
78
  type MreactRouterPlugin = Plugin & {
75
- [mreactRouterConfigKey]: ResolvedAppRouterProject;
79
+ [mreactRouterConfigKey]: MreactRouterPluginConfig;
76
80
  };
77
81
 
78
82
  export function createAppRouterVitePlugin(options: AppRouterVitePluginOptions): Plugin {
@@ -151,7 +155,10 @@ export function createAppRouterVitePlugin(options: AppRouterVitePluginOptions):
151
155
  ]);
152
156
 
153
157
  const plugin: MreactRouterPlugin = {
154
- [mreactRouterConfigKey]: project,
158
+ [mreactRouterConfigKey]: {
159
+ ...project,
160
+ ...(options.importPolicy === undefined ? {} : { importPolicy: options.importPolicy }),
161
+ },
155
162
  enforce: "pre",
156
163
  name: "mreact-router",
157
164
  config() {
@@ -351,7 +358,13 @@ export function mreactRouterConfigFromPlugins(
351
358
  ): ResolvedAppRouterProject | undefined {
352
359
  for (const plugin of plugins.flat(Infinity)) {
353
360
  if (plugin !== null && typeof plugin === "object" && mreactRouterConfigKey in plugin) {
354
- return (plugin as MreactRouterPlugin)[mreactRouterConfigKey];
361
+ const config = (plugin as MreactRouterPlugin)[mreactRouterConfigKey];
362
+
363
+ if ("project" in config) {
364
+ return (config as unknown as { project: ResolvedAppRouterProject }).project;
365
+ }
366
+
367
+ return config as unknown as ResolvedAppRouterProject;
355
368
  }
356
369
  }
357
370