@cedarjs/vite 5.0.2-rc.5 → 6.0.0-canary.2619

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 (78) hide show
  1. package/dist/apiDevMiddleware.d.ts.map +1 -1
  2. package/dist/apiDevMiddleware.js +16 -2
  3. package/dist/buildApp.d.ts.map +1 -1
  4. package/dist/buildApp.js +8 -3
  5. package/dist/bundled/react-server-dom-webpack.server.js +1224 -28521
  6. package/dist/cjs/apiDevMiddleware.js +16 -2
  7. package/dist/cjs/buildApp.js +8 -3
  8. package/dist/cjs/devFeServer.js +268 -15
  9. package/dist/cjs/index.js +21 -13
  10. package/dist/cjs/lib/registerFwGlobalsAndShims.js +9 -0
  11. package/dist/cjs/plugins/vite-plugin-cedar-data-uri-shim.js +67 -0
  12. package/dist/cjs/plugins/vite-plugin-cedar-gqlorm-inject.js +106 -0
  13. package/dist/cjs/plugins/vite-plugin-cedar-graphql-options-extract.js +113 -0
  14. package/dist/cjs/plugins/vite-plugin-cedar-otel-wrapping.js +225 -0
  15. package/dist/cjs/plugins/vite-plugin-cedar-universal-deploy.js +1 -1
  16. package/dist/cjs/plugins/{vite-plugin-cedar-context-wrapping.js → vite-plugin-handler-als-wrapping.js} +21 -21
  17. package/dist/cjs/plugins/vite-plugin-rsc-ssr-router-import.js +2 -2
  18. package/dist/cjs/plugins/vite-plugin-rsc-transform-server.js +4 -0
  19. package/dist/cjs/rsc/rscRenderer.js +23 -9
  20. package/dist/cjs/rsc/rscRequestHandler.js +6 -6
  21. package/dist/cjs/rsc/rscWebSocketServer.js +56 -0
  22. package/dist/cjs/rsc/utils.js +56 -0
  23. package/dist/cjs/runFeServer.js +3 -22
  24. package/dist/cjs/streaming/createReactStreamingHandler.js +13 -10
  25. package/dist/cjs/streaming/streamHelpers.js +49 -32
  26. package/dist/cjs/streaming/triggerRouteHooks.js +3 -3
  27. package/dist/devFeServer.js +272 -16
  28. package/dist/index.d.ts +5 -3
  29. package/dist/index.d.ts.map +1 -1
  30. package/dist/index.js +17 -9
  31. package/dist/lib/registerFwGlobalsAndShims.js +9 -0
  32. package/dist/plugins/vite-plugin-cedar-data-uri-shim.d.ts +3 -0
  33. package/dist/plugins/vite-plugin-cedar-data-uri-shim.d.ts.map +1 -0
  34. package/dist/plugins/vite-plugin-cedar-data-uri-shim.js +43 -0
  35. package/dist/plugins/vite-plugin-cedar-gqlorm-inject.d.ts +29 -0
  36. package/dist/plugins/vite-plugin-cedar-gqlorm-inject.d.ts.map +1 -0
  37. package/dist/plugins/vite-plugin-cedar-gqlorm-inject.js +76 -0
  38. package/dist/plugins/vite-plugin-cedar-graphql-options-extract.d.ts +38 -0
  39. package/dist/plugins/vite-plugin-cedar-graphql-options-extract.d.ts.map +1 -0
  40. package/dist/plugins/vite-plugin-cedar-graphql-options-extract.js +88 -0
  41. package/dist/plugins/vite-plugin-cedar-otel-wrapping.d.ts +39 -0
  42. package/dist/plugins/vite-plugin-cedar-otel-wrapping.d.ts.map +1 -0
  43. package/dist/plugins/vite-plugin-cedar-otel-wrapping.js +200 -0
  44. package/dist/plugins/vite-plugin-cedar-universal-deploy.js +1 -1
  45. package/dist/plugins/{vite-plugin-cedar-context-wrapping.d.ts → vite-plugin-handler-als-wrapping.d.ts} +12 -12
  46. package/dist/plugins/vite-plugin-handler-als-wrapping.d.ts.map +1 -0
  47. package/dist/plugins/{vite-plugin-cedar-context-wrapping.js → vite-plugin-handler-als-wrapping.js} +16 -16
  48. package/dist/plugins/vite-plugin-rsc-ssr-router-import.d.ts.map +1 -1
  49. package/dist/plugins/vite-plugin-rsc-ssr-router-import.js +2 -2
  50. package/dist/plugins/vite-plugin-rsc-transform-server.d.ts.map +1 -1
  51. package/dist/plugins/vite-plugin-rsc-transform-server.js +4 -0
  52. package/dist/rsc/rscRenderer.d.ts.map +1 -1
  53. package/dist/rsc/rscRenderer.js +19 -5
  54. package/dist/rsc/rscRequestHandler.d.ts +2 -2
  55. package/dist/rsc/rscRequestHandler.d.ts.map +1 -1
  56. package/dist/rsc/rscRequestHandler.js +4 -4
  57. package/dist/rsc/rscWebSocketServer.d.ts +2 -0
  58. package/dist/rsc/rscWebSocketServer.d.ts.map +1 -0
  59. package/dist/rsc/rscWebSocketServer.js +22 -0
  60. package/dist/rsc/utils.d.ts +10 -0
  61. package/dist/rsc/utils.d.ts.map +1 -0
  62. package/dist/rsc/utils.js +21 -0
  63. package/dist/runFeServer.js +2 -21
  64. package/dist/streaming/createReactStreamingHandler.d.ts +1 -1
  65. package/dist/streaming/createReactStreamingHandler.d.ts.map +1 -1
  66. package/dist/streaming/createReactStreamingHandler.js +13 -10
  67. package/dist/streaming/streamHelpers.d.ts +2 -2
  68. package/dist/streaming/streamHelpers.d.ts.map +1 -1
  69. package/dist/streaming/streamHelpers.js +49 -31
  70. package/dist/streaming/triggerRouteHooks.d.ts +2 -2
  71. package/dist/streaming/triggerRouteHooks.d.ts.map +1 -1
  72. package/dist/streaming/triggerRouteHooks.js +3 -3
  73. package/package.json +19 -20
  74. package/dist/cjs/plugins/vite-plugin-cedar-node-polyfills.js +0 -43
  75. package/dist/plugins/vite-plugin-cedar-context-wrapping.d.ts.map +0 -1
  76. package/dist/plugins/vite-plugin-cedar-node-polyfills.d.ts +0 -3
  77. package/dist/plugins/vite-plugin-cedar-node-polyfills.d.ts.map +0 -1
  78. package/dist/plugins/vite-plugin-cedar-node-polyfills.js +0 -19
