@cedarjs/prerender 1.0.0-canary.12535 → 1.0.0-canary.12537

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/graphql/vite-plugin-cedar-import-dir.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAA;AAIlC;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,oBAAoB,IAAI,MAAM,CA8L7C"}
1
+ {"version":3,"file":"vite-plugin-cedar-import-dir.d.ts","sourceRoot":"","sources":["../../src/graphql/vite-plugin-cedar-import-dir.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAA;AAIlC;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,oBAAoB,IAAI,MAAM,CAgH7C"}
@@ -1,30 +1,8 @@
1
1
  import path from "node:path";
2
- import * as swc from "@swc/core";
2
+ import { parse, Lang } from "@ast-grep/napi";
3
3
  import fg from "fast-glob";
4
4
  import { importStatementPath, getPaths } from "@cedarjs/project-config";
5
5
  function cedarImportDirPlugin() {
6
- const createSpan = () => ({
7
- start: 0,
8
- end: 0,
9
- // ctxt is not actually used, I just have to add it because of broken swc
10
- // types, see https://github.com/Menci/vite-plugin-top-level-await/issues/52
11
- ctxt: 0
12
- });
13
- const createIdentifier = (value, ctxt) => ({
14
- type: "Identifier",
15
- span: createSpan(),
16
- // @ts-expect-error - See https://github.com/Menci/vite-plugin-top-level-await/issues/52
17
- ctxt,
18
- value,
19
- optional: false,
20
- typeAnnotation: null
21
- });
22
- const createStringLiteral = (value) => ({
23
- type: "StringLiteral",
24
- span: createSpan(),
25
- value,
26
- raw: `'${value}'`
27
- });
28
6
  return {
29
7
  name: "vite-plugin-cedar-import-dir",
30
8
  enforce: "pre",
@@ -33,109 +11,66 @@ function cedarImportDirPlugin() {
33
11
  return null;
34
12
  }
35
13
  const ext = path.extname(id);
36
- const ast = await swc.parse(code, {
37
- syntax: ext === ".ts" || ext === ".tsx" ? "typescript" : "ecmascript",
38
- tsx: ext === ".tsx"
39
- });
14
+ const language = ext === ".ts" || ext === ".tsx" ? Lang.TypeScript : Lang.JavaScript;
15
+ let ast;
16
+ try {
17
+ ast = parse(language, code);
18
+ } catch (error) {
19
+ console.warn("Failed to parse file:", id);
20
+ console.warn(error);
21
+ return null;
22
+ }
23
+ const root = ast.root();
40
24
  let hasTransformations = false;
41
- const newBody = [];
42
- const ctxt = ast.body.length > 0 ? ast.body[0].ctxt || 0 : 0;
43
- for (const item of ast.body) {
44
- if (item.type === "ImportDeclaration" && item.source.value.includes("/**/")) {
45
- hasTransformations = true;
46
- const defaultSpecifier = item.specifiers.find(
47
- (spec) => spec.type === "ImportDefaultSpecifier"
25
+ const edits = [];
26
+ const globImports = root.findAll({
27
+ rule: {
28
+ pattern: "import $DEFAULT_IMPORT from $SOURCE"
29
+ }
30
+ });
31
+ for (const importNode of globImports) {
32
+ const sourceNode = importNode.getMatch("SOURCE");
33
+ const defaultImportNode = importNode.getMatch("DEFAULT_IMPORT");
34
+ if (!sourceNode || !defaultImportNode) {
35
+ continue;
36
+ }
37
+ const sourceValue = sourceNode.text().slice(1, -1);
38
+ if (!sourceValue.includes("/**/")) {
39
+ continue;
40
+ }
41
+ hasTransformations = true;
42
+ const importName = defaultImportNode.text();
43
+ const importGlob = importStatementPath(sourceValue);
44
+ const cwd = importGlob.startsWith("src/") ? getPaths().api.base : path.dirname(id);
45
+ try {
46
+ const dirFiles = fg.sync(importGlob, { cwd }).filter(
47
+ (n) => !n.includes(".test.") && !n.includes(".scenarios.") && !n.includes(".d.ts")
48
48
  );
49
- if (!defaultSpecifier) {
50
- newBody.push(item);
51
- continue;
49
+ const staticGlob = importGlob.split("*")[0];
50
+ const filePathToVarName = (filePath) => {
51
+ return filePath.replace(staticGlob, "").replace(/\.(js|ts)$/, "").replace(/[^a-zA-Z0-9]/g, "_");
52
+ };
53
+ let replacement = `let ${importName} = {};
54
+ `;
55
+ for (const filePath of dirFiles) {
56
+ const { dir: fileDir, name: fileName } = path.parse(filePath);
57
+ const fileImportPath = fileDir + "/" + fileName;
58
+ const filePathVarName = filePathToVarName(filePath);
59
+ const namespaceImportName = `${importName}_${filePathVarName}`;
60
+ replacement += `import * as ${namespaceImportName} from '${fileImportPath}';
61
+ `;
62
+ replacement += `${importName}.${filePathVarName} = ${namespaceImportName};
63
+ `;
52
64
  }
53
- const importName = defaultSpecifier.local.value;
54
- const importPath = item.source.value;
55
- const importGlob = importStatementPath(importPath);
56
- const cwd = importGlob.startsWith("src/") ? getPaths().api.base : path.dirname(id);
57
- try {
58
- const dirFiles = fg.sync(importGlob, { cwd }).filter(
59
- (n) => !n.includes(".test.") && !n.includes(".scenarios.") && !n.includes(".d.ts")
60
- );
61
- const staticGlob = importGlob.split("*")[0];
62
- const filePathToVarName = (filePath) => {
63
- return filePath.replace(staticGlob, "").replace(/\.(js|ts)$/, "").replace(/[^a-zA-Z0-9]/g, "_");
64
- };
65
- newBody.push({
66
- type: "VariableDeclaration",
67
- span: createSpan(),
68
- // @ts-expect-error - See https://github.com/Menci/vite-plugin-top-level-await/issues/52
69
- ctxt,
70
- kind: "let",
71
- declare: false,
72
- declarations: [
73
- {
74
- type: "VariableDeclarator",
75
- span: createSpan(),
76
- id: createIdentifier(importName, ctxt),
77
- init: {
78
- type: "ObjectExpression",
79
- span: createSpan(),
80
- properties: []
81
- },
82
- definite: false
83
- }
84
- ]
85
- });
86
- for (const filePath of dirFiles) {
87
- const { dir: fileDir, name: fileName } = path.parse(filePath);
88
- const fileImportPath = fileDir + "/" + fileName;
89
- const filePathVarName = filePathToVarName(filePath);
90
- const namespaceImportName = `${importName}_${filePathVarName}`;
91
- newBody.push({
92
- type: "ImportDeclaration",
93
- span: createSpan(),
94
- specifiers: [
95
- {
96
- type: "ImportNamespaceSpecifier",
97
- span: createSpan(),
98
- local: createIdentifier(namespaceImportName, ctxt)
99
- }
100
- ],
101
- source: createStringLiteral(fileImportPath),
102
- typeOnly: false
103
- });
104
- newBody.push({
105
- type: "ExpressionStatement",
106
- span: createSpan(),
107
- expression: {
108
- type: "AssignmentExpression",
109
- span: createSpan(),
110
- operator: "=",
111
- left: {
112
- type: "MemberExpression",
113
- span: createSpan(),
114
- object: createIdentifier(importName, ctxt),
115
- property: createIdentifier(filePathVarName, ctxt)
116
- },
117
- right: createIdentifier(namespaceImportName, ctxt)
118
- }
119
- });
120
- }
121
- } catch (error) {
122
- console.warn(`Failed to process glob import: ${importPath}`, error);
123
- newBody.push(item);
124
- }
125
- } else {
126
- newBody.push(item);
65
+ edits.push(importNode.replace(replacement.trim()));
66
+ } catch (error) {
67
+ console.warn(`Failed to process glob import: ${sourceValue}`, error);
127
68
  }
128
69
  }
129
- if (hasTransformations) {
130
- const transformedAst = {
131
- ...ast,
132
- body: newBody
133
- };
134
- const output = await swc.print(transformedAst, {
135
- minify: false
136
- });
70
+ if (hasTransformations && edits.length > 0) {
71
+ const transformedCode = root.commitEdits(edits);
137
72
  return {
138
- code: output.code,
73
+ code: transformedCode,
139
74
  map: null
140
75
  // For simplicity, not generating source maps
141
76
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cedarjs/prerender",
3
- "version": "1.0.0-canary.12535+8272baddd",
3
+ "version": "1.0.0-canary.12537+62d58ab28",
4
4
  "description": "RedwoodJS prerender",
5
5
  "repository": {
6
6
  "type": "git",
@@ -40,15 +40,16 @@
40
40
  "test:watch": "vitest watch"
41
41
  },
42
42
  "dependencies": {
43
+ "@ast-grep/napi": "0.39.3",
43
44
  "@babel/generator": "7.27.5",
44
45
  "@babel/parser": "7.27.5",
45
46
  "@babel/traverse": "7.27.4",
46
- "@cedarjs/babel-config": "1.0.0-canary.12535",
47
- "@cedarjs/project-config": "1.0.0-canary.12535",
48
- "@cedarjs/router": "1.0.0-canary.12535",
49
- "@cedarjs/structure": "1.0.0-canary.12535",
50
- "@cedarjs/vite": "1.0.0-canary.12535",
51
- "@cedarjs/web": "1.0.0-canary.12535",
47
+ "@cedarjs/babel-config": "1.0.0-canary.12537",
48
+ "@cedarjs/project-config": "1.0.0-canary.12537",
49
+ "@cedarjs/router": "1.0.0-canary.12537",
50
+ "@cedarjs/structure": "1.0.0-canary.12537",
51
+ "@cedarjs/vite": "1.0.0-canary.12537",
52
+ "@cedarjs/web": "1.0.0-canary.12537",
52
53
  "@rollup/plugin-alias": "5.1.1",
53
54
  "@rollup/plugin-commonjs": "28.0.6",
54
55
  "@rollup/plugin-node-resolve": "16.0.1",
@@ -67,7 +68,7 @@
67
68
  "vite-node": "3.2.4"
68
69
  },
69
70
  "devDependencies": {
70
- "@cedarjs/framework-tools": "1.0.0-canary.12535",
71
+ "@cedarjs/framework-tools": "1.0.0-canary.12537",
71
72
  "@types/mime-types": "2.1.4",
72
73
  "@types/react": "^18.2.55",
73
74
  "babel-plugin-tester": "11.0.4",
@@ -85,5 +86,5 @@
85
86
  "react": "react",
86
87
  "react-dom": "react-dom"
87
88
  },
88
- "gitHead": "8272baddd291bbdfc78994649d4bf0651cc8d15e"
89
+ "gitHead": "62d58ab286ec0e460bc11a601fca078c92e78718"
89
90
  }