payload-react-router 0.0.0-stage → 0.1.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.
Files changed (93) hide show
  1. package/LICENSE +24 -0
  2. package/README.md +263 -2
  3. package/dist/client/PayloadRootProviders.d.ts +22 -0
  4. package/dist/client/PayloadRootProviders.js +29 -0
  5. package/dist/client/PayloadRootProviders.js.map +1 -0
  6. package/dist/client/RouterAdapter.d.ts +14 -0
  7. package/dist/client/RouterAdapter.js +64 -0
  8. package/dist/client/RouterAdapter.js.map +1 -0
  9. package/dist/client/navigation.d.ts +11 -0
  10. package/dist/client/navigation.js +31 -0
  11. package/dist/client/navigation.js.map +1 -0
  12. package/dist/exports/client.d.ts +2 -0
  13. package/dist/exports/client.js +3 -0
  14. package/dist/exports/client.js.map +1 -0
  15. package/dist/exports/server.d.ts +10 -0
  16. package/dist/exports/server.js +11 -0
  17. package/dist/exports/server.js.map +1 -0
  18. package/dist/exports/vite.d.ts +1 -0
  19. package/dist/exports/vite.js +2 -0
  20. package/dist/exports/vite.js.map +1 -0
  21. package/dist/server/adminPage.d.ts +50 -0
  22. package/dist/server/adminPage.js +136 -0
  23. package/dist/server/adminPage.js.map +1 -0
  24. package/dist/server/auth.d.ts +16 -0
  25. package/dist/server/auth.js +17 -0
  26. package/dist/server/auth.js.map +1 -0
  27. package/dist/server/cookies.d.ts +3 -0
  28. package/dist/server/cookies.js +27 -0
  29. package/dist/server/cookies.js.map +1 -0
  30. package/dist/server/graphql.d.ts +9 -0
  31. package/dist/server/graphql.js +114 -0
  32. package/dist/server/graphql.js.map +1 -0
  33. package/dist/server/initAdminContext.d.ts +17 -0
  34. package/dist/server/initAdminContext.js +39 -0
  35. package/dist/server/initAdminContext.js.map +1 -0
  36. package/dist/server/layout.d.ts +28 -0
  37. package/dist/server/layout.js +37 -0
  38. package/dist/server/layout.js.map +1 -0
  39. package/dist/server/middleware.d.ts +16 -0
  40. package/dist/server/middleware.js +35 -0
  41. package/dist/server/middleware.js.map +1 -0
  42. package/dist/server/requestStore.d.ts +29 -0
  43. package/dist/server/requestStore.js +54 -0
  44. package/dist/server/requestStore.js.map +1 -0
  45. package/dist/server/rest.d.ts +34 -0
  46. package/dist/server/rest.js +54 -0
  47. package/dist/server/rest.js.map +1 -0
  48. package/dist/server/searchParams.d.ts +6 -0
  49. package/dist/server/searchParams.js +13 -0
  50. package/dist/server/searchParams.js.map +1 -0
  51. package/dist/server/serverAdapter.d.ts +28 -0
  52. package/dist/server/serverAdapter.js +79 -0
  53. package/dist/server/serverAdapter.js.map +1 -0
  54. package/dist/server/serverFunctions.d.ts +21 -0
  55. package/dist/server/serverFunctions.js +25 -0
  56. package/dist/server/serverFunctions.js.map +1 -0
  57. package/dist/vite/config/external.d.ts +43 -0
  58. package/dist/vite/config/external.js +102 -0
  59. package/dist/vite/config/external.js.map +1 -0
  60. package/dist/vite/config/optimizeDeps.d.ts +24 -0
  61. package/dist/vite/config/optimizeDeps.js +162 -0
  62. package/dist/vite/config/optimizeDeps.js.map +1 -0
  63. package/dist/vite/config/scss.d.ts +13 -0
  64. package/dist/vite/config/scss.js +29 -0
  65. package/dist/vite/config/scss.js.map +1 -0
  66. package/dist/vite/devConfigReload.d.ts +13 -0
  67. package/dist/vite/devConfigReload.js +74 -0
  68. package/dist/vite/devConfigReload.js.map +1 -0
  69. package/dist/vite/devConfigReload.server.d.ts +1 -0
  70. package/dist/vite/devConfigReload.server.js +62 -0
  71. package/dist/vite/devConfigReload.server.js.map +1 -0
  72. package/dist/vite/devConfigReloadEvent.d.ts +8 -0
  73. package/dist/vite/devConfigReloadEvent.js +9 -0
  74. package/dist/vite/devConfigReloadEvent.js.map +1 -0
  75. package/dist/vite/index.d.ts +31 -0
  76. package/dist/vite/index.js +148 -0
  77. package/dist/vite/index.js.map +1 -0
  78. package/dist/vite/workarounds/clientModuleResolution.d.ts +13 -0
  79. package/dist/vite/workarounds/clientModuleResolution.js +80 -0
  80. package/dist/vite/workarounds/clientModuleResolution.js.map +1 -0
  81. package/dist/vite/workarounds/reactDomServerInRsc.d.ts +26 -0
  82. package/dist/vite/workarounds/reactDomServerInRsc.js +84 -0
  83. package/dist/vite/workarounds/reactDomServerInRsc.js.map +1 -0
  84. package/dist/vite/workarounds/stripDistStyleImports.d.ts +20 -0
  85. package/dist/vite/workarounds/stripDistStyleImports.js +140 -0
  86. package/dist/vite/workarounds/stripDistStyleImports.js.map +1 -0
  87. package/dist/vite/workarounds/stubPrettierInClient.d.ts +12 -0
  88. package/dist/vite/workarounds/stubPrettierInClient.js +32 -0
  89. package/dist/vite/workarounds/stubPrettierInClient.js.map +1 -0
  90. package/dist/vite/workarounds/wrapCjsForClient.d.ts +14 -0
  91. package/dist/vite/workarounds/wrapCjsForClient.js +104 -0
  92. package/dist/vite/workarounds/wrapCjsForClient.js.map +1 -0
  93. package/package.json +79 -4