@@ -1,7 +1,12 @@
1
1
  #!/usr/bin/env node
2
+ import http from "node:http";
2
3
  import { createServerAdapter } from "@whatwg-node/server";
3
4
  import express from "express";
4
- import { createServer as createViteServer } from "vite";
5
+ import {
6
+ createServer as createViteServer,
7
+ createServerModuleRunner,
8
+ defaultServerConditions
9
+ } from "vite";
5
10
  import { cjsInterop } from "vite-plugin-cjs-interop";
6
11
  import { getProjectRoutes } from "@cedarjs/internal/dist/routes.js";
7
12
  import { getConfig, getPaths } from "@cedarjs/project-config";
@@ -10,6 +15,10 @@ import { registerFwGlobalsAndShims } from "./lib/registerFwGlobalsAndShims.js";
10
15
  import { invoke } from "./middleware/invokeMiddleware.js";
11
16
  import { createMiddlewareRouter } from "./middleware/register.js";
12
17
  import { rscRoutesAutoLoader } from "./plugins/vite-plugin-rsc-routes-auto-loader.js";
18
+ import { rscRoutesImports } from "./plugins/vite-plugin-rsc-routes-imports.js";
19
+ import { rscSsrRouterImport } from "./plugins/vite-plugin-rsc-ssr-router-import.js";
20
+ import { rscTransformUseServerPlugin } from "./plugins/vite-plugin-rsc-transform-server.js";
21
+ import { createWebSocketServer } from "./rsc/rscWebSocketServer.js";
13
22
  import { collectCssPaths, componentsModules } from "./streaming/collectCss.js";
14
23
  import { createReactStreamingHandler } from "./streaming/createReactStreamingHandler.js";
