@granite-js/plugin-router 0.1.31 → 0.1.33
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/CHANGELOG.md +14 -0
- package/dist/index.cjs +33 -49
- package/dist/index.d.cts +0 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.js +26 -43
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @granite-js/plugin-router
|
|
2
2
|
|
|
3
|
+
## 0.1.33
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- @granite-js/plugin-core@0.1.33
|
|
8
|
+
|
|
9
|
+
## 0.1.32
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- 7572713: bump version up babel
|
|
14
|
+
- Updated dependencies [7572713]
|
|
15
|
+
- @granite-js/plugin-core@0.1.32
|
|
16
|
+
|
|
3
17
|
## 0.1.31
|
|
4
18
|
|
|
5
19
|
### Patch Changes
|
package/dist/index.cjs
CHANGED
|
@@ -21,12 +21,13 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
21
21
|
}) : target, mod));
|
|
22
22
|
|
|
23
23
|
//#endregion
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
24
|
+
let fs = require("fs");
|
|
25
|
+
let path = require("path");
|
|
26
|
+
let __swc_core = require("@swc/core");
|
|
27
|
+
let es_toolkit = require("es-toolkit");
|
|
28
|
+
let fs_promises = require("fs/promises");
|
|
29
|
+
let chokidar = require("chokidar");
|
|
30
|
+
chokidar = __toESM(chokidar);
|
|
30
31
|
|
|
31
32
|
//#region src/checkExportRoute.ts
|
|
32
33
|
/**
|
|
@@ -38,20 +39,18 @@ function checkExportRoute(path$1) {
|
|
|
38
39
|
syntax: "typescript",
|
|
39
40
|
tsx: true
|
|
40
41
|
});
|
|
41
|
-
|
|
42
|
+
if (ast.body.some((node) => {
|
|
42
43
|
if (node.type !== "ExportNamedDeclaration") return false;
|
|
43
44
|
return node.specifiers?.some((specifier) => {
|
|
44
45
|
if (specifier.type !== "ExportSpecifier") return false;
|
|
45
46
|
return specifier.orig?.value === "Route";
|
|
46
47
|
});
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
const hasExportNamedVariable = ast.body.some((node) => {
|
|
48
|
+
})) return true;
|
|
49
|
+
return ast.body.some((node) => {
|
|
50
50
|
if (node.type !== "ExportDeclaration") return false;
|
|
51
51
|
if (node.declaration.type !== "VariableDeclaration") return false;
|
|
52
52
|
return node.declaration.declarations.some((declaration) => declaration.id.type === "Identifier" && declaration.id.value === "Route");
|
|
53
53
|
});
|
|
54
|
-
return hasExportNamedVariable;
|
|
55
54
|
} catch {
|
|
56
55
|
return false;
|
|
57
56
|
}
|
|
@@ -60,11 +59,9 @@ function checkExportRoute(path$1) {
|
|
|
60
59
|
//#endregion
|
|
61
60
|
//#region src/getComponentName.ts
|
|
62
61
|
function getComponentName(filePath) {
|
|
63
|
-
const
|
|
64
|
-
const segments = path$1.split("/").filter(Boolean);
|
|
62
|
+
const segments = filePath.replace(/^pages\//, "").replace(/\.(tsx|ts)$/, "").split("/").filter(Boolean);
|
|
65
63
|
if (segments[segments.length - 1] === "index") segments.pop();
|
|
66
|
-
|
|
67
|
-
return componentName || "Index";
|
|
64
|
+
return segments.map((segment) => (0, es_toolkit.pascalCase)(segment)).join("") || "Index";
|
|
68
65
|
}
|
|
69
66
|
|
|
70
67
|
//#endregion
|
|
@@ -145,8 +142,7 @@ function transformTemplate(templateString, values) {
|
|
|
145
142
|
function generateRouterFile() {
|
|
146
143
|
const cwd = process.cwd();
|
|
147
144
|
function getPageFiles(dir, prefix = "") {
|
|
148
|
-
|
|
149
|
-
return files.reduce((acc, file) => {
|
|
145
|
+
return (0, fs.readdirSync)((0, path.join)(cwd, dir), { withFileTypes: true }).reduce((acc, file) => {
|
|
150
146
|
if (file.isDirectory()) return [...acc, ...getPageFiles(`${dir}/${file.name}`, `${prefix}${file.name}/`)];
|
|
151
147
|
if (file.name.endsWith(".tsx") || file.name.endsWith(".ts")) {
|
|
152
148
|
const name = (0, path.parse)(file.name).name;
|
|
@@ -159,39 +155,29 @@ function generateRouterFile() {
|
|
|
159
155
|
const allPages = getPageFiles("pages");
|
|
160
156
|
const exportRouteMap = new Map(allPages.map((page) => [page, checkExportRoute((0, path.join)(cwd, "pages", page))]));
|
|
161
157
|
const pageFiles = allPages.filter((page) => !page.startsWith("_") && exportRouteMap.get(page));
|
|
162
|
-
const pageImports = pageFiles.map((page) => {
|
|
163
|
-
const componentName = getComponentName(page);
|
|
164
|
-
const pagePath = getPath(page);
|
|
165
|
-
return transformTemplate("import { Route as _%%componentName%%Route } from '../pages%%pagePath%%';", {
|
|
166
|
-
componentName,
|
|
167
|
-
pagePath
|
|
168
|
-
});
|
|
169
|
-
}).join("\n");
|
|
170
|
-
const pageInputRoutes = pageFiles.map((page) => {
|
|
171
|
-
const componentName = getComponentName(page);
|
|
172
|
-
const pagePath = getPath(page);
|
|
173
|
-
return transformTemplate(" '%%pagePath%%': (typeof _%%componentName%%Route)['_inputType'];", {
|
|
174
|
-
componentName,
|
|
175
|
-
pagePath
|
|
176
|
-
});
|
|
177
|
-
}).join("\n");
|
|
178
|
-
const pageRoutes = pageFiles.map((page) => {
|
|
179
|
-
const componentName = getComponentName(page);
|
|
180
|
-
const pagePath = getPath(page);
|
|
181
|
-
return transformTemplate(" '%%pagePath%%': (typeof _%%componentName%%Route)['_outputType'];", {
|
|
182
|
-
componentName,
|
|
183
|
-
pagePath
|
|
184
|
-
});
|
|
185
|
-
}).join("\n");
|
|
186
158
|
const generatedContent = transformTemplate(ROUTER_GEN_TEMPLATE, {
|
|
187
|
-
pageImports
|
|
188
|
-
|
|
189
|
-
|
|
159
|
+
pageImports: pageFiles.map((page) => {
|
|
160
|
+
return transformTemplate("import { Route as _%%componentName%%Route } from '../pages%%pagePath%%';", {
|
|
161
|
+
componentName: getComponentName(page),
|
|
162
|
+
pagePath: getPath(page)
|
|
163
|
+
});
|
|
164
|
+
}).join("\n"),
|
|
165
|
+
pageInputRoutes: pageFiles.map((page) => {
|
|
166
|
+
return transformTemplate(" '%%pagePath%%': (typeof _%%componentName%%Route)['_inputType'];", {
|
|
167
|
+
componentName: getComponentName(page),
|
|
168
|
+
pagePath: getPath(page)
|
|
169
|
+
});
|
|
170
|
+
}).join("\n"),
|
|
171
|
+
pageRoutes: pageFiles.map((page) => {
|
|
172
|
+
return transformTemplate(" '%%pagePath%%': (typeof _%%componentName%%Route)['_outputType'];", {
|
|
173
|
+
componentName: getComponentName(page),
|
|
174
|
+
pagePath: getPath(page)
|
|
175
|
+
});
|
|
176
|
+
}).join("\n")
|
|
190
177
|
});
|
|
191
178
|
const routerFilePath = (0, path.join)(cwd, "src", "router.gen.ts");
|
|
192
179
|
if ((0, fs.existsSync)(routerFilePath)) {
|
|
193
|
-
|
|
194
|
-
if (existingContent === generatedContent) return;
|
|
180
|
+
if ((0, fs.readFileSync)(routerFilePath, "utf-8") === generatedContent) return;
|
|
195
181
|
}
|
|
196
182
|
(0, fs.writeFileSync)(routerFilePath, generatedContent);
|
|
197
183
|
console.log("✅ Router file generated successfully!");
|
|
@@ -234,9 +220,7 @@ function watchRouter() {
|
|
|
234
220
|
cwd: process.cwd()
|
|
235
221
|
});
|
|
236
222
|
const handleAdd = async (path$1) => {
|
|
237
|
-
|
|
238
|
-
const code = await (0, fs_promises.readFile)(file, "utf8");
|
|
239
|
-
if (code !== "") return;
|
|
223
|
+
if (await (0, fs_promises.readFile)((0, path.join)(process.cwd(), path$1), "utf8") !== "") return;
|
|
240
224
|
const filename = (0, path.parse)(path$1).name;
|
|
241
225
|
if (filename.startsWith("_")) switch (filename) {
|
|
242
226
|
case "_layout":
|
package/dist/index.d.cts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -15,20 +15,18 @@ function checkExportRoute(path) {
|
|
|
15
15
|
syntax: "typescript",
|
|
16
16
|
tsx: true
|
|
17
17
|
});
|
|
18
|
-
|
|
18
|
+
if (ast.body.some((node) => {
|
|
19
19
|
if (node.type !== "ExportNamedDeclaration") return false;
|
|
20
20
|
return node.specifiers?.some((specifier) => {
|
|
21
21
|
if (specifier.type !== "ExportSpecifier") return false;
|
|
22
22
|
return specifier.orig?.value === "Route";
|
|
23
23
|
});
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
const hasExportNamedVariable = ast.body.some((node) => {
|
|
24
|
+
})) return true;
|
|
25
|
+
return ast.body.some((node) => {
|
|
27
26
|
if (node.type !== "ExportDeclaration") return false;
|
|
28
27
|
if (node.declaration.type !== "VariableDeclaration") return false;
|
|
29
28
|
return node.declaration.declarations.some((declaration) => declaration.id.type === "Identifier" && declaration.id.value === "Route");
|
|
30
29
|
});
|
|
31
|
-
return hasExportNamedVariable;
|
|
32
30
|
} catch {
|
|
33
31
|
return false;
|
|
34
32
|
}
|
|
@@ -37,11 +35,9 @@ function checkExportRoute(path) {
|
|
|
37
35
|
//#endregion
|
|
38
36
|
//#region src/getComponentName.ts
|
|
39
37
|
function getComponentName(filePath) {
|
|
40
|
-
const
|
|
41
|
-
const segments = path.split("/").filter(Boolean);
|
|
38
|
+
const segments = filePath.replace(/^pages\//, "").replace(/\.(tsx|ts)$/, "").split("/").filter(Boolean);
|
|
42
39
|
if (segments[segments.length - 1] === "index") segments.pop();
|
|
43
|
-
|
|
44
|
-
return componentName || "Index";
|
|
40
|
+
return segments.map((segment) => pascalCase(segment)).join("") || "Index";
|
|
45
41
|
}
|
|
46
42
|
|
|
47
43
|
//#endregion
|
|
@@ -122,8 +118,7 @@ function transformTemplate(templateString, values) {
|
|
|
122
118
|
function generateRouterFile() {
|
|
123
119
|
const cwd = process.cwd();
|
|
124
120
|
function getPageFiles(dir, prefix = "") {
|
|
125
|
-
|
|
126
|
-
return files.reduce((acc, file) => {
|
|
121
|
+
return readdirSync(join(cwd, dir), { withFileTypes: true }).reduce((acc, file) => {
|
|
127
122
|
if (file.isDirectory()) return [...acc, ...getPageFiles(`${dir}/${file.name}`, `${prefix}${file.name}/`)];
|
|
128
123
|
if (file.name.endsWith(".tsx") || file.name.endsWith(".ts")) {
|
|
129
124
|
const name = parse(file.name).name;
|
|
@@ -136,39 +131,29 @@ function generateRouterFile() {
|
|
|
136
131
|
const allPages = getPageFiles("pages");
|
|
137
132
|
const exportRouteMap = new Map(allPages.map((page) => [page, checkExportRoute(join(cwd, "pages", page))]));
|
|
138
133
|
const pageFiles = allPages.filter((page) => !page.startsWith("_") && exportRouteMap.get(page));
|
|
139
|
-
const pageImports = pageFiles.map((page) => {
|
|
140
|
-
const componentName = getComponentName(page);
|
|
141
|
-
const pagePath = getPath(page);
|
|
142
|
-
return transformTemplate("import { Route as _%%componentName%%Route } from '../pages%%pagePath%%';", {
|
|
143
|
-
componentName,
|
|
144
|
-
pagePath
|
|
145
|
-
});
|
|
146
|
-
}).join("\n");
|
|
147
|
-
const pageInputRoutes = pageFiles.map((page) => {
|
|
148
|
-
const componentName = getComponentName(page);
|
|
149
|
-
const pagePath = getPath(page);
|
|
150
|
-
return transformTemplate(" '%%pagePath%%': (typeof _%%componentName%%Route)['_inputType'];", {
|
|
151
|
-
componentName,
|
|
152
|
-
pagePath
|
|
153
|
-
});
|
|
154
|
-
}).join("\n");
|
|
155
|
-
const pageRoutes = pageFiles.map((page) => {
|
|
156
|
-
const componentName = getComponentName(page);
|
|
157
|
-
const pagePath = getPath(page);
|
|
158
|
-
return transformTemplate(" '%%pagePath%%': (typeof _%%componentName%%Route)['_outputType'];", {
|
|
159
|
-
componentName,
|
|
160
|
-
pagePath
|
|
161
|
-
});
|
|
162
|
-
}).join("\n");
|
|
163
134
|
const generatedContent = transformTemplate(ROUTER_GEN_TEMPLATE, {
|
|
164
|
-
pageImports
|
|
165
|
-
|
|
166
|
-
|
|
135
|
+
pageImports: pageFiles.map((page) => {
|
|
136
|
+
return transformTemplate("import { Route as _%%componentName%%Route } from '../pages%%pagePath%%';", {
|
|
137
|
+
componentName: getComponentName(page),
|
|
138
|
+
pagePath: getPath(page)
|
|
139
|
+
});
|
|
140
|
+
}).join("\n"),
|
|
141
|
+
pageInputRoutes: pageFiles.map((page) => {
|
|
142
|
+
return transformTemplate(" '%%pagePath%%': (typeof _%%componentName%%Route)['_inputType'];", {
|
|
143
|
+
componentName: getComponentName(page),
|
|
144
|
+
pagePath: getPath(page)
|
|
145
|
+
});
|
|
146
|
+
}).join("\n"),
|
|
147
|
+
pageRoutes: pageFiles.map((page) => {
|
|
148
|
+
return transformTemplate(" '%%pagePath%%': (typeof _%%componentName%%Route)['_outputType'];", {
|
|
149
|
+
componentName: getComponentName(page),
|
|
150
|
+
pagePath: getPath(page)
|
|
151
|
+
});
|
|
152
|
+
}).join("\n")
|
|
167
153
|
});
|
|
168
154
|
const routerFilePath = join(cwd, "src", "router.gen.ts");
|
|
169
155
|
if (existsSync(routerFilePath)) {
|
|
170
|
-
|
|
171
|
-
if (existingContent === generatedContent) return;
|
|
156
|
+
if (readFileSync(routerFilePath, "utf-8") === generatedContent) return;
|
|
172
157
|
}
|
|
173
158
|
writeFileSync(routerFilePath, generatedContent);
|
|
174
159
|
console.log("✅ Router file generated successfully!");
|
|
@@ -211,9 +196,7 @@ function watchRouter() {
|
|
|
211
196
|
cwd: process.cwd()
|
|
212
197
|
});
|
|
213
198
|
const handleAdd = async (path) => {
|
|
214
|
-
|
|
215
|
-
const code = await readFile(file, "utf8");
|
|
216
|
-
if (code !== "") return;
|
|
199
|
+
if (await readFile(join(process.cwd(), path), "utf8") !== "") return;
|
|
217
200
|
const filename = parse(path).name;
|
|
218
201
|
if (filename.startsWith("_")) switch (filename) {
|
|
219
202
|
case "_layout":
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@granite-js/plugin-router",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.33",
|
|
5
5
|
"description": "A Route Generator for Granite project",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
7
7
|
"module": "./dist/index.js",
|
|
@@ -35,13 +35,13 @@
|
|
|
35
35
|
],
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@types/node": "^22.10.2",
|
|
38
|
-
"@vitest/coverage-v8": "^
|
|
39
|
-
"tsdown": "^0.
|
|
38
|
+
"@vitest/coverage-v8": "^4.0.12",
|
|
39
|
+
"tsdown": "^0.16.5",
|
|
40
40
|
"typescript": "5.8.3",
|
|
41
|
-
"vitest": "^
|
|
41
|
+
"vitest": "^4.0.12"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@granite-js/plugin-core": "0.1.
|
|
44
|
+
"@granite-js/plugin-core": "0.1.33",
|
|
45
45
|
"@swc/core": "1.5.24",
|
|
46
46
|
"chokidar": "4.0.1",
|
|
47
47
|
"es-toolkit": "^1.39.8"
|