@cedarjs/internal 6.0.0-rc.312 → 6.0.0-rc.340

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.
@@ -1,6 +1,6 @@
1
1
  import type { BuildOptions } from 'esbuild';
2
2
  export { applySrcAlias } from './src-alias.js';
3
- export { applyEsmExtensions } from './esm-extensions.js';
3
+ export { applyImportExtensions } from './import-extensions.js';
4
4
  export declare const buildApi: () => Promise<import("esbuild").BuildResult<BuildOptions>>;
5
5
  export declare const rebuildApi: () => Promise<import("esbuild").BuildResult<BuildOptions>>;
6
6
  export declare const cleanApiBuild: () => Promise<void>;
@@ -1 +1 @@
1
- {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../src/build/api.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAgB,YAAY,EAAe,MAAM,SAAS,CAAA;AA6CtE,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AAC9C,OAAO,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAA;AAIxD,eAAO,MAAM,QAAQ,4DAIpB,CAAA;AAED,eAAO,MAAM,UAAU,4DAStB,CAAA;AAED,eAAO,MAAM,aAAa,qBAGzB,CAAA;AA0VD,eAAO,MAAM,gBAAgB,iHAiE5B,CAAA;AAcD,wBAAsB,aAAa,CACjC,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,IAAI,CAAC,CAoDf"}
1
+ {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../src/build/api.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAgB,YAAY,EAAe,MAAM,SAAS,CAAA;AA6CtE,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;AAyVD,eAAO,MAAM,gBAAgB,iHAiE5B,CAAA;AAcD,wBAAsB,aAAa,CACjC,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,IAAI,CAAC,CAoDf"}
package/dist/build/api.js CHANGED
@@ -30,13 +30,13 @@ import { applyDirectoryNamedImport } from "./directory-named-import.js";
30
30
  import { cedarApiGraphqlPlugin } from "./esbuild-plugin-api-graphql.js";
31
31
  import { applyOtelWrapping } from "./esbuild-plugin-cedar-otel-wrapping.js";
32
32
  import { applyHandlerAlsWrapping } from "./esbuild-plugin-handler-als-wrapping.js";
33
- import { applyEsmExtensions } from "./esm-extensions.js";
34
33
  import { applyImportDir } from "./import-dir.js";
34
+ import { applyImportExtensions } from "./import-extensions.js";
35
35
  import { applyJobPathInjector } from "./job-path-injector.js";
36
36
  import { applySrcAlias } from "./src-alias.js";
37
37
  import { applyTsconfigPaths } from "./tsconfig-paths.js";
38
38
  import { applySrcAlias as applySrcAlias2 } from "./src-alias.js";
39
- import { applyEsmExtensions as applyEsmExtensions2 } from "./esm-extensions.js";
39
+ import { applyImportExtensions as applyImportExtensions2 } from "./import-extensions.js";
40
40
  let BUILD_CTX = null;
41
41
  const buildApi = async () => {
42
42
  BUILD_CTX?.dispose();
@@ -95,9 +95,7 @@ const runCedarBabelTransformsPlugin = {
95
95
  }
96
96
  code = transformedCode.code;
97
97
  }
98
- if (isEsm) {
99
- code = applyEsmExtensions(code, args.path);
100
- }
98
+ code = applyImportExtensions(code, args.path);
101
99
  const functionsDir = normalizePath(cedarPaths.api.functions);
102
100
  if (normalizedPath.startsWith(functionsDir + "/")) {
103
101
  code = applyHandlerAlsWrapping(code, {
@@ -222,9 +220,7 @@ function createCedarViteApiPlugin() {
222
220
  if (cedarConfig.experimental?.opentelemetry?.enabled && cedarConfig.experimental?.opentelemetry?.wrapApi) {
223
221
  outputCode = applyOtelWrapping(outputCode, id, cedarPaths.api.src) ?? outputCode;
224
222
  }
225
- if (isEsm) {
226
- outputCode = applyEsmExtensions(outputCode, id);
227
- }
223
+ outputCode = applyImportExtensions(outputCode, id);
228
224
  const functionsDir = normalizePath(cedarPaths.api.functions);
229
225
  if (normalizedId.startsWith(functionsDir + "/")) {
230
226
  outputCode = applyHandlerAlsWrapping(outputCode, {
@@ -369,7 +365,7 @@ function getEsbuildOptions(files) {
369
365
  };
370
366
  }
371
367
  export {
372
- applyEsmExtensions2 as applyEsmExtensions,
368
+ applyImportExtensions2 as applyImportExtensions,
373
369
  applySrcAlias2 as applySrcAlias,
374
370
  buildApi,
375
371
  buildApiWithVite,
@@ -14,8 +14,8 @@ import { applyAutoImports } from "./auto-import.js";
14
14
  import { applyDirectoryNamedImport } from "./directory-named-import.js";
15
15
  import { applyOtelWrapping } from "./esbuild-plugin-cedar-otel-wrapping.js";
16
16
  import { applyHandlerAlsWrapping } from "./esbuild-plugin-handler-als-wrapping.js";
17
- import { applyEsmExtensions } from "./esm-extensions.js";
18
17
  import { applyImportDir } from "./import-dir.js";
18
+ import { applyImportExtensions } from "./import-extensions.js";
19
19
  import { applySrcAlias } from "./src-alias.js";
20
20
  import { applyTsconfigPaths } from "./tsconfig-paths.js";
21
21
  const cedarApiGraphqlPlugin = {
@@ -55,9 +55,7 @@ const cedarApiGraphqlPlugin = {
55
55
  }
56
56
  code = transformedCode.code;
57
57
  }
58
- if (projectSideIsEsm("api")) {
59
- code = applyEsmExtensions(code, args.path);
60
- }
58
+ code = applyImportExtensions(code, args.path);
61
59
  if (cedarConfig.experimental?.opentelemetry?.enabled && cedarConfig.experimental?.opentelemetry?.wrapApi) {
62
60
  code = applyOtelWrapping(code, args.path, getPaths().api.src) ?? code;
63
61
  }
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Rewrites relative import specifiers so they point at the compiled `.js`
3
+ * output files instead of the TypeScript/JSX source files:
4
+ *
5
+ * - Extensionless imports (`from '../lib/db'`) get `.js` appended so that
6
+ * Node's ESM resolver can locate the compiled output at runtime. Node's
7
+ * CJS resolver appends the extension itself, so for CJS output this
8
+ * rewrite is behavior-neutral.
9
+ * - Explicit TypeScript-extension imports (`from './hello.ts'`, allowed by
10
+ * `allowImportingTsExtensions` in the project tsconfig) are rewritten to
11
+ * `.js`, since the file on disk after compilation is `hello.js`. This is
12
+ * required for both CJS and ESM output — neither Node resolver maps a
13
+ * `.ts` specifier to a `.js` file.
14
+ *
15
+ * Handles static imports/re-exports (`from '../lib/db'`), side-effect
16
+ * imports (`import '../lib/setup'`), dynamic imports (`import('../lib/db')`),
17
+ * and `require('../lib/db')` calls (emitted when a custom api/babel.config.js
18
+ * lowers ESM to CommonJS before this function runs).
19
+ *
20
+ * Always outputs `.js` because both build pipelines compile every source
21
+ * file (`.ts`, `.tsx`, `.jsx`) to `.js` output. For a source file
22
+ * `api/src/lib/db.ts` or `api/src/lib/db.tsx`, the output is always
23
+ * `api/dist/lib/db.js`, so all import specifiers must use `.js` at runtime.
24
+ *
25
+ * For the standalone esbuild build path (`runCedarBabelTransformsPlugin`)
26
+ * this replaces the `resolvePath` hook in `babel-plugin-module-resolver`,
27
+ * which is not part of `getApiSideBabelPluginsForVite()`.
28
+ *
29
+ * It is a plain function called inline — not an esbuild plugin — following
30
+ * the same pattern as `applySrcAlias`, `applyTsconfigPaths`, etc.
31
+ *
32
+ * Also handles Cedar SDL files whose names include a compound extension:
33
+ * `contacts.sdl.ts` compiles to `contacts.sdl.js`, so an import specifier
34
+ * `../graphql/contacts.sdl` (generated by applyImportDir's glob expansion)
35
+ * is rewritten to `../graphql/contacts.sdl.js`.
36
+ *
37
+ * Example: in `api/src/functions/graphql.ts` importing `../lib/db`, this
38
+ * rewrites it to `../lib/db.js` when `api/src/lib/db.ts` (or `db.tsx`)
39
+ * exists on disk.
40
+ *
41
+ * Files under the generated data directory (`.cedar/` or `.redwood/`) are
42
+ * excluded from rewriting: generated artifacts such as the gqlorm backend
43
+ * (`.cedar/gqlorm/backend.ts`) are never compiled to `.js`, so an explicit
44
+ * `.ts` import pointing there must stay `.ts` at runtime.
45
+ */
46
+ export declare function applyImportExtensions(code: string, fromFile: string): string;
47
+ //# sourceMappingURL=import-extensions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"import-extensions.d.ts","sourceRoot":"","sources":["../../src/build/import-extensions.ts"],"names":[],"mappings":"AAKA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CA+G5E"}
@@ -1,10 +1,20 @@
1
1
  import fs from "node:fs";
2
2
  import path from "node:path";
3
- function applyEsmExtensions(code, fromFile) {
3
+ import { getPaths } from "@cedarjs/project-config";
4
+ function applyImportExtensions(code, fromFile) {
4
5
  const fromDir = path.dirname(fromFile);
6
+ const generatedBase = getPaths().generated.base;
5
7
  const rewriteImportPath = (importPath, preservePrefix, preserveSuffix) => {
6
8
  const existingExt = path.extname(importPath);
7
9
  const absImport = path.join(fromDir, importPath);
10
+ const relativeToGeneratedBase = path.relative(generatedBase, absImport);
11
+ if (relativeToGeneratedBase === "" || !relativeToGeneratedBase.startsWith("..") && !path.isAbsolute(relativeToGeneratedBase)) {
12
+ return preservePrefix + importPath + preserveSuffix;
13
+ }
14
+ if ((existingExt === ".ts" || existingExt === ".tsx") && fs.existsSync(absImport)) {
15
+ const base2 = importPath.slice(0, -existingExt.length);
16
+ return preservePrefix + base2 + ".js" + preserveSuffix;
17
+ }
8
18
  if (existingExt && existingExt !== ".js" && existingExt !== ".jsx") {
9
19
  if (fs.existsSync(absImport + ".ts") || fs.existsSync(absImport + ".js") || fs.existsSync(absImport + ".tsx") || fs.existsSync(absImport + ".jsx")) {
10
20
  return preservePrefix + importPath + ".js" + preserveSuffix;
@@ -24,14 +34,26 @@ function applyEsmExtensions(code, fromFile) {
24
34
  return `from ${rewriteImportPath(importPath, quote, quote)}`;
25
35
  }
26
36
  );
37
+ result = result.replace(
38
+ /(^[ \t]*|;\s*)import\s+(['"])(\.\.?\/[^'"]+)\2/gm,
39
+ (match, prefix, quote, importPath) => {
40
+ return `${prefix}import ${rewriteImportPath(importPath, quote, quote)}`;
41
+ }
42
+ );
27
43
  result = result.replace(
28
44
  /\bimport\s*\(\s*(['"])(\.\.?\/[^'"]+)\1\s*\)/g,
29
45
  (match, quote, importPath) => {
30
46
  return `import(${rewriteImportPath(importPath, quote, quote)})`;
31
47
  }
32
48
  );
49
+ result = result.replace(
50
+ /\brequire\s*\(\s*(['"])(\.\.?\/[^'"]+)\1\s*\)/g,
51
+ (match, quote, importPath) => {
52
+ return `require(${rewriteImportPath(importPath, quote, quote)})`;
53
+ }
54
+ );
33
55
  return result;
34
56
  }
35
57
  export {
36
- applyEsmExtensions
58
+ applyImportExtensions
37
59
  };
@@ -1 +1 @@
1
- {"version":3,"file":"graphqlCodeGen.d.ts","sourceRoot":"","sources":["../../src/generate/graphqlCodeGen.ts"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAA;AAmB9D,KAAK,aAAa,GAAG;IACnB,YAAY,EAAE,MAAM,EAAE,CAAA;IACtB,MAAM,EAAE;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,OAAO,CAAA;KAAE,EAAE,CAAA;CAC9C,CAAA;AAED,eAAO,MAAM,yBAAyB,QAAa,OAAO,CAAC,aAAa,CAiFvE,CAAA;AAED,eAAO,MAAM,yBAAyB,QAAa,OAAO,CAAC,aAAa,CAwDvE,CAAA;AAmCD,wBAAgB,uBAAuB,CAAC,QAAQ,EAAE,MAAM;SACD,MAAM;GAQ5D;AAmKD,eAAO,MAAM,iBAAiB,4RAe7B,CAAA"}
1
+ {"version":3,"file":"graphqlCodeGen.d.ts","sourceRoot":"","sources":["../../src/generate/graphqlCodeGen.ts"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAA;AAmB9D,KAAK,aAAa,GAAG;IACnB,YAAY,EAAE,MAAM,EAAE,CAAA;IACtB,MAAM,EAAE;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,OAAO,CAAA;KAAE,EAAE,CAAA;CAC9C,CAAA;AAED,eAAO,MAAM,yBAAyB,QAAa,OAAO,CAAC,aAAa,CAiFvE,CAAA;AAED,eAAO,MAAM,yBAAyB,QAAa,OAAO,CAAC,aAAa,CAwDvE,CAAA;AAmCD,wBAAgB,uBAAuB,CAAC,QAAQ,EAAE,MAAM;SACD,MAAM;GAQ5D;AA2OD,eAAO,MAAM,iBAAiB,4RAe7B,CAAA"}
@@ -171,15 +171,37 @@ function getLoadDocumentsOptions(filename) {
171
171
  };
172
172
  return loadTypedefsConfig;
173
173
  }
174
- async function importGeneratedPrismaClient() {
174
+ class PrismaModelsFormatError extends Error {
175
+ }
176
+ const MODEL_REEXPORT_RE = /^export type \* from '\.\/models\/(\w+)\.\w+'/gm;
177
+ function readPrismaModelNames(clientPath) {
178
+ const ext = path.extname(clientPath);
179
+ const modelsPath = path.join(path.dirname(clientPath), `models${ext}`);
180
+ const source = fs.readFileSync(modelsPath, "utf8");
181
+ const modelNames = {};
182
+ for (const match of source.matchAll(MODEL_REEXPORT_RE)) {
183
+ modelNames[match[1]] = match[1];
184
+ }
185
+ if (Object.keys(modelNames).length === 0) {
186
+ throw new PrismaModelsFormatError(
187
+ `Could not read any Prisma model names from ${modelsPath}. Expected lines like \`export type * from './models/Post.ts'\`. This is most likely a change in what Prisma generates. Please open an issue at https://github.com/cedarjs/cedar/issues.`
188
+ );
189
+ }
190
+ return modelNames;
191
+ }
192
+ async function readGeneratedPrismaModelNames() {
175
193
  const cacheBuster = `?t=${Date.now()}`;
176
194
  const { clientPath, error } = await resolveGeneratedPrismaClient();
177
195
  if (!clientPath) {
178
196
  throw new Error(error);
179
197
  }
198
+ if (/\.[mc]?ts$/.test(clientPath)) {
199
+ return readPrismaModelNames(clientPath);
200
+ }
180
201
  const fileUrl = pathToFileURL(clientPath).href + cacheBuster;
181
202
  const freshPrisma = await import(fileUrl);
182
- return freshPrisma;
203
+ const modelName = getModelName(freshPrisma);
204
+ return modelName ?? {};
183
205
  }
184
206
  function isModelNameRecord(value) {
185
207
  if (typeof value !== "object" || value === null) {
@@ -206,23 +228,27 @@ function getModelName(mod) {
206
228
  }
207
229
  async function getPrismaClient() {
208
230
  try {
209
- const localPrisma = await importGeneratedPrismaClient();
210
- const modelName = getModelName(localPrisma);
211
- if (modelName) {
231
+ const modelName = await readGeneratedPrismaModelNames();
232
+ if (Object.keys(modelName).length > 0) {
212
233
  return { ModelName: modelName };
213
234
  }
214
- } catch {
235
+ } catch (e) {
236
+ if (e instanceof PrismaModelsFormatError) {
237
+ throw e;
238
+ }
215
239
  }
216
240
  const pm = getPackageManager();
217
241
  const pmExec = pm === "npm" ? "npx" : pm;
218
242
  execa.sync(pmExec, ["cedar", "prisma", "generate"]);
219
243
  try {
220
- const freshPrisma = await importGeneratedPrismaClient();
221
- const modelName = getModelName(freshPrisma);
222
- if (modelName) {
244
+ const modelName = await readGeneratedPrismaModelNames();
245
+ if (Object.keys(modelName).length > 0) {
223
246
  return { ModelName: modelName };
224
247
  }
225
- } catch {
248
+ } catch (e) {
249
+ if (e instanceof PrismaModelsFormatError) {
250
+ throw e;
251
+ }
226
252
  }
227
253
  return { ModelName: {} };
228
254
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cedarjs/internal",
3
- "version": "6.0.0-rc.312",
3
+ "version": "6.0.0-rc.340",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/cedarjs/cedar.git",
@@ -118,17 +118,17 @@
118
118
  },
119
119
  "dependencies": {
120
120
  "@babel/core": "^7.26.10",
121
- "@babel/parser": "7.29.7",
121
+ "@babel/parser": "7.29.8",
122
122
  "@babel/plugin-transform-typescript": "^7.26.8",
123
- "@babel/traverse": "7.29.7",
124
- "@cedarjs/api": "6.0.0-rc.312",
125
- "@cedarjs/babel-config": "6.0.0-rc.312",
126
- "@cedarjs/cli-helpers": "6.0.0-rc.312",
127
- "@cedarjs/graphql-server": "6.0.0-rc.312",
128
- "@cedarjs/project-config": "6.0.0-rc.312",
129
- "@cedarjs/router": "6.0.0-rc.312",
130
- "@cedarjs/structure": "6.0.0-rc.312",
131
- "@cedarjs/utils": "6.0.0-rc.312",
123
+ "@babel/traverse": "7.29.8",
124
+ "@cedarjs/api": "6.0.0-rc.340",
125
+ "@cedarjs/babel-config": "6.0.0-rc.340",
126
+ "@cedarjs/cli-helpers": "6.0.0-rc.340",
127
+ "@cedarjs/graphql-server": "6.0.0-rc.340",
128
+ "@cedarjs/project-config": "6.0.0-rc.340",
129
+ "@cedarjs/router": "6.0.0-rc.340",
130
+ "@cedarjs/structure": "6.0.0-rc.340",
131
+ "@cedarjs/utils": "6.0.0-rc.340",
132
132
  "@graphql-codegen/add": "6.0.1",
133
133
  "@graphql-codegen/cli": "6.3.1",
134
134
  "@graphql-codegen/client-preset": "5.3.0",
@@ -166,7 +166,7 @@
166
166
  "vite-tsconfig-paths": "^6.1.1"
167
167
  },
168
168
  "devDependencies": {
169
- "@cedarjs/framework-tools": "6.0.0-rc.312",
169
+ "@cedarjs/framework-tools": "6.0.0-rc.340",
170
170
  "@jridgewell/trace-mapping": "0.3.31",
171
171
  "@oxc-project/types": "^0.144.0",
172
172
  "concurrently": "9.2.4",
@@ -1,33 +0,0 @@
1
- /**
2
- * For ESM projects using esbuild with `bundle: false`, rewrites extensionless
3
- * relative imports to include `.js` extension so that Node's ESM resolver can
4
- * locate the compiled output files at runtime.
5
- *
6
- * Handles both static imports (`from '../lib/db'`) and dynamic imports
7
- * (`import('../lib/db')`).
8
- *
9
- * Always outputs `.js` because esbuild with `loader: 'js'` compiles all source
10
- * files (`.ts`, `.tsx`, `.jsx`) to `.js` output. For a source file `api/src/lib/db.ts`
11
- * or `api/src/lib/db.tsx`, the output is always `api/dist/lib/db.js`, so all
12
- * import specifiers must use `.js` at runtime.
13
- *
14
- * This replaces the `resolvePath` hook in `babel-plugin-module-resolver` for
15
- * the standalone esbuild build path (`runCedarBabelTransformsPlugin`), which
16
- * was previously responsible for this extension-appending behaviour. Vite
17
- * and Rollup resolve extensions themselves during bundling; only the esbuild
18
- * `bundle: false` path needs explicit extension suffixes in the source.
19
- *
20
- * It is a plain function called inline — not an esbuild plugin — following
21
- * the same pattern as `applySrcAlias`, `applyTsconfigPaths`, etc.
22
- *
23
- * Also handles Cedar SDL files whose names include a compound extension:
24
- * `contacts.sdl.ts` compiles to `contacts.sdl.js`, so an import specifier
25
- * `../graphql/contacts.sdl` (generated by applyImportDir's glob expansion)
26
- * is rewritten to `../graphql/contacts.sdl.js`.
27
- *
28
- * Example: in `api/src/functions/graphql.ts` importing `../lib/db`, this
29
- * rewrites it to `../lib/db.js` when `api/src/lib/db.ts` (or `db.tsx`)
30
- * exists on disk.
31
- */
32
- export declare function applyEsmExtensions(code: string, fromFile: string): string;
33
- //# sourceMappingURL=esm-extensions.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"esm-extensions.d.ts","sourceRoot":"","sources":["../../src/build/esm-extensions.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAiEzE"}