@cedarjs/vite 4.1.1-next.14 → 4.1.1-next.64
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
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var catch_all_exports = {};
|
|
20
|
+
__export(catch_all_exports, {
|
|
21
|
+
createCatchAllHandler: () => createCatchAllHandler
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(catch_all_exports);
|
|
24
|
+
var import_rou3 = require("rou3");
|
|
25
|
+
function createCatchAllHandler(options) {
|
|
26
|
+
const router = (0, import_rou3.createRouter)();
|
|
27
|
+
for (const route of options.routes) {
|
|
28
|
+
const methods = route.methods.length > 0 ? route.methods : ["GET", "HEAD", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"];
|
|
29
|
+
for (const method of methods) {
|
|
30
|
+
(0, import_rou3.addRoute)(router, method, route.path, route.module);
|
|
31
|
+
(0, import_rou3.addRoute)(router, method, `${route.path}/**`, route.module);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const apiRootPath = options.apiRootPath;
|
|
35
|
+
function normalizePathname(requestUrl) {
|
|
36
|
+
const url = new URL(requestUrl);
|
|
37
|
+
let pathname = url.pathname;
|
|
38
|
+
if (apiRootPath !== "/" && pathname.startsWith(apiRootPath)) {
|
|
39
|
+
pathname = pathname.slice(apiRootPath.length - 1);
|
|
40
|
+
}
|
|
41
|
+
if (!pathname.startsWith("/")) {
|
|
42
|
+
pathname = "/" + pathname;
|
|
43
|
+
}
|
|
44
|
+
return pathname;
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
async fetch(request) {
|
|
48
|
+
const pathname = normalizePathname(request.url);
|
|
49
|
+
const match = (0, import_rou3.findRoute)(router, request.method, pathname);
|
|
50
|
+
if (!match) {
|
|
51
|
+
return new Response("Not Found", { status: 404 });
|
|
52
|
+
}
|
|
53
|
+
try {
|
|
54
|
+
return await match.data.fetch(request);
|
|
55
|
+
} catch (err) {
|
|
56
|
+
console.error("Unhandled error in fetch handler for", pathname, err);
|
|
57
|
+
return new Response("Internal Server Error", { status: 500 });
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
63
|
+
0 && (module.exports = {
|
|
64
|
+
createCatchAllHandler
|
|
65
|
+
});
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var function_exports = {};
|
|
20
|
+
__export(function_exports, {
|
|
21
|
+
createFunctionHandler: () => createFunctionHandler
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(function_exports);
|
|
24
|
+
var import_node_url = require("node:url");
|
|
25
|
+
var import_runtime = require("@cedarjs/api/runtime");
|
|
26
|
+
var import_udFetchable = require("@cedarjs/api-server/udFetchable");
|
|
27
|
+
function createFunctionHandler(options) {
|
|
28
|
+
const handleRequest = async (request, ctx) => {
|
|
29
|
+
const mod = await import((0, import_node_url.pathToFileURL)(options.distPath).href);
|
|
30
|
+
const nativeHandler = mod.handleRequest || mod.default?.handleRequest;
|
|
31
|
+
if (nativeHandler) {
|
|
32
|
+
return nativeHandler(request, ctx);
|
|
33
|
+
}
|
|
34
|
+
const legacyHandler = mod.handler || mod.default?.handler;
|
|
35
|
+
if (legacyHandler) {
|
|
36
|
+
return (0, import_runtime.wrapLegacyHandler)(legacyHandler)(request, ctx);
|
|
37
|
+
}
|
|
38
|
+
throw new Error(
|
|
39
|
+
`Handler not found in ${options.distPath}. Expected \`export async function handleRequest(request, ctx)\`, \`export default { handleRequest }\`, or a legacy Lambda-shaped \`handler\`.`
|
|
40
|
+
);
|
|
41
|
+
};
|
|
42
|
+
return (0, import_udFetchable.createCedarFetchable)(handleRequest);
|
|
43
|
+
}
|
|
44
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
45
|
+
0 && (module.exports = {
|
|
46
|
+
createFunctionHandler
|
|
47
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var graphql_exports = {};
|
|
20
|
+
__export(graphql_exports, {
|
|
21
|
+
createGraphQLHandler: () => createGraphQLHandler
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(graphql_exports);
|
|
24
|
+
var import_node_url = require("node:url");
|
|
25
|
+
var import_runtime = require("@cedarjs/api/runtime");
|
|
26
|
+
var import_graphql_server = require("@cedarjs/graphql-server");
|
|
27
|
+
function createGraphQLHandler(options) {
|
|
28
|
+
let yogaInitPromise = null;
|
|
29
|
+
async function getYoga() {
|
|
30
|
+
if (!yogaInitPromise) {
|
|
31
|
+
yogaInitPromise = (async () => {
|
|
32
|
+
const mod = await import((0, import_node_url.pathToFileURL)(options.distPath).href);
|
|
33
|
+
const opts = mod.__rw_graphqlOptions;
|
|
34
|
+
const { yoga } = await (0, import_graphql_server.createGraphQLYoga)(opts);
|
|
35
|
+
return { yoga, graphqlOptions: opts };
|
|
36
|
+
})();
|
|
37
|
+
}
|
|
38
|
+
return yogaInitPromise;
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
async fetch(request) {
|
|
42
|
+
const { yoga, graphqlOptions } = await getYoga();
|
|
43
|
+
const cedarContext = await (0, import_runtime.buildCedarContext)(request, {
|
|
44
|
+
authDecoder: graphqlOptions?.authDecoder
|
|
45
|
+
});
|
|
46
|
+
const event = await (0, import_runtime.requestToLegacyEvent)(request, cedarContext);
|
|
47
|
+
return yoga.handle(request, {
|
|
48
|
+
request,
|
|
49
|
+
cedarContext,
|
|
50
|
+
event,
|
|
51
|
+
requestContext: void 0
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
57
|
+
0 && (module.exports = {
|
|
58
|
+
createGraphQLHandler
|
|
59
|
+
});
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { PluginOption } from 'vite';
|
|
2
2
|
export { cedarAutoImportsPlugin } from './plugins/vite-plugin-cedar-auto-import.js';
|
|
3
|
+
export { cedarCjsCompatPlugin } from './plugins/vite-plugin-cedar-cjs-compat.js';
|
|
3
4
|
export { cedarCellTransform } from './plugins/vite-plugin-cedar-cell.js';
|
|
4
5
|
export { cedarEntryInjectionPlugin } from './plugins/vite-plugin-cedar-entry-injection.js';
|
|
5
6
|
export { cedarHtmlEnvPlugin } from './plugins/vite-plugin-cedar-html-env.js';
|
|
@@ -12,7 +13,6 @@ export { cedarTransformJsAsJsx } from './plugins/vite-plugin-jsx-loader.js';
|
|
|
12
13
|
export { cedarMergedConfig } from './plugins/vite-plugin-merged-config.js';
|
|
13
14
|
export { cedarSwapApolloProvider } from './plugins/vite-plugin-swap-apollo-provider.js';
|
|
14
15
|
export { cedarUniversalDeployPlugin } from './plugins/vite-plugin-cedar-universal-deploy.js';
|
|
15
|
-
export { cedarDevDispatcherPlugin } from './plugins/vite-plugin-cedar-dev-dispatcher.js';
|
|
16
16
|
export { cedarWaitForApiServer } from './plugins/vite-plugin-cedar-wait-for-api-server.js';
|
|
17
17
|
type PluginOptions = {
|
|
18
18
|
mode?: string | undefined;
|
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;AAqBxC,OAAO,EAAE,sBAAsB,EAAE,MAAM,4CAA4C,CAAA;AACnF,OAAO,EAAE,kBAAkB,EAAE,MAAM,qCAAqC,CAAA;AACxE,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,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,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,MAAM,CAAA;AAqBxC,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,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,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,CAyClE;AAED,8DAA8D;AAC9D,eAAe,KAAK,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -17,6 +17,7 @@ import { cedarTransformJsAsJsx } from "./plugins/vite-plugin-jsx-loader.js";
|
|
|
17
17
|
import { cedarMergedConfig } from "./plugins/vite-plugin-merged-config.js";
|
|
18
18
|
import { cedarSwapApolloProvider } from "./plugins/vite-plugin-swap-apollo-provider.js";
|
|
19
19
|
import { cedarAutoImportsPlugin } from "./plugins/vite-plugin-cedar-auto-import.js";
|
|
20
|
+
import { cedarCjsCompatPlugin } from "./plugins/vite-plugin-cedar-cjs-compat.js";
|
|
20
21
|
import { cedarCellTransform as cedarCellTransform2 } from "./plugins/vite-plugin-cedar-cell.js";
|
|
21
22
|
import { cedarEntryInjectionPlugin as cedarEntryInjectionPlugin2 } from "./plugins/vite-plugin-cedar-entry-injection.js";
|
|
22
23
|
import { cedarHtmlEnvPlugin as cedarHtmlEnvPlugin2 } from "./plugins/vite-plugin-cedar-html-env.js";
|
|
@@ -29,7 +30,6 @@ import { cedarTransformJsAsJsx as cedarTransformJsAsJsx2 } from "./plugins/vite-
|
|
|
29
30
|
import { cedarMergedConfig as cedarMergedConfig2 } from "./plugins/vite-plugin-merged-config.js";
|
|
30
31
|
import { cedarSwapApolloProvider as cedarSwapApolloProvider2 } from "./plugins/vite-plugin-swap-apollo-provider.js";
|
|
31
32
|
import { cedarUniversalDeployPlugin } from "./plugins/vite-plugin-cedar-universal-deploy.js";
|
|
32
|
-
import { cedarDevDispatcherPlugin } from "./plugins/vite-plugin-cedar-dev-dispatcher.js";
|
|
33
33
|
import { cedarWaitForApiServer as cedarWaitForApiServer2 } from "./plugins/vite-plugin-cedar-wait-for-api-server.js";
|
|
34
34
|
function cedar({ mode } = {}) {
|
|
35
35
|
const cedarConfig = getConfig();
|
|
@@ -69,7 +69,7 @@ export {
|
|
|
69
69
|
cedar,
|
|
70
70
|
cedarAutoImportsPlugin,
|
|
71
71
|
cedarCellTransform2 as cedarCellTransform,
|
|
72
|
-
|
|
72
|
+
cedarCjsCompatPlugin,
|
|
73
73
|
cedarEntryInjectionPlugin2 as cedarEntryInjectionPlugin,
|
|
74
74
|
cedarHtmlEnvPlugin2 as cedarHtmlEnvPlugin,
|
|
75
75
|
cedarImportDirPlugin,
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Plugin } from 'vite';
|
|
2
|
+
/**
|
|
3
|
+
* A Vite plugin that transforms CommonJS files to ESM so they work with
|
|
4
|
+
* Vite 6's RunnableDevEnvironment / ESModulesEvaluator, which doesn't
|
|
5
|
+
* understand `module.exports` syntax.
|
|
6
|
+
*
|
|
7
|
+
* Uses `cjs-module-lexer` (a Vite transitive dependency) to detect named
|
|
8
|
+
* exports so they are individually re-exported and accessible without going
|
|
9
|
+
* through `.default`.
|
|
10
|
+
*
|
|
11
|
+
* Known limitations (documented inline where relevant):
|
|
12
|
+
* - No source-map support (`map: null`).
|
|
13
|
+
* - Object.defineProperty(exports, key, { get: () => ... }) with getter or
|
|
14
|
+
* setter descriptors are evaluated eagerly at module-load time rather than
|
|
15
|
+
* lazily; plain value descriptors (e.g. __esModule) are allowed.
|
|
16
|
+
* - Properties added to a function/class after `module.exports = fn` are
|
|
17
|
+
* not re-exported.
|
|
18
|
+
* - Circular dependencies rely on Node's native behaviour via
|
|
19
|
+
* `createRequire` and are not handled as robustly as Rollup's synthetic
|
|
20
|
+
* namespace objects.
|
|
21
|
+
*/
|
|
22
|
+
export declare function cedarCjsCompatPlugin(): Plugin;
|
|
23
|
+
//# sourceMappingURL=vite-plugin-cedar-cjs-compat.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vite-plugin-cedar-cjs-compat.d.ts","sourceRoot":"","sources":["../../src/plugins/vite-plugin-cedar-cjs-compat.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAA;AAmOlC;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,oBAAoB,IAAI,MAAM,CA0S7C"}
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { parse } from "acorn";
|
|
3
|
+
const UMD_REGEXES = [
|
|
4
|
+
/\btypeof\s+(module\.exports|module|exports)\s*===?\s*['"]object['"]/,
|
|
5
|
+
/['"]object['"]\s*===?\s*typeof\s+(module\.exports|module|exports)/,
|
|
6
|
+
/\btypeof\s+define\s*===?\s*['"]function['"]/,
|
|
7
|
+
/['"]function['"]\s*===?\s*typeof\s+define/
|
|
8
|
+
];
|
|
9
|
+
function isAstNode(value) {
|
|
10
|
+
return value !== null && typeof value === "object" && "type" in value;
|
|
11
|
+
}
|
|
12
|
+
function hasProperty(obj, key) {
|
|
13
|
+
return key in obj;
|
|
14
|
+
}
|
|
15
|
+
function walkAst(node, visitor) {
|
|
16
|
+
if (Array.isArray(node)) {
|
|
17
|
+
for (const child of node) {
|
|
18
|
+
walkAst(child, visitor);
|
|
19
|
+
}
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (!isAstNode(node)) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const shouldDescend = visitor(node);
|
|
26
|
+
if (shouldDescend === false) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
for (const key of Object.keys(node)) {
|
|
30
|
+
if (key === "type" || key === "loc" || key === "range") {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
const child = node[key];
|
|
34
|
+
if (child && typeof child === "object") {
|
|
35
|
+
walkAst(child, visitor);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function getIdentifierName(node) {
|
|
40
|
+
if (!isAstNode(node)) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
return node.type === "Identifier" && typeof node.name === "string" ? node.name : null;
|
|
44
|
+
}
|
|
45
|
+
function isModuleExports(node) {
|
|
46
|
+
if (!isAstNode(node)) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
return node.type === "MemberExpression" && getIdentifierName(node.object) === "module" && getIdentifierName(node.property) === "exports";
|
|
50
|
+
}
|
|
51
|
+
function isReExport(node) {
|
|
52
|
+
if (!isAstNode(node) || node.type !== "AssignmentExpression") {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
return isModuleExports(node.left) && isAstNode(node.right) && node.right.type === "CallExpression" && getIdentifierName(node.right.callee) === "require";
|
|
56
|
+
}
|
|
57
|
+
function isObjectDefinePropertyWithGetterOnExports(node) {
|
|
58
|
+
if (!isAstNode(node) || node.type !== "CallExpression") {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
if (!isObjectDefineProperty(node)) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
const args = node.arguments;
|
|
65
|
+
if (!Array.isArray(args) || args.length < 3 || getIdentifierName(args[0]) !== "exports") {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
const descriptor = args[2];
|
|
69
|
+
if (!isAstNode(descriptor) || descriptor.type !== "ObjectExpression") {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
const props = descriptor.properties;
|
|
73
|
+
if (!Array.isArray(props)) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
return props.some((prop) => {
|
|
77
|
+
if (!isAstNode(prop) || prop.type !== "Property") {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
const name = getIdentifierName(prop.key);
|
|
81
|
+
return name === "get" || name === "set";
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
function getModuleExportsObjectLiteral(node) {
|
|
85
|
+
if (!isAstNode(node) || node.type !== "AssignmentExpression") {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
if (isModuleExports(node.left) && isAstNode(node.right) && node.right.type === "ObjectExpression") {
|
|
89
|
+
return node.right;
|
|
90
|
+
}
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
function isObjectDefineProperty(node) {
|
|
94
|
+
if (!isAstNode(node) || node.type !== "CallExpression") {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
const callee = node.callee;
|
|
98
|
+
if (!isAstNode(callee) || callee.type !== "MemberExpression") {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
return getIdentifierName(callee.object) === "Object" && getIdentifierName(callee.property) === "defineProperty";
|
|
102
|
+
}
|
|
103
|
+
function formatLoc(node) {
|
|
104
|
+
const loc = node.loc;
|
|
105
|
+
if (typeof loc !== "object" || loc === null || !hasProperty(loc, "start")) {
|
|
106
|
+
return "unknown location";
|
|
107
|
+
}
|
|
108
|
+
const start = loc.start;
|
|
109
|
+
if (typeof start !== "object" || start === null || !hasProperty(start, "line") || !hasProperty(start, "column")) {
|
|
110
|
+
return "unknown location";
|
|
111
|
+
}
|
|
112
|
+
const line = start.line;
|
|
113
|
+
const column = start.column;
|
|
114
|
+
if (typeof line !== "number" || typeof column !== "number") {
|
|
115
|
+
return "unknown location";
|
|
116
|
+
}
|
|
117
|
+
return `line ${line}, column ${column}`;
|
|
118
|
+
}
|
|
119
|
+
function cedarCjsCompatPlugin() {
|
|
120
|
+
let lexerInitialized = false;
|
|
121
|
+
return {
|
|
122
|
+
name: "cedar-cjs-compat",
|
|
123
|
+
enforce: "pre",
|
|
124
|
+
async transform(code, id) {
|
|
125
|
+
if (!/\.[cm]?js$/.test(id)) {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
if (!/\bmodule\.exports\b|\bexports\.[a-zA-Z_$]/.test(code)) {
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
const isUmd = UMD_REGEXES.some((re) => re.test(code));
|
|
132
|
+
if (isUmd) {
|
|
133
|
+
throw new Error(
|
|
134
|
+
`CedarJS CJS compat plugin does not support UMD modules. File: ${id}
|
|
135
|
+
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.`
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
let ast;
|
|
139
|
+
try {
|
|
140
|
+
ast = parse(code, { ecmaVersion: "latest", sourceType: "script" });
|
|
141
|
+
} catch {
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
function scanForUnsupportedPatterns(astBody) {
|
|
145
|
+
let objectLiteral = null;
|
|
146
|
+
walkAst(astBody, (node) => {
|
|
147
|
+
if (node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression") {
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
if (node.type !== "ExpressionStatement") {
|
|
151
|
+
return void 0;
|
|
152
|
+
}
|
|
153
|
+
const expr = node.expression;
|
|
154
|
+
if (!isAstNode(expr)) {
|
|
155
|
+
return void 0;
|
|
156
|
+
}
|
|
157
|
+
if (isReExport(expr)) {
|
|
158
|
+
throw new Error(
|
|
159
|
+
`CedarJS CJS compat plugin does not support re-exports (module.exports = require(...)). File: ${id}
|
|
160
|
+
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.`
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
if (isObjectDefinePropertyWithGetterOnExports(expr)) {
|
|
164
|
+
throw new Error(
|
|
165
|
+
`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}
|
|
166
|
+
Convert the file to plain property assignments (exports.foo = ...) or use an ESM build of the package.`
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
const objectLiteralExpr = getModuleExportsObjectLiteral(expr);
|
|
170
|
+
if (objectLiteralExpr) {
|
|
171
|
+
objectLiteral = objectLiteralExpr;
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
return void 0;
|
|
175
|
+
});
|
|
176
|
+
return objectLiteral;
|
|
177
|
+
}
|
|
178
|
+
const objectLiteralAssignment = scanForUnsupportedPatterns(ast);
|
|
179
|
+
walkAst(ast, (node) => {
|
|
180
|
+
if (node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression") {
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
if (node.type !== "VariableDeclaration") {
|
|
184
|
+
return void 0;
|
|
185
|
+
}
|
|
186
|
+
const declarations = node.declarations;
|
|
187
|
+
if (!Array.isArray(declarations)) {
|
|
188
|
+
return void 0;
|
|
189
|
+
}
|
|
190
|
+
for (const decl of declarations) {
|
|
191
|
+
if (!isAstNode(decl)) {
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
const declId = decl.id;
|
|
195
|
+
if (!isAstNode(declId)) {
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
if (declId.type === "Identifier" && typeof declId.name === "string" && declId.name === "exports") {
|
|
199
|
+
throw new Error(
|
|
200
|
+
`CedarJS CJS compat plugin does not support local variables named 'exports' because they shadow the injected CJS globals. File: ${id}
|
|
201
|
+
Rename the local variable to something else.`
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
if (declId.type === "ObjectPattern") {
|
|
205
|
+
const props = declId.properties;
|
|
206
|
+
if (Array.isArray(props)) {
|
|
207
|
+
for (const prop of props) {
|
|
208
|
+
if (isAstNode(prop) && prop.type === "Property" && getIdentifierName(prop.value) === "exports") {
|
|
209
|
+
throw new Error(
|
|
210
|
+
`CedarJS CJS compat plugin does not support destructuring into a local variable named 'exports' because it shadows the injected CJS globals. File: ${id}
|
|
211
|
+
Rename the local variable to something else.`
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return void 0;
|
|
219
|
+
});
|
|
220
|
+
let namedExports = [];
|
|
221
|
+
try {
|
|
222
|
+
if (!lexerInitialized) {
|
|
223
|
+
const { init } = await import("cjs-module-lexer");
|
|
224
|
+
await init();
|
|
225
|
+
lexerInitialized = true;
|
|
226
|
+
}
|
|
227
|
+
const { parse: parseLexer } = await import("cjs-module-lexer");
|
|
228
|
+
const { exports } = parseLexer(code);
|
|
229
|
+
namedExports = exports.filter(
|
|
230
|
+
(e) => /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(e) && e !== "default"
|
|
231
|
+
);
|
|
232
|
+
} catch {
|
|
233
|
+
}
|
|
234
|
+
if (namedExports.length === 0 && objectLiteralAssignment) {
|
|
235
|
+
const props = objectLiteralAssignment.properties;
|
|
236
|
+
if (Array.isArray(props)) {
|
|
237
|
+
for (const prop of props) {
|
|
238
|
+
if (!isAstNode(prop)) {
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
if (prop.type === "SpreadElement") {
|
|
242
|
+
throw new Error(
|
|
243
|
+
`CedarJS CJS compat plugin encountered an unsupported pattern in module.exports = { ... } at ${formatLoc(prop)} (spread element (...)). File: ${id}
|
|
244
|
+
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.`
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
if (prop.computed) {
|
|
248
|
+
throw new Error(
|
|
249
|
+
`CedarJS CJS compat plugin encountered an unsupported pattern in module.exports = { ... } at ${formatLoc(prop)} (computed property key ([expr])). File: ${id}
|
|
250
|
+
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.`
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
if (prop.method) {
|
|
254
|
+
throw new Error(
|
|
255
|
+
`CedarJS CJS compat plugin encountered an unsupported pattern in module.exports = { ... } at ${formatLoc(prop)} (method shorthand). File: ${id}
|
|
256
|
+
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.`
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
if (prop.shorthand) {
|
|
260
|
+
throw new Error(
|
|
261
|
+
`CedarJS CJS compat plugin encountered an unsupported pattern in module.exports = { ... } at ${formatLoc(prop)} (shorthand property). File: ${id}
|
|
262
|
+
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.`
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
const keyName = getIdentifierName(prop.key);
|
|
266
|
+
if (keyName && keyName !== "default") {
|
|
267
|
+
namedExports.push(keyName);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
const dirPath = JSON.stringify(path.dirname(id));
|
|
273
|
+
const filePath = JSON.stringify(id);
|
|
274
|
+
const hasEsModuleFlag = namedExports.includes("__esModule");
|
|
275
|
+
const safeNamedExports = namedExports.filter(
|
|
276
|
+
(e) => e !== "__esModule" && e !== "default"
|
|
277
|
+
);
|
|
278
|
+
const namedExportLines = safeNamedExports.map(
|
|
279
|
+
(name) => `export const ${name} = __cjs_result__[${JSON.stringify(name)}]`
|
|
280
|
+
).join("\n");
|
|
281
|
+
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__`;
|
|
282
|
+
return {
|
|
283
|
+
code: `
|
|
284
|
+
import { createRequire as __createRequire__ } from 'node:module'
|
|
285
|
+
const require = __createRequire__(${filePath})
|
|
286
|
+
const module = { exports: {} }
|
|
287
|
+
const exports = module.exports
|
|
288
|
+
const __dirname = ${dirPath}
|
|
289
|
+
const __filename = ${filePath}
|
|
290
|
+
;(function() {
|
|
291
|
+
${code}
|
|
292
|
+
}).call(module.exports)
|
|
293
|
+
const __cjs_result__ = module.exports
|
|
294
|
+
${defaultExportLine}
|
|
295
|
+
${namedExportLines}
|
|
296
|
+
`,
|
|
297
|
+
// Source maps are not generated. If you hit a break-point issue inside
|
|
298
|
+
// a CJS file loaded through this plugin, the line numbers will be off
|
|
299
|
+
// by the number of lines in the wrapper preamble (~10).
|
|
300
|
+
map: null
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
export {
|
|
306
|
+
cedarCjsCompatPlugin
|
|
307
|
+
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vite-plugin-cedar-universal-deploy.d.ts","sourceRoot":"","sources":["../../src/plugins/vite-plugin-cedar-universal-deploy.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"vite-plugin-cedar-universal-deploy.d.ts","sourceRoot":"","sources":["../../src/plugins/vite-plugin-cedar-universal-deploy.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAA;AAMlC,MAAM,WAAW,iCAAiC;IAChD,WAAW,CAAC,EAAE,MAAM,CAAA;CACrB;AA6GD,wBAAgB,0BAA0B,CACxC,OAAO,GAAE,iCAAsC,GAC9C,MAAM,CA4DR"}
|