@immense/vue-pom-generator 1.0.56 → 1.0.57

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/dist/index.mjs CHANGED
@@ -3116,6 +3116,56 @@ function createRouterIntrospectionVueStubPlugin(options) {
3116
3116
  }
3117
3117
  };
3118
3118
  }
3119
+ function snapshotGlobalValue(name) {
3120
+ const g = globalThis;
3121
+ return {
3122
+ descriptor: Object.getOwnPropertyDescriptor(globalThis, name),
3123
+ value: g[name]
3124
+ };
3125
+ }
3126
+ function setTemporaryGlobal(name, value, snapshots) {
3127
+ if (!snapshots.has(name))
3128
+ snapshots.set(name, snapshotGlobalValue(name));
3129
+ const snapshot = snapshots.get(name);
3130
+ if (!snapshot)
3131
+ return false;
3132
+ if (!snapshot.descriptor || snapshot.descriptor.configurable) {
3133
+ Object.defineProperty(globalThis, name, {
3134
+ configurable: true,
3135
+ enumerable: snapshot.descriptor?.enumerable ?? true,
3136
+ writable: true,
3137
+ value
3138
+ });
3139
+ return true;
3140
+ }
3141
+ if ("writable" in snapshot.descriptor && snapshot.descriptor.writable) {
3142
+ Reflect.set(globalThis, name, value);
3143
+ return true;
3144
+ }
3145
+ if (snapshot.descriptor.set) {
3146
+ snapshot.descriptor.set.call(globalThis, value);
3147
+ return true;
3148
+ }
3149
+ return false;
3150
+ }
3151
+ function restoreTemporaryGlobals(snapshots) {
3152
+ for (const [name, snapshot] of Array.from(snapshots.entries()).reverse()) {
3153
+ const { descriptor, value } = snapshot;
3154
+ if (!descriptor) {
3155
+ Reflect.deleteProperty(globalThis, name);
3156
+ continue;
3157
+ }
3158
+ if (descriptor.configurable) {
3159
+ Object.defineProperty(globalThis, name, descriptor);
3160
+ continue;
3161
+ }
3162
+ if ("writable" in descriptor && descriptor.writable) {
3163
+ Reflect.set(globalThis, name, value);
3164
+ continue;
3165
+ }
3166
+ descriptor.set?.call(globalThis, value);
3167
+ }
3168
+ }
3119
3169
  const PARAM_TOKEN_PREFIX = "__VUE_TESTID_PARAM__";
3120
3170
  function getParamToken(name) {
3121
3171
  return `${PARAM_TOKEN_PREFIX}${name}__`;
@@ -3464,12 +3514,18 @@ function createMinimalLocation() {
3464
3514
  ancestorOrigins: { length: 0, contains: () => false, item: () => null, [Symbol.iterator]: [][Symbol.iterator] }
3465
3515
  };
3466
3516
  }
3467
- async function ensureDomShim() {
3517
+ function ensureDomShim() {
3468
3518
  const g = globalThis;
3469
3519
  if (typeof document !== "undefined" && typeof window !== "undefined")
3470
- return;
3520
+ return () => {
3521
+ };
3471
3522
  const minimalDoc = createMinimalDocument();
3472
3523
  const minimalLocation = createMinimalLocation();
3524
+ const snapshots = /* @__PURE__ */ new Map();
3525
+ const setGlobal = (name, value) => {
3526
+ if (!setTemporaryGlobal(name, value, snapshots))
3527
+ debugLog(`could not temporarily install global ${name}`);
3528
+ };
3473
3529
  const win = {
3474
3530
  document: minimalDoc,
3475
3531
  location: minimalLocation,
@@ -3514,17 +3570,17 @@ async function ensureDomShim() {
3514
3570
  queueMicrotask,
3515
3571
  performance: globalThis.performance
3516
3572
  };
3517
- g.window = win;
3518
- g.document = minimalDoc;
3519
- g.location = minimalLocation;
3573
+ setGlobal("window", win);
3574
+ setGlobal("document", minimalDoc);
3575
+ setGlobal("location", minimalLocation);
3520
3576
  if (!g.self)
3521
- g.self = win;
3577
+ setGlobal("self", win);
3522
3578
  if (!g.navigator)
3523
- g.navigator = win.navigator;
3579
+ setGlobal("navigator", win.navigator);
3524
3580
  if (!g.history)
3525
- g.history = win.history;
3581
+ setGlobal("history", win.history);
3526
3582
  if (!g.MutationObserver) {
3527
- g.MutationObserver = class {
3583
+ setGlobal("MutationObserver", class {
3528
3584
  disconnect() {
3529
3585
  }
3530
3586
  observe() {
@@ -3532,20 +3588,20 @@ async function ensureDomShim() {
3532
3588
  takeRecords() {
3533
3589
  return [];
3534
3590
  }
3535
- };
3591
+ });
3536
3592
  }
3537
3593
  if (!g.ResizeObserver) {
3538
- g.ResizeObserver = class {
3594
+ setGlobal("ResizeObserver", class {
3539
3595
  disconnect() {
3540
3596
  }
3541
3597
  observe() {
3542
3598
  }
3543
3599
  unobserve() {
3544
3600
  }
3545
- };
3601
+ });
3546
3602
  }
3547
3603
  if (!g.IntersectionObserver) {
3548
- g.IntersectionObserver = class {
3604
+ setGlobal("IntersectionObserver", class {
3549
3605
  disconnect() {
3550
3606
  }
3551
3607
  observe() {
@@ -3555,10 +3611,10 @@ async function ensureDomShim() {
3555
3611
  takeRecords() {
3556
3612
  return [];
3557
3613
  }
3558
- };
3614
+ });
3559
3615
  }
3560
3616
  if (!g.requestIdleCallback) {
3561
- g.requestIdleCallback = (cb) => setTimeout(() => cb({ didTimeout: false, timeRemaining: () => 0 }), 1);
3617
+ setGlobal("requestIdleCallback", (cb) => setTimeout(() => cb({ didTimeout: false, timeRemaining: () => 0 }), 1));
3562
3618
  }
3563
3619
  if (!g.localStorage || !g.sessionStorage) {
3564
3620
  const storageFactory = () => {
@@ -3581,12 +3637,15 @@ async function ensureDomShim() {
3581
3637
  };
3582
3638
  };
3583
3639
  if (!g.localStorage)
3584
- g.localStorage = storageFactory();
3640
+ setGlobal("localStorage", storageFactory());
3585
3641
  if (!g.sessionStorage)
3586
- g.sessionStorage = storageFactory();
3642
+ setGlobal("sessionStorage", storageFactory());
3587
3643
  }
3588
3644
  if (!g.requestAnimationFrame)
3589
- g.requestAnimationFrame = (cb) => setTimeout(() => cb(Date.now()), 16);
3645
+ setGlobal("requestAnimationFrame", (cb) => setTimeout(() => cb(Date.now()), 16));
3646
+ return () => {
3647
+ restoreTemporaryGlobals(snapshots);
3648
+ };
3590
3649
  }
3591
3650
  function unwrapNuxtPageSegment(segment, prefix, suffix) {
3592
3651
  if (!segment.startsWith(prefix) || !segment.endsWith(suffix))
@@ -3704,85 +3763,89 @@ async function parseRouterFileFromCwd(routerEntryPath, options = {}) {
3704
3763
  }
3705
3764
  const cwd = path.dirname(routerEntry);
3706
3765
  const moduleShims = normalizeRouterIntrospectionModuleShims(options.moduleShims);
3707
- await ensureDomShim();
3708
- debugLog(`parseRouterFileFromCwd cwd=${cwd}`);
3709
- const vite = await import("vite");
3710
- const server = await vite.createServer({
3711
- root: cwd,
3712
- configFile: false,
3713
- logLevel: "error",
3714
- // This server is created only to SSR-load the router module. Disable HMR/WebSocket
3715
- // to avoid port conflicts in dev/test environments.
3716
- server: { middlewareMode: true, hmr: false, ws: false },
3717
- appType: "custom",
3718
- // IMPORTANT:
3719
- // This internal, short-lived Vite server exists only to `ssrLoadModule()` the router entry.
3720
- // We close it immediately after reading routes.
3721
- //
3722
- // Vite's dependency optimizer (vite:dep-scan / optimizeDeps) runs asynchronously and can
3723
- // still have pending resolve requests when we call `server.close()`, which surfaces as:
3724
- // "The server is being restarted or closed. Request is outdated [plugin vite:dep-scan]"
3725
- //
3726
- // Disable optimizeDeps entirely for this internal server to avoid that race.
3727
- optimizeDeps: {
3728
- disabled: true
3729
- },
3730
- resolve: {
3731
- alias: {
3732
- "@": cwd
3733
- }
3734
- },
3735
- // Important: Do NOT include @vitejs/plugin-vue here.
3736
- // We stub all `.vue` imports ourselves, and including the Vue plugin would attempt to parse
3737
- // those stubbed modules as real SFCs (and fail).
3738
- plugins: [createRouterIntrospectionVueStubPlugin({ routerEntryAbs: routerEntry, moduleShims })]
3739
- });
3766
+ const restoreDomShim = ensureDomShim();
3740
3767
  try {
3741
- const moduleId = pathToFileURL(routerEntry).href;
3742
- debugLog(`ssrLoadModule(${moduleId}) start`);
3743
- const mod = await server.ssrLoadModule(moduleId);
3744
- debugLog(`ssrLoadModule(${moduleId}) done; hasDefault=${typeof mod?.default === "function"}`);
3745
- const makeRouter = mod?.default;
3746
- if (typeof makeRouter !== "function") {
3747
- throw new TypeError(`[vue-pom-generator] ${routerEntry} must export a default router factory function (export default makeRouter).`);
3748
- }
3749
- let router;
3768
+ debugLog(`parseRouterFileFromCwd cwd=${cwd}`);
3769
+ const vite = await import("vite");
3770
+ const server = await vite.createServer({
3771
+ root: cwd,
3772
+ configFile: false,
3773
+ logLevel: "error",
3774
+ // This server is created only to SSR-load the router module. Disable HMR/WebSocket
3775
+ // to avoid port conflicts in dev/test environments.
3776
+ server: { middlewareMode: true, hmr: false, ws: false },
3777
+ appType: "custom",
3778
+ // IMPORTANT:
3779
+ // This internal, short-lived Vite server exists only to `ssrLoadModule()` the router entry.
3780
+ // We close it immediately after reading routes.
3781
+ //
3782
+ // Vite's dependency optimizer (vite:dep-scan / optimizeDeps) runs asynchronously and can
3783
+ // still have pending resolve requests when we call `server.close()`, which surfaces as:
3784
+ // "The server is being restarted or closed. Request is outdated [plugin vite:dep-scan]"
3785
+ //
3786
+ // Disable optimizeDeps entirely for this internal server to avoid that race.
3787
+ optimizeDeps: {
3788
+ disabled: true
3789
+ },
3790
+ resolve: {
3791
+ alias: {
3792
+ "@": cwd
3793
+ }
3794
+ },
3795
+ // Important: Do NOT include @vitejs/plugin-vue here.
3796
+ // We stub all `.vue` imports ourselves, and including the Vue plugin would attempt to parse
3797
+ // those stubbed modules as real SFCs (and fail).
3798
+ plugins: [createRouterIntrospectionVueStubPlugin({ routerEntryAbs: routerEntry, moduleShims })]
3799
+ });
3750
3800
  try {
3751
- router = makeRouter();
3752
- } catch (err) {
3753
- throw new Error(`[vue-pom-generator] makeRouter() invocation failed: ${String(err)}`);
3754
- }
3755
- const routeNameMap = /* @__PURE__ */ new Map();
3756
- const routePathMap = /* @__PURE__ */ new Map();
3757
- const routeMetaEntries = [];
3758
- for (const r of router.getRoutes()) {
3759
- const componentInfo = await getComponentInfoFromRouteRecord(r, { rootDir: cwd });
3760
- const componentName = resolveIntrospectedComponentName(componentInfo, options.componentNaming);
3761
- if (!componentName)
3762
- continue;
3763
- if (typeof r.path === "string" && r.path.length) {
3764
- routePathMap.set(r.path, componentName);
3801
+ const moduleId = pathToFileURL(routerEntry).href;
3802
+ debugLog(`ssrLoadModule(${moduleId}) start`);
3803
+ const mod = await server.ssrLoadModule(moduleId);
3804
+ debugLog(`ssrLoadModule(${moduleId}) done; hasDefault=${typeof mod?.default === "function"}`);
3805
+ const makeRouter = mod?.default;
3806
+ if (typeof makeRouter !== "function") {
3807
+ throw new TypeError(`[vue-pom-generator] ${routerEntry} must export a default router factory function (export default makeRouter).`);
3765
3808
  }
3766
- if (typeof r.name === "string" && r.name.length) {
3767
- const key = toPascalCase(r.name);
3768
- routeNameMap.set(key, componentName);
3809
+ let router;
3810
+ try {
3811
+ router = makeRouter();
3812
+ } catch (err) {
3813
+ throw new Error(`[vue-pom-generator] makeRouter() invocation failed: ${String(err)}`);
3769
3814
  }
3770
- const { paramKeys, queryKeys } = getRoutePropsKeys(r);
3771
- const paramsMeta = getRouteParamMeta(router, r, paramKeys);
3772
- const pathTemplate = buildRouteTemplate(router, r, paramsMeta.map((p) => p.name));
3773
- if (typeof pathTemplate === "string" && pathTemplate.length) {
3774
- routeMetaEntries.push({
3775
- componentName,
3776
- pathTemplate,
3777
- params: paramsMeta,
3778
- query: queryKeys
3779
- });
3815
+ const routeNameMap = /* @__PURE__ */ new Map();
3816
+ const routePathMap = /* @__PURE__ */ new Map();
3817
+ const routeMetaEntries = [];
3818
+ for (const r of router.getRoutes()) {
3819
+ const componentInfo = await getComponentInfoFromRouteRecord(r, { rootDir: cwd });
3820
+ const componentName = resolveIntrospectedComponentName(componentInfo, options.componentNaming);
3821
+ if (!componentName)
3822
+ continue;
3823
+ if (typeof r.path === "string" && r.path.length) {
3824
+ routePathMap.set(r.path, componentName);
3825
+ }
3826
+ if (typeof r.name === "string" && r.name.length) {
3827
+ const key = toPascalCase(r.name);
3828
+ routeNameMap.set(key, componentName);
3829
+ }
3830
+ const { paramKeys, queryKeys } = getRoutePropsKeys(r);
3831
+ const paramsMeta = getRouteParamMeta(router, r, paramKeys);
3832
+ const pathTemplate = buildRouteTemplate(router, r, paramsMeta.map((p) => p.name));
3833
+ if (typeof pathTemplate === "string" && pathTemplate.length) {
3834
+ routeMetaEntries.push({
3835
+ componentName,
3836
+ pathTemplate,
3837
+ params: paramsMeta,
3838
+ query: queryKeys
3839
+ });
3840
+ }
3780
3841
  }
3842
+ return { routeNameMap, routePathMap, routeMetaEntries };
3843
+ } finally {
3844
+ debugLog("closing internal vite server");
3845
+ await server.close();
3781
3846
  }
3782
- return { routeNameMap, routePathMap, routeMetaEntries };
3783
3847
  } finally {
3784
- debugLog("closing internal vite server");
3785
- await server.close();
3848
+ restoreDomShim();
3786
3849
  }
3787
3850
  });
3788
3851
  }