@@ -0,0 +1,20 @@
1
+ import type { PluginOption } from 'vite';
2
+ /**
3
+ * Stops Vite (and the underlying Node ESM loader) from trying to load
4
+ * SCSS/CSS/LESS during SSR/RSC when the importer lives inside a built
5
+ * `dist/` directory or when the specifier is a bare package name.
6
+ *
7
+ * We do this two ways, because either layer can fail:
8
+ *
9
+ * 1. `resolveId` redirects style specifiers to a virtual empty module —
10
+ * handles cases where Vite asks us to resolve them.
11
+ * 2. `transform` strips top-level `import './x.css'` statements out of any
12
+ * JS/TS file living under `node_modules/.../dist/` for non-client envs.
13
+ * This is the bulletproof path for prod-packed `@payloadcms/ui` (and
14
+ * similar) dependencies that get pre-bundled by Vite's SSR/RSC dep
15
+ * optimizer (esbuild). Esbuild preserves `.css` import statements as-is,
16
+ * and Node's native ESM loader then crashes with
17
+ * `Unknown file extension ".css"`. Removing them at the source avoids
18
+ * that entirely.
19
+ */
20
+ export declare function ssrStripDistStyleImports(): PluginOption;
@@ -0,0 +1,140 @@
1
+ const STYLE_EXTENSION_RE = /\.(?:s?css|less)$/i;
2
+ /**
3
+ * Static `import './foo.css'` (or .scss/.less) — top-level only.
4
+ * Captures the entire statement so we can replace it with an empty line.
5
+ *
6
+ * Matches:
7
+ * import './foo.css';
8
+ * import "../bar.scss"
9
+ * import './baz.less' ;
10
+ */
11
+ const STATIC_STYLE_IMPORT_RE = /^[ \t]*import\s+['"][^'"]+\.(?:s?css|less)['"]\s*(?:;[ \t]*)?$/gm;
12
+ /**
13
+ * Monorepo Payload package source, e.g. `…/packages/ui/src/…`. In the
14
+ * core-dev / test setup Payload packages resolve to their workspace `src`
15
+ * (not a published `dist`), so the `dist/` rule below doesn't cover them and
16
+ * their `.css` side-effect imports survive into the SSR/RSC graph.
17
+ */
18
+ const PAYLOAD_PKG_SRC_RE = /\/packages\/[^/]+\/src\//;
19
+ /**
20
+ * The version-diff component trees — matched in either published `dist/` or
21
+ * workspace `src/` form. The `.css` side-effect imports of these must be
22
+ * stripped in the RSC env (the rest of `@payloadcms/ui` must NOT be — see the
23
+ * note in `resolveId`/`transform` below), for two reasons rooted in the same
24
+ * `@vitejs/plugin-rsc` behaviour: it wraps every exported CSS-importing
25
+ * component with an async CSS-collector child
26
+ * (`__vite_rsc_wrap_css__` → `await import('virtual:vite-rsc/css?…')`).
27
+ *
28
+ * 1. Correctness: the diff converters render `CheckIcon` (`icons/*`) and `File`
29
+ * (`graphics/*`) through the SYNCHRONOUS `renderToStaticMarkup` (see
30
+ * `reactDomServerInRsc`). The async wrapper suspends, crashing that render
31
+ * with "A component suspended while responding to synchronous input" and
32
+ * taking down the entire field-diffs tree.
33
+ * 2. Performance: the diff view (`views/Version/*` — `RenderFieldsToDiff` and
34
+ * its per-field components, `DiffCollapser`, `SelectComparison`, the
35
+ * `Default` template — plus the `HTMLDiff`/`FieldDiffContainer`/
36
+ * `FieldDiffLabel` elements and lexical's `field/Diff/*`) renders dozens of
37
+ * these wrapped components in the Flight stream. Each async CSS import
38
+ * serializes a round-trip, ballooning the render from ~3s to ~25s and
39
+ * blowing past the e2e waits. Stripping them collapses it back to ~3s.
40
+ *
41
+ * Safe because every one of these components' styles also ship in the global
42
+ * `@payloadcms/ui/css/app.css` the admin imports, so dropping the per-module
43
+ * RSC collection here changes nothing visually (verified: diff + Nav stay
44
+ * styled). The admin `Nav` etc. are deliberately excluded — they rely on the
45
+ * RSC collection and a broad strip leaves them unstyled.
46
+ */
47
+ const DIFF_VIEW_COMPONENT_RE = /@payloadcms\/ui\/(?:dist|src)\/(?:icons|graphics|views\/Version|elements\/(?:HTMLDiff|FieldDiffContainer|FieldDiffLabel))\/|@payloadcms\/richtext-lexical\/(?:dist|src)\/field\/Diff\//;
48
+ /**
49
+ * Stops Vite (and the underlying Node ESM loader) from trying to load
50
+ * SCSS/CSS/LESS during SSR/RSC when the importer lives inside a built
51
+ * `dist/` directory or when the specifier is a bare package name.
52
+ *
53
+ * We do this two ways, because either layer can fail:
54
+ *
55
+ * 1. `resolveId` redirects style specifiers to a virtual empty module —
56
+ * handles cases where Vite asks us to resolve them.
57
+ * 2. `transform` strips top-level `import './x.css'` statements out of any
58
+ * JS/TS file living under `node_modules/.../dist/` for non-client envs.
59
+ * This is the bulletproof path for prod-packed `@payloadcms/ui` (and
60
+ * similar) dependencies that get pre-bundled by Vite's SSR/RSC dep
61
+ * optimizer (esbuild). Esbuild preserves `.css` import statements as-is,
62
+ * and Node's native ESM loader then crashes with
63
+ * `Unknown file extension ".css"`. Removing them at the source avoids
64
+ * that entirely.
65
+ */
66
+ export function ssrStripDistStyleImports() {
67
+ return {
68
+ name: 'payload:ssr-strip-dist-style-imports',
69
+ enforce: 'pre',
70
+ load(id) {
71
+ if (id === '\0ssr-empty-style') {
72
+ return '';
73
+ }
74
+ },
75
+ resolveId(id, importer, options) {
76
+ const envName = this.environment?.name;
77
+ const isServerEnv = options?.ssr || (envName && envName !== 'client');
78
+ if (!isServerEnv) {
79
+ return;
80
+ }
81
+ // Do NOT strip in the RSC environment. Server components (e.g. the admin
82
+ // `Nav`, a non-'use client' component that `import './index.css'`) render
83
+ // in the RSC graph, and `@vitejs/plugin-rsc` must SEE their `.css` imports
84
+ // there to collect them as client stylesheets. Stripping them here means
85
+ // their CSS is never emitted and the admin renders unstyled (broken nav
86
+ // scroll/layout, etc.). The Node-side `.css` no-op is handled by
87
+ // the test dev server's asset loader and by Vite's CSS extraction (build), so the
88
+ // crash this plugin guards against does not require touching the RSC env.
89
+ if (envName === 'rsc') {
90
+ return;
91
+ }
92
+ if (!STYLE_EXTENSION_RE.test(id)) {
93
+ return;
94
+ }
95
+ if (importer && (/\/dist\//.test(importer) || PAYLOAD_PKG_SRC_RE.test(importer))) {
96
+ return '\0ssr-empty-style';
97
+ }
98
+ if (/^@?[a-z]/.test(id) && !id.startsWith('.') && !id.startsWith('/')) {
99
+ return '\0ssr-empty-style';
100
+ }
101
+ },
102
+ transform(code, id) {
103
+ const envName = this.environment?.name;
104
+ const isServerEnv = envName && envName !== 'client';
105
+ if (!isServerEnv) {
106
+ return;
107
+ }
108
+ // In the RSC env, only strip from the version-diff component trees (see
109
+ // `DIFF_VIEW_COMPONENT_RE`). Every other server component (the admin
110
+ // `Nav`, etc.) must keep its `.css` import so plugin-rsc can collect it —
111
+ // see the matching note in `resolveId` above.
112
+ if (envName === 'rsc' && !DIFF_VIEW_COMPONENT_RE.test(id)) {
113
+ return;
114
+ }
115
+ // Only touch Payload dependency files: published `node_modules/.../dist/`
116
+ // builds, or workspace `…/packages/<pkg>/src/…` sources in the core-dev /
117
+ // test setup. Don't strip from the consumer's own app source — devs may
118
+ // legitimately want SSR-rendered <link>s from their own CSS imports.
119
+ const isPayloadDistFile = /\/node_modules\//.test(id) && /\/dist\//.test(id);
120
+ const isPayloadSrcFile = PAYLOAD_PKG_SRC_RE.test(id);
121
+ if (!isPayloadDistFile && !isPayloadSrcFile) {
122
+ return;
123
+ }
124
+ // Allow a trailing query (`?v=…`, `?t=…`): Vite appends version/timestamp
125
+ // queries to module ids, especially in the RSC graph, so a strict `$`
126
+ // anchor would skip e.g. `icons/Check/index.js?v=83de8543`.
127
+ if (!/\.[mc]?[jt]sx?(?:$|\?)/.test(id)) {
128
+ return;
129
+ }
130
+ if (!STATIC_STYLE_IMPORT_RE.test(code)) {
131
+ STATIC_STYLE_IMPORT_RE.lastIndex = 0;
132
+ return;
133
+ }
134
+ STATIC_STYLE_IMPORT_RE.lastIndex = 0;
135
+ const stripped = code.replace(STATIC_STYLE_IMPORT_RE, '');
136
+ return { code: stripped, map: null };
137
+ },
138
+ };
139
+ }
140
+ //# sourceMappingURL=stripDistStyleImports.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stripDistStyleImports.js","sourceRoot":"","sources":["../../../src/vite/workarounds/stripDistStyleImports.ts"],"names":[],"mappings":"AAEA,MAAM,kBAAkB,GAAG,oBAAoB,CAAA;AAE/C;;;;;;;;GAQG;AACH,MAAM,sBAAsB,GAAG,kEAAkE,CAAA;AAEjG;;;;;GAKG;AACH,MAAM,kBAAkB,GAAG,0BAA0B,CAAA;AAErD;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,sBAAsB,GAC1B,wLAAwL,CAAA;AAE1L;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,wBAAwB;IACtC,OAAO;QACL,IAAI,EAAE,sCAAsC;QAC5C,OAAO,EAAE,KAAK;QACd,IAAI,CAAC,EAAE;YACL,IAAI,EAAE,KAAK,mBAAmB,EAAE,CAAC;gBAC/B,OAAO,EAAE,CAAA;YACX,CAAC;QACH,CAAC;QACD,SAAS,CAAC,EAAE,EAAE,QAAQ,EAAE,OAAO;YAC7B,MAAM,OAAO,GAAI,IAAY,CAAC,WAAW,EAAE,IAA0B,CAAA;YACrE,MAAM,WAAW,GAAG,OAAO,EAAE,GAAG,IAAI,CAAC,OAAO,IAAI,OAAO,KAAK,QAAQ,CAAC,CAAA;YACrE,IAAI,CAAC,WAAW,EAAE,CAAC;gBACjB,OAAM;YACR,CAAC;YACD,yEAAyE;YACzE,0EAA0E;YAC1E,2EAA2E;YAC3E,yEAAyE;YACzE,wEAAwE;YACxE,iEAA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iB,IAAI,CAAC,gBAAgB,EAAE,CAAC;gBAC5C,OAAM;YACR,CAAC;YACD,0EAA0E;YAC1E,sEAAsE;YACtE,4DAA4D;YAC5D,IAAI,CAAC,wBAAwB,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC;gBACvC,OAAM;YACR,CAAC;YACD,IAAI,CAAC,sBAAsB,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;gBACvC,sBAAsB,CAAC,SAAS,GAAG,CAAC,CAAA;gBACpC,OAAM;YACR,CAAC;YACD,sBAAsB,CAAC,SAAS,GAAG,CAAC,CAAA;YACpC,MAAM,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,sBAAsB,EAAE,EAAE,CAAC,CAAA;YACzD,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,EAAE,IAAI,EAAE,CAAA;QACtC,CAAC;KACF,CAAA;AACH,CAAC"}
@@ -0,0 +1,12 @@
1
+ import type { PluginOption } from 'vite';
2
+ /**
3
+ * Stubs `prettier` in the CLIENT bundle only.
4
+ *
5
+ * `payload`'s barrel transitively pulls `json-schema-to-typescript` → `prettier`
6
+ * (CommonJS) into the client graph. That path is type-gen only and never runs in
7
+ * the browser, but the bundler still drags in `prettier`'s `index.cjs`, which
8
+ * fails to parse as ESM and breaks the client build. SSR/RSC keep real prettier.
9
+ *
10
+ * Delete once `payload`'s barrel no longer reaches type-gen from the client graph.
11
+ */
12
+ export declare function stubPrettierInClient(): PluginOption;
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Stubs `prettier` in the CLIENT bundle only.
3
+ *
4
+ * `payload`'s barrel transitively pulls `json-schema-to-typescript` → `prettier`
5
+ * (CommonJS) into the client graph. That path is type-gen only and never runs in
6
+ * the browser, but the bundler still drags in `prettier`'s `index.cjs`, which
7
+ * fails to parse as ESM and breaks the client build. SSR/RSC keep real prettier.
8
+ *
9
+ * Delete once `payload`'s barrel no longer reaches type-gen from the client graph.
10
+ */
11
+ export function stubPrettierInClient() {
12
+ const RESOLVED_ID = '\0payload:prettier-client-stub';
13
+ return {
14
+ name: 'payload:stub-prettier-in-client',
15
+ enforce: 'pre',
16
+ load(id) {
17
+ if (id === RESOLVED_ID) {
18
+ return 'export const format = (source) => source;\nexport default { format };';
19
+ }
20
+ },
21
+ resolveId(id, importer, options) {
22
+ if (options?.ssr) {
23
+ return;
24
+ }
25
+ // Only stub the type-gen import; leave a host's own client `prettier` alone.
26
+ if (id === 'prettier' && importer?.includes('json-schema-to-typescript')) {
27
+ return RESOLVED_ID;
28
+ }
29
+ },
30
+ };
31
+ }
32
+ //# sourceMappingURL=stubPrettierInClient.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stubPrettierInClient.js","sourceRoot":"","sources":["../../../src/vite/workarounds/stubPrettierInClient.ts"],"names":[],"mappings":"AAEA;;;;;;;;;GASG;AACH,MAAM,UAAU,oBAAoB;IAClC,MAAM,WAAW,GAAG,gCAAgC,CAAA;IAEpD,OAAO;QACL,IAAI,EAAE,iCAAiC;QACvC,OAAO,EAAE,KAAK;QACd,IAAI,CAAC,EAAE;YACL,IAAI,EAAE,KAAK,WAAW,EAAE,CAAC;gBACvB,OAAO,uEAAuE,CAAA;YAChF,CAAC;QACH,CAAC;QACD,SAAS,CAAC,EAAE,EAAE,QAAQ,EAAE,OAAO;YAC7B,IAAI,OAAO,EAAE,GAAG,EAAE,CAAC;gBACjB,OAAM;YACR,CAAC;YACD,6EAA6E;YAC7E,IAAI,EAAE,KAAK,UAAU,IAAI,QAAQ,EAAE,QAAQ,CAAC,2BAA2B,CAAC,EAAE,CAAC;gBACzE,OAAO,WAAW,CAAA;YACpB,CAAC;QACH,CAAC;KACF,CAAA;AACH,CAAC"}
@@ -0,0 +1,14 @@
1
+ import type { PluginOption } from 'vite';
2
+ /**
3
+ * Wraps CJS `node_modules` files in ESM-compatible code when served to the
4
+ * client.
5
+ *
6
+ * Packages in `optimizeDeps.exclude` (like `payload`, `@payloadcms/ui`) import
7
+ * CJS dependencies that Vite serves via raw `/@fs/` URLs, bypassing
8
+ * pre-bundling. The browser fails to parse them because they use
9
+ * `module.exports` / `exports.X` syntax.
10
+ *
11
+ * This plugin detects CJS patterns in the transform phase and wraps them with
12
+ * a CommonJS-like runtime shim so the browser can execute them as ESM.
13
+ */
14
+ export declare function wrapCjsForClient(): PluginOption;
@@ -0,0 +1,104 @@
1
+ /**
2
+ * Wraps CJS `node_modules` files in ESM-compatible code when served to the
3
+ * client.
4
+ *
5
+ * Packages in `optimizeDeps.exclude` (like `payload`, `@payloadcms/ui`) import
6
+ * CJS dependencies that Vite serves via raw `/@fs/` URLs, bypassing
7
+ * pre-bundling. The browser fails to parse them because they use
8
+ * `module.exports` / `exports.X` syntax.
9
+ *
10
+ * This plugin detects CJS patterns in the transform phase and wraps them with
11
+ * a CommonJS-like runtime shim so the browser can execute them as ESM.
12
+ */
13
+ export function wrapCjsForClient() {
14
+ return {
15
+ name: 'payload:wrap-cjs-client',
16
+ apply: 'serve',
17
+ enforce: 'post',
18
+ transform(code, id, options) {
19
+ if (options?.ssr) {
20
+ return;
21
+ }
22
+ if (!id.includes('/node_modules/') || id.includes('/node_modules/.vite/')) {
23
+ return;
24
+ }
25
+ const cleanId = id.replace(/\?.*$/, '');
26
+ if (!cleanId.endsWith('.js') && !cleanId.endsWith('.cjs')) {
27
+ return;
28
+ }
29
+ if (code.includes('import ') || code.includes('export ')) {
30
+ return;
31
+ }
32
+ if (!code.includes('module.exports') &&
33
+ !code.includes('exports.') &&
34
+ !code.includes('Object.defineProperty(exports')) {
35
+ return;
36
+ }
37
+ const namedExports = extractCjsExports(code);
38
+ const names = Object.keys(namedExports);
39
+ const declaredIdentifiers = extractDeclaredIdentifiers(code);
40
+ const safeNames = names.filter((name) => !declaredIdentifiers.has(name));
41
+ // If the module already declares its own top-level `require` (rare but
42
+ // possible for hand-written CJS), skip injecting our stub so we don't
43
+ // produce a `SyntaxError: Identifier 'require' has already been declared`.
44
+ const injectRequireStub = !declaredIdentifiers.has('require');
45
+ const wrapped = [
46
+ `var module = { exports: {} };`,
47
+ `var exports = module.exports;`,
48
+ // Expose a `require` stub so UMD wrappers that gate their CJS branch on
49
+ // `typeof require === 'function'` (e.g. pluralize) take that branch and
50
+ // assign to our `module.exports` instead of falling through to the
51
+ // browser-globals branch (`root.foo = ...`), which throws in strict ESM
52
+ // because the IIFE receives `this === undefined`.
53
+ // The stub throws on actual invocation so any module that *really* needs
54
+ // require fails loudly instead of silently misbehaving.
55
+ injectRequireStub
56
+ ? `var require = function require() { throw new Error("require() is not available in the browser bundle"); };`
57
+ : ``,
58
+ ``,
59
+ code,
60
+ ``,
61
+ `var __cjs_result__ = module.exports;`,
62
+ `export default __cjs_result__;`,
63
+ ...safeNames.map((name) => `export var ${name} = typeof __cjs_result__ === 'object' && __cjs_result__ !== null ? __cjs_result__["${name}"] : undefined;`),
64
+ ].join('\n');
65
+ return { code: wrapped, map: null };
66
+ },
67
+ };
68
+ }
69
+ function extractCjsExports(code) {
70
+ const found = {};
71
+ const patterns = [
72
+ /exports\.(\w+)\s*=/g,
73
+ /exports\[["'](\w+)["']\]\s*=/g,
74
+ /Object\.defineProperty\(exports,\s*["'](\w+)["']/g,
75
+ ];
76
+ for (const re of patterns) {
77
+ let m;
78
+ while ((m = re.exec(code)) !== null) {
79
+ const name = m[1];
80
+ if (name !== '__esModule' && name !== 'default') {
81
+ found[name] = true;
82
+ }
83
+ }
84
+ }
85
+ return found;
86
+ }
87
+ /**
88
+ * Detects top-level identifiers declared with class/function/var/let/const in
89
+ * CJS code. Used to avoid re-declaring them via `export var` in the CJS
90
+ * wrapper, which would cause `SyntaxError: Identifier has already been
91
+ * declared` in strict mode (ESM).
92
+ */
93
+ function extractDeclaredIdentifiers(code) {
94
+ const identifiers = new Set();
95
+ const patterns = [/\bclass\s+(\w+)/g, /\bfunction\s+(\w+)/g, /\b(?:var|let|const)\s+(\w+)/g];
96
+ for (const re of patterns) {
97
+ let m;
98
+ while ((m = re.exec(code)) !== null) {
99
+ identifiers.add(m[1]);
100
+ }
101
+ }
102
+ return identifiers;
103
+ }
104
+ //# sourceMappingURL=wrapCjsForClient.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wrapCjsForClient.js","sourceRoot":"","sources":["../../../src/vite/workarounds/wrapCjsForClient.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,gBAAgB;IAC9B,OAAO;QACL,IAAI,EAAE,yBAAyB;QAC/B,KAAK,EAAE,OAAO;QACd,OAAO,EAAE,MAAM;QACf,SAAS,CAAC,IAAI,EAAE,EAAE,EAAE,OAAO;YACzB,IAAI,OAAO,EAAE,GAAG,EAAE,CAAC;gBACjB,OAAM;YACR,CAAC;YAED,IAAI,CAAC,EAAE,CAAC,QAAQ,CAAC,gBAAgB,CAAC,IAAI,EAAE,CAAC,QAAQ,CAAC,sBAAsB,CAAC,EAAE,CAAC;gBAC1E,OAAM;YACR,CAAC;YAED,MAAM,OAAO,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAA;YACvC,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC;gBAC1D,OAAM;YACR,CAAC;YAED,IAAI,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,IAAI,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC;gBACzD,OAAM;YACR,CAAC;YAED,IACE,CAAC,IAAI,CAAC,QAAQ,CAAC,gBAAgB,CAAC;gBAChC,CAAC,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC;gBAC1B,CAAC,IAAI,CAAC,QAAQ,CAAC,+BAA+B,CAAC,EAC/C,CAAC;gBACD,OAAM;YACR,CAAC;YAED,MAAM,YAAY,GAAG,iBAAiB,CAAC,IAAI,CAAC,CAAA;YAC5C,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAA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AAG,CAAC,CAAC,CAAC,CAAE,CAAA;YAClB,IAAI,IAAI,KAAK,YAAY,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;gBAChD,KAAK,CAAC,IAAI,CAAC,GAAG,IAAI,CAAA;YACpB,CAAC;QACH,CAAC;IACH,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC;AAED;;;;;GAKG;AACH,SAAS,0BAA0B,CAAC,IAAY;IAC9C,MAAM,WAAW,GAAG,IAAI,GAAG,EAAU,CAAA;IACrC,MAAM,QAAQ,GAAG,CAAC,kBAAkB,EAAE,qBAAqB,EAAE,8BAA8B,CAAC,CAAA;IAC5F,KAAK,MAAM,EAAE,IAAI,QAAQ,EAAE,CAAC;QAC1B,IAAI,CAAC,CAAA;QACL,OAAO,CAAC,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,KAAK,IAAI,EAAE,CAAC;YACpC,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAE,CAAC,CAAA;QACxB,CAAC;IACH,CAAC;IACD,OAAO,WAAW,CAAA;AACpB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,81 @@
1
1
  {
2
2
  "name": "payload-react-router",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "React Router v8 (RSC framework mode) adapter for the Payload CMS admin panel and REST/GraphQL API",
5
+ "keywords": [
6
+ "payload",
7
+ "payloadcms",
8
+ "react-router",
9
+ "rsc",
10
+ "adapter",
11
+ "vite"
12
+ ],
13
+ "license": "MIT",
14
+ "author": "Sakuga Software (https://sakuga.dev)",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/sakuga-software/payload-react-router.git"
18
+ },
19
+ "bugs": "https://github.com/sakuga-software/payload-react-router/issues",
20
+ "homepage": "https://github.com/sakuga-software/payload-react-router#readme",
21
+ "type": "module",
22
+ "exports": {
23
+ "./client": {
24
+ "types": "./dist/exports/client.d.ts",
25
+ "default": "./dist/exports/client.js"
26
+ },
27
+ "./server": {
28
+ "types": "./dist/exports/server.d.ts",
29
+ "default": "./dist/exports/server.js"
30
+ },
31
+ "./vite": {
32
+ "types": "./dist/exports/vite.d.ts",
33
+ "default": "./dist/exports/vite.js"
34
+ },
35
+ "./package.json": "./package.json"
36
+ },
37
+ "files": [
38
+ "dist"
39
+ ],
40
+ "engines": {
41
+ "node": ">=24.15.0"
42
+ },
43
+ "packageManager": "pnpm@10.33.2",
44
+ "scripts": {
45
+ "build": "rm -rf dist && tsc -p tsconfig.build.json",
46
+ "typecheck": "tsc --noEmit",
47
+ "test": "node --test 'test/*.test.ts'",
48
+ "prepack": "pnpm build"
49
+ },
50
+ "dependencies": {
51
+ "esbuild": "^0.27.0 || ^0.28.0",
52
+ "graphql-http": "1.22.4"
53
+ },
54
+ "peerDependencies": {
55
+ "@payloadcms/graphql": "4.0.0-canary.39",
56
+ "@payloadcms/translations": "4.0.0-canary.39",
57
+ "@payloadcms/ui": "4.0.0-canary.39",
58
+ "@vitejs/plugin-rsc": "0.5.35",
59
+ "payload": "4.0.0-canary.39",
60
+ "react": "^19.2.7",
61
+ "react-dom": "^19.2.7",
62
+ "react-router": "8.4.0",
63
+ "vite": ">=8.0.0"
64
+ },
65
+ "devDependencies": {
66
+ "@payloadcms/graphql": "4.0.0-canary.39",
67
+ "@payloadcms/translations": "4.0.0-canary.39",
68
+ "@payloadcms/ui": "4.0.0-canary.39",
69
+ "@types/node": "^24.10.1",
70
+ "@types/react": "19.2.14",
71
+ "@types/react-dom": "19.2.3",
72
+ "@vitejs/plugin-rsc": "0.5.35",
73
+ "graphql": "^16.8.1",
74
+ "payload": "4.0.0-canary.39",
75
+ "react": "19.3.0",
76
+ "react-dom": "19.3.0",
77
+ "react-router": "8.4.0",
78
+ "typescript": "^5.9.3",
79
+ "vite": "^8.0.3"
80
+ }
81
+ }