15
24
  import {
@@ -18,10 +27,13 @@ import {
18
27
  getFullUrl
19
28
  } from "./utils.js";
20
29
  globalThis.__REDWOOD__PRERENDER_PAGES = {};
30
+ globalThis.__cedarjs__vite_ssr_runtime = void 0;
31
+ globalThis.__cedarjs__vite_rsc_runtime = void 0;
21
32
  async function createServer() {
22
33
  ensureProcessDirWeb();
23
34
  registerFwGlobalsAndShims();
24
35
  const app = express();
36
+ const server = http.createServer(app);
25
37
  const rwPaths = getPaths();
26
38
  const rscEnabled = getConfig().experimental.rsc?.enabled ?? false;
27
39
  const serverStorage = createServerStorage();
@@ -44,8 +56,78 @@ async function createServer() {
44
56
  "Vite config not found. Please set up Vite before running the dev server."
45
57
  );
46
58
  }
47
- const vite = await createViteServer({
59
+ const viteSsrDevServer = await createViteServer({
48
60
  configFile: rwPaths.web.viteConfig,
61
+ envFile: false,
62
+ define: {
63
+ "process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV)
64
+ },
65
+ ssr: {
66
+ // Inline every file apart from node built-ins. We want vite/rollup to
67
+ // inline dependencies in the server build. This gets round runtime
68
+ // importing of "server-only" and other packages with poisoned imports.
69
+ //
70
+ // Files included in `noExternal` are files we want Vite to analyze
71
+ // As of vite 5.2 `true` here means "all except node built-ins"
72
+ // noExternal: true,
73
+ // TODO (RSC): Other frameworks build for RSC without `noExternal: true`.
74
+ // What are we missing here? When/why is that a better choice? I know
75
+ // we would have to explicitly add a bunch of packages to noExternal, if
76
+ // we wanted to go that route.
77
+ // noExternal: ['@tobbe.dev/rsc-test'],
78
+ // Can't inline prisma client (db calls fail at runtime) or react-dom
79
+ // (css pre-init failure)
80
+ // Server store has to be externalized, because it's a singleton (shared between FW and App)
81
+ external: [
82
+ "@prisma/client",
83
+ "@prisma/adapter-better-sqlite3",
84
+ "@prisma/fetch-engine",
85
+ "@prisma/internals",
86
+ "better-sqlite3",
87
+ "@cedarjs/auth-dbauth-api",
88
+ "@cedarjs/cookie-jar",
89
+ "@cedarjs/server-store",
90
+ "@simplewebauthn/server",
91
+ "graphql-scalars",
92
+ "minimatch",
93
+ "playwright",
94
+ "react-dom"
95
+ ],
96
+ resolve: {
97
+ // These conditions are used in the plugin pipeline, and only affect non-externalized
98
+ // dependencies during the SSR build. Which because of `noExternal: true` means all
99
+ // dependencies apart from node built-ins.
100
+ // TODO (RSC): What's the difference between `conditions` and
101
+ // `externalConditions`? When is one used over the other?
102
+ // conditions: ['react-server'],
103
+ // externalConditions: ['react-server'],
104
+ },
105
+ optimizeDeps: {
106
+ // We need Vite to optimize these dependencies so that they are resolved
107
+ // with the correct conditions. And so that CJS modules work correctly.
108
+ // include: [
109
+ // 'react/**/*',
110
+ // 'react-dom/server',
111
+ // 'react-dom/server.edge',
112
+ // 'rehackt',
113
+ // 'react-server-dom-webpack/server',
114
+ // 'react-server-dom-webpack/client',
115
+ // '@apollo/client/cache/*',
116
+ // '@apollo/client/utilities/*',
117
+ // '@apollo/client/react/hooks/*',
118
+ // 'react-fast-compare',
119
+ // 'invariant',
120
+ // 'shallowequal',
121
+ // 'graphql/language/*',
122
+ // 'stacktracey',
123
+ // 'deepmerge',
124
+ // 'fast-glob',
125
+ // ],
126
+ }
127
+ },
128
+ resolve: {
129
+ // conditions: ['react-server'],
130
+ },
49
131
  plugins: [
50
132
  cjsInterop({
51
133
  dependencies: [
@@ -59,16 +141,183 @@ async function createServer() {
59
141
  "@cedarjs/auth-!(dbauth)-web"
60
142
  ]
61
143
  }),
62
- rscEnabled && rscRoutesAutoLoader()
144
+ rscEnabled && rscRoutesAutoLoader(),
145
+ rscEnabled && rscSsrRouterImport()
63
146
  ],
64
147
  server: { middlewareMode: true },
65
148
  logLevel: "info",
66
149
  clearScreen: false,
67
150
  appType: "custom"
68
151
  });
152
+ globalThis.__cedarjs__vite_ssr_runtime = createServerModuleRunner(
153
+ viteSsrDevServer.environments.ssr
154
+ );
155
+ globalThis.__cedarjs__client_references = /* @__PURE__ */ new Set();
156
+ globalThis.__cedarjs__server_references = /* @__PURE__ */ new Set();
157
+ const viteRscServer = await createViteServer({
158
+ envFile: false,
159
+ define: {
160
+ "process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV)
161
+ },
162
+ ssr: {
163
+ // Inline every file apart from node built-ins. We want vite/rollup to
164
+ // inline dependencies in the server build. This gets round runtime
165
+ // importing of "server-only" and other packages with poisoned imports.
166
+ //
167
+ // Files included in `noExternal` are files we want Vite to analyze
168
+ // As of vite 5.2 `true` here means "all except node built-ins"
169
+ noExternal: true,
170
+ // TODO (RSC): Other frameworks build for RSC without `noExternal: true`.
171
+ // What are we missing here? When/why is that a better choice? I know
172
+ // we would have to explicitly add a bunch of packages to noExternal, if
173
+ // we wanted to go that route.
174
+ // noExternal: ['@tobbe.dev/rsc-test'],
175
+ // Can't inline prisma client (db calls fail at runtime) or react-dom
176
+ // (css pre-init failure)
177
+ // Server store has to be externalized, because it's a singleton (shared between FW and App)
178
+ external: [
179
+ "@prisma/client",
180
+ "@prisma/adapter-better-sqlite3",
181
+ "@prisma/fetch-engine",
182
+ "@prisma/internals",
183
+ "better-sqlite3",
184
+ "@cedarjs/auth-dbauth-api",
185
+ "@cedarjs/cookie-jar",
186
+ "@cedarjs/server-store",
187
+ "@cedarjs/structure",
188
+ "@simplewebauthn/server",
189
+ "graphql-scalars",
190
+ "minimatch",
191
+ "playwright",
192
+ "react-dom"
193
+ ],
194
+ resolve: {
195
+ // These conditions are used in the plugin pipeline, and only affect non-externalized
196
+ // dependencies during the SSR build. Which because of `noExternal: true` means all
197
+ // dependencies apart from node built-ins.
198
+ // TODO (RSC): What's the difference between `conditions` and
199
+ // `externalConditions`? When is one used over the other?
200
+ // In Vite 6, we must include `defaultServerConditions` alongside
201
+ // `react-server` so that nested condition maps (e.g. the `node`
202
+ // sub-condition inside `react-server-dom-webpack/server`'s exports)
203
+ // can still be resolved. Without `node` (or another environment
204
+ // condition), the resolver throws "No known conditions for
205
+ // './server' specifier in 'react-server-dom-webpack' package".
206
+ conditions: ["react-server", ...defaultServerConditions],
207
+ externalConditions: ["react-server", ...defaultServerConditions]
208
+ },
209
+ optimizeDeps: {
210
+ // We need Vite to optimize these dependencies so that they are resolved
211
+ // with the correct conditions. And so that CJS modules work correctly.
212
+ include: [
213
+ "react/**/*",
214
+ "react-dom/server",
215
+ "react-dom/server.edge",
216
+ "rehackt",
217
+ "react-server-dom-webpack/server",
218
+ "react-server-dom-webpack/server.edge",
219
+ "react-server-dom-webpack/client",
220
+ "react-server-dom-webpack/client.edge",
221
+ "@apollo/client/cache/*",
222
+ "@apollo/client/utilities/*",
223
+ "@apollo/client/react/hooks/*",
224
+ "react-fast-compare",
225
+ "invariant",
226
+ "shallowequal",
227
+ "graphql/language/*",
228
+ "stacktracey",
229
+ "deepmerge",
230
+ "fast-glob",
231
+ "@whatwg-node/fetch",
232
+ "busboy",
233
+ "cookie"
234
+ ],
235
+ // Ensure `util` resolves to Node's built-in module and not to
236
+ // Browserify's `node-util` polyfill (which lacks TextEncoder).
237
+ // The polyfill may be hoisted into node_modules by optional
238
+ // dependencies like `@cedarjs/storybook`.
239
+ exclude: ["util"]
240
+ }
241
+ },
242
+ resolve: {
243
+ // See comment above in ssr.resolve for why we include defaultServerConditions.
244
+ conditions: ["react-server", ...defaultServerConditions]
245
+ },
246
+ plugins: [
247
+ {
248
+ name: "rsc-record-and-tranform-use-client-plugin",
249
+ transform(code, id, _options) {
250
+ globalThis.__cedarjs__client_references?.delete(id);
251
+ if (!/^(["'])use client\1/.test(code)) {
252
+ return void 0;
253
+ }
254
+ console.log(
255
+ "rsc-record-and-transform-use-client-plugin: adding client reference",
256
+ id
257
+ );
258
+ globalThis.__cedarjs__client_references?.add(id);
259
+ const fns = code.matchAll(/export function (\w+)\(/g);
260
+ const consts = code.matchAll(/export const (\w+) = \(/g);
261
+ const names = [...fns, ...consts].map(([, name]) => name);
262
+ const result = [
263
+ `import { registerClientReference } from "react-server-dom-webpack/server.edge";`,
264
+ "",
265
+ ...names.map((name) => {
266
+ return name === "default" ? `export default registerClientReference({}, "${id}", "${name}");` : `export const ${name} = registerClientReference({}, "${id}", "${name}");`;
267
+ })
268
+ ].join("\n");
269
+ console.log("rsc-record-and-transform-use-client-plugin result");
270
+ console.log(
271
+ result.split("\n").map((line, i) => ` ${i + 1}: ${line}`).join("\n")
272
+ );
273
+ return { code: result, map: null };
274
+ }
275
+ },
276
+ rscTransformUseServerPlugin("", {}),
277
+ // The rscTransformUseClientPlugin maps paths like
278
+ // /Users/tobbe/.../rw-app/node_modules/@tobbe.dev/rsc-test/dist/rsc-test.es.js
279
+ // to
280
+ // /Users/tobbe/.../rw-app/web/dist/ssr/assets/rsc0.js
281
+ // That's why it needs the `clientEntryFiles` data
282
+ // (It does other things as well, but that's why it needs clientEntryFiles)
283
+ // rscTransformUseClientPlugin(clientEntryFiles),
284
+ // rscTransformUseServerPlugin(outDir, serverEntryFiles),
285
+ rscRoutesImports(),
286
+ {
287
+ name: "rsc-hot-update",
288
+ handleHotUpdate(ctx) {
289
+ console.log("rsc-hot-update ctx.modules", ctx.modules);
290
+ return [];
291
+ }
292
+ }
293
+ ],
294
+ build: {
295
+ ssr: true
296
+ },
297
+ server: {
298
+ // We never call `viteRscServer.listen()`, so we should run this in
299
+ // middleware mode
300
+ middlewareMode: true,
301
+ // The hmr/fast-refresh websocket in this server collides with the one in
302
+ // the other Vite server. So we can either disable it or run it on a
303
+ // different port.
304
+ // TODO (RSC): Figure out if we should disable or just pick a different
305
+ // port
306
+ ws: false
307
+ // hmr: {
308
+ // port: 24679,
309
+ // },
310
+ },
311
+ appType: "custom",
312
+ // Using a unique cache dir here to not clash with our other vite server
313
+ cacheDir: "../node_modules/.vite-rsc"
314
+ });
315
+ globalThis.__cedarjs__vite_rsc_runtime = createServerModuleRunner(
316
+ viteRscServer.environments.ssr
317
+ );
69
318
  const handleWithMiddleware = (route) => {
70
319
  return createServerAdapter(async (req) => {
71
- const middlewareRouter = await createMiddlewareRouter(vite);
320
+ const middlewareRouter = await createMiddlewareRouter(viteSsrDevServer);
72
321
  const middleware = middlewareRouter.find(
73
322
  req.method,
74
323
  req.url
@@ -78,19 +327,22 @@ async function createServer() {
78
327
  }
79
328
  const [mwRes] = await invoke(req, middleware, {
80
329
  route,
81
- viteDevServer: vite
330
+ viteSsrDevServer
82
331
  });
83
332
  return mwRes.toResponse();
84
333
  });
85
334
  };
86
- app.use(vite.middlewares);
335
+ app.use(viteSsrDevServer.middlewares);
87
336
  if (rscEnabled) {
88
- const { createRscRequestHandler } = await import("./rsc/rscRequestHandler.js");
337
+ createWebSocketServer();
338
+ const { createRscRequestHandler } = await globalThis.__cedarjs__vite_rsc_runtime.import(
339
+ new URL("./rsc/rscRequestHandler.js", import.meta.url).pathname
340
+ );
89
341
  app.use(
90
342
  "/rw-rsc",
91
- createRscRequestHandler({
92
- getMiddlewareRouter: async () => createMiddlewareRouter(vite),
93
- viteDevServer: vite
343
+ await createRscRequestHandler({
344
+ getMiddlewareRouter: async () => createMiddlewareRouter(viteSsrDevServer),
345
+ viteSsrDevServer
94
346
  })
95
347
  );
96
348
  }
@@ -100,18 +352,22 @@ async function createServer() {
100
352
  routes,
101
353
  clientEntryPath: rwPaths.web.entryClient,
102
354
  getStylesheetLinks: (route) => {
103
- return getCssLinks({ rwPaths, route, vite });
355
+ return getCssLinks({
356
+ rwPaths,
357
+ route,
358
+ viteSsrDevServer
359
+ });
104
360
  },
105
361
  // Recreate middleware router on each request in dev
106
- getMiddlewareRouter: async () => createMiddlewareRouter(vite)
362
+ getMiddlewareRouter: async () => createMiddlewareRouter(viteSsrDevServer)
107
363
  },
108
- vite
364
+ viteSsrDevServer
109
365
  );
110
366
  app.get("*", createServerAdapter(routeHandler));
111
367
  app.post("*", handleWithMiddleware());
112
368
  const port = getConfig().web.port;
113
369
  console.log(`Started server on http://localhost:${port}`);
114
- return app.listen(port);
370
+ return server.listen(port);
115
371
  }
116
372
  let devApp = createServer();
117
373
  process.stdin.on("data", async (data) => {
@@ -126,11 +382,11 @@ process.stdin.on("data", async (data) => {
126
382
  function getCssLinks({
127
383
  rwPaths,
128
384
  route,
129
- vite
385
+ viteSsrDevServer
130
386
  }) {
131
387
  const appAndRouteModules = componentsModules(
132
388
  [rwPaths.web.app, route?.filePath].filter(Boolean),
133
- vite
389
+ viteSsrDevServer
134
390
  );
135
391
  const collectedCss = collectCssPaths(appAndRouteModules);
136
392
  const cssLinks = Array.from(collectedCss);
package/dist/index.d.ts CHANGED
@@ -2,12 +2,14 @@ import type { PluginOption } from 'vite';
2
2
  export { cedarAutoImportsPlugin } from './plugins/vite-plugin-cedar-auto-import.js';
3
3
  export { cedarCjsCompatPlugin } from './plugins/vite-plugin-cedar-cjs-compat.js';
4
4
  export { cedarCellTransform } from './plugins/vite-plugin-cedar-cell.js';
5
- export { cedarContextWrappingPlugin } from './plugins/vite-plugin-cedar-context-wrapping.js';
6
- export { applyContextWrapping } from './plugins/vite-plugin-cedar-context-wrapping.js';
5
+ export { cedarGqlormInjectPlugin } from './plugins/vite-plugin-cedar-gqlorm-inject.js';
6
+ export { cedarGraphqlOptionsExtractPlugin } from './plugins/vite-plugin-cedar-graphql-options-extract.js';
7
+ export { cedarOtelWrappingPlugin } from './plugins/vite-plugin-cedar-otel-wrapping.js';
8
+ export { applyHandlerAlsWrapping, handlerAlsWrappingPlugin, } from './plugins/vite-plugin-handler-als-wrapping.js';
7
9
  export { cedarEntryInjectionPlugin } from './plugins/vite-plugin-cedar-entry-injection.js';
8
10
  export { cedarHtmlEnvPlugin } from './plugins/vite-plugin-cedar-html-env.js';
9
11
  export { cedarImportDirPlugin } from './plugins/vite-plugin-cedar-import-dir.js';
10
- export { cedarNodePolyfills } from './plugins/vite-plugin-cedar-node-polyfills.js';
12
+ export { cedarDataUriShim } from './plugins/vite-plugin-cedar-data-uri-shim.js';
11
13
  export { cedarRemoveDevFatalErrorPage } from './plugins/vite-plugin-cedar-remove-dev-fatal-error-page.js';
12
14
  export { cedarRoutesAutoLoaderPlugin } from './plugins/vite-plugin-cedar-routes-auto-loader.js';
13
15
  export { cedarRemoveFromBundle } from './plugins/vite-plugin-cedar-remove-from-bundle.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,MAAM,CAAA;AAuBxC,OAAO,EAAE,sBAAsB,EAAE,MAAM,4CAA4C,CAAA;AACnF,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAA;AAChF,OAAO,EAAE,kBAAkB,EAAE,MAAM,qCAAqC,CAAA;AACxE,OAAO,EAAE,0BAA0B,EAAE,MAAM,iDAAiD,CAAA;AAC5F,OAAO,EAAE,oBAAoB,EAAE,MAAM,iDAAiD,CAAA;AACtF,OAAO,EAAE,yBAAyB,EAAE,MAAM,gDAAgD,CAAA;AAC1F,OAAO,EAAE,kBAAkB,EAAE,MAAM,yCAAyC,CAAA;AAC5E,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAA;AAChF,OAAO,EAAE,kBAAkB,EAAE,MAAM,+CAA+C,CAAA;AAClF,OAAO,EAAE,4BAA4B,EAAE,MAAM,4DAA4D,CAAA;AACzG,OAAO,EAAE,2BAA2B,EAAE,MAAM,mDAAmD,CAAA;AAC/F,OAAO,EAAE,qBAAqB,EAAE,MAAM,mDAAmD,CAAA;AACzF,OAAO,EAAE,qCAAqC,EAAE,MAAM,8DAA8D,CAAA;AACpH,OAAO,EAAE,4BAA4B,EAAE,MAAM,oDAAoD,CAAA;AACjG,OAAO,EAAE,qBAAqB,EAAE,MAAM,qCAAqC,CAAA;AAC3E,OAAO,EAAE,iBAAiB,EAAE,MAAM,wCAAwC,CAAA;AAC1E,OAAO,EAAE,uBAAuB,EAAE,MAAM,+CAA+C,CAAA;AACvF,OAAO,EAAE,0BAA0B,EAAE,MAAM,iDAAiD,CAAA;AAC5F,OAAO,EAAE,qBAAqB,EAAE,MAAM,oDAAoD,CAAA;AAE1F,KAAK,aAAa,GAAG;IACnB,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAC1B,CAAA;AAED;;GAEG;AACH,wBAAgB,KAAK,CAAC,EAAE,IAAI,EAAE,GAAE,aAAkB,GAAG,YAAY,EAAE,CAgClE;AAED,8DAA8D;AAC9D,eAAe,KAAK,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,MAAM,CAAA;AAuBxC,OAAO,EAAE,sBAAsB,EAAE,MAAM,4CAA4C,CAAA;AACnF,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAA;AAChF,OAAO,EAAE,kBAAkB,EAAE,MAAM,qCAAqC,CAAA;AACxE,OAAO,EAAE,uBAAuB,EAAE,MAAM,8CAA8C,CAAA;AACtF,OAAO,EAAE,gCAAgC,EAAE,MAAM,wDAAwD,CAAA;AACzG,OAAO,EAAE,uBAAuB,EAAE,MAAM,8CAA8C,CAAA;AACtF,OAAO,EACL,uBAAuB,EACvB,wBAAwB,GACzB,MAAM,+CAA+C,CAAA;AACtD,OAAO,EAAE,yBAAyB,EAAE,MAAM,gDAAgD,CAAA;AAC1F,OAAO,EAAE,kBAAkB,EAAE,MAAM,yCAAyC,CAAA;AAC5E,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAA;AAChF,OAAO,EAAE,gBAAgB,EAAE,MAAM,8CAA8C,CAAA;AAC/E,OAAO,EAAE,4BAA4B,EAAE,MAAM,4DAA4D,CAAA;AACzG,OAAO,EAAE,2BAA2B,EAAE,MAAM,mDAAmD,CAAA;AAC/F,OAAO,EAAE,qBAAqB,EAAE,MAAM,mDAAmD,CAAA;AACzF,OAAO,EAAE,qCAAqC,EAAE,MAAM,8DAA8D,CAAA;AACpH,OAAO,EAAE,4BAA4B,EAAE,MAAM,oDAAoD,CAAA;AACjG,OAAO,EAAE,qBAAqB,EAAE,MAAM,qCAAqC,CAAA;AAC3E,OAAO,EAAE,iBAAiB,EAAE,MAAM,wCAAwC,CAAA;AAC1E,OAAO,EAAE,uBAAuB,EAAE,MAAM,+CAA+C,CAAA;AACvF,OAAO,EAAE,0BAA0B,EAAE,MAAM,iDAAiD,CAAA;AAC5F,OAAO,EAAE,qBAAqB,EAAE,MAAM,oDAAoD,CAAA;AAE1F,KAAK,aAAa,GAAG;IACnB,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAC1B,CAAA;AAED;;GAEG;AACH,wBAAgB,KAAK,CAAC,EAAE,IAAI,EAAE,GAAE,aAAkB,GAAG,YAAY,EAAE,CAgClE;AAED,8DAA8D;AAC9D,eAAe,KAAK,CAAA"}
package/dist/index.js CHANGED
@@ -7,9 +7,9 @@ import {
7
7
  createAuthImportTransformPlugin
8
8
  } from "@cedarjs/testing/web/vitest";
9
9
  import { cedarCellTransform } from "./plugins/vite-plugin-cedar-cell.js";
10
+ import { cedarDataUriShim } from "./plugins/vite-plugin-cedar-data-uri-shim.js";
10
11
  import { cedarEntryInjectionPlugin } from "./plugins/vite-plugin-cedar-entry-injection.js";
11
12
  import { cedarHtmlEnvPlugin } from "./plugins/vite-plugin-cedar-html-env.js";
12
- import { cedarNodePolyfills } from "./plugins/vite-plugin-cedar-node-polyfills.js";
13
13
  import { cedarRemoveDevFatalErrorPage } from "./plugins/vite-plugin-cedar-remove-dev-fatal-error-page.js";
14
14
  import { cedarRemoveFromBundle } from "./plugins/vite-plugin-cedar-remove-from-bundle.js";
15
15
  import { cedarRoutesAutoLoaderPlugin } from "./plugins/vite-plugin-cedar-routes-auto-loader.js";
@@ -21,12 +21,17 @@ import { cedarSwapApolloProvider } from "./plugins/vite-plugin-swap-apollo-provi
21
21
  import { cedarAutoImportsPlugin } from "./plugins/vite-plugin-cedar-auto-import.js";
22
22
  import { cedarCjsCompatPlugin } from "./plugins/vite-plugin-cedar-cjs-compat.js";
23
23
  import { cedarCellTransform as cedarCellTransform2 } from "./plugins/vite-plugin-cedar-cell.js";
24
- import { cedarContextWrappingPlugin } from "./plugins/vite-plugin-cedar-context-wrapping.js";
25
- import { applyContextWrapping } from "./plugins/vite-plugin-cedar-context-wrapping.js";
24
+ import { cedarGqlormInjectPlugin } from "./plugins/vite-plugin-cedar-gqlorm-inject.js";
25
+ import { cedarGraphqlOptionsExtractPlugin } from "./plugins/vite-plugin-cedar-graphql-options-extract.js";
26
+ import { cedarOtelWrappingPlugin } from "./plugins/vite-plugin-cedar-otel-wrapping.js";
27
+ import {
28
+ applyHandlerAlsWrapping,
29
+ handlerAlsWrappingPlugin
30
+ } from "./plugins/vite-plugin-handler-als-wrapping.js";
26
31
  import { cedarEntryInjectionPlugin as cedarEntryInjectionPlugin2 } from "./plugins/vite-plugin-cedar-entry-injection.js";
27
32
  import { cedarHtmlEnvPlugin as cedarHtmlEnvPlugin2 } from "./plugins/vite-plugin-cedar-html-env.js";
28
33
  import { cedarImportDirPlugin } from "./plugins/vite-plugin-cedar-import-dir.js";
29
- import { cedarNodePolyfills as cedarNodePolyfills2 } from "./plugins/vite-plugin-cedar-node-polyfills.js";
34
+ import { cedarDataUriShim as cedarDataUriShim2 } from "./plugins/vite-plugin-cedar-data-uri-shim.js";
30
35
  import { cedarRemoveDevFatalErrorPage as cedarRemoveDevFatalErrorPage2 } from "./plugins/vite-plugin-cedar-remove-dev-fatal-error-page.js";
31
36
  import { cedarRoutesAutoLoaderPlugin as cedarRoutesAutoLoaderPlugin2 } from "./plugins/vite-plugin-cedar-routes-auto-loader.js";
32
37
  import { cedarRemoveFromBundle as cedarRemoveFromBundle2 } from "./plugins/vite-plugin-cedar-remove-from-bundle.js";
@@ -51,7 +56,7 @@ function cedar({ mode } = {}) {
51
56
  mode === "test" && createAuthImportTransformPlugin(),
52
57
  mode === "test" && autoImportsPlugin(),
53
58
  cedarWaitForApiServer(),
54
- cedarNodePolyfills(),
59
+ cedarDataUriShim(),
55
60
  cedarHtmlEnvPlugin(),
56
61
  cedarEntryInjectionPlugin(),
57
62
  cedarMergedConfig(),
@@ -68,17 +73,19 @@ function cedar({ mode } = {}) {
68
73
  }
69
74
  var index_default = cedar;
70
75
  export {
71
- applyContextWrapping,
76
+ applyHandlerAlsWrapping,
72
77
  cedar,
73
78
  cedarAutoImportsPlugin,
74
79
  cedarCellTransform2 as cedarCellTransform,
75
80
  cedarCjsCompatPlugin,
76
- cedarContextWrappingPlugin,
81
+ cedarDataUriShim2 as cedarDataUriShim,
77
82
  cedarEntryInjectionPlugin2 as cedarEntryInjectionPlugin,
83
+ cedarGqlormInjectPlugin,
84
+ cedarGraphqlOptionsExtractPlugin,
78
85
  cedarHtmlEnvPlugin2 as cedarHtmlEnvPlugin,
79
86
  cedarImportDirPlugin,
80
87
  cedarMergedConfig2 as cedarMergedConfig,
81
- cedarNodePolyfills2 as cedarNodePolyfills,
88
+ cedarOtelWrappingPlugin,
82
89
  cedarRemoveDevFatalErrorPage2 as cedarRemoveDevFatalErrorPage,
83
90
  cedarRemoveFromBundle2 as cedarRemoveFromBundle,
84
91
  cedarRoutesAutoLoaderPlugin2 as cedarRoutesAutoLoaderPlugin,
@@ -88,5 +95,6 @@ export {
88
95
  cedarWaitForApiServer2 as cedarWaitForApiServer,
89
96
  cedarjsJobPathInjectorPlugin,
90
97
  cedarjsResolveCedarStyleImportsPlugin2 as cedarjsResolveCedarStyleImportsPlugin,
91
- index_default as default
98
+ index_default as default,
99
+ handlerAlsWrappingPlugin
92
100
  };
@@ -55,6 +55,15 @@ function registerFwShims() {
55
55
  globalThis.__rw_module_cache__ ||= /* @__PURE__ */ new Map();
56
56
  globalThis.__webpack_chunk_load__ ||= async (id) => {
57
57
  console.log("registerFwShims chunk load id", id);
58
+ if (globalThis.__cedarjs__vite_ssr_runtime) {
59
+ return globalThis.__cedarjs__vite_ssr_runtime?.import(id).then((mod) => {
60
+ console.log("registerFwShims chunk load mod", mod);
61
+ if (mod.default && typeof mod.default === "object") {
62
+ return globalThis.__rw_module_cache__.set(id, mod.default);
63
+ }
64
+ return globalThis.__rw_module_cache__.set(id, mod);
65
+ });
66
+ }
58
67
  return import(id).then((mod) => {
59
68
  console.log("registerFwShims chunk load mod", mod);
60
69
  if (mod.default && typeof mod.default === "object") {
@@ -0,0 +1,3 @@
1
+ import type { Plugin } from 'vite';
2
+ export declare function cedarDataUriShim(): Plugin | undefined;
3
+ //# sourceMappingURL=vite-plugin-cedar-data-uri-shim.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vite-plugin-cedar-data-uri-shim.d.ts","sourceRoot":"","sources":["../../src/plugins/vite-plugin-cedar-data-uri-shim.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAA;AAgClC,wBAAgB,gBAAgB,IAAI,MAAM,GAAG,SAAS,CA4BrD"}
@@ -0,0 +1,43 @@
1
+ import { getConfig } from "@cedarjs/project-config";
2
+ function dataUriToBuffer(uri) {
3
+ const match = uri.match(/^data:(.*?)(;(.*?))?(,(.*))$/);
4
+ if (!match) {
5
+ throw new Error("Invalid data URI");
6
+ }
7
+ const mediaType = match[1] || "text/plain";
8
+ const params = match[3] || "";
9
+ const data = match[5];
10
+ const [type] = mediaType.split(";");
11
+ const typeFull = params ? mediaType + ";" + params : mediaType;
12
+ const lowerParams = params.toLowerCase();
13
+ const isBase64 = lowerParams.includes("base64");
14
+ const charset = lowerParams.startsWith("charset=") ? lowerParams.slice(8) : "";
15
+ const bytes = isBase64 ? Uint8Array.from(atob(data), (c) => c.charCodeAt(0)) : new TextEncoder().encode(decodeURIComponent(data));
16
+ const decoder = new TextDecoder();
17
+ bytes.toString = () => decoder.decode(bytes);
18
+ return Object.assign(bytes, { type, typeFull, charset });
19
+ }
20
+ function cedarDataUriShim() {
21
+ if (getConfig().experimental?.rsc?.enabled) {
22
+ return void 0;
23
+ }
24
+ return {
25
+ name: "cedar-data-uri-shim",
26
+ apply: "serve",
27
+ resolveId(id) {
28
+ if (id === "data-uri-to-buffer") {
29
+ return "\0cedar-data-uri-to-buffer";
30
+ }
31
+ return void 0;
32
+ },
33
+ load(id) {
34
+ if (id === "\0cedar-data-uri-to-buffer") {
35
+ return "export default " + dataUriToBuffer.toString();
36
+ }
37
+ return void 0;
38
+ }
39
+ };
40
+ }
41
+ export {
42
+ cedarDataUriShim
43
+ };
@@ -0,0 +1,29 @@
1
+ import type { Plugin } from 'vite';
2
+ /**
3
+ * Vite plugin that injects the auto-generated gqlorm backend into
4
+ * `api/src/functions/graphql.ts` at build time.
5
+ *
6
+ * When `experimental.gqlorm.enabled = true` and `.cedar/gqlorm/backend.ts`
7
+ * exists, this plugin:
8
+ *
9
+ * 1. Adds imports at the top of graphql.ts:
10
+ * import * as __gqlorm_sdl__ from '../../../.cedar/gqlorm/backend'
11
+ * import { db as __gqlorm_db__ } from 'src/lib/db'
12
+ *
13
+ * 2. Inserts a statement immediately before the `createGraphQLHandler` call:
14
+ * Object.assign(sdls, {
15
+ * __gqlorm__: {
16
+ * schema: __gqlorm_sdl__.schema,
17
+ * resolvers: __gqlorm_sdl__.createGqlormResolvers(__gqlorm_db__),
18
+ * },
19
+ * })
20
+ *
21
+ * The `sdls` variable is already a `let` binding because
22
+ * `vite-plugin-cedar-import-dir` transforms the glob import
23
+ * `import sdls from 'src/graphql/**\/*.sdl.{js,ts}'` into `let sdls = {}`.
24
+ *
25
+ * This plugin is a no-op when gqlorm is disabled or the backend file does
26
+ * not exist.
27
+ */
28
+ export declare function cedarGqlormInjectPlugin(): Plugin;
29
+ //# sourceMappingURL=vite-plugin-cedar-gqlorm-inject.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vite-plugin-cedar-gqlorm-inject.d.ts","sourceRoot":"","sources":["../../src/plugins/vite-plugin-cedar-gqlorm-inject.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAA;AAQlC;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,uBAAuB,IAAI,MAAM,CAgHhD"}
@@ -0,0 +1,76 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import {
4
+ getConfig,
5
+ getPaths,
6
+ importStatementPath
7
+ } from "@cedarjs/project-config";
8
+ function cedarGqlormInjectPlugin() {
9
+ return {
10
+ name: "cedar-gqlorm-inject",
11
+ transform(code, id) {
12
+ if (!id.endsWith("/graphql.ts") && !id.endsWith("/graphql.js")) {
13
+ return null;
14
+ }
15
+ if (code.includes("__gqlorm_sdl__")) {
16
+ return null;
17
+ }
18
+ if (!code.includes("createGraphQLHandler")) {
19
+ return null;
20
+ }
21
+ let config;
22
+ try {
23
+ config = getConfig();
24
+ } catch {
25
+ return null;
26
+ }
27
+ if (!config.experimental?.gqlorm?.enabled) {
28
+ return null;
29
+ }
30
+ let paths;
31
+ try {
32
+ paths = getPaths();
33
+ } catch {
34
+ return null;
35
+ }
36
+ const backendPathWithoutExt = path.join(
37
+ paths.generated.base,
38
+ "gqlorm",
39
+ "backend"
40
+ );
41
+ if (!fs.existsSync(backendPathWithoutExt + ".ts")) {
42
+ return null;
43
+ }
44
+ const handlerPattern = /^export\s+const\s+(\w+)\s*=\s*createGraphQLHandler\s*\(/m;
45
+ const handlerMatch = handlerPattern.exec(code);
46
+ if (!handlerMatch) {
47
+ return null;
48
+ }
49
+ const handlerLineStart = code.lastIndexOf("\n", handlerMatch.index) + 1;
50
+ const relPath = importStatementPath(
51
+ path.relative(path.dirname(id), backendPathWithoutExt)
52
+ ) + ".ts";
53
+ const dbSrcPath = path.join(paths.api.src, "lib", "db");
54
+ const relDbPath = importStatementPath(path.relative(path.dirname(id), dbSrcPath)) + ".ts";
55
+ const importDb = `import { db as __gqlorm_db__ } from '${relDbPath}'`;
56
+ const importSdl = `import * as __gqlorm_sdl__ from '${relPath}'`;
57
+ const importsToAdd = `${importDb}
58
+ ${importSdl}
59
+ `;
60
+ const sdlsMutation = `Object.assign(sdls, {
61
+ __gqlorm__: {
62
+ schema: __gqlorm_sdl__.schema,
63
+ resolvers: __gqlorm_sdl__.createGqlormResolvers(__gqlorm_db__),
64
+ },
65
+ })
66
+ `;
67
+ const transformed = importsToAdd + code.slice(0, handlerLineStart) + sdlsMutation + code.slice(handlerLineStart);
68
+ return {
69
+ code: transformed
70
+ };
71
+ }
72
+ };
73
+ }
74
+ export {
75
+ cedarGqlormInjectPlugin
76
+ };