@cedarjs/vite 6.0.0-canary.2799 → 6.0.0-canary.2800

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 +1 @@
1
- {"version":3,"file":"vite-plugin-cedar-import-dir.d.ts","sourceRoot":"","sources":["../../src/plugins/vite-plugin-cedar-import-dir.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAA;AAIlC;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,oBAAoB,IAAI,MAAM,CAqI7C"}
1
+ {"version":3,"file":"vite-plugin-cedar-import-dir.d.ts","sourceRoot":"","sources":["../../src/plugins/vite-plugin-cedar-import-dir.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAA;AAIlC;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,oBAAoB,IAAI,MAAM,CA4H7C"}
@@ -1,7 +1,7 @@
1
1
  import fs from "node:fs";
2
2
  import path from "node:path";
3
- import { parse, Lang } from "@ast-grep/napi";
4
3
  import MagicString from "magic-string";
4
+ import { parseSync } from "oxc-parser";
5
5
  import { normalizePath } from "vite";
6
6
  import { importStatementPath, getPaths } from "@cedarjs/project-config";
7
7
  function cedarImportDirPlugin() {
@@ -12,36 +12,32 @@ function cedarImportDirPlugin() {
12
12
  if (!code.includes("/**/")) {
13
13
  return null;
14
14
  }
15
- const ext = path.extname(id);
16
- const language = ext === ".ts" || ext === ".tsx" ? Lang.TypeScript : Lang.JavaScript;
17
- let ast;
15
+ let program;
18
16
  try {
19
- ast = parse(language, code);
17
+ program = parseSync(id, code).program;
20
18
  } catch (error) {
21
19
  console.warn("Failed to parse file:", id);
22
20
  console.warn(error);
23
21
  return null;
24
22
  }
25
- const root = ast.root();
26
23
  let hasTransformations = false;
27
24
  const s = new MagicString(code);
28
- const globImports = root.findAll({
29
- rule: {
30
- pattern: "import $DEFAULT_IMPORT from $SOURCE"
25
+ for (const node of program.body) {
26
+ if (node.type !== "ImportDeclaration") {
27
+ continue;
31
28
  }
32
- });
33
- for (const importNode of globImports) {
34
- const sourceNode = importNode.getMatch("SOURCE");
35
- const defaultImportNode = importNode.getMatch("DEFAULT_IMPORT");
36
- if (!sourceNode || !defaultImportNode) {
29
+ const defaultSpecifier = node.specifiers.find(
30
+ (specifier) => specifier.type === "ImportDefaultSpecifier"
31
+ );
32
+ if (!defaultSpecifier) {
37
33
  continue;
38
34
  }
39
- const sourceValue = sourceNode.text().slice(1, -1);
35
+ const sourceValue = node.source.value;
40
36
  if (!sourceValue.includes("/**/")) {
41
37
  continue;
42
38
  }
43
39
  hasTransformations = true;
44
- const importName = defaultImportNode.text();
40
+ const importName = defaultSpecifier.local.name;
45
41
  const importGlob = importStatementPath(sourceValue);
46
42
  let cwd = path.dirname(id);
47
43
  if (importGlob.startsWith("src/")) {
@@ -81,8 +77,7 @@ function cedarImportDirPlugin() {
81
77
  replacement += `${importName}.${filePathVarName} = ${namespaceImportName};
82
78
  `;
83
79
  }
84
- const range = importNode.range();
85
- s.overwrite(range.start.index, range.end.index, replacement.trim());
80
+ s.overwrite(node.start, node.end, replacement.trim());
86
81
  } catch (error) {
87
82
  console.warn(`Failed to process glob import: ${sourceValue}`, error);
88
83
  }
@@ -1,3 +1,32 @@
1
+ import type { SourceMap } from 'magic-string';
1
2
  import type { Plugin } from 'vite';
3
+ /**
4
+ * Vite plugin that injects `path` and `name` properties into `createJob()`
5
+ * definitions so the jobs runner can locate and identify the job's module at
6
+ * runtime.
7
+ *
8
+ * Transforms:
9
+ * export const myJob = jobs.createJob({ perform: ... })
10
+ * into:
11
+ * export const myJob = jobs.createJob({ perform: ..., path: "...", name: "myJob" })
12
+ *
13
+ * The extraction uses an oxc-parser AST walk so the transform is robust
14
+ * against nested object properties, aliased imports, and TypeScript syntax.
15
+ * The same logic is duplicated in `@cedarjs/internal`
16
+ * (`applyJobPathInjector`) for the standalone esbuild API build.
17
+ */
2
18
  export declare function cedarjsJobPathInjectorPlugin(): Plugin;
19
+ /**
20
+ * Injects `path` and `name` properties into `createJob()` definitions.
21
+ * Returns the transformed code with a sourcemap, or null if no
22
+ * transformation was needed.
23
+ *
24
+ * Duplicate of `@cedarjs/internal`'s `applyJobPathInjector` so this logic can
25
+ * run inside the Vite plugin pipeline without depending on internal's build
26
+ * output.
27
+ */
28
+ export declare function applyJobPathInjector(code: string, filePath: string, jobsDir: string): {
29
+ code: string;
30
+ map: SourceMap;
31
+ } | null;
3
32
  //# sourceMappingURL=vite-plugin-cedarjs-job-path-injector.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"vite-plugin-cedarjs-job-path-injector.d.ts","sourceRoot":"","sources":["../../src/plugins/vite-plugin-cedarjs-job-path-injector.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAA;AAIlC,wBAAgB,4BAA4B,IAAI,MAAM,CAqHrD"}
1
+ {"version":3,"file":"vite-plugin-cedarjs-job-path-injector.d.ts","sourceRoot":"","sources":["../../src/plugins/vite-plugin-cedarjs-job-path-injector.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAE7C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAA;AAIlC;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,4BAA4B,IAAI,MAAM,CAoBrD;AAED;;;;;;;;GAQG;AACH,wBAAgB,oBAAoB,CAClC,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,MAAM,GACd;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,SAAS,CAAA;CAAE,GAAG,IAAI,CAiFzC"}
@@ -1,5 +1,6 @@
1
1
  import path from "node:path";
2
- import { parse, Lang } from "@ast-grep/napi";
2
+ import MagicString from "magic-string";
3
+ import { parseSync } from "oxc-parser";
3
4
  import { getPaths } from "@cedarjs/project-config";
4
5
  function cedarjsJobPathInjectorPlugin() {
5
6
  return {
@@ -8,93 +9,74 @@ function cedarjsJobPathInjectorPlugin() {
8
9
  if (!code.includes("createJob")) {
9
10
  return null;
10
11
  }
11
- const isTypescript = id.endsWith(".ts") || id.endsWith(".tsx");
12
- const language = isTypescript ? Lang.TypeScript : Lang.JavaScript;
13
- let ast;
14
- try {
15
- ast = parse(language, code);
16
- } catch (error) {
17
- console.warn("Failed to parse file:", id);
18
- console.warn(error);
12
+ const result = applyJobPathInjector(code, id, getPaths().api.jobs);
13
+ if (!result) {
19
14
  return null;
20
15
  }
21
- const paths = getPaths();
22
- const edits = [];
23
- const root = ast.root();
24
- const createJobCalls = root.findAll({
25
- rule: {
26
- pattern: "export const $VAR_NAME = $OBJ.createJob({ $$$PROPS })"
27
- }
28
- });
29
- for (const callNode of createJobCalls) {
30
- const varNameNode = callNode.getMatch("VAR_NAME");
31
- const propsNodes = callNode.getMultipleMatches("PROPS");
32
- if (!varNameNode) {
33
- continue;
34
- }
35
- const importName = varNameNode.text();
36
- const importPath = path.relative(paths.api.jobs, id);
37
- const importPathWithoutExtension = importPath.replace(/\.[^/.]+$/, "");
38
- const pathProperty = `path: ${JSON.stringify(importPathWithoutExtension)}`;
39
- const nameProperty = `name: ${JSON.stringify(importName)}`;
40
- let insertText = "";
41
- if (propsNodes.length > 0) {
42
- const objectLiteral = callNode.find({
43
- rule: {
44
- kind: "object",
45
- inside: {
46
- kind: "arguments"
47
- }
48
- }
49
- });
50
- if (objectLiteral) {
51
- insertText = `, ${pathProperty}, ${nameProperty}`;
52
- const objectText = objectLiteral.text();
53
- const closeBraceIndex = objectText.lastIndexOf("}");
54
- if (closeBraceIndex !== -1) {
55
- const range = objectLiteral.range();
56
- edits.push({
57
- startPos: range.start.index + closeBraceIndex,
58
- endPos: range.start.index + closeBraceIndex,
59
- insertedText: insertText
60
- });
61
- }
62
- }
63
- } else {
64
- const objectLiteral = callNode.find({
65
- rule: {
66
- kind: "object",
67
- inside: {
68
- kind: "arguments"
69
- }
70
- }
71
- });
72
- if (objectLiteral) {
73
- insertText = `${pathProperty}, ${nameProperty}`;
74
- const objectText = objectLiteral.text();
75
- const openBraceIndex = objectText.indexOf("{");
76
- if (openBraceIndex !== -1) {
77
- const range = objectLiteral.range();
78
- edits.push({
79
- startPos: range.start.index + openBraceIndex + 1,
80
- endPos: range.start.index + openBraceIndex + 1,
81
- insertedText: insertText
82
- });
83
- }
84
- }
85
- }
86
- }
87
- if (edits.length === 0) {
88
- return null;
89
- }
90
- const modifiedCode = root.commitEdits(edits);
91
16
  return {
92
- code: modifiedCode,
93
- map: null
17
+ code: result.code,
18
+ map: result.map
94
19
  };
95
20
  }
96
21
  };
97
22
  }
23
+ function applyJobPathInjector(code, filePath, jobsDir) {
24
+ let program;
25
+ try {
26
+ program = parseSync(filePath, code, { sourceType: "module" }).program;
27
+ } catch (error) {
28
+ console.warn("Failed to parse file:", filePath);
29
+ console.warn(error);
30
+ return null;
31
+ }
32
+ const importPath = path.relative(jobsDir, filePath);
33
+ const importPathWithoutExtension = importPath.replace(/\.[^/.]+$/, "");
34
+ const s = new MagicString(code);
35
+ let hasTransformations = false;
36
+ for (const node of program.body) {
37
+ if (node.type !== "ExportNamedDeclaration") {
38
+ continue;
39
+ }
40
+ const decl = node.declaration;
41
+ if (decl?.type !== "VariableDeclaration") {
42
+ continue;
43
+ }
44
+ for (const declarator of decl.declarations) {
45
+ if (declarator.id.type !== "Identifier") {
46
+ continue;
47
+ }
48
+ const init = declarator.init;
49
+ if (init?.type !== "CallExpression") {
50
+ continue;
51
+ }
52
+ const callee = init.callee;
53
+ if (callee.type !== "MemberExpression" || callee.computed || callee.property.type !== "Identifier" || callee.property.name !== "createJob") {
54
+ continue;
55
+ }
56
+ const configArg = init.arguments[0];
57
+ if (configArg?.type !== "ObjectExpression") {
58
+ continue;
59
+ }
60
+ hasTransformations = true;
61
+ const pathProperty = `path: ${JSON.stringify(importPathWithoutExtension)}`;
62
+ const nameProperty = `name: ${JSON.stringify(declarator.id.name)}`;
63
+ const lastProperty = configArg.properties.at(-1);
64
+ if (lastProperty) {
65
+ s.appendLeft(lastProperty.end, `, ${pathProperty}, ${nameProperty}`);
66
+ } else {
67
+ s.appendLeft(configArg.start + 1, `${pathProperty}, ${nameProperty}`);
68
+ }
69
+ }
70
+ }
71
+ if (!hasTransformations) {
72
+ return null;
73
+ }
74
+ return {
75
+ code: s.toString(),
76
+ map: s.generateMap({ hires: true })
77
+ };
78
+ }
98
79
  export {
80
+ applyJobPathInjector,
99
81
  cedarjsJobPathInjectorPlugin
100
82
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cedarjs/vite",
3
- "version": "6.0.0-canary.2799",
3
+ "version": "6.0.0-canary.2800",
4
4
  "description": "Vite configuration package for CedarJS",
5
5
  "repository": {
6
6
  "type": "git",
@@ -48,21 +48,20 @@
48
48
  "test:watch": "vitest watch"
49
49
  },
50
50
  "dependencies": {
51
- "@ast-grep/napi": "0.45.0",
52
51
  "@babel/generator": "7.29.7",
53
52
  "@babel/parser": "7.29.7",
54
53
  "@babel/traverse": "7.29.7",
55
54
  "@babel/types": "7.29.7",
56
- "@cedarjs/api": "6.0.0-canary.2799",
57
- "@cedarjs/api-server": "6.0.0-canary.2799",
58
- "@cedarjs/babel-config": "6.0.0-canary.2799",
59
- "@cedarjs/context": "6.0.0-canary.2799",
60
- "@cedarjs/graphql-server": "6.0.0-canary.2799",
61
- "@cedarjs/internal": "6.0.0-canary.2799",
62
- "@cedarjs/project-config": "6.0.0-canary.2799",
63
- "@cedarjs/server-store": "6.0.0-canary.2799",
64
- "@cedarjs/structure": "6.0.0-canary.2799",
65
- "@cedarjs/testing": "6.0.0-canary.2799",
55
+ "@cedarjs/api": "6.0.0-canary.2800",
56
+ "@cedarjs/api-server": "6.0.0-canary.2800",
57
+ "@cedarjs/babel-config": "6.0.0-canary.2800",
58
+ "@cedarjs/context": "6.0.0-canary.2800",
59
+ "@cedarjs/graphql-server": "6.0.0-canary.2800",
60
+ "@cedarjs/internal": "6.0.0-canary.2800",
61
+ "@cedarjs/project-config": "6.0.0-canary.2800",
62
+ "@cedarjs/server-store": "6.0.0-canary.2800",
63
+ "@cedarjs/structure": "6.0.0-canary.2800",
64
+ "@cedarjs/testing": "6.0.0-canary.2800",
66
65
  "@swc/core": "1.15.46",
67
66
  "@universal-deploy/store": "^0.2.1",
68
67
  "@universal-deploy/vite": "^0.1.9",
@@ -93,10 +92,10 @@
93
92
  "yargs-parser": "21.1.1"
94
93
  },
95
94
  "devDependencies": {
96
- "@cedarjs/auth": "6.0.0-canary.2799",
97
- "@cedarjs/framework-tools": "6.0.0-canary.2799",
98
- "@cedarjs/router": "6.0.0-canary.2799",
99
- "@cedarjs/web": "6.0.0-canary.2799",
95
+ "@cedarjs/auth": "6.0.0-canary.2800",
96
+ "@cedarjs/framework-tools": "6.0.0-canary.2800",
97
+ "@cedarjs/router": "6.0.0-canary.2800",
98
+ "@cedarjs/web": "6.0.0-canary.2800",
100
99
  "@dr.pogodin/react-helmet": "2.0.4",
101
100
  "@hyrious/esbuild-plugin-commonjs": "0.2.6",
102
101
  "@jridgewell/trace-mapping": "0.3.31",
@@ -116,9 +115,9 @@
116
115
  "vitest": "4.1.10"
117
116
  },
118
117
  "peerDependencies": {
119
- "@cedarjs/auth": "6.0.0-canary.2799",
120
- "@cedarjs/router": "6.0.0-canary.2799",
121
- "@cedarjs/web": "6.0.0-canary.2799"
118
+ "@cedarjs/auth": "6.0.0-canary.2800",
119
+ "@cedarjs/router": "6.0.0-canary.2800",
120
+ "@cedarjs/web": "6.0.0-canary.2800"
122
121
  },
123
122
  "engines": {
124
123
  "node": ">=24"