@cedarjs/vite 4.1.1-next.1 → 4.1.1-next.55
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/apiDevMiddleware.d.ts +15 -0
- package/dist/apiDevMiddleware.d.ts.map +1 -0
- package/dist/{apiDevServer.js → apiDevMiddleware.js} +95 -107
- package/dist/build/build.d.ts +1 -1
- package/dist/build/build.d.ts.map +1 -1
- package/dist/build/build.js +2 -0
- package/dist/buildApp.d.ts +15 -0
- package/dist/buildApp.d.ts.map +1 -0
- package/dist/buildApp.js +150 -0
- package/dist/buildUDApiServer.d.ts +7 -5
- package/dist/buildUDApiServer.d.ts.map +1 -1
- package/dist/buildUDApiServer.js +8 -26
- package/dist/cedar-unified-dev.js +59 -5
- package/dist/cjs/{apiDevServer.js → apiDevMiddleware.js} +102 -110
- package/dist/cjs/build/build.js +3 -0
- package/dist/cjs/buildApp.js +181 -0
- package/dist/cjs/buildUDApiServer.js +8 -26
- package/dist/cjs/cedar-unified-dev.js +59 -5
- package/dist/cjs/index.js +3 -3
- package/dist/cjs/plugins/vite-plugin-cedar-cjs-compat.js +341 -0
- package/dist/cjs/plugins/vite-plugin-cedar-universal-deploy.js +114 -19
- package/dist/cjs/plugins/vite-plugin-cedar-wait-for-api-server.js +2 -1
- package/dist/cjs/ud-handlers/catch-all.js +65 -0
- package/dist/cjs/ud-handlers/function.js +47 -0
- package/dist/cjs/ud-handlers/graphql.js +59 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/plugins/vite-plugin-cedar-cjs-compat.d.ts +23 -0
- package/dist/plugins/vite-plugin-cedar-cjs-compat.d.ts.map +1 -0
- package/dist/plugins/vite-plugin-cedar-cjs-compat.js +307 -0
- package/dist/plugins/vite-plugin-cedar-universal-deploy.d.ts.map +1 -1
- package/dist/plugins/vite-plugin-cedar-universal-deploy.js +105 -20
- package/dist/plugins/vite-plugin-cedar-wait-for-api-server.d.ts.map +1 -1
- package/dist/plugins/vite-plugin-cedar-wait-for-api-server.js +2 -1
- package/dist/ud-handlers/catch-all.d.ts +15 -0
- package/dist/ud-handlers/catch-all.d.ts.map +1 -0
- package/dist/ud-handlers/catch-all.js +41 -0
- package/dist/ud-handlers/function.d.ts +5 -0
- package/dist/ud-handlers/function.d.ts.map +1 -0
- package/dist/ud-handlers/function.js +23 -0
- package/dist/ud-handlers/graphql.d.ts +7 -0
- package/dist/ud-handlers/graphql.d.ts.map +1 -0
- package/dist/ud-handlers/graphql.js +35 -0
- package/package.json +52 -23
- package/LICENSE +0 -21
- package/dist/apiDevServer.d.ts +0 -18
- package/dist/apiDevServer.d.ts.map +0 -1
- package/dist/cjs/plugins/vite-plugin-cedar-dev-dispatcher.js +0 -223
- package/dist/plugins/vite-plugin-cedar-dev-dispatcher.d.ts +0 -3
- package/dist/plugins/vite-plugin-cedar-dev-dispatcher.d.ts.map +0 -1
- package/dist/plugins/vite-plugin-cedar-dev-dispatcher.js +0 -189
|
@@ -22,11 +22,20 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
22
22
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
23
23
|
mod
|
|
24
24
|
));
|
|
25
|
+
var import_server = require("@whatwg-node/server");
|
|
25
26
|
var import_vite = require("vite");
|
|
26
27
|
var import_yargs_parser = __toESM(require("yargs-parser"), 1);
|
|
27
28
|
var import_project_config = require("@cedarjs/project-config");
|
|
28
|
-
var
|
|
29
|
+
var import_apiDevMiddleware = require("./apiDevMiddleware.js");
|
|
30
|
+
function isViteInternalRequest(url) {
|
|
31
|
+
const pathname = url.split("?")[0];
|
|
32
|
+
return pathname.startsWith("/@") || pathname.startsWith("/__vite") || pathname.startsWith("/__hmr");
|
|
33
|
+
}
|
|
34
|
+
function isApiRequest(url, apiUrl, apiGqlUrl) {
|
|
35
|
+
return url === apiUrl || url.startsWith(apiUrl + "/") || url.startsWith(apiUrl + "?") || url === apiGqlUrl || url.startsWith(apiGqlUrl + "/") || url.startsWith(apiGqlUrl + "?");
|
|
36
|
+
}
|
|
29
37
|
const startUnifiedDevServer = async () => {
|
|
38
|
+
process.env.__CEDAR_UNIFIED_DEV = "true";
|
|
30
39
|
const rwPaths = (0, import_project_config.getPaths)();
|
|
31
40
|
const cedarConfig = (0, import_project_config.getConfig)();
|
|
32
41
|
const configFile = rwPaths.web.viteConfig;
|
|
@@ -37,7 +46,7 @@ const startUnifiedDevServer = async () => {
|
|
|
37
46
|
force: forceOptimize,
|
|
38
47
|
debug,
|
|
39
48
|
port: portArg,
|
|
40
|
-
apiPort:
|
|
49
|
+
apiPort: _apiPortArg,
|
|
41
50
|
_: _positional,
|
|
42
51
|
...serverArgs
|
|
43
52
|
} = (0, import_yargs_parser.default)(process.argv.slice(2), {
|
|
@@ -45,8 +54,8 @@ const startUnifiedDevServer = async () => {
|
|
|
45
54
|
number: ["port", "apiPort"]
|
|
46
55
|
});
|
|
47
56
|
const webPort = portArg ?? cedarConfig.web.port ?? 8910;
|
|
48
|
-
const
|
|
49
|
-
const
|
|
57
|
+
const { close: closeApi, handler: apiHandler } = await (0, import_apiDevMiddleware.startApiDevMiddleware)();
|
|
58
|
+
const apiAdapter = (0, import_server.createServerAdapter)(apiHandler);
|
|
50
59
|
const devServer = await (0, import_vite.createServer)({
|
|
51
60
|
configFile,
|
|
52
61
|
// env file is handled by Cedar's plugins
|
|
@@ -59,7 +68,52 @@ const startUnifiedDevServer = async () => {
|
|
|
59
68
|
port: webPort,
|
|
60
69
|
...serverArgs
|
|
61
70
|
},
|
|
62
|
-
logLevel: debug ? "info" : void 0
|
|
71
|
+
logLevel: debug ? "info" : void 0,
|
|
72
|
+
plugins: [
|
|
73
|
+
{
|
|
74
|
+
name: "cedar-api-middleware",
|
|
75
|
+
apply: "serve",
|
|
76
|
+
configureServer(server) {
|
|
77
|
+
const apiUrl = cedarConfig.web.apiUrl.replace(/\/$/, "");
|
|
78
|
+
const apiGqlUrl = cedarConfig.web.apiGraphQLUrl ?? apiUrl + "/graphql";
|
|
79
|
+
server.middlewares.use(
|
|
80
|
+
async (req, res, next) => {
|
|
81
|
+
const url = req.url ?? "/";
|
|
82
|
+
if (isViteInternalRequest(url)) {
|
|
83
|
+
return next();
|
|
84
|
+
}
|
|
85
|
+
if (!isApiRequest(url, apiUrl, apiGqlUrl)) {
|
|
86
|
+
return next();
|
|
87
|
+
}
|
|
88
|
+
try {
|
|
89
|
+
await apiAdapter(req, res);
|
|
90
|
+
} catch (err) {
|
|
91
|
+
console.error(
|
|
92
|
+
"[cedar-api-middleware] Error handling API request:",
|
|
93
|
+
err
|
|
94
|
+
);
|
|
95
|
+
if (!res.headersSent) {
|
|
96
|
+
res.writeHead(500, { "Content-Type": "application/json" });
|
|
97
|
+
}
|
|
98
|
+
res.end(
|
|
99
|
+
JSON.stringify(
|
|
100
|
+
{
|
|
101
|
+
errors: [
|
|
102
|
+
{
|
|
103
|
+
message: err instanceof Error ? err.message : "Internal Server Error"
|
|
104
|
+
}
|
|
105
|
+
]
|
|
106
|
+
},
|
|
107
|
+
null,
|
|
108
|
+
2
|
|
109
|
+
)
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
]
|
|
63
117
|
});
|
|
64
118
|
await devServer.listen();
|
|
65
119
|
process.stdin.on("data", async (data) => {
|
package/dist/cjs/index.js
CHANGED
|
@@ -31,7 +31,7 @@ __export(index_exports, {
|
|
|
31
31
|
cedar: () => cedar,
|
|
32
32
|
cedarAutoImportsPlugin: () => import_vite_plugin_cedar_auto_import.cedarAutoImportsPlugin,
|
|
33
33
|
cedarCellTransform: () => import_vite_plugin_cedar_cell2.cedarCellTransform,
|
|
34
|
-
|
|
34
|
+
cedarCjsCompatPlugin: () => import_vite_plugin_cedar_cjs_compat.cedarCjsCompatPlugin,
|
|
35
35
|
cedarEntryInjectionPlugin: () => import_vite_plugin_cedar_entry_injection2.cedarEntryInjectionPlugin,
|
|
36
36
|
cedarHtmlEnvPlugin: () => import_vite_plugin_cedar_html_env2.cedarHtmlEnvPlugin,
|
|
37
37
|
cedarImportDirPlugin: () => import_vite_plugin_cedar_import_dir.cedarImportDirPlugin,
|
|
@@ -62,6 +62,7 @@ var import_vite_plugin_jsx_loader = require("./plugins/vite-plugin-jsx-loader.js
|
|
|
62
62
|
var import_vite_plugin_merged_config = require("./plugins/vite-plugin-merged-config.js");
|
|
63
63
|
var import_vite_plugin_swap_apollo_provider = require("./plugins/vite-plugin-swap-apollo-provider.js");
|
|
64
64
|
var import_vite_plugin_cedar_auto_import = require("./plugins/vite-plugin-cedar-auto-import.js");
|
|
65
|
+
var import_vite_plugin_cedar_cjs_compat = require("./plugins/vite-plugin-cedar-cjs-compat.js");
|
|
65
66
|
var import_vite_plugin_cedar_cell2 = require("./plugins/vite-plugin-cedar-cell.js");
|
|
66
67
|
var import_vite_plugin_cedar_entry_injection2 = require("./plugins/vite-plugin-cedar-entry-injection.js");
|
|
67
68
|
var import_vite_plugin_cedar_html_env2 = require("./plugins/vite-plugin-cedar-html-env.js");
|
|
@@ -74,7 +75,6 @@ var import_vite_plugin_jsx_loader2 = require("./plugins/vite-plugin-jsx-loader.j
|
|
|
74
75
|
var import_vite_plugin_merged_config2 = require("./plugins/vite-plugin-merged-config.js");
|
|
75
76
|
var import_vite_plugin_swap_apollo_provider2 = require("./plugins/vite-plugin-swap-apollo-provider.js");
|
|
76
77
|
var import_vite_plugin_cedar_universal_deploy = require("./plugins/vite-plugin-cedar-universal-deploy.js");
|
|
77
|
-
var import_vite_plugin_cedar_dev_dispatcher = require("./plugins/vite-plugin-cedar-dev-dispatcher.js");
|
|
78
78
|
var import_vite_plugin_cedar_wait_for_api_server2 = require("./plugins/vite-plugin-cedar-wait-for-api-server.js");
|
|
79
79
|
function cedar({ mode } = {}) {
|
|
80
80
|
const cedarConfig = (0, import_project_config.getConfig)();
|
|
@@ -115,7 +115,7 @@ var index_default = cedar;
|
|
|
115
115
|
cedar,
|
|
116
116
|
cedarAutoImportsPlugin,
|
|
117
117
|
cedarCellTransform,
|
|
118
|
-
|
|
118
|
+
cedarCjsCompatPlugin,
|
|
119
119
|
cedarEntryInjectionPlugin,
|
|
120
120
|
cedarHtmlEnvPlugin,
|
|
121
121
|
cedarImportDirPlugin,
|
|
@@ -0,0 +1,341 @@
|
|
|
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_cjs_compat_exports = {};
|
|
30
|
+
__export(vite_plugin_cedar_cjs_compat_exports, {
|
|
31
|
+
cedarCjsCompatPlugin: () => cedarCjsCompatPlugin
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(vite_plugin_cedar_cjs_compat_exports);
|
|
34
|
+
var import_node_path = __toESM(require("node:path"), 1);
|
|
35
|
+
var import_acorn = require("acorn");
|
|
36
|
+
const UMD_REGEXES = [
|
|
37
|
+
/\btypeof\s+(module\.exports|module|exports)\s*===?\s*['"]object['"]/,
|
|
38
|
+
/['"]object['"]\s*===?\s*typeof\s+(module\.exports|module|exports)/,
|
|
39
|
+
/\btypeof\s+define\s*===?\s*['"]function['"]/,
|
|
40
|
+
/['"]function['"]\s*===?\s*typeof\s+define/
|
|
41
|
+
];
|
|
42
|
+
function isAstNode(value) {
|
|
43
|
+
return value !== null && typeof value === "object" && "type" in value;
|
|
44
|
+
}
|
|
45
|
+
function hasProperty(obj, key) {
|
|
46
|
+
return key in obj;
|
|
47
|
+
}
|
|
48
|
+
function walkAst(node, visitor) {
|
|
49
|
+
if (Array.isArray(node)) {
|
|
50
|
+
for (const child of node) {
|
|
51
|
+
walkAst(child, visitor);
|
|
52
|
+
}
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (!isAstNode(node)) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const shouldDescend = visitor(node);
|
|
59
|
+
if (shouldDescend === false) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
for (const key of Object.keys(node)) {
|
|
63
|
+
if (key === "type" || key === "loc" || key === "range") {
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
const child = node[key];
|
|
67
|
+
if (child && typeof child === "object") {
|
|
68
|
+
walkAst(child, visitor);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function getIdentifierName(node) {
|
|
73
|
+
if (!isAstNode(node)) {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
return node.type === "Identifier" && typeof node.name === "string" ? node.name : null;
|
|
77
|
+
}
|
|
78
|
+
function isModuleExports(node) {
|
|
79
|
+
if (!isAstNode(node)) {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
return node.type === "MemberExpression" && getIdentifierName(node.object) === "module" && getIdentifierName(node.property) === "exports";
|
|
83
|
+
}
|
|
84
|
+
function isReExport(node) {
|
|
85
|
+
if (!isAstNode(node) || node.type !== "AssignmentExpression") {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
return isModuleExports(node.left) && isAstNode(node.right) && node.right.type === "CallExpression" && getIdentifierName(node.right.callee) === "require";
|
|
89
|
+
}
|
|
90
|
+
function isObjectDefinePropertyWithGetterOnExports(node) {
|
|
91
|
+
if (!isAstNode(node) || node.type !== "CallExpression") {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
if (!isObjectDefineProperty(node)) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
const args = node.arguments;
|
|
98
|
+
if (!Array.isArray(args) || args.length < 3 || getIdentifierName(args[0]) !== "exports") {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
const descriptor = args[2];
|
|
102
|
+
if (!isAstNode(descriptor) || descriptor.type !== "ObjectExpression") {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
const props = descriptor.properties;
|
|
106
|
+
if (!Array.isArray(props)) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
return props.some((prop) => {
|
|
110
|
+
if (!isAstNode(prop) || prop.type !== "Property") {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
const name = getIdentifierName(prop.key);
|
|
114
|
+
return name === "get" || name === "set";
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
function getModuleExportsObjectLiteral(node) {
|
|
118
|
+
if (!isAstNode(node) || node.type !== "AssignmentExpression") {
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
if (isModuleExports(node.left) && isAstNode(node.right) && node.right.type === "ObjectExpression") {
|
|
122
|
+
return node.right;
|
|
123
|
+
}
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
function isObjectDefineProperty(node) {
|
|
127
|
+
if (!isAstNode(node) || node.type !== "CallExpression") {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
const callee = node.callee;
|
|
131
|
+
if (!isAstNode(callee) || callee.type !== "MemberExpression") {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
return getIdentifierName(callee.object) === "Object" && getIdentifierName(callee.property) === "defineProperty";
|
|
135
|
+
}
|
|
136
|
+
function formatLoc(node) {
|
|
137
|
+
const loc = node.loc;
|
|
138
|
+
if (typeof loc !== "object" || loc === null || !hasProperty(loc, "start")) {
|
|
139
|
+
return "unknown location";
|
|
140
|
+
}
|
|
141
|
+
const start = loc.start;
|
|
142
|
+
if (typeof start !== "object" || start === null || !hasProperty(start, "line") || !hasProperty(start, "column")) {
|
|
143
|
+
return "unknown location";
|
|
144
|
+
}
|
|
145
|
+
const line = start.line;
|
|
146
|
+
const column = start.column;
|
|
147
|
+
if (typeof line !== "number" || typeof column !== "number") {
|
|
148
|
+
return "unknown location";
|
|
149
|
+
}
|
|
150
|
+
return `line ${line}, column ${column}`;
|
|
151
|
+
}
|
|
152
|
+
function cedarCjsCompatPlugin() {
|
|
153
|
+
let lexerInitialized = false;
|
|
154
|
+
return {
|
|
155
|
+
name: "cedar-cjs-compat",
|
|
156
|
+
enforce: "pre",
|
|
157
|
+
async transform(code, id) {
|
|
158
|
+
if (!/\.[cm]?js$/.test(id)) {
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
if (!/\bmodule\.exports\b|\bexports\.[a-zA-Z_$]/.test(code)) {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
const isUmd = UMD_REGEXES.some((re) => re.test(code));
|
|
165
|
+
if (isUmd) {
|
|
166
|
+
throw new Error(
|
|
167
|
+
`CedarJS CJS compat plugin does not support UMD modules. File: ${id}
|
|
168
|
+
If you need to load this file in a Vite RunnableDevEnvironment, consider converting it to pure ESM or using a pre-bundled ESM build from the package author.`
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
let ast;
|
|
172
|
+
try {
|
|
173
|
+
ast = (0, import_acorn.parse)(code, { ecmaVersion: "latest", sourceType: "script" });
|
|
174
|
+
} catch {
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
function scanForUnsupportedPatterns(astBody) {
|
|
178
|
+
let objectLiteral = null;
|
|
179
|
+
walkAst(astBody, (node) => {
|
|
180
|
+
if (node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression") {
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
if (node.type !== "ExpressionStatement") {
|
|
184
|
+
return void 0;
|
|
185
|
+
}
|
|
186
|
+
const expr = node.expression;
|
|
187
|
+
if (!isAstNode(expr)) {
|
|
188
|
+
return void 0;
|
|
189
|
+
}
|
|
190
|
+
if (isReExport(expr)) {
|
|
191
|
+
throw new Error(
|
|
192
|
+
`CedarJS CJS compat plugin does not support re-exports (module.exports = require(...)). File: ${id}
|
|
193
|
+
Named exports from the re-exported module would be lost. Convert the file to explicit named exports, or import the target module directly in the consumer.`
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
if (isObjectDefinePropertyWithGetterOnExports(expr)) {
|
|
197
|
+
throw new Error(
|
|
198
|
+
`CedarJS CJS compat plugin does not support Object.defineProperty with getter/setter descriptors on exports because they would be evaluated eagerly at load time rather than lazily. File: ${id}
|
|
199
|
+
Convert the file to plain property assignments (exports.foo = ...) or use an ESM build of the package.`
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
const objectLiteralExpr = getModuleExportsObjectLiteral(expr);
|
|
203
|
+
if (objectLiteralExpr) {
|
|
204
|
+
objectLiteral = objectLiteralExpr;
|
|
205
|
+
return false;
|
|
206
|
+
}
|
|
207
|
+
return void 0;
|
|
208
|
+
});
|
|
209
|
+
return objectLiteral;
|
|
210
|
+
}
|
|
211
|
+
const objectLiteralAssignment = scanForUnsupportedPatterns(ast);
|
|
212
|
+
walkAst(ast, (node) => {
|
|
213
|
+
if (node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression") {
|
|
214
|
+
return false;
|
|
215
|
+
}
|
|
216
|
+
if (node.type !== "VariableDeclaration") {
|
|
217
|
+
return void 0;
|
|
218
|
+
}
|
|
219
|
+
const declarations = node.declarations;
|
|
220
|
+
if (!Array.isArray(declarations)) {
|
|
221
|
+
return void 0;
|
|
222
|
+
}
|
|
223
|
+
for (const decl of declarations) {
|
|
224
|
+
if (!isAstNode(decl)) {
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
const declId = decl.id;
|
|
228
|
+
if (!isAstNode(declId)) {
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
if (declId.type === "Identifier" && typeof declId.name === "string" && declId.name === "exports") {
|
|
232
|
+
throw new Error(
|
|
233
|
+
`CedarJS CJS compat plugin does not support local variables named 'exports' because they shadow the injected CJS globals. File: ${id}
|
|
234
|
+
Rename the local variable to something else.`
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
if (declId.type === "ObjectPattern") {
|
|
238
|
+
const props = declId.properties;
|
|
239
|
+
if (Array.isArray(props)) {
|
|
240
|
+
for (const prop of props) {
|
|
241
|
+
if (isAstNode(prop) && prop.type === "Property" && getIdentifierName(prop.value) === "exports") {
|
|
242
|
+
throw new Error(
|
|
243
|
+
`CedarJS CJS compat plugin does not support destructuring into a local variable named 'exports' because it shadows the injected CJS globals. File: ${id}
|
|
244
|
+
Rename the local variable to something else.`
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return void 0;
|
|
252
|
+
});
|
|
253
|
+
let namedExports = [];
|
|
254
|
+
try {
|
|
255
|
+
if (!lexerInitialized) {
|
|
256
|
+
const { init } = await import("cjs-module-lexer");
|
|
257
|
+
await init();
|
|
258
|
+
lexerInitialized = true;
|
|
259
|
+
}
|
|
260
|
+
const { parse: parseLexer } = await import("cjs-module-lexer");
|
|
261
|
+
const { exports: exports2 } = parseLexer(code);
|
|
262
|
+
namedExports = exports2.filter(
|
|
263
|
+
(e) => /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(e) && e !== "default"
|
|
264
|
+
);
|
|
265
|
+
} catch {
|
|
266
|
+
}
|
|
267
|
+
if (namedExports.length === 0 && objectLiteralAssignment) {
|
|
268
|
+
const props = objectLiteralAssignment.properties;
|
|
269
|
+
if (Array.isArray(props)) {
|
|
270
|
+
for (const prop of props) {
|
|
271
|
+
if (!isAstNode(prop)) {
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
if (prop.type === "SpreadElement") {
|
|
275
|
+
throw new Error(
|
|
276
|
+
`CedarJS CJS compat plugin encountered an unsupported pattern in module.exports = { ... } at ${formatLoc(prop)} (spread element (...)). File: ${id}
|
|
277
|
+
Convert the object literal to plain property assignments (exports.foo = ...) so that cjs-module-lexer can detect the named exports, or use an ESM build of the package.`
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
if (prop.computed) {
|
|
281
|
+
throw new Error(
|
|
282
|
+
`CedarJS CJS compat plugin encountered an unsupported pattern in module.exports = { ... } at ${formatLoc(prop)} (computed property key ([expr])). File: ${id}
|
|
283
|
+
Convert the object literal to plain property assignments (exports.foo = ...) so that cjs-module-lexer can detect the named exports, or use an ESM build of the package.`
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
if (prop.method) {
|
|
287
|
+
throw new Error(
|
|
288
|
+
`CedarJS CJS compat plugin encountered an unsupported pattern in module.exports = { ... } at ${formatLoc(prop)} (method shorthand). File: ${id}
|
|
289
|
+
Convert the object literal to plain property assignments (exports.foo = ...) so that cjs-module-lexer can detect the named exports, or use an ESM build of the package.`
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
if (prop.shorthand) {
|
|
293
|
+
throw new Error(
|
|
294
|
+
`CedarJS CJS compat plugin encountered an unsupported pattern in module.exports = { ... } at ${formatLoc(prop)} (shorthand property). File: ${id}
|
|
295
|
+
Convert the object literal to plain property assignments (exports.foo = ...) so that cjs-module-lexer can detect the named exports, or use an ESM build of the package.`
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
const keyName = getIdentifierName(prop.key);
|
|
299
|
+
if (keyName && keyName !== "default") {
|
|
300
|
+
namedExports.push(keyName);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
const dirPath = JSON.stringify(import_node_path.default.dirname(id));
|
|
306
|
+
const filePath = JSON.stringify(id);
|
|
307
|
+
const hasEsModuleFlag = namedExports.includes("__esModule");
|
|
308
|
+
const safeNamedExports = namedExports.filter(
|
|
309
|
+
(e) => e !== "__esModule" && e !== "default"
|
|
310
|
+
);
|
|
311
|
+
const namedExportLines = safeNamedExports.map(
|
|
312
|
+
(name) => `export const ${name} = __cjs_result__[${JSON.stringify(name)}]`
|
|
313
|
+
).join("\n");
|
|
314
|
+
const defaultExportLine = hasEsModuleFlag ? `export default (__cjs_result__ != null && typeof __cjs_result__ === 'object' && 'default' in __cjs_result__ ? __cjs_result__.default : __cjs_result__)` : `export default __cjs_result__`;
|
|
315
|
+
return {
|
|
316
|
+
code: `
|
|
317
|
+
import { createRequire as __createRequire__ } from 'node:module'
|
|
318
|
+
const require = __createRequire__(${filePath})
|
|
319
|
+
const module = { exports: {} }
|
|
320
|
+
const exports = module.exports
|
|
321
|
+
const __dirname = ${dirPath}
|
|
322
|
+
const __filename = ${filePath}
|
|
323
|
+
;(function() {
|
|
324
|
+
${code}
|
|
325
|
+
}).call(module.exports)
|
|
326
|
+
const __cjs_result__ = module.exports
|
|
327
|
+
${defaultExportLine}
|
|
328
|
+
${namedExportLines}
|
|
329
|
+
`,
|
|
330
|
+
// Source maps are not generated. If you hit a break-point issue inside
|
|
331
|
+
// a CJS file loaded through this plugin, the line numbers will be off
|
|
332
|
+
// by the number of lines in the wrapper preamble (~10).
|
|
333
|
+
map: null
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
339
|
+
0 && (module.exports = {
|
|
340
|
+
cedarCjsCompatPlugin
|
|
341
|
+
});
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
8
|
var __export = (target, all) => {
|
|
7
9
|
for (var name in all)
|
|
@@ -15,48 +17,141 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
17
|
}
|
|
16
18
|
return to;
|
|
17
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
|
+
));
|
|
18
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
29
|
var vite_plugin_cedar_universal_deploy_exports = {};
|
|
20
30
|
__export(vite_plugin_cedar_universal_deploy_exports, {
|
|
21
31
|
cedarUniversalDeployPlugin: () => cedarUniversalDeployPlugin
|
|
22
32
|
});
|
|
23
33
|
module.exports = __toCommonJS(vite_plugin_cedar_universal_deploy_exports);
|
|
34
|
+
var import_node_path = __toESM(require("node:path"), 1);
|
|
24
35
|
var import_store = require("@universal-deploy/store");
|
|
25
|
-
|
|
26
|
-
|
|
36
|
+
var import_files = require("@cedarjs/internal/dist/files.js");
|
|
37
|
+
var import_project_config = require("@cedarjs/project-config");
|
|
38
|
+
const VIRTUAL_CEDAR_FN_PREFIX = "virtual:cedar-api:fn:";
|
|
39
|
+
const RESOLVED_CEDAR_FN_PREFIX = "\0virtual:cedar-api:fn:";
|
|
40
|
+
const GRAPHQL_METHODS = ["GET", "POST", "OPTIONS"];
|
|
41
|
+
function normaliseApiPrefix(apiPrefix) {
|
|
42
|
+
apiPrefix = apiPrefix.trim();
|
|
43
|
+
while (apiPrefix.startsWith("/")) {
|
|
44
|
+
apiPrefix = apiPrefix.slice(1);
|
|
45
|
+
}
|
|
46
|
+
while (apiPrefix.endsWith("/")) {
|
|
47
|
+
apiPrefix = apiPrefix.slice(0, -1);
|
|
48
|
+
}
|
|
49
|
+
return apiPrefix ? "/" + apiPrefix : "";
|
|
50
|
+
}
|
|
51
|
+
function discoverCedarRoutes(apiRootPath) {
|
|
52
|
+
const srcFunctions = (0, import_project_config.getPaths)().api.functions;
|
|
53
|
+
const distFunctions = import_node_path.default.join((0, import_project_config.getPaths)().api.base, "dist", "functions");
|
|
54
|
+
const sourceFiles = (0, import_files.findApiServerFunctions)(srcFunctions);
|
|
55
|
+
const routes = [];
|
|
56
|
+
for (const sourcePath of sourceFiles) {
|
|
57
|
+
const relative = import_node_path.default.relative(srcFunctions, sourcePath);
|
|
58
|
+
const { dir, name, ext: _ext } = import_node_path.default.parse(relative);
|
|
59
|
+
let routeName;
|
|
60
|
+
if (dir === name) {
|
|
61
|
+
routeName = dir;
|
|
62
|
+
} else if (dir === "") {
|
|
63
|
+
routeName = name;
|
|
64
|
+
} else if (dir.length && name === "index") {
|
|
65
|
+
routeName = dir;
|
|
66
|
+
} else {
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
const apiPrefix = normaliseApiPrefix(apiRootPath);
|
|
70
|
+
const routePath = routeName === "graphql" ? `${apiPrefix}/graphql` : `${apiPrefix}/${routeName}`;
|
|
71
|
+
const methods = routeName === "graphql" ? [...GRAPHQL_METHODS] : [];
|
|
72
|
+
const type = routeName === "graphql" ? "graphql" : routeName === "health" ? "health" : routeName.toLowerCase().includes("auth") ? "auth" : "function";
|
|
73
|
+
const distPath = import_node_path.default.join(distFunctions, dir, name + ".js");
|
|
74
|
+
routes.push({
|
|
75
|
+
id: routePath,
|
|
76
|
+
path: routePath,
|
|
77
|
+
methods,
|
|
78
|
+
type,
|
|
79
|
+
entry: distPath
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
const gqlIndex = routes.findIndex((r) => r.type === "graphql");
|
|
83
|
+
if (gqlIndex > 0) {
|
|
84
|
+
const [gqlRoute] = routes.splice(gqlIndex, 1);
|
|
85
|
+
routes.unshift(gqlRoute);
|
|
86
|
+
}
|
|
87
|
+
return routes;
|
|
88
|
+
}
|
|
89
|
+
function toEntryMeta(route) {
|
|
90
|
+
const routePatterns = route.path === "/**" ? ["/**"] : [route.path, `${route.path}/**`];
|
|
91
|
+
return {
|
|
92
|
+
id: `${VIRTUAL_CEDAR_FN_PREFIX}${route.id}`,
|
|
93
|
+
route: routePatterns,
|
|
94
|
+
...route.methods.length > 0 && {
|
|
95
|
+
method: route.methods
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
}
|
|
27
99
|
function cedarUniversalDeployPlugin(options = {}) {
|
|
28
100
|
const { apiRootPath } = options;
|
|
101
|
+
const routes = discoverCedarRoutes(apiRootPath ?? "/");
|
|
102
|
+
let entriesInjected = false;
|
|
29
103
|
return {
|
|
30
104
|
name: "cedar-universal-deploy",
|
|
31
105
|
apply: "build",
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
106
|
+
config: {
|
|
107
|
+
order: "pre",
|
|
108
|
+
handler() {
|
|
109
|
+
if (entriesInjected) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
entriesInjected = true;
|
|
113
|
+
for (const route of routes) {
|
|
114
|
+
(0, import_store.addEntry)(toEntryMeta(route));
|
|
115
|
+
}
|
|
116
|
+
}
|
|
37
117
|
},
|
|
38
118
|
resolveId(id) {
|
|
39
|
-
if (id
|
|
40
|
-
return
|
|
119
|
+
if (id.startsWith(RESOLVED_CEDAR_FN_PREFIX)) {
|
|
120
|
+
return id;
|
|
41
121
|
}
|
|
42
|
-
if (id
|
|
43
|
-
return
|
|
122
|
+
if (id.startsWith(VIRTUAL_CEDAR_FN_PREFIX)) {
|
|
123
|
+
return "\0" + id;
|
|
44
124
|
}
|
|
45
125
|
return void 0;
|
|
46
126
|
},
|
|
47
127
|
load(id) {
|
|
48
|
-
if (id
|
|
49
|
-
|
|
128
|
+
if (id.startsWith(RESOLVED_CEDAR_FN_PREFIX)) {
|
|
129
|
+
const routeId = id.slice(RESOLVED_CEDAR_FN_PREFIX.length);
|
|
130
|
+
const route = routes.find((r) => r.id === routeId);
|
|
131
|
+
if (!route) {
|
|
132
|
+
return void 0;
|
|
133
|
+
}
|
|
134
|
+
if (route.type === "graphql") {
|
|
135
|
+
return generateGraphQLModule(route.entry);
|
|
136
|
+
}
|
|
137
|
+
return generateFunctionModule(route.entry);
|
|
50
138
|
}
|
|
51
|
-
|
|
52
|
-
return `
|
|
53
|
-
import { buildCedarDispatcher } from '@cedarjs/api-server/udDispatcher';
|
|
54
|
-
const { fetchable } = await buildCedarDispatcher(${apiRootPathArg});
|
|
55
|
-
export default fetchable;
|
|
56
|
-
`;
|
|
139
|
+
return void 0;
|
|
57
140
|
}
|
|
58
141
|
};
|
|
59
142
|
}
|
|
143
|
+
function generateGraphQLModule(distPath) {
|
|
144
|
+
return `
|
|
145
|
+
import { createGraphQLHandler } from '@cedarjs/vite/ud-handlers/graphql';
|
|
146
|
+
export default createGraphQLHandler({ distPath: ${JSON.stringify(distPath)} });
|
|
147
|
+
`;
|
|
148
|
+
}
|
|
149
|
+
function generateFunctionModule(distPath) {
|
|
150
|
+
return `
|
|
151
|
+
import { createFunctionHandler } from '@cedarjs/vite/ud-handlers/function';
|
|
152
|
+
export default createFunctionHandler({ distPath: ${JSON.stringify(distPath)} });
|
|
153
|
+
`;
|
|
154
|
+
}
|
|
60
155
|
// Annotate the CommonJS export names for ESM import in node:
|
|
61
156
|
0 && (module.exports = {
|
|
62
157
|
cedarUniversalDeployPlugin
|
|
@@ -39,6 +39,7 @@ function cedarWaitForApiServer() {
|
|
|
39
39
|
const cedarConfig = (0, import_project_config.getConfig)();
|
|
40
40
|
const apiPort = cedarConfig.api.port;
|
|
41
41
|
const apiHost = cedarConfig.api.host || "localhost";
|
|
42
|
+
const isUnifiedDev = process.env.__CEDAR_UNIFIED_DEV === "true";
|
|
42
43
|
return {
|
|
43
44
|
name: "cedar-wait-for-api-server",
|
|
44
45
|
apply: "serve",
|
|
@@ -51,7 +52,7 @@ function cedarWaitForApiServer() {
|
|
|
51
52
|
// don't use startsWith here
|
|
52
53
|
url === apiGqlUrl || // The two checks below are for when we support GraphQL-over-HTTP
|
|
53
54
|
url.startsWith(apiGqlUrl + "/") || url.startsWith(apiGqlUrl + "?"));
|
|
54
|
-
if (!isApiRequest || serverHasBeenUp) {
|
|
55
|
+
if (!isApiRequest || serverHasBeenUp || isUnifiedDev) {
|
|
55
56
|
return next();
|
|
56
57
|
}
|
|
57
58
|
try {
|