@cedarjs/internal 9.0.0-canary.1784 → 9.0.0-canary.3124

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. package/dist/ast.d.ts +1 -0
  2. package/dist/ast.d.ts.map +1 -1
  3. package/dist/ast.js +21 -0
  4. package/dist/build/api-graphql-transforms.d.ts +34 -0
  5. package/dist/build/api-graphql-transforms.d.ts.map +1 -0
  6. package/dist/build/api-graphql-transforms.js +129 -0
  7. package/dist/build/api.d.ts +4 -1
  8. package/dist/build/api.d.ts.map +1 -1
  9. package/dist/build/api.js +307 -21
  10. package/dist/build/auto-import.d.ts +10 -0
  11. package/dist/build/auto-import.d.ts.map +1 -0
  12. package/dist/build/auto-import.js +120 -0
  13. package/dist/build/directory-named-import.d.ts +2 -0
  14. package/dist/build/directory-named-import.d.ts.map +1 -0
  15. package/dist/build/directory-named-import.js +26 -0
  16. package/dist/build/esbuild-plugin-api-graphql.d.ts +6 -0
  17. package/dist/build/esbuild-plugin-api-graphql.d.ts.map +1 -0
  18. package/dist/build/esbuild-plugin-api-graphql.js +76 -0
  19. package/dist/build/esbuild-plugin-cedar-otel-wrapping.d.ts +13 -0
  20. package/dist/build/esbuild-plugin-cedar-otel-wrapping.d.ts.map +1 -0
  21. package/dist/build/esbuild-plugin-cedar-otel-wrapping.js +180 -0
  22. package/dist/build/esbuild-plugin-handler-als-wrapping.d.ts +2 -0
  23. package/dist/build/esbuild-plugin-handler-als-wrapping.d.ts.map +1 -0
  24. package/dist/build/esbuild-plugin-handler-als-wrapping.js +34 -0
  25. package/dist/build/import-dir.d.ts +22 -0
  26. package/dist/build/import-dir.d.ts.map +1 -0
  27. package/dist/build/import-dir.js +54 -0
  28. package/dist/build/import-extensions.d.ts +47 -0
  29. package/dist/build/import-extensions.d.ts.map +1 -0
  30. package/dist/build/import-extensions.js +59 -0
  31. package/dist/build/job-path-injector.d.ts +15 -0
  32. package/dist/build/job-path-injector.d.ts.map +1 -0
  33. package/dist/build/job-path-injector.js +69 -0
  34. package/dist/build/src-alias.d.ts +14 -0
  35. package/dist/build/src-alias.d.ts.map +1 -0
  36. package/dist/build/src-alias.js +17 -0
  37. package/dist/build/tsconfig-paths.d.ts +2 -0
  38. package/dist/build/tsconfig-paths.d.ts.map +1 -0
  39. package/dist/build/tsconfig-paths.js +72 -0
  40. package/dist/files.js +1 -1
  41. package/dist/generate/clientPreset.d.ts +0 -7
  42. package/dist/generate/clientPreset.d.ts.map +1 -1
  43. package/dist/generate/clientPreset.js +12 -6
  44. package/dist/generate/generate.d.ts.map +1 -1
  45. package/dist/generate/generate.js +14 -2
  46. package/dist/generate/gqlormSchema.d.ts +113 -0
  47. package/dist/generate/gqlormSchema.d.ts.map +1 -0
  48. package/dist/generate/gqlormSchema.js +1020 -0
  49. package/dist/generate/graphqlCodeGen.d.ts.map +1 -1
  50. package/dist/generate/graphqlCodeGen.js +94 -30
  51. package/dist/generate/graphqlSchema.d.ts.map +1 -1
  52. package/dist/generate/graphqlSchema.js +10 -15
  53. package/dist/generate/plugins/rw-typescript-resolvers/index.d.ts.map +1 -1
  54. package/dist/generate/plugins/rw-typescript-resolvers/index.js +3 -3
  55. package/dist/generate/plugins/rw-typescript-resolvers/visitor.d.ts +3 -2
  56. package/dist/generate/plugins/rw-typescript-resolvers/visitor.d.ts.map +1 -1
  57. package/dist/generate/plugins/rw-typescript-resolvers/visitor.js +35 -30
  58. package/dist/generate/templates/all-gqlorm.d.ts.template +7 -0
  59. package/dist/generate/templates/mirror-cell-fragment.d.ts.template +18 -0
  60. package/dist/generate/templates.d.ts.map +1 -1
  61. package/dist/generate/templates.js +1 -6
  62. package/dist/generate/typeDefinitions.d.ts +1 -0
  63. package/dist/generate/typeDefinitions.d.ts.map +1 -1
  64. package/dist/generate/typeDefinitions.js +25 -9
  65. package/dist/generate/watch.js +16 -8
  66. package/dist/gql.d.ts +20 -0
  67. package/dist/gql.d.ts.map +1 -1
  68. package/dist/gql.js +24 -0
  69. package/dist/index.d.ts +0 -1
  70. package/dist/index.d.ts.map +1 -1
  71. package/dist/index.js +0 -1
  72. package/dist/jsx.d.ts.map +1 -1
  73. package/dist/routes.d.ts.map +1 -1
  74. package/dist/routes.js +2 -3
  75. package/dist/validateSchema.js +2 -2
  76. package/package.json +92 -102
  77. package/LICENSE +0 -21
  78. package/dist/cjs/ast.d.ts +0 -26
  79. package/dist/cjs/ast.d.ts.map +0 -1
  80. package/dist/cjs/ast.js +0 -201
  81. package/dist/cjs/build/api.d.ts +0 -6
  82. package/dist/cjs/build/api.d.ts.map +0 -1
  83. package/dist/cjs/build/api.js +0 -109
  84. package/dist/cjs/cliLogger.d.ts +0 -11
  85. package/dist/cjs/cliLogger.d.ts.map +0 -1
  86. package/dist/cjs/cliLogger.js +0 -36
  87. package/dist/cjs/dev.d.ts +0 -2
  88. package/dist/cjs/dev.d.ts.map +0 -1
  89. package/dist/cjs/dev.js +0 -41
  90. package/dist/cjs/files.d.ts +0 -56
  91. package/dist/cjs/files.d.ts.map +0 -1
  92. package/dist/cjs/files.js +0 -230
  93. package/dist/cjs/generate/clientPreset.d.ts +0 -17
  94. package/dist/cjs/generate/clientPreset.d.ts.map +0 -1
  95. package/dist/cjs/generate/clientPreset.js +0 -85
  96. package/dist/cjs/generate/generate.d.ts +0 -10
  97. package/dist/cjs/generate/generate.d.ts.map +0 -1
  98. package/dist/cjs/generate/generate.js +0 -91
  99. package/dist/cjs/generate/graphqlCodeGen.d.ts +0 -16
  100. package/dist/cjs/generate/graphqlCodeGen.d.ts.map +0 -1
  101. package/dist/cjs/generate/graphqlCodeGen.js +0 -346
  102. package/dist/cjs/generate/graphqlSchema.d.ts +0 -8
  103. package/dist/cjs/generate/graphqlSchema.d.ts.map +0 -1
  104. package/dist/cjs/generate/graphqlSchema.js +0 -162
  105. package/dist/cjs/generate/plugins/rw-typescript-resolvers/index.d.ts +0 -7
  106. package/dist/cjs/generate/plugins/rw-typescript-resolvers/index.d.ts.map +0 -1
  107. package/dist/cjs/generate/plugins/rw-typescript-resolvers/index.js +0 -65
  108. package/dist/cjs/generate/plugins/rw-typescript-resolvers/visitor.d.ts +0 -9
  109. package/dist/cjs/generate/plugins/rw-typescript-resolvers/visitor.d.ts.map +0 -1
  110. package/dist/cjs/generate/plugins/rw-typescript-resolvers/visitor.js +0 -84
  111. package/dist/cjs/generate/possibleTypes.d.ts +0 -36
  112. package/dist/cjs/generate/possibleTypes.d.ts.map +0 -1
  113. package/dist/cjs/generate/possibleTypes.js +0 -106
  114. package/dist/cjs/generate/templates/all-currentUser.d.ts.template +0 -30
  115. package/dist/cjs/generate/templates/api-globImports.d.ts.template +0 -9
  116. package/dist/cjs/generate/templates/api-globalContext.d.ts.template +0 -7
  117. package/dist/cjs/generate/templates/api-scenarios.d.ts.template +0 -10
  118. package/dist/cjs/generate/templates/api-test-globals.d.ts.template +0 -5
  119. package/dist/cjs/generate/templates/mirror-cell.d.ts.template +0 -13
  120. package/dist/cjs/generate/templates/mirror-directoryNamedModule.d.ts.template +0 -5
  121. package/dist/cjs/generate/templates/web-routerRoutes.d.ts.template +0 -20
  122. package/dist/cjs/generate/templates/web-routesPages.d.ts.template +0 -13
  123. package/dist/cjs/generate/templates/web-test-globals.d.ts.template +0 -7
  124. package/dist/cjs/generate/templates.d.ts +0 -6
  125. package/dist/cjs/generate/templates.d.ts.map +0 -1
  126. package/dist/cjs/generate/templates.js +0 -68
  127. package/dist/cjs/generate/trustedDocuments.d.ts +0 -3
  128. package/dist/cjs/generate/trustedDocuments.d.ts.map +0 -1
  129. package/dist/cjs/generate/trustedDocuments.js +0 -95
  130. package/dist/cjs/generate/typeDefinitions.d.ts +0 -30
  131. package/dist/cjs/generate/typeDefinitions.d.ts.map +0 -1
  132. package/dist/cjs/generate/typeDefinitions.js +0 -369
  133. package/dist/cjs/generate/types.d.ts +0 -6
  134. package/dist/cjs/generate/types.d.ts.map +0 -1
  135. package/dist/cjs/generate/types.js +0 -16
  136. package/dist/cjs/generate/watch.d.ts +0 -3
  137. package/dist/cjs/generate/watch.d.ts.map +0 -1
  138. package/dist/cjs/generate/watch.js +0 -124
  139. package/dist/cjs/gql.d.ts +0 -14
  140. package/dist/cjs/gql.d.ts.map +0 -1
  141. package/dist/cjs/gql.js +0 -108
  142. package/dist/cjs/index.d.ts +0 -11
  143. package/dist/cjs/index.d.ts.map +0 -1
  144. package/dist/cjs/index.js +0 -49
  145. package/dist/cjs/jsx.d.ts +0 -16
  146. package/dist/cjs/jsx.d.ts.map +0 -1
  147. package/dist/cjs/jsx.js +0 -101
  148. package/dist/cjs/jsxAttributeValue.d.ts +0 -2
  149. package/dist/cjs/jsxAttributeValue.d.ts.map +0 -1
  150. package/dist/cjs/jsxAttributeValue.js +0 -129
  151. package/dist/cjs/package.json +0 -1
  152. package/dist/cjs/project.d.ts +0 -8
  153. package/dist/cjs/project.d.ts.map +0 -1
  154. package/dist/cjs/project.js +0 -82
  155. package/dist/cjs/routes.d.ts +0 -42
  156. package/dist/cjs/routes.d.ts.map +0 -1
  157. package/dist/cjs/routes.js +0 -112
  158. package/dist/cjs/ts2js.d.ts +0 -40
  159. package/dist/cjs/ts2js.d.ts.map +0 -1
  160. package/dist/cjs/ts2js.js +0 -159
  161. package/dist/cjs/validateSchema.d.ts +0 -14
  162. package/dist/cjs/validateSchema.d.ts.map +0 -1
  163. package/dist/cjs/validateSchema.js +0 -195
  164. package/dist/ts2js.d.ts +0 -40
  165. package/dist/ts2js.d.ts.map +0 -1
  166. package/dist/ts2js.js +0 -119
package/dist/ast.d.ts CHANGED
@@ -17,6 +17,7 @@ export declare const getNamedExports: (ast: types.Node) => NamedExports[];
17
17
  */
18
18
  export declare const getGqlQueries: (ast: types.Node) => string[];
19
19
  export declare const getCellGqlQuery: (ast: types.Node) => undefined;
20
+ export declare const getCellGqlFragment: (ast: types.Node) => undefined;
20
21
  export declare const hasDefaultExport: (ast: types.Node) => boolean;
21
22
  export declare const getDefaultExportLocation: (ast: types.Node) => {
22
23
  line: number;
package/dist/ast.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"ast.d.ts","sourceRoot":"","sources":["../src/ast.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAA;AAmBnC,eAAO,MAAM,SAAS,GAAI,UAAU,MAAM,KAAG,KAAK,CAAC,IAyBlD,CAAA;AAED,UAAU,YAAY;IACpB,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,WAAW,GAAG,UAAU,GAAG,UAAU,GAAG,OAAO,CAAA;IACrD,QAAQ,EAAE;QACR,IAAI,EAAE,MAAM,CAAA;QACZ,MAAM,EAAE,MAAM,CAAA;KACf,CAAA;CACF;AACD;;GAEG;AACH,eAAO,MAAM,eAAe,GAAI,KAAK,KAAK,CAAC,IAAI,KAAG,YAAY,EA4D7D,CAAA;AAED;;GAEG;AACH,eAAO,MAAM,aAAa,GAAI,KAAK,KAAK,CAAC,IAAI,aAY5C,CAAA;AAED,eAAO,MAAM,eAAe,GAAI,KAAK,KAAK,CAAC,IAAI,cA8B9C,CAAA;AAED,eAAO,MAAM,gBAAgB,GAAI,KAAK,KAAK,CAAC,IAAI,KAAG,OASlD,CAAA;AAED,eAAO,MAAM,wBAAwB,GACnC,KAAK,KAAK,CAAC,IAAI,KACd;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAAG,IA0CrC,CAAA"}
1
+ {"version":3,"file":"ast.d.ts","sourceRoot":"","sources":["../src/ast.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAA;AAYnC,eAAO,MAAM,SAAS,GAAI,UAAU,MAAM,KAAG,KAAK,CAAC,IAyBlD,CAAA;AAED,UAAU,YAAY;IACpB,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,WAAW,GAAG,UAAU,GAAG,UAAU,GAAG,OAAO,CAAA;IACrD,QAAQ,EAAE;QACR,IAAI,EAAE,MAAM,CAAA;QACZ,MAAM,EAAE,MAAM,CAAA;KACf,CAAA;CACF;AACD;;GAEG;AACH,eAAO,MAAM,eAAe,GAAI,KAAK,KAAK,CAAC,IAAI,KAAG,YAAY,EA4D7D,CAAA;AAED;;GAEG;AACH,eAAO,MAAM,aAAa,GAAI,KAAK,KAAK,CAAC,IAAI,aAY5C,CAAA;AAED,eAAO,MAAM,eAAe,GAAI,KAAK,KAAK,CAAC,IAAI,cA8B9C,CAAA;AAED,eAAO,MAAM,kBAAkB,GAAI,KAAK,KAAK,CAAC,IAAI,cA8BjD,CAAA;AAED,eAAO,MAAM,gBAAgB,GAAI,KAAK,KAAK,CAAC,IAAI,KAAG,OASlD,CAAA;AAED,eAAO,MAAM,wBAAwB,GACnC,KAAK,KAAK,CAAC,IAAI,KACd;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAAG,IA0CrC,CAAA"}
package/dist/ast.js CHANGED
@@ -115,6 +115,26 @@ const getCellGqlQuery = (ast) => {
115
115
  });
116
116
  return cellQuery;
117
117
  };
118
+ const getCellGqlFragment = (ast) => {
119
+ let cellFragment = void 0;
120
+ traverse(ast, {
121
+ ExportNamedDeclaration({ node }) {
122
+ if (node.exportKind === "value" && types.isVariableDeclaration(node.declaration)) {
123
+ const exportedFragmentNode = node.declaration.declarations.find(
124
+ (d) => {
125
+ return types.isIdentifier(d.id) && d.id.name === "FRAGMENT" && types.isTaggedTemplateExpression(d.init);
126
+ }
127
+ );
128
+ if (exportedFragmentNode) {
129
+ const templateExpression = exportedFragmentNode.init;
130
+ cellFragment = templateExpression.quasi.quasis[0].value.raw;
131
+ }
132
+ }
133
+ return;
134
+ }
135
+ });
136
+ return cellFragment;
137
+ };
118
138
  const hasDefaultExport = (ast) => {
119
139
  let exported = false;
120
140
  traverse(ast, {
@@ -154,6 +174,7 @@ const getDefaultExportLocation = (ast) => {
154
174
  };
155
175
  export {
156
176
  fileToAst,
177
+ getCellGqlFragment,
157
178
  getCellGqlQuery,
158
179
  getDefaultExportLocation,
159
180
  getGqlQueries,
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Extracts the options argument from createGraphQLHandler calls and stores
3
+ * them in an exported variable. Returns the transformed code, or null if no
4
+ * transformation was needed.
5
+ *
6
+ * Transforms:
7
+ * export const handler = createGraphQLHandler({ options })
8
+ * into:
9
+ * export const __cedar_graphqlOptions = { options }
10
+ * export const handler = createGraphQLHandler(__cedar_graphqlOptions)
11
+ */
12
+ export declare function applyGraphqlOptionsExtract(code: string): string | null;
13
+ /**
14
+ * Injects the auto-generated gqlorm backend into graphql.ts at build time.
15
+ *
16
+ * When `experimental.gqlorm.enabled = true` and `.cedar/gqlorm/backend.ts`
17
+ * exists, this function:
18
+ *
19
+ * 1. Adds imports at the top of graphql.ts:
20
+ * import * as __gqlorm_sdl__ from '../../../.cedar/gqlorm/backend'
21
+ * import { db as __gqlorm_db__ } from 'src/lib/db'
22
+ *
23
+ * 2. Inserts a statement immediately before the `createGraphQLHandler` call:
24
+ * Object.assign(sdls, {
25
+ * __gqlorm__: {
26
+ * schema: __gqlorm_sdl__.schema,
27
+ * resolvers: __gqlorm_sdl__.createGqlormResolvers(__gqlorm_db__),
28
+ * },
29
+ * })
30
+ *
31
+ * Returns the transformed code, or null if no transformation was needed.
32
+ */
33
+ export declare function applyGqlormInject(code: string, id: string, dbExt?: '.ts' | '.js'): string | null;
34
+ //# sourceMappingURL=api-graphql-transforms.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api-graphql-transforms.d.ts","sourceRoot":"","sources":["../../src/build/api-graphql-transforms.ts"],"names":[],"mappings":"AAkBA;;;;;;;;;;GAUG;AACH,wBAAgB,0BAA0B,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CA8GtE;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,iBAAiB,CAC/B,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,MAAM,EACV,KAAK,GAAE,KAAK,GAAG,KAAa,GAC3B,MAAM,GAAG,IAAI,CAmFf"}
@@ -0,0 +1,129 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import { parseSync, Visitor } from "oxc-parser";
4
+ import {
5
+ getConfig,
6
+ getPaths,
7
+ importStatementPath
8
+ } from "@cedarjs/project-config";
9
+ const GRAPHQL_OPTIONS_ERROR_PREFIX = "Could not read the GraphQL options in api/src/functions/graphql. Cedar needs to statically extract them so the api server can serve /graphql and its health endpoint. ";
10
+ const GRAPHQL_OPTIONS_ERROR_SUFFIX = " Expected a single `createGraphQLHandler({ ... })` call with the options passed directly as an object, an identifier, a call, or a ternary.";
11
+ function applyGraphqlOptionsExtract(code) {
12
+ if (code.includes("__cedar_graphqlOptions")) {
13
+ return null;
14
+ }
15
+ const { program } = parseSync("graphql.ts", code, {
16
+ // lang is only a parse hint; 'ts' also parses JS (the graphql handler can
17
+ // be graphql.js in JS projects), so this is safe for both file types.
18
+ lang: "ts",
19
+ sourceType: "module"
20
+ });
21
+ const importNames = /* @__PURE__ */ new Set();
22
+ for (const node of program.body) {
23
+ if (node.type === "ImportDeclaration") {
24
+ if (node.source.value !== "@cedarjs/graphql-server") {
25
+ continue;
26
+ }
27
+ for (const specifier of node.specifiers) {
28
+ if (specifier.type === "ImportSpecifier" && specifier.imported.type === "Identifier" && specifier.imported.name === "createGraphQLHandler") {
29
+ importNames.add(specifier.local.name);
30
+ }
31
+ }
32
+ }
33
+ }
34
+ if (importNames.size === 0) {
35
+ return null;
36
+ }
37
+ const callExpressionPaths = [];
38
+ new Visitor({
39
+ CallExpression(node) {
40
+ if (node.callee.type === "Identifier" && importNames.has(node.callee.name)) {
41
+ callExpressionPaths.push(node);
42
+ }
43
+ }
44
+ }).visit(program);
45
+ if (callExpressionPaths.length > 1) {
46
+ throw new Error(
47
+ GRAPHQL_OPTIONS_ERROR_PREFIX + "Found more than one call to createGraphQLHandler." + GRAPHQL_OPTIONS_ERROR_SUFFIX
48
+ );
49
+ }
50
+ const callExpression = callExpressionPaths[0];
51
+ if (!callExpression) {
52
+ throw new Error(
53
+ GRAPHQL_OPTIONS_ERROR_PREFIX + "createGraphQLHandler is imported but never called." + GRAPHQL_OPTIONS_ERROR_SUFFIX
54
+ );
55
+ }
56
+ const options = callExpression.arguments[0];
57
+ if (!options) {
58
+ throw new Error(
59
+ GRAPHQL_OPTIONS_ERROR_PREFIX + "createGraphQLHandler was called without any options." + GRAPHQL_OPTIONS_ERROR_SUFFIX
60
+ );
61
+ }
62
+ if (options.type !== "Identifier" && options.type !== "ObjectExpression" && options.type !== "CallExpression" && options.type !== "ConditionalExpression") {
63
+ throw new Error(
64
+ GRAPHQL_OPTIONS_ERROR_PREFIX + `The options argument is a ${options.type}, which can't be read statically.` + GRAPHQL_OPTIONS_ERROR_SUFFIX
65
+ );
66
+ }
67
+ const optionsStart = options.start;
68
+ const optionsEnd = options.end;
69
+ const lineStart = code.lastIndexOf("\n", callExpression.start) + 1;
70
+ const indentMatch = /^[ \t]*/.exec(code.slice(lineStart));
71
+ const indent = indentMatch ? indentMatch[0] : "";
72
+ const optionsConst = `${indent}export const __cedar_graphqlOptions = ${code.slice(
73
+ optionsStart,
74
+ optionsEnd
75
+ )}
76
+ `;
77
+ const before = code.slice(0, lineStart);
78
+ const between = code.slice(lineStart, optionsStart);
79
+ const after = code.slice(optionsEnd);
80
+ return before + optionsConst + between + "__cedar_graphqlOptions" + after;
81
+ }
82
+ function applyGqlormInject(code, id, dbExt = ".ts") {
83
+ if (code.includes("__gqlorm_sdl__")) {
84
+ return null;
85
+ }
86
+ if (!code.includes("createGraphQLHandler")) {
87
+ return null;
88
+ }
89
+ if (!getConfig().experimental?.gqlorm?.enabled) {
90
+ return null;
91
+ }
92
+ const backendPathWithoutExt = path.join(
93
+ getPaths().generated.base,
94
+ "gqlorm",
95
+ "backend"
96
+ );
97
+ if (!fs.existsSync(backendPathWithoutExt + ".ts")) {
98
+ return null;
99
+ }
100
+ const handlerPattern = /^export\s+const\s+(\w+)\s*=\s*createGraphQLHandler\s*\(/m;
101
+ const handlerMatch = handlerPattern.exec(code);
102
+ if (!handlerMatch) {
103
+ return null;
104
+ }
105
+ const handlerLineStart = code.lastIndexOf("\n", handlerMatch.index) + 1;
106
+ const relPath = importStatementPath(
107
+ path.relative(path.dirname(id), backendPathWithoutExt)
108
+ ) + ".ts";
109
+ const dbSrcPath = path.join(getPaths().api.src, "lib", "db");
110
+ const relDbPath = importStatementPath(path.relative(path.dirname(id), dbSrcPath)) + dbExt;
111
+ const importDb = `import { db as __gqlorm_db__ } from '${relDbPath}'`;
112
+ const importSdl = `import * as __gqlorm_sdl__ from '${relPath}'`;
113
+ const importsToAdd = `${importDb}
114
+ ${importSdl}
115
+ `;
116
+ const sdlsMutation = `Object.assign(sdls, {
117
+ __gqlorm__: {
118
+ schema: __gqlorm_sdl__.schema,
119
+ resolvers: __gqlorm_sdl__.createGqlormResolvers(__gqlorm_db__),
120
+ },
121
+ })
122
+ `;
123
+ const transformed = importsToAdd + code.slice(0, handlerLineStart) + sdlsMutation + code.slice(handlerLineStart);
124
+ return transformed;
125
+ }
126
+ export {
127
+ applyGqlormInject,
128
+ applyGraphqlOptionsExtract
129
+ };
@@ -1,6 +1,9 @@
1
1
  import type { BuildOptions } from 'esbuild';
2
+ export { applySrcAlias } from './src-alias.js';
3
+ export { applyImportExtensions } from './import-extensions.js';
2
4
  export declare const buildApi: () => Promise<import("esbuild").BuildResult<BuildOptions>>;
3
5
  export declare const rebuildApi: () => Promise<import("esbuild").BuildResult<BuildOptions>>;
4
6
  export declare const cleanApiBuild: () => Promise<void>;
5
- export declare const transpileApi: (files: string[]) => Promise<import("esbuild").BuildResult<BuildOptions>>;
7
+ export declare const buildApiWithVite: () => Promise<import("rollup").RollupOutput | import("rollup").RollupOutput[] | import("rollup").RollupWatcher>;
8
+ export declare function fixSourceMaps(distDir: string, srcDir: string): Promise<void>;
6
9
  //# sourceMappingURL=api.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../src/build/api.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAgB,YAAY,EAAe,MAAM,SAAS,CAAA;AAatE,eAAO,MAAM,QAAQ,4DAOpB,CAAA;AAED,eAAO,MAAM,UAAU,4DAQtB,CAAA;AAED,eAAO,MAAM,aAAa,qBAGzB,CAAA;AAiCD,eAAO,MAAM,YAAY,GAAU,OAAO,MAAM,EAAE,yDAEjD,CAAA"}
1
+ {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../src/build/api.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAgB,YAAY,EAAe,MAAM,SAAS,CAAA;AAwCtE,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AAC9C,OAAO,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAA;AAI9D,eAAO,MAAM,QAAQ,4DAIpB,CAAA;AAED,eAAO,MAAM,UAAU,4DAStB,CAAA;AAED,eAAO,MAAM,aAAa,qBAGzB,CAAA;AA8UD,eAAO,MAAM,gBAAgB,iHA+D5B,CAAA;AAcD,wBAAsB,aAAa,CACjC,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,IAAI,CAAC,CAoDf"}
package/dist/build/api.js CHANGED
@@ -1,11 +1,37 @@
1
1
  import fs from "node:fs";
2
+ import path from "node:path";
2
3
  import { build, context } from "esbuild";
4
+ import MagicString from "magic-string";
5
+ import { build as viteBuild, normalizePath } from "vite";
6
+ import tsPathsMod from "vite-tsconfig-paths";
7
+ const tsconfigPaths = (
8
+ // @ts-expect-error – .default only exists at runtime in CJS double-wrap
9
+ // interop
10
+ tsPathsMod.default?.default || tsPathsMod.default || tsPathsMod
11
+ );
3
12
  import {
4
- getApiSideBabelPlugins,
13
+ getApiSideBabelConfigPath,
14
+ getApiSideBabelPluginsForVite,
5
15
  transformWithBabel
6
16
  } from "@cedarjs/babel-config";
7
- import { getConfig, getPaths, projectSideIsEsm } from "@cedarjs/project-config";
17
+ import { getConfig, getPaths, resolveFile } from "@cedarjs/project-config";
8
18
  import { findApiFiles } from "../files.js";
19
+ import {
20
+ applyGqlormInject,
21
+ applyGraphqlOptionsExtract
22
+ } from "./api-graphql-transforms.js";
23
+ import { applyAutoImports } from "./auto-import.js";
24
+ import { applyDirectoryNamedImport } from "./directory-named-import.js";
25
+ import { cedarApiGraphqlPlugin } from "./esbuild-plugin-api-graphql.js";
26
+ import { applyOtelWrapping } from "./esbuild-plugin-cedar-otel-wrapping.js";
27
+ import { applyHandlerAlsWrapping } from "./esbuild-plugin-handler-als-wrapping.js";
28
+ import { applyImportDir } from "./import-dir.js";
29
+ import { applyImportExtensions } from "./import-extensions.js";
30
+ import { applyJobPathInjector } from "./job-path-injector.js";
31
+ import { applySrcAlias } from "./src-alias.js";
32
+ import { applyTsconfigPaths } from "./tsconfig-paths.js";
33
+ import { applySrcAlias as applySrcAlias2 } from "./src-alias.js";
34
+ import { applyImportExtensions as applyImportExtensions2 } from "./import-extensions.js";
9
35
  let BUILD_CTX = null;
10
36
  const buildApi = async () => {
11
37
  BUILD_CTX?.dispose();
@@ -17,7 +43,10 @@ const rebuildApi = async () => {
17
43
  if (!BUILD_CTX) {
18
44
  BUILD_CTX = await context(getEsbuildOptions(apiFiles));
19
45
  }
20
- return BUILD_CTX.rebuild();
46
+ const result = await BUILD_CTX.rebuild();
47
+ const cedarPaths = getPaths();
48
+ await fixSourceMaps(cedarPaths.api.dist, cedarPaths.api.src);
49
+ return result;
21
50
  };
22
51
  const cleanApiBuild = async () => {
23
52
  const cedarPaths = getPaths();
@@ -26,40 +55,294 @@ const cleanApiBuild = async () => {
26
55
  const runCedarBabelTransformsPlugin = {
27
56
  name: "cedar-esbuild-babel-transform",
28
57
  setup(build2) {
29
- const cedarConfig = getConfig();
30
58
  build2.onLoad({ filter: /\.(js|ts|tsx|jsx)$/ }, async (args) => {
31
- const transformedCode = await transformWithBabel(
59
+ let fileContents = await fs.promises.readFile(args.path, "utf-8");
60
+ fileContents = applySrcAlias(
61
+ fileContents,
62
+ path.dirname(
63
+ path.relative(build2.initialOptions.absWorkingDir + "/src", args.path)
64
+ )
65
+ );
66
+ fileContents = applyTsconfigPaths(
67
+ fileContents,
32
68
  args.path,
33
- getApiSideBabelPlugins({
34
- openTelemetry: cedarConfig.experimental.opentelemetry.enabled && cedarConfig.experimental.opentelemetry.wrapApi,
35
- projectIsEsm: projectSideIsEsm("api")
36
- })
69
+ getPaths().api.base
37
70
  );
38
- if (transformedCode?.code) {
71
+ fileContents = applyDirectoryNamedImport(fileContents, args.path);
72
+ fileContents = applyAutoImports(fileContents);
73
+ fileContents = applyImportDir(fileContents, args.path)?.code ?? fileContents;
74
+ fileContents = applyJobPathInjector(fileContents, args.path, getPaths().api.jobs) ?? fileContents;
75
+ const normalizedPath = normalizePath(args.path);
76
+ const cedarPaths = getPaths();
77
+ const apiBabelConfigPath = getApiSideBabelConfigPath();
78
+ let code = fileContents;
79
+ if (apiBabelConfigPath) {
80
+ const transformedCode = await transformWithBabel(
81
+ fileContents,
82
+ args.path,
83
+ getApiSideBabelPluginsForVite(),
84
+ "inline",
85
+ true
86
+ );
87
+ if (!transformedCode?.code) {
88
+ throw new Error(`Could not transform file: ${args.path}`);
89
+ }
90
+ code = transformedCode.code;
91
+ }
92
+ code = applyImportExtensions(code, args.path);
93
+ const functionsDir = normalizePath(cedarPaths.api.functions);
94
+ if (normalizedPath.startsWith(functionsDir + "/")) {
95
+ code = applyHandlerAlsWrapping(code) ?? code;
96
+ }
97
+ if (normalizedPath.startsWith(normalizePath(cedarPaths.api.src) + "/")) {
98
+ code = applyOtelWrapping(code, args.path, cedarPaths.api.src) ?? code;
99
+ }
100
+ return {
101
+ contents: code,
102
+ // Babel output is always plain JS. Without Babel the contents are
103
+ // still TypeScript/JSX, so pick the loader matching the file's
104
+ // extension and let esbuild do the stripping.
105
+ loader: apiBabelConfigPath ? "js" : getEsbuildLoader(args.path)
106
+ };
107
+ });
108
+ }
109
+ };
110
+ function getEsbuildLoader(filePath) {
111
+ switch (path.extname(filePath)) {
112
+ case ".ts":
113
+ return "ts";
114
+ case ".tsx":
115
+ return "tsx";
116
+ case ".jsx":
117
+ return "jsx";
118
+ default:
119
+ return "js";
120
+ }
121
+ }
122
+ function createImportDirVitePlugin() {
123
+ return {
124
+ name: "cedar-internal-import-dir",
125
+ enforce: "pre",
126
+ transform(code, id) {
127
+ if (!/\.(js|ts|tsx|jsx)$/.test(id)) {
128
+ return null;
129
+ }
130
+ if (id.includes("node_modules")) {
131
+ return null;
132
+ }
133
+ const result = applyImportDir(code, id);
134
+ if (!result) {
135
+ return null;
136
+ }
137
+ return { code: result.code, map: result.map };
138
+ }
139
+ };
140
+ }
141
+ function createDirectoryNamedImportVitePlugin() {
142
+ return {
143
+ name: "cedar-internal-directory-named-import",
144
+ enforce: "pre",
145
+ resolveId(id, importer) {
146
+ if (!id.startsWith(".") || !importer) {
147
+ return null;
148
+ }
149
+ if (importer.includes("node_modules")) {
150
+ return null;
151
+ }
152
+ const absolutePath = path.resolve(path.dirname(importer), id);
153
+ if (resolveFile(absolutePath)) {
154
+ return null;
155
+ }
156
+ const indexPath = path.join(absolutePath, "index");
157
+ const resolvedIndex = resolveFile(indexPath);
158
+ if (resolvedIndex) {
159
+ return normalizePath(resolvedIndex);
160
+ }
161
+ const basename = path.basename(absolutePath);
162
+ const dirNamedPath = path.join(absolutePath, basename);
163
+ const resolvedDirNamed = resolveFile(dirNamedPath);
164
+ if (resolvedDirNamed) {
165
+ return normalizePath(resolvedDirNamed);
166
+ }
167
+ return null;
168
+ }
169
+ };
170
+ }
171
+ function createCedarViteApiPlugin() {
172
+ const cedarConfig = getConfig();
173
+ return {
174
+ name: "cedar-vite-api-babel-transform",
175
+ enforce: "pre",
176
+ async transform(code, id) {
177
+ if (!/\.(js|ts|tsx|jsx)$/.test(id)) {
178
+ return null;
179
+ }
180
+ if (id.includes("node_modules")) {
181
+ return null;
182
+ }
183
+ const cedarPaths = getPaths();
184
+ if (!normalizePath(id).startsWith(normalizePath(cedarPaths.api.base))) {
185
+ return null;
186
+ }
187
+ let sourceCode = code;
188
+ const normalizedId = normalizePath(id);
189
+ sourceCode = applySrcAlias(
190
+ sourceCode,
191
+ path.dirname(path.relative(cedarPaths.api.src, id))
192
+ );
193
+ sourceCode = applyAutoImports(sourceCode);
194
+ sourceCode = applyJobPathInjector(sourceCode, id, cedarPaths.api.jobs) ?? sourceCode;
195
+ if (normalizedId.endsWith("/graphql.ts") || normalizedId.endsWith("/graphql.js")) {
196
+ sourceCode = applyGraphqlOptionsExtract(sourceCode) ?? sourceCode;
197
+ sourceCode = applyGqlormInject(sourceCode, id) ?? sourceCode;
198
+ }
199
+ const apiBabelConfigPath = getApiSideBabelConfigPath();
200
+ const transformedCode = apiBabelConfigPath ? await transformWithBabel(
201
+ sourceCode,
202
+ id,
203
+ getApiSideBabelPluginsForVite(),
204
+ true,
205
+ true
206
+ ) : null;
207
+ if (apiBabelConfigPath && !transformedCode?.code) {
208
+ throw new Error(`Could not transform file: ${id}`);
209
+ }
210
+ let outputCode = transformedCode?.code ?? sourceCode;
211
+ if (cedarConfig.experimental?.opentelemetry?.enabled && cedarConfig.experimental?.opentelemetry?.wrapApi) {
212
+ outputCode = applyOtelWrapping(outputCode, id, cedarPaths.api.src) ?? outputCode;
213
+ }
214
+ outputCode = applyImportExtensions(outputCode, id);
215
+ const functionsDir = normalizePath(cedarPaths.api.functions);
216
+ if (normalizedId.startsWith(functionsDir + "/")) {
217
+ outputCode = applyHandlerAlsWrapping(outputCode) ?? outputCode;
218
+ }
219
+ if (!transformedCode) {
220
+ if (outputCode === code) {
221
+ return null;
222
+ }
39
223
  return {
40
- contents: transformedCode.code,
41
- loader: "js"
224
+ code: outputCode,
225
+ map: new MagicString(code).generateMap({ hires: true })
42
226
  };
43
227
  }
44
- throw new Error(`Could not transform file: ${args.path}`);
45
- });
228
+ return {
229
+ code: outputCode,
230
+ map: transformedCode.map ?? null
231
+ };
232
+ }
233
+ };
234
+ }
235
+ const buildApiWithVite = async () => {
236
+ const cedarPaths = getPaths();
237
+ const apiFiles = findApiFiles();
238
+ const input = {};
239
+ for (const f of apiFiles) {
240
+ const key = path.relative(cedarPaths.api.src, f).replace(/\.(ts|tsx|mts|js|jsx|mjs)$/, "");
241
+ input[key] = f;
46
242
  }
243
+ return viteBuild({
244
+ root: cedarPaths.api.base,
245
+ logLevel: "warn",
246
+ build: {
247
+ ssr: true,
248
+ sourcemap: true,
249
+ outDir: cedarPaths.api.dist,
250
+ rollupOptions: {
251
+ input,
252
+ output: {
253
+ format: "es",
254
+ preserveModules: true,
255
+ preserveModulesRoot: cedarPaths.api.src,
256
+ entryFileNames: "[name].js",
257
+ // Directives (and other entry modules) intentionally use both named
258
+ // and default exports (e.g. `export const schema` + `export default`).
259
+ // Tell Rollup to expect this so it doesn't warn about mixed exports.
260
+ exports: "named"
261
+ },
262
+ external: (id) => {
263
+ if (id.startsWith("node:")) {
264
+ return true;
265
+ }
266
+ if (!id.startsWith(".") && !path.isAbsolute(id)) {
267
+ return true;
268
+ }
269
+ return false;
270
+ }
271
+ }
272
+ },
273
+ // cedarImportDirPlugin must run before the Babel transform so glob imports
274
+ // are expanded before Babel sees the code. The Babel import-dir plugin is
275
+ // not part of getApiSideBabelPluginsForVite(); this inline Vite plugin is
276
+ // its replacement for this code path (both CJS and ESM output via Rollup).
277
+ // tsconfigPaths resolves user-defined tsconfig.json `paths` aliases; it
278
+ // replaces the Babel module-resolver's tsconfig-paths handling for this
279
+ // code path.
280
+ plugins: [
281
+ tsconfigPaths(),
282
+ createImportDirVitePlugin(),
283
+ createDirectoryNamedImportVitePlugin(),
284
+ createCedarViteApiPlugin()
285
+ ]
286
+ });
47
287
  };
48
288
  const transpileApi = async (files) => {
49
- return build(getEsbuildOptions(files));
289
+ const result = await build(getEsbuildOptions(files));
290
+ const cedarPaths = getPaths();
291
+ await fixSourceMaps(cedarPaths.api.dist, cedarPaths.api.src);
292
+ return result;
50
293
  };
294
+ async function fixSourceMaps(distDir, srcDir) {
295
+ let entries;
296
+ try {
297
+ entries = await fs.promises.readdir(distDir, {
298
+ recursive: true,
299
+ withFileTypes: true
300
+ });
301
+ } catch {
302
+ return;
303
+ }
304
+ const mapFiles = entries.filter((e) => e.isFile() && e.name.endsWith(".js.map")).map((e) => path.join(e.parentPath ?? e.path, e.name));
305
+ await Promise.all(
306
+ mapFiles.map(async (mapFile) => {
307
+ try {
308
+ const raw = await fs.promises.readFile(mapFile, "utf8");
309
+ const map = JSON.parse(raw);
310
+ if (!Array.isArray(map.sources) || map.sources.length === 0) {
311
+ return;
312
+ }
313
+ const jsFile = mapFile.slice(0, -4);
314
+ const baseName = path.relative(distDir, jsFile).replace(/\.js$/, "");
315
+ const srcFile = [".ts", ".tsx", ".jsx", ".js"].map((ext) => path.join(srcDir, baseName + ext)).find((f) => fs.existsSync(f));
316
+ if (!srcFile) {
317
+ return;
318
+ }
319
+ const correctRelPath = normalizePath(
320
+ path.relative(path.dirname(mapFile), srcFile)
321
+ );
322
+ if (map.sources[0] === correctRelPath) {
323
+ return;
324
+ }
325
+ map.sources = [correctRelPath];
326
+ await fs.promises.writeFile(mapFile, JSON.stringify(map), "utf8");
327
+ } catch {
328
+ }
329
+ })
330
+ );
331
+ }
51
332
  function getEsbuildOptions(files) {
52
333
  const cedarPaths = getPaths();
53
- const format = projectSideIsEsm("api") ? "esm" : "cjs";
54
334
  return {
55
335
  absWorkingDir: cedarPaths.api.base,
56
336
  entryPoints: files,
57
337
  platform: "node",
58
- target: "node20",
59
- format,
338
+ target: "node24",
339
+ format: "esm",
60
340
  allowOverwrite: true,
61
341
  bundle: false,
62
- plugins: [runCedarBabelTransformsPlugin],
342
+ // Registration order matters: cedarApiGraphqlPlugin (narrow filter) must
343
+ // come first so it claims graphql.ts before runCedarBabelTransformsPlugin
344
+ // (broad filter) can. See the NOTE on cedarApiGraphqlPlugin.
345
+ plugins: [cedarApiGraphqlPlugin, runCedarBabelTransformsPlugin],
63
346
  outdir: cedarPaths.api.dist,
64
347
  // setting this to 'true' will generate an external sourcemap x.js.map
65
348
  // AND set the sourceMappingURL comment
@@ -68,8 +351,11 @@ function getEsbuildOptions(files) {
68
351
  };
69
352
  }
70
353
  export {
354
+ applyImportExtensions2 as applyImportExtensions,
355
+ applySrcAlias2 as applySrcAlias,
71
356
  buildApi,
357
+ buildApiWithVite,
72
358
  cleanApiBuild,
73
- rebuildApi,
74
- transpileApi
359
+ fixSourceMaps,
360
+ rebuildApi
75
361
  };
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Injects imports for `gql` and `context` into API source files that reference
3
+ * them without importing them. Uses an oxc-parser AST walk to check bindings,
4
+ * so local declarations never receive a conflicting import.
5
+ *
6
+ * This replaces `babel-plugin-auto-import` for the esbuild API build path,
7
+ * matching the behavior of `cedarAutoImportsPlugin` in Vite builds.
8
+ */
9
+ export declare function applyAutoImports(code: string): string;
10
+ //# sourceMappingURL=auto-import.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auto-import.d.ts","sourceRoot":"","sources":["../../src/build/auto-import.ts"],"names":[],"mappings":"AAGA;;;;;;;GAOG;AAEH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAoBrD"}