@cedarjs/vite 4.2.1-next.258 → 4.2.1-next.269
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/buildApp.d.ts.map +1 -1
- package/dist/buildApp.js +34 -28
- package/dist/cjs/buildApp.js +34 -28
- package/dist/cjs/index.js +13 -8
- package/dist/cjs/plugins/vite-plugin-cedar-context-wrapping.js +95 -0
- package/dist/cjs/plugins/vite-plugin-cedar-routes-auto-loader.js +126 -0
- package/dist/cjs/plugins/vite-plugin-cedar-universal-deploy.js +9 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -8
- package/dist/plugins/vite-plugin-cedar-context-wrapping.d.ts +44 -0
- package/dist/plugins/vite-plugin-cedar-context-wrapping.d.ts.map +1 -0
- package/dist/plugins/vite-plugin-cedar-context-wrapping.js +60 -0
- package/dist/plugins/vite-plugin-cedar-routes-auto-loader.d.ts +24 -0
- package/dist/plugins/vite-plugin-cedar-routes-auto-loader.d.ts.map +1 -0
- package/dist/plugins/vite-plugin-cedar-routes-auto-loader.js +98 -0
- package/dist/plugins/vite-plugin-cedar-universal-deploy.js +9 -1
- package/package.json +2 -2
package/dist/buildApp.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buildApp.d.ts","sourceRoot":"","sources":["../src/buildApp.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"buildApp.d.ts","sourceRoot":"","sources":["../src/buildApp.ts"],"names":[],"mappings":"AA+BA,MAAM,WAAW,oBAAoB;IACnC,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,SAAS,CAAC,EAAE,MAAM,EAAE,CAAA;IACpB,EAAE,CAAC,EAAE,OAAO,CAAA;CACb;AAED;;;;;;;;GAQG;AACH,wBAAsB,aAAa,CAAC,EAClC,OAAe,EACf,SAA0B,EAC1B,EAAU,GACX,GAAE,oBAAyB,iBA8U3B"}
|
package/dist/buildApp.js
CHANGED
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
import { findApiFiles } from "@cedarjs/internal/dist/files.js";
|
|
11
11
|
import { getConfig, getPaths, projectSideIsEsm } from "@cedarjs/project-config";
|
|
12
12
|
import { getWorkspacePackageAliases } from "./lib/workspacePackageAliases.js";
|
|
13
|
+
import { cedarContextWrappingPlugin } from "./plugins/vite-plugin-cedar-context-wrapping.js";
|
|
13
14
|
function resolveWithExtensions(id) {
|
|
14
15
|
if (fs.existsSync(id)) {
|
|
15
16
|
return id;
|
|
@@ -221,36 +222,41 @@ async function buildCedarApp({
|
|
|
221
222
|
return null;
|
|
222
223
|
}
|
|
223
224
|
});
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
return null;
|
|
237
|
-
}
|
|
238
|
-
const transformedCode = await transformWithBabel(
|
|
239
|
-
code,
|
|
240
|
-
id,
|
|
241
|
-
babelPlugins,
|
|
242
|
-
true
|
|
243
|
-
);
|
|
244
|
-
if (transformedCode?.code) {
|
|
245
|
-
return {
|
|
246
|
-
code: transformedCode.code,
|
|
247
|
-
map: transformedCode.map ?? null
|
|
248
|
-
};
|
|
249
|
-
}
|
|
225
|
+
}
|
|
226
|
+
if (workspace.includes("api")) {
|
|
227
|
+
plugins.push(
|
|
228
|
+
cedarContextWrappingPlugin({ projectIsEsm: projectSideIsEsm("api") })
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
if (babelPlugins) {
|
|
232
|
+
plugins.push({
|
|
233
|
+
name: "cedar-vite-api-babel-transform",
|
|
234
|
+
enforce: "pre",
|
|
235
|
+
async transform(code, id) {
|
|
236
|
+
if (!/\.(js|ts|tsx|jsx)$/.test(id)) {
|
|
250
237
|
return null;
|
|
251
238
|
}
|
|
252
|
-
|
|
253
|
-
|
|
239
|
+
if (id.includes("node_modules")) {
|
|
240
|
+
return null;
|
|
241
|
+
}
|
|
242
|
+
if (!normalizePath(id).startsWith(normalizePath(cedarPaths.api.base))) {
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
const transformedCode = await transformWithBabel(
|
|
246
|
+
code,
|
|
247
|
+
id,
|
|
248
|
+
babelPlugins,
|
|
249
|
+
true
|
|
250
|
+
);
|
|
251
|
+
if (transformedCode?.code) {
|
|
252
|
+
return {
|
|
253
|
+
code: transformedCode.code,
|
|
254
|
+
map: transformedCode.map ?? null
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
259
|
+
});
|
|
254
260
|
}
|
|
255
261
|
const builder = await createBuilder({
|
|
256
262
|
configFile: cedarPaths.web.viteConfig,
|
package/dist/cjs/buildApp.js
CHANGED
|
@@ -40,6 +40,7 @@ var import_babel_config = require("@cedarjs/babel-config");
|
|
|
40
40
|
var import_files = require("@cedarjs/internal/dist/files.js");
|
|
41
41
|
var import_project_config = require("@cedarjs/project-config");
|
|
42
42
|
var import_workspacePackageAliases = require("./lib/workspacePackageAliases.js");
|
|
43
|
+
var import_vite_plugin_cedar_context_wrapping = require("./plugins/vite-plugin-cedar-context-wrapping.js");
|
|
43
44
|
function resolveWithExtensions(id) {
|
|
44
45
|
if (import_node_fs.default.existsSync(id)) {
|
|
45
46
|
return id;
|
|
@@ -251,36 +252,41 @@ async function buildCedarApp({
|
|
|
251
252
|
return null;
|
|
252
253
|
}
|
|
253
254
|
});
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
return null;
|
|
267
|
-
}
|
|
268
|
-
const transformedCode = await (0, import_babel_config.transformWithBabel)(
|
|
269
|
-
code,
|
|
270
|
-
id,
|
|
271
|
-
babelPlugins,
|
|
272
|
-
true
|
|
273
|
-
);
|
|
274
|
-
if (transformedCode?.code) {
|
|
275
|
-
return {
|
|
276
|
-
code: transformedCode.code,
|
|
277
|
-
map: transformedCode.map ?? null
|
|
278
|
-
};
|
|
279
|
-
}
|
|
255
|
+
}
|
|
256
|
+
if (workspace.includes("api")) {
|
|
257
|
+
plugins.push(
|
|
258
|
+
(0, import_vite_plugin_cedar_context_wrapping.cedarContextWrappingPlugin)({ projectIsEsm: (0, import_project_config.projectSideIsEsm)("api") })
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
if (babelPlugins) {
|
|
262
|
+
plugins.push({
|
|
263
|
+
name: "cedar-vite-api-babel-transform",
|
|
264
|
+
enforce: "pre",
|
|
265
|
+
async transform(code, id) {
|
|
266
|
+
if (!/\.(js|ts|tsx|jsx)$/.test(id)) {
|
|
280
267
|
return null;
|
|
281
268
|
}
|
|
282
|
-
|
|
283
|
-
|
|
269
|
+
if (id.includes("node_modules")) {
|
|
270
|
+
return null;
|
|
271
|
+
}
|
|
272
|
+
if (!(0, import_vite2.normalizePath)(id).startsWith((0, import_vite2.normalizePath)(cedarPaths.api.base))) {
|
|
273
|
+
return null;
|
|
274
|
+
}
|
|
275
|
+
const transformedCode = await (0, import_babel_config.transformWithBabel)(
|
|
276
|
+
code,
|
|
277
|
+
id,
|
|
278
|
+
babelPlugins,
|
|
279
|
+
true
|
|
280
|
+
);
|
|
281
|
+
if (transformedCode?.code) {
|
|
282
|
+
return {
|
|
283
|
+
code: transformedCode.code,
|
|
284
|
+
map: transformedCode.map ?? null
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
return null;
|
|
288
|
+
}
|
|
289
|
+
});
|
|
284
290
|
}
|
|
285
291
|
const builder = await (0, import_vite2.createBuilder)({
|
|
286
292
|
configFile: cedarPaths.web.viteConfig,
|
package/dist/cjs/index.js
CHANGED
|
@@ -28,10 +28,12 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
28
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
29
|
var index_exports = {};
|
|
30
30
|
__export(index_exports, {
|
|
31
|
+
applyContextWrapping: () => import_vite_plugin_cedar_context_wrapping2.applyContextWrapping,
|
|
31
32
|
cedar: () => cedar,
|
|
32
33
|
cedarAutoImportsPlugin: () => import_vite_plugin_cedar_auto_import.cedarAutoImportsPlugin,
|
|
33
34
|
cedarCellTransform: () => import_vite_plugin_cedar_cell2.cedarCellTransform,
|
|
34
35
|
cedarCjsCompatPlugin: () => import_vite_plugin_cedar_cjs_compat.cedarCjsCompatPlugin,
|
|
36
|
+
cedarContextWrappingPlugin: () => import_vite_plugin_cedar_context_wrapping.cedarContextWrappingPlugin,
|
|
35
37
|
cedarEntryInjectionPlugin: () => import_vite_plugin_cedar_entry_injection2.cedarEntryInjectionPlugin,
|
|
36
38
|
cedarHtmlEnvPlugin: () => import_vite_plugin_cedar_html_env2.cedarHtmlEnvPlugin,
|
|
37
39
|
cedarImportDirPlugin: () => import_vite_plugin_cedar_import_dir.cedarImportDirPlugin,
|
|
@@ -39,6 +41,7 @@ __export(index_exports, {
|
|
|
39
41
|
cedarNodePolyfills: () => import_vite_plugin_cedar_node_polyfills2.cedarNodePolyfills,
|
|
40
42
|
cedarRemoveDevFatalErrorPage: () => import_vite_plugin_cedar_remove_dev_fatal_error_page2.cedarRemoveDevFatalErrorPage,
|
|
41
43
|
cedarRemoveFromBundle: () => import_vite_plugin_cedar_remove_from_bundle2.cedarRemoveFromBundle,
|
|
44
|
+
cedarRoutesAutoLoaderPlugin: () => import_vite_plugin_cedar_routes_auto_loader2.cedarRoutesAutoLoaderPlugin,
|
|
42
45
|
cedarSwapApolloProvider: () => import_vite_plugin_swap_apollo_provider2.cedarSwapApolloProvider,
|
|
43
46
|
cedarTransformJsAsJsx: () => import_vite_plugin_jsx_loader2.cedarTransformJsAsJsx,
|
|
44
47
|
cedarUniversalDeployPlugin: () => import_vite_plugin_cedar_universal_deploy.cedarUniversalDeployPlugin,
|
|
@@ -58,6 +61,7 @@ var import_vite_plugin_cedar_html_env = require("./plugins/vite-plugin-cedar-htm
|
|
|
58
61
|
var import_vite_plugin_cedar_node_polyfills = require("./plugins/vite-plugin-cedar-node-polyfills.js");
|
|
59
62
|
var import_vite_plugin_cedar_remove_dev_fatal_error_page = require("./plugins/vite-plugin-cedar-remove-dev-fatal-error-page.js");
|
|
60
63
|
var import_vite_plugin_cedar_remove_from_bundle = require("./plugins/vite-plugin-cedar-remove-from-bundle.js");
|
|
64
|
+
var import_vite_plugin_cedar_routes_auto_loader = require("./plugins/vite-plugin-cedar-routes-auto-loader.js");
|
|
61
65
|
var import_vite_plugin_cedar_wait_for_api_server = require("./plugins/vite-plugin-cedar-wait-for-api-server.js");
|
|
62
66
|
var import_vite_plugin_cedarjs_resolve_cedar_style_imports = require("./plugins/vite-plugin-cedarjs-resolve-cedar-style-imports.js");
|
|
63
67
|
var import_vite_plugin_jsx_loader = require("./plugins/vite-plugin-jsx-loader.js");
|
|
@@ -66,11 +70,14 @@ var import_vite_plugin_swap_apollo_provider = require("./plugins/vite-plugin-swa
|
|
|
66
70
|
var import_vite_plugin_cedar_auto_import = require("./plugins/vite-plugin-cedar-auto-import.js");
|
|
67
71
|
var import_vite_plugin_cedar_cjs_compat = require("./plugins/vite-plugin-cedar-cjs-compat.js");
|
|
68
72
|
var import_vite_plugin_cedar_cell2 = require("./plugins/vite-plugin-cedar-cell.js");
|
|
73
|
+
var import_vite_plugin_cedar_context_wrapping = require("./plugins/vite-plugin-cedar-context-wrapping.js");
|
|
74
|
+
var import_vite_plugin_cedar_context_wrapping2 = require("./plugins/vite-plugin-cedar-context-wrapping.js");
|
|
69
75
|
var import_vite_plugin_cedar_entry_injection2 = require("./plugins/vite-plugin-cedar-entry-injection.js");
|
|
70
76
|
var import_vite_plugin_cedar_html_env2 = require("./plugins/vite-plugin-cedar-html-env.js");
|
|
71
77
|
var import_vite_plugin_cedar_import_dir = require("./plugins/vite-plugin-cedar-import-dir.js");
|
|
72
78
|
var import_vite_plugin_cedar_node_polyfills2 = require("./plugins/vite-plugin-cedar-node-polyfills.js");
|
|
73
79
|
var import_vite_plugin_cedar_remove_dev_fatal_error_page2 = require("./plugins/vite-plugin-cedar-remove-dev-fatal-error-page.js");
|
|
80
|
+
var import_vite_plugin_cedar_routes_auto_loader2 = require("./plugins/vite-plugin-cedar-routes-auto-loader.js");
|
|
74
81
|
var import_vite_plugin_cedar_remove_from_bundle2 = require("./plugins/vite-plugin-cedar-remove-from-bundle.js");
|
|
75
82
|
var import_vite_plugin_cedarjs_resolve_cedar_style_imports2 = require("./plugins/vite-plugin-cedarjs-resolve-cedar-style-imports.js");
|
|
76
83
|
var import_vite_plugin_cedarjs_job_path_injector = require("./plugins/vite-plugin-cedarjs-job-path-injector.js");
|
|
@@ -86,14 +93,7 @@ function cedar({ mode } = {}) {
|
|
|
86
93
|
forVite: true
|
|
87
94
|
});
|
|
88
95
|
const babelConfig = {
|
|
89
|
-
...webSideDefaultBabelConfig
|
|
90
|
-
// For RSC we don't want to include the routes auto-loader plugin as we
|
|
91
|
-
// handle that differently in each specific RSC build stage
|
|
92
|
-
overrides: rscEnabled ? webSideDefaultBabelConfig.overrides.filter((override) => {
|
|
93
|
-
return !override.plugins?.some((plugin) => {
|
|
94
|
-
return Array.isArray(plugin) && plugin[2] === "babel-plugin-redwood-routes-auto-loader";
|
|
95
|
-
});
|
|
96
|
-
}) : webSideDefaultBabelConfig.overrides
|
|
96
|
+
...webSideDefaultBabelConfig
|
|
97
97
|
};
|
|
98
98
|
return [
|
|
99
99
|
mode === "test" && (0, import_vitest.cedarJsRouterImportTransformPlugin)(),
|
|
@@ -110,16 +110,20 @@ function cedar({ mode } = {}) {
|
|
|
110
110
|
(0, import_vite_plugin_jsx_loader.cedarTransformJsAsJsx)(),
|
|
111
111
|
(0, import_vite_plugin_cedar_remove_from_bundle.cedarRemoveFromBundle)(),
|
|
112
112
|
(0, import_vite_plugin_cedar_remove_dev_fatal_error_page.cedarRemoveDevFatalErrorPage)(),
|
|
113
|
+
// RSC handles route auto-loading differently in each build stage
|
|
114
|
+
!rscEnabled && (0, import_vite_plugin_cedar_routes_auto_loader.cedarRoutesAutoLoaderPlugin)(),
|
|
113
115
|
(0, import_plugin_react.default)({ babel: babelConfig })
|
|
114
116
|
];
|
|
115
117
|
}
|
|
116
118
|
var index_default = cedar;
|
|
117
119
|
// Annotate the CommonJS export names for ESM import in node:
|
|
118
120
|
0 && (module.exports = {
|
|
121
|
+
applyContextWrapping,
|
|
119
122
|
cedar,
|
|
120
123
|
cedarAutoImportsPlugin,
|
|
121
124
|
cedarCellTransform,
|
|
122
125
|
cedarCjsCompatPlugin,
|
|
126
|
+
cedarContextWrappingPlugin,
|
|
123
127
|
cedarEntryInjectionPlugin,
|
|
124
128
|
cedarHtmlEnvPlugin,
|
|
125
129
|
cedarImportDirPlugin,
|
|
@@ -127,6 +131,7 @@ var index_default = cedar;
|
|
|
127
131
|
cedarNodePolyfills,
|
|
128
132
|
cedarRemoveDevFatalErrorPage,
|
|
129
133
|
cedarRemoveFromBundle,
|
|
134
|
+
cedarRoutesAutoLoaderPlugin,
|
|
130
135
|
cedarSwapApolloProvider,
|
|
131
136
|
cedarTransformJsAsJsx,
|
|
132
137
|
cedarUniversalDeployPlugin,
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var vite_plugin_cedar_context_wrapping_exports = {};
|
|
30
|
+
__export(vite_plugin_cedar_context_wrapping_exports, {
|
|
31
|
+
applyContextWrapping: () => applyContextWrapping,
|
|
32
|
+
cedarContextWrappingPlugin: () => cedarContextWrappingPlugin
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(vite_plugin_cedar_context_wrapping_exports);
|
|
35
|
+
var import_node_path = __toESM(require("node:path"), 1);
|
|
36
|
+
var import_vite = require("vite");
|
|
37
|
+
var import_project_config = require("@cedarjs/project-config");
|
|
38
|
+
function cedarContextWrappingPlugin({
|
|
39
|
+
projectIsEsm = false
|
|
40
|
+
} = {}) {
|
|
41
|
+
return {
|
|
42
|
+
name: "cedar-context-wrapping",
|
|
43
|
+
transform(code, id) {
|
|
44
|
+
let paths;
|
|
45
|
+
try {
|
|
46
|
+
paths = (0, import_project_config.getPaths)();
|
|
47
|
+
} catch {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
const functionsDir = (0, import_vite.normalizePath)(import_node_path.default.join(paths.api.src, "functions"));
|
|
51
|
+
if (!(0, import_vite.normalizePath)(id).startsWith(functionsDir + "/")) {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
const wrapped = applyContextWrapping(code, { projectIsEsm });
|
|
55
|
+
return wrapped ? { code: wrapped, map: null } : null;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
function applyContextWrapping(code, { projectIsEsm = false } = {}) {
|
|
60
|
+
const handlerRe = /^export\s+(?:const|let|var)\s+handler(?:[^=]|=>)*?=(?![>=])/m;
|
|
61
|
+
const handlerMatch = handlerRe.exec(code);
|
|
62
|
+
if (!handlerMatch) {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
const afterEquals = code.slice(handlerMatch.index + handlerMatch[0].length).trimStart();
|
|
66
|
+
const isAsync = /^async(?:\s*[\(\*]|\s+function)/.test(afterEquals);
|
|
67
|
+
const storePath = projectIsEsm ? "@cedarjs/context/dist/store.js" : "@cedarjs/context/dist/store";
|
|
68
|
+
const importStatement = `import { getAsyncStoreInstance as __rw_getAsyncStoreInstance } from '${storePath}'
|
|
69
|
+
`;
|
|
70
|
+
const handlerStart = handlerMatch.index;
|
|
71
|
+
const before = code.slice(0, handlerStart);
|
|
72
|
+
const after = code.slice(handlerStart);
|
|
73
|
+
const renamed = after.replace(handlerRe, "const __rw_handler =");
|
|
74
|
+
const wrappedHandler = `
|
|
75
|
+
export const handler = ${isAsync ? "async " : ""}(__rw_event, __rw__context) => {
|
|
76
|
+
// The store will be undefined if no context isolation has been performed yet
|
|
77
|
+
const __rw_contextStore = __rw_getAsyncStoreInstance().getStore()
|
|
78
|
+
if (__rw_contextStore === undefined) {
|
|
79
|
+
return __rw_getAsyncStoreInstance().run(
|
|
80
|
+
new Map(),
|
|
81
|
+
__rw_handler,
|
|
82
|
+
__rw_event,
|
|
83
|
+
__rw__context
|
|
84
|
+
)
|
|
85
|
+
}
|
|
86
|
+
return __rw_handler(__rw_event, __rw__context)
|
|
87
|
+
}
|
|
88
|
+
`;
|
|
89
|
+
return before + importStatement + renamed + wrappedHandler;
|
|
90
|
+
}
|
|
91
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
92
|
+
0 && (module.exports = {
|
|
93
|
+
applyContextWrapping,
|
|
94
|
+
cedarContextWrappingPlugin
|
|
95
|
+
});
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var vite_plugin_cedar_routes_auto_loader_exports = {};
|
|
30
|
+
__export(vite_plugin_cedar_routes_auto_loader_exports, {
|
|
31
|
+
cedarRoutesAutoLoaderPlugin: () => cedarRoutesAutoLoaderPlugin
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(vite_plugin_cedar_routes_auto_loader_exports);
|
|
34
|
+
var import_node_fs = __toESM(require("node:fs"), 1);
|
|
35
|
+
var import_node_path = __toESM(require("node:path"), 1);
|
|
36
|
+
var import_vite = require("vite");
|
|
37
|
+
var import_project_config = require("@cedarjs/project-config");
|
|
38
|
+
function getPathRelativeToSrc(maybeAbsolutePath) {
|
|
39
|
+
const withoutExt = maybeAbsolutePath.replace(/\.[^/.]+$/, "");
|
|
40
|
+
if (withoutExt.startsWith("src/")) {
|
|
41
|
+
const basePath = import_node_path.default.join((0, import_project_config.getPaths)().web.base, withoutExt);
|
|
42
|
+
const resolved = (0, import_project_config.resolveFile)(basePath) || (0, import_project_config.resolveFile)(import_node_path.default.join(basePath, "index")) || (0, import_project_config.resolveFile)(import_node_path.default.join(basePath, import_node_path.default.basename(basePath)));
|
|
43
|
+
if (resolved) {
|
|
44
|
+
const resolvedWithoutExt = resolved.replace(/\.[^/.]+$/, "");
|
|
45
|
+
return "./" + import_node_path.default.relative((0, import_project_config.getPaths)().web.src, resolvedWithoutExt);
|
|
46
|
+
}
|
|
47
|
+
return "./" + withoutExt.slice("src/".length);
|
|
48
|
+
}
|
|
49
|
+
if (!import_node_path.default.isAbsolute(withoutExt)) {
|
|
50
|
+
return withoutExt;
|
|
51
|
+
}
|
|
52
|
+
return `./${import_node_path.default.relative((0, import_project_config.getPaths)().web.src, withoutExt)}`;
|
|
53
|
+
}
|
|
54
|
+
function withRelativeImports(page) {
|
|
55
|
+
return {
|
|
56
|
+
...page,
|
|
57
|
+
relativeImport: (0, import_project_config.ensurePosixPath)(getPathRelativeToSrc(page.importPath))
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function cedarRoutesAutoLoaderPlugin() {
|
|
61
|
+
const routesFileId = (0, import_vite.normalizePath)((0, import_project_config.getPaths)().web.routes);
|
|
62
|
+
const initialPages = (0, import_project_config.processPagesDir)().map(withRelativeImports);
|
|
63
|
+
const duplicatePageImportNames = /* @__PURE__ */ new Set();
|
|
64
|
+
const sortedPageImportNames = initialPages.map((page) => page.importName).sort();
|
|
65
|
+
for (let i = 0; i < sortedPageImportNames.length - 1; i++) {
|
|
66
|
+
if (sortedPageImportNames[i + 1] === sortedPageImportNames[i]) {
|
|
67
|
+
duplicatePageImportNames.add(sortedPageImportNames[i]);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (duplicatePageImportNames.size > 0) {
|
|
71
|
+
const pageNames = Array.from(duplicatePageImportNames).map((name) => `'${name}'`).join(", ");
|
|
72
|
+
throw new Error(
|
|
73
|
+
`Unable to find only a single file ending in 'Page.{js,jsx,ts,tsx}' in the following page directories: ${pageNames}`
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
return {
|
|
77
|
+
name: "cedar-routes-auto-loader",
|
|
78
|
+
transform(code, id) {
|
|
79
|
+
if ((0, import_vite.normalizePath)(id) !== routesFileId) {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
let pages = (0, import_project_config.processPagesDir)().map(withRelativeImports);
|
|
83
|
+
const importRe = /^import\s+\w+\s+from\s+['"]([^'"]+)['"]/gm;
|
|
84
|
+
const appPath = (0, import_project_config.resolveFile)(import_node_path.default.join((0, import_project_config.getPaths)().web.src, "App"));
|
|
85
|
+
if (appPath) {
|
|
86
|
+
const appSource = import_node_fs.default.readFileSync(appPath, "utf8");
|
|
87
|
+
let appMatch;
|
|
88
|
+
while ((appMatch = importRe.exec(appSource)) !== null) {
|
|
89
|
+
const rel = (0, import_project_config.ensurePosixPath)(
|
|
90
|
+
getPathRelativeToSrc((0, import_project_config.importStatementPath)(appMatch[1]))
|
|
91
|
+
);
|
|
92
|
+
pages = pages.filter((page) => page.relativeImport !== rel);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
importRe.lastIndex = 0;
|
|
96
|
+
let routesMatch;
|
|
97
|
+
while ((routesMatch = importRe.exec(code)) !== null) {
|
|
98
|
+
const rel = (0, import_project_config.ensurePosixPath)(
|
|
99
|
+
getPathRelativeToSrc((0, import_project_config.importStatementPath)(routesMatch[1]))
|
|
100
|
+
);
|
|
101
|
+
pages = pages.filter((page) => page.relativeImport !== rel);
|
|
102
|
+
}
|
|
103
|
+
if (pages.length === 0) {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
const lines = [`import { lazy } from 'react'`];
|
|
107
|
+
for (const { importName, relativeImport } of pages) {
|
|
108
|
+
lines.push(
|
|
109
|
+
`const ${importName} = {`,
|
|
110
|
+
` name: "${importName}",`,
|
|
111
|
+
` prerenderLoader: (name) => ({ default: globalThis.__REDWOOD__PRERENDER_PAGES[name] }),`,
|
|
112
|
+
` LazyComponent: lazy(() => import("${relativeImport}")),`,
|
|
113
|
+
`}`
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
return {
|
|
117
|
+
code: lines.join("\n") + "\n\n" + code,
|
|
118
|
+
map: null
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
124
|
+
0 && (module.exports = {
|
|
125
|
+
cedarRoutesAutoLoaderPlugin
|
|
126
|
+
});
|
|
@@ -327,7 +327,15 @@ async function generateFunctionModule(distPath) {
|
|
|
327
327
|
export default {
|
|
328
328
|
async fetch(request) {
|
|
329
329
|
const ctx = await buildCedarContext(request);
|
|
330
|
-
|
|
330
|
+
// Wrap the handler in an AsyncLocalStorage run so the global
|
|
331
|
+
// @cedarjs/context is available inside it (and isolated per request).
|
|
332
|
+
// Mirrors the GraphQL module above and the cedarContextWrappingPlugin
|
|
333
|
+
// used for non-UD builds.
|
|
334
|
+
const { getAsyncStoreInstance } = await import(
|
|
335
|
+
'@cedarjs/context/dist/store'
|
|
336
|
+
);
|
|
337
|
+
const store = getAsyncStoreInstance();
|
|
338
|
+
return store.run(new Map(), () => _handler(request, ctx));
|
|
331
339
|
}
|
|
332
340
|
};
|
|
333
341
|
`;
|
package/dist/index.d.ts
CHANGED
|
@@ -2,11 +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
7
|
export { cedarEntryInjectionPlugin } from './plugins/vite-plugin-cedar-entry-injection.js';
|
|
6
8
|
export { cedarHtmlEnvPlugin } from './plugins/vite-plugin-cedar-html-env.js';
|
|
7
9
|
export { cedarImportDirPlugin } from './plugins/vite-plugin-cedar-import-dir.js';
|
|
8
10
|
export { cedarNodePolyfills } from './plugins/vite-plugin-cedar-node-polyfills.js';
|
|
9
11
|
export { cedarRemoveDevFatalErrorPage } from './plugins/vite-plugin-cedar-remove-dev-fatal-error-page.js';
|
|
12
|
+
export { cedarRoutesAutoLoaderPlugin } from './plugins/vite-plugin-cedar-routes-auto-loader.js';
|
|
10
13
|
export { cedarRemoveFromBundle } from './plugins/vite-plugin-cedar-remove-from-bundle.js';
|
|
11
14
|
export { cedarjsResolveCedarStyleImportsPlugin } from './plugins/vite-plugin-cedarjs-resolve-cedar-style-imports.js';
|
|
12
15
|
export { cedarjsJobPathInjectorPlugin } from './plugins/vite-plugin-cedarjs-job-path-injector.js';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -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;
|
|
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"}
|
package/dist/index.js
CHANGED
|
@@ -12,6 +12,7 @@ import { cedarHtmlEnvPlugin } from "./plugins/vite-plugin-cedar-html-env.js";
|
|
|
12
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
|
+
import { cedarRoutesAutoLoaderPlugin } from "./plugins/vite-plugin-cedar-routes-auto-loader.js";
|
|
15
16
|
import { cedarWaitForApiServer } from "./plugins/vite-plugin-cedar-wait-for-api-server.js";
|
|
16
17
|
import { cedarjsResolveCedarStyleImportsPlugin } from "./plugins/vite-plugin-cedarjs-resolve-cedar-style-imports.js";
|
|
17
18
|
import { cedarTransformJsAsJsx } from "./plugins/vite-plugin-jsx-loader.js";
|
|
@@ -20,11 +21,14 @@ import { cedarSwapApolloProvider } from "./plugins/vite-plugin-swap-apollo-provi
|
|
|
20
21
|
import { cedarAutoImportsPlugin } from "./plugins/vite-plugin-cedar-auto-import.js";
|
|
21
22
|
import { cedarCjsCompatPlugin } from "./plugins/vite-plugin-cedar-cjs-compat.js";
|
|
22
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";
|
|
23
26
|
import { cedarEntryInjectionPlugin as cedarEntryInjectionPlugin2 } from "./plugins/vite-plugin-cedar-entry-injection.js";
|
|
24
27
|
import { cedarHtmlEnvPlugin as cedarHtmlEnvPlugin2 } from "./plugins/vite-plugin-cedar-html-env.js";
|
|
25
28
|
import { cedarImportDirPlugin } from "./plugins/vite-plugin-cedar-import-dir.js";
|
|
26
29
|
import { cedarNodePolyfills as cedarNodePolyfills2 } from "./plugins/vite-plugin-cedar-node-polyfills.js";
|
|
27
30
|
import { cedarRemoveDevFatalErrorPage as cedarRemoveDevFatalErrorPage2 } from "./plugins/vite-plugin-cedar-remove-dev-fatal-error-page.js";
|
|
31
|
+
import { cedarRoutesAutoLoaderPlugin as cedarRoutesAutoLoaderPlugin2 } from "./plugins/vite-plugin-cedar-routes-auto-loader.js";
|
|
28
32
|
import { cedarRemoveFromBundle as cedarRemoveFromBundle2 } from "./plugins/vite-plugin-cedar-remove-from-bundle.js";
|
|
29
33
|
import { cedarjsResolveCedarStyleImportsPlugin as cedarjsResolveCedarStyleImportsPlugin2 } from "./plugins/vite-plugin-cedarjs-resolve-cedar-style-imports.js";
|
|
30
34
|
import { cedarjsJobPathInjectorPlugin } from "./plugins/vite-plugin-cedarjs-job-path-injector.js";
|
|
@@ -40,14 +44,7 @@ function cedar({ mode } = {}) {
|
|
|
40
44
|
forVite: true
|
|
41
45
|
});
|
|
42
46
|
const babelConfig = {
|
|
43
|
-
...webSideDefaultBabelConfig
|
|
44
|
-
// For RSC we don't want to include the routes auto-loader plugin as we
|
|
45
|
-
// handle that differently in each specific RSC build stage
|
|
46
|
-
overrides: rscEnabled ? webSideDefaultBabelConfig.overrides.filter((override) => {
|
|
47
|
-
return !override.plugins?.some((plugin) => {
|
|
48
|
-
return Array.isArray(plugin) && plugin[2] === "babel-plugin-redwood-routes-auto-loader";
|
|
49
|
-
});
|
|
50
|
-
}) : webSideDefaultBabelConfig.overrides
|
|
47
|
+
...webSideDefaultBabelConfig
|
|
51
48
|
};
|
|
52
49
|
return [
|
|
53
50
|
mode === "test" && cedarJsRouterImportTransformPlugin(),
|
|
@@ -64,15 +61,19 @@ function cedar({ mode } = {}) {
|
|
|
64
61
|
cedarTransformJsAsJsx(),
|
|
65
62
|
cedarRemoveFromBundle(),
|
|
66
63
|
cedarRemoveDevFatalErrorPage(),
|
|
64
|
+
// RSC handles route auto-loading differently in each build stage
|
|
65
|
+
!rscEnabled && cedarRoutesAutoLoaderPlugin(),
|
|
67
66
|
react({ babel: babelConfig })
|
|
68
67
|
];
|
|
69
68
|
}
|
|
70
69
|
var index_default = cedar;
|
|
71
70
|
export {
|
|
71
|
+
applyContextWrapping,
|
|
72
72
|
cedar,
|
|
73
73
|
cedarAutoImportsPlugin,
|
|
74
74
|
cedarCellTransform2 as cedarCellTransform,
|
|
75
75
|
cedarCjsCompatPlugin,
|
|
76
|
+
cedarContextWrappingPlugin,
|
|
76
77
|
cedarEntryInjectionPlugin2 as cedarEntryInjectionPlugin,
|
|
77
78
|
cedarHtmlEnvPlugin2 as cedarHtmlEnvPlugin,
|
|
78
79
|
cedarImportDirPlugin,
|
|
@@ -80,6 +81,7 @@ export {
|
|
|
80
81
|
cedarNodePolyfills2 as cedarNodePolyfills,
|
|
81
82
|
cedarRemoveDevFatalErrorPage2 as cedarRemoveDevFatalErrorPage,
|
|
82
83
|
cedarRemoveFromBundle2 as cedarRemoveFromBundle,
|
|
84
|
+
cedarRoutesAutoLoaderPlugin2 as cedarRoutesAutoLoaderPlugin,
|
|
83
85
|
cedarSwapApolloProvider2 as cedarSwapApolloProvider,
|
|
84
86
|
cedarTransformJsAsJsx2 as cedarTransformJsAsJsx,
|
|
85
87
|
cedarUniversalDeployPlugin,
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { Plugin } from 'vite';
|
|
2
|
+
/**
|
|
3
|
+
* Vite plugin that wraps user API functions to ensure context isolation has
|
|
4
|
+
* been performed. This should already be done at the request level but in
|
|
5
|
+
* serverless environments like Netlify we need to do this at the function
|
|
6
|
+
* level as a safeguard.
|
|
7
|
+
*
|
|
8
|
+
* For each file in `api/src/functions/` that exports a `handler`, this plugin:
|
|
9
|
+
*
|
|
10
|
+
* 1. Adds an import at the top of the file:
|
|
11
|
+
* import { getAsyncStoreInstance as __rw_getAsyncStoreInstance } from '@cedarjs/context/dist/store'
|
|
12
|
+
*
|
|
13
|
+
* 2. Renames the original handler:
|
|
14
|
+
* const __rw_handler = <original handler value>
|
|
15
|
+
*
|
|
16
|
+
* 3. Replaces the handler export with a wrapper that checks context isolation:
|
|
17
|
+
* export const handler = (__rw_event, __rw__context) => {
|
|
18
|
+
* const __rw_contextStore = __rw_getAsyncStoreInstance().getStore()
|
|
19
|
+
* if (__rw_contextStore === undefined) {
|
|
20
|
+
* return __rw_getAsyncStoreInstance().run(new Map(), __rw_handler, __rw_event, __rw__context)
|
|
21
|
+
* }
|
|
22
|
+
* return __rw_handler(__rw_event, __rw__context)
|
|
23
|
+
* }
|
|
24
|
+
*
|
|
25
|
+
* This replaces `babel-plugin-redwood-context-wrapping` for Vite builds.
|
|
26
|
+
* The babel plugin is still used for Jest and prerender.
|
|
27
|
+
*/
|
|
28
|
+
export declare function cedarContextWrappingPlugin({ projectIsEsm, }?: {
|
|
29
|
+
projectIsEsm?: boolean;
|
|
30
|
+
}): Plugin;
|
|
31
|
+
/**
|
|
32
|
+
* Wraps the `handler` export of an API function file with an async context
|
|
33
|
+
* store guard. Returns the transformed code, or `null` if the file does not
|
|
34
|
+
* export a `handler`.
|
|
35
|
+
*
|
|
36
|
+
* This is the context-isolation safeguard that used to be performed by
|
|
37
|
+
* `babel-plugin-redwood-context-wrapping`. It is exported as a standalone
|
|
38
|
+
* function so it can be applied from build pipelines that don't go through
|
|
39
|
+
* Vite's plugin pipeline (e.g. the legacy esbuild API build).
|
|
40
|
+
*/
|
|
41
|
+
export declare function applyContextWrapping(code: string, { projectIsEsm }?: {
|
|
42
|
+
projectIsEsm?: boolean;
|
|
43
|
+
}): string | null;
|
|
44
|
+
//# sourceMappingURL=vite-plugin-cedar-context-wrapping.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vite-plugin-cedar-context-wrapping.d.ts","sourceRoot":"","sources":["../../src/plugins/vite-plugin-cedar-context-wrapping.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAA;AAKlC;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,0BAA0B,CAAC,EACzC,YAAoB,GACrB,GAAE;IACD,YAAY,CAAC,EAAE,OAAO,CAAA;CAClB,GAAG,MAAM,CAuBd;AAED;;;;;;;;;GASG;AACH,wBAAgB,oBAAoB,CAClC,IAAI,EAAE,MAAM,EACZ,EAAE,YAAoB,EAAE,GAAE;IAAE,YAAY,CAAC,EAAE,OAAO,CAAA;CAAO,GACxD,MAAM,GAAG,IAAI,CAsDf"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { normalizePath } from "vite";
|
|
3
|
+
import { getPaths } from "@cedarjs/project-config";
|
|
4
|
+
function cedarContextWrappingPlugin({
|
|
5
|
+
projectIsEsm = false
|
|
6
|
+
} = {}) {
|
|
7
|
+
return {
|
|
8
|
+
name: "cedar-context-wrapping",
|
|
9
|
+
transform(code, id) {
|
|
10
|
+
let paths;
|
|
11
|
+
try {
|
|
12
|
+
paths = getPaths();
|
|
13
|
+
} catch {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
const functionsDir = normalizePath(path.join(paths.api.src, "functions"));
|
|
17
|
+
if (!normalizePath(id).startsWith(functionsDir + "/")) {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
const wrapped = applyContextWrapping(code, { projectIsEsm });
|
|
21
|
+
return wrapped ? { code: wrapped, map: null } : null;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function applyContextWrapping(code, { projectIsEsm = false } = {}) {
|
|
26
|
+
const handlerRe = /^export\s+(?:const|let|var)\s+handler(?:[^=]|=>)*?=(?![>=])/m;
|
|
27
|
+
const handlerMatch = handlerRe.exec(code);
|
|
28
|
+
if (!handlerMatch) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
const afterEquals = code.slice(handlerMatch.index + handlerMatch[0].length).trimStart();
|
|
32
|
+
const isAsync = /^async(?:\s*[\(\*]|\s+function)/.test(afterEquals);
|
|
33
|
+
const storePath = projectIsEsm ? "@cedarjs/context/dist/store.js" : "@cedarjs/context/dist/store";
|
|
34
|
+
const importStatement = `import { getAsyncStoreInstance as __rw_getAsyncStoreInstance } from '${storePath}'
|
|
35
|
+
`;
|
|
36
|
+
const handlerStart = handlerMatch.index;
|
|
37
|
+
const before = code.slice(0, handlerStart);
|
|
38
|
+
const after = code.slice(handlerStart);
|
|
39
|
+
const renamed = after.replace(handlerRe, "const __rw_handler =");
|
|
40
|
+
const wrappedHandler = `
|
|
41
|
+
export const handler = ${isAsync ? "async " : ""}(__rw_event, __rw__context) => {
|
|
42
|
+
// The store will be undefined if no context isolation has been performed yet
|
|
43
|
+
const __rw_contextStore = __rw_getAsyncStoreInstance().getStore()
|
|
44
|
+
if (__rw_contextStore === undefined) {
|
|
45
|
+
return __rw_getAsyncStoreInstance().run(
|
|
46
|
+
new Map(),
|
|
47
|
+
__rw_handler,
|
|
48
|
+
__rw_event,
|
|
49
|
+
__rw__context
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
return __rw_handler(__rw_event, __rw__context)
|
|
53
|
+
}
|
|
54
|
+
`;
|
|
55
|
+
return before + importStatement + renamed + wrappedHandler;
|
|
56
|
+
}
|
|
57
|
+
export {
|
|
58
|
+
applyContextWrapping,
|
|
59
|
+
cedarContextWrappingPlugin
|
|
60
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Plugin } from 'vite';
|
|
2
|
+
/**
|
|
3
|
+
* Vite plugin to auto-load page components into the Routes file.
|
|
4
|
+
*
|
|
5
|
+
* For each page found in `web/src/pages` that is not already explicitly
|
|
6
|
+
* imported in Routes.tsx, this plugin prepends a lazy-loaded declaration:
|
|
7
|
+
*
|
|
8
|
+
* ```js
|
|
9
|
+
* const PageName = {
|
|
10
|
+
* name: "PageName",
|
|
11
|
+
* prerenderLoader: (name) => ({ default: globalThis.__REDWOOD__PRERENDER_PAGES[name] }),
|
|
12
|
+
* LazyComponent: lazy(() => import("./pages/PageName/PageName")),
|
|
13
|
+
* }
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* Pages already imported by App.tsx are also excluded to avoid Vite's
|
|
17
|
+
* "dynamically imported by Routes.tsx but also statically imported by App.tsx"
|
|
18
|
+
* warning.
|
|
19
|
+
*
|
|
20
|
+
* This replaces `babel-plugin-redwood-routes-auto-loader` for Vite builds.
|
|
21
|
+
* The babel plugin is still used for Jest and prerender.
|
|
22
|
+
*/
|
|
23
|
+
export declare function cedarRoutesAutoLoaderPlugin(): Plugin;
|
|
24
|
+
//# sourceMappingURL=vite-plugin-cedar-routes-auto-loader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vite-plugin-cedar-routes-auto-loader.d.ts","sourceRoot":"","sources":["../../src/plugins/vite-plugin-cedar-routes-auto-loader.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAA;AAqDlC;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,2BAA2B,IAAI,MAAM,CAsFpD"}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { normalizePath } from "vite";
|
|
4
|
+
import {
|
|
5
|
+
ensurePosixPath,
|
|
6
|
+
getPaths,
|
|
7
|
+
importStatementPath,
|
|
8
|
+
processPagesDir,
|
|
9
|
+
resolveFile
|
|
10
|
+
} from "@cedarjs/project-config";
|
|
11
|
+
function getPathRelativeToSrc(maybeAbsolutePath) {
|
|
12
|
+
const withoutExt = maybeAbsolutePath.replace(/\.[^/.]+$/, "");
|
|
13
|
+
if (withoutExt.startsWith("src/")) {
|
|
14
|
+
const basePath = path.join(getPaths().web.base, withoutExt);
|
|
15
|
+
const resolved = resolveFile(basePath) || resolveFile(path.join(basePath, "index")) || resolveFile(path.join(basePath, path.basename(basePath)));
|
|
16
|
+
if (resolved) {
|
|
17
|
+
const resolvedWithoutExt = resolved.replace(/\.[^/.]+$/, "");
|
|
18
|
+
return "./" + path.relative(getPaths().web.src, resolvedWithoutExt);
|
|
19
|
+
}
|
|
20
|
+
return "./" + withoutExt.slice("src/".length);
|
|
21
|
+
}
|
|
22
|
+
if (!path.isAbsolute(withoutExt)) {
|
|
23
|
+
return withoutExt;
|
|
24
|
+
}
|
|
25
|
+
return `./${path.relative(getPaths().web.src, withoutExt)}`;
|
|
26
|
+
}
|
|
27
|
+
function withRelativeImports(page) {
|
|
28
|
+
return {
|
|
29
|
+
...page,
|
|
30
|
+
relativeImport: ensurePosixPath(getPathRelativeToSrc(page.importPath))
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function cedarRoutesAutoLoaderPlugin() {
|
|
34
|
+
const routesFileId = normalizePath(getPaths().web.routes);
|
|
35
|
+
const initialPages = processPagesDir().map(withRelativeImports);
|
|
36
|
+
const duplicatePageImportNames = /* @__PURE__ */ new Set();
|
|
37
|
+
const sortedPageImportNames = initialPages.map((page) => page.importName).sort();
|
|
38
|
+
for (let i = 0; i < sortedPageImportNames.length - 1; i++) {
|
|
39
|
+
if (sortedPageImportNames[i + 1] === sortedPageImportNames[i]) {
|
|
40
|
+
duplicatePageImportNames.add(sortedPageImportNames[i]);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (duplicatePageImportNames.size > 0) {
|
|
44
|
+
const pageNames = Array.from(duplicatePageImportNames).map((name) => `'${name}'`).join(", ");
|
|
45
|
+
throw new Error(
|
|
46
|
+
`Unable to find only a single file ending in 'Page.{js,jsx,ts,tsx}' in the following page directories: ${pageNames}`
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
name: "cedar-routes-auto-loader",
|
|
51
|
+
transform(code, id) {
|
|
52
|
+
if (normalizePath(id) !== routesFileId) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
let pages = processPagesDir().map(withRelativeImports);
|
|
56
|
+
const importRe = /^import\s+\w+\s+from\s+['"]([^'"]+)['"]/gm;
|
|
57
|
+
const appPath = resolveFile(path.join(getPaths().web.src, "App"));
|
|
58
|
+
if (appPath) {
|
|
59
|
+
const appSource = fs.readFileSync(appPath, "utf8");
|
|
60
|
+
let appMatch;
|
|
61
|
+
while ((appMatch = importRe.exec(appSource)) !== null) {
|
|
62
|
+
const rel = ensurePosixPath(
|
|
63
|
+
getPathRelativeToSrc(importStatementPath(appMatch[1]))
|
|
64
|
+
);
|
|
65
|
+
pages = pages.filter((page) => page.relativeImport !== rel);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
importRe.lastIndex = 0;
|
|
69
|
+
let routesMatch;
|
|
70
|
+
while ((routesMatch = importRe.exec(code)) !== null) {
|
|
71
|
+
const rel = ensurePosixPath(
|
|
72
|
+
getPathRelativeToSrc(importStatementPath(routesMatch[1]))
|
|
73
|
+
);
|
|
74
|
+
pages = pages.filter((page) => page.relativeImport !== rel);
|
|
75
|
+
}
|
|
76
|
+
if (pages.length === 0) {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
const lines = [`import { lazy } from 'react'`];
|
|
80
|
+
for (const { importName, relativeImport } of pages) {
|
|
81
|
+
lines.push(
|
|
82
|
+
`const ${importName} = {`,
|
|
83
|
+
` name: "${importName}",`,
|
|
84
|
+
` prerenderLoader: (name) => ({ default: globalThis.__REDWOOD__PRERENDER_PAGES[name] }),`,
|
|
85
|
+
` LazyComponent: lazy(() => import("${relativeImport}")),`,
|
|
86
|
+
`}`
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
return {
|
|
90
|
+
code: lines.join("\n") + "\n\n" + code,
|
|
91
|
+
map: null
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
export {
|
|
97
|
+
cedarRoutesAutoLoaderPlugin
|
|
98
|
+
};
|
|
@@ -294,7 +294,15 @@ async function generateFunctionModule(distPath) {
|
|
|
294
294
|
export default {
|
|
295
295
|
async fetch(request) {
|
|
296
296
|
const ctx = await buildCedarContext(request);
|
|
297
|
-
|
|
297
|
+
// Wrap the handler in an AsyncLocalStorage run so the global
|
|
298
|
+
// @cedarjs/context is available inside it (and isolated per request).
|
|
299
|
+
// Mirrors the GraphQL module above and the cedarContextWrappingPlugin
|
|
300
|
+
// used for non-UD builds.
|
|
301
|
+
const { getAsyncStoreInstance } = await import(
|
|
302
|
+
'@cedarjs/context/dist/store'
|
|
303
|
+
);
|
|
304
|
+
const store = getAsyncStoreInstance();
|
|
305
|
+
return store.run(new Map(), () => _handler(request, ctx));
|
|
298
306
|
}
|
|
299
307
|
};
|
|
300
308
|
`;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cedarjs/vite",
|
|
3
|
-
"version": "4.2.1-next.
|
|
3
|
+
"version": "4.2.1-next.269",
|
|
4
4
|
"description": "Vite configuration package for CedarJS",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
"inject"
|
|
50
50
|
],
|
|
51
51
|
"scripts": {
|
|
52
|
-
"build": "
|
|
52
|
+
"build": "node ./build.ts && yarn build:types",
|
|
53
53
|
"build:pack": "yarn pack -o cedarjs-vite.tgz",
|
|
54
54
|
"build:types": "tsc --build --verbose ./tsconfig.build.json",
|
|
55
55
|
"check:attw": "tsx ./attw.ts",
|