@granite-js/plugin-router 2.5.5 → 3.0.0
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 +24 -0
- package/README.md +17 -0
- package/dist/index.cjs +54 -268
- package/dist/index.d.cts +3 -6
- package/dist/index.d.ts +3 -6
- package/dist/index.js +53 -240
- package/dist/mpack.cjs +30 -0
- package/dist/mpack.d.cts +9 -0
- package/dist/mpack.d.ts +9 -0
- package/dist/mpack.js +29 -0
- package/dist/watchRouter-CfoP6FM2.cjs +276 -0
- package/dist/watchRouter-opiv55Vn.js +231 -0
- package/package.json +21 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @granite-js/plugin-router
|
|
2
2
|
|
|
3
|
+
## 3.0.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- @granite-js/plugin-core@3.0.0
|
|
8
|
+
|
|
3
9
|
## 2.5.5
|
|
4
10
|
|
|
5
11
|
### Patch Changes
|
|
@@ -96,6 +102,24 @@
|
|
|
96
102
|
|
|
97
103
|
- @granite-js/plugin-core@2.0.0
|
|
98
104
|
|
|
105
|
+
## 1.0.45
|
|
106
|
+
|
|
107
|
+
### Patch Changes
|
|
108
|
+
|
|
109
|
+
- @granite-js/plugin-core@1.0.45
|
|
110
|
+
|
|
111
|
+
## 1.0.44
|
|
112
|
+
|
|
113
|
+
### Patch Changes
|
|
114
|
+
|
|
115
|
+
- @granite-js/plugin-core@1.0.44
|
|
116
|
+
|
|
117
|
+
## 1.0.43
|
|
118
|
+
|
|
119
|
+
### Patch Changes
|
|
120
|
+
|
|
121
|
+
- @granite-js/plugin-core@1.0.43
|
|
122
|
+
|
|
99
123
|
## 1.0.42
|
|
100
124
|
|
|
101
125
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -1,3 +1,20 @@
|
|
|
1
1
|
# @granite-js/plugin-router
|
|
2
2
|
|
|
3
3
|
A Route Generator for Granite project
|
|
4
|
+
|
|
5
|
+
## Rollipop
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { router } from '@granite-js/plugin-router';
|
|
9
|
+
|
|
10
|
+
const plugins = [router()];
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Mpack (deprecated)
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { defineConfig } from '@granite-js/mpack/config';
|
|
17
|
+
import { router } from '@granite-js/plugin-router/mpack';
|
|
18
|
+
|
|
19
|
+
export default defineConfig({ plugins: [router()] });
|
|
20
|
+
```
|
package/dist/index.cjs
CHANGED
|
@@ -1,284 +1,70 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
get: ((k) => from[k]).bind(null, key),
|
|
16
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
return to;
|
|
22
|
-
};
|
|
23
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
24
|
-
value: mod,
|
|
25
|
-
enumerable: true
|
|
26
|
-
}) : target, mod));
|
|
27
|
-
|
|
28
|
-
//#endregion
|
|
29
|
-
let fs = require("fs");
|
|
30
|
-
let path = require("path");
|
|
31
|
-
let _swc_core = require("@swc/core");
|
|
32
|
-
let es_toolkit = require("es-toolkit");
|
|
33
|
-
let fs_promises = require("fs/promises");
|
|
34
|
-
let chokidar = require("chokidar");
|
|
35
|
-
chokidar = __toESM(chokidar);
|
|
36
|
-
|
|
37
|
-
//#region src/checkExportRoute.ts
|
|
38
|
-
/**
|
|
39
|
-
* Checks if Route is exported from the file.
|
|
40
|
-
*/
|
|
41
|
-
function checkExportRoute(path) {
|
|
42
|
-
try {
|
|
43
|
-
const ast = (0, _swc_core.parseFileSync)(path, {
|
|
44
|
-
syntax: "typescript",
|
|
45
|
-
tsx: true
|
|
46
|
-
});
|
|
47
|
-
if (ast.body.some((node) => {
|
|
48
|
-
if (node.type !== "ExportNamedDeclaration") return false;
|
|
49
|
-
return node.specifiers?.some((specifier) => {
|
|
50
|
-
if (specifier.type !== "ExportSpecifier") return false;
|
|
51
|
-
return specifier.orig?.value === "Route";
|
|
52
|
-
});
|
|
53
|
-
})) return true;
|
|
54
|
-
return ast.body.some((node) => {
|
|
55
|
-
if (node.type !== "ExportDeclaration") return false;
|
|
56
|
-
if (node.declaration.type !== "VariableDeclaration") return false;
|
|
57
|
-
return node.declaration.declarations.some((declaration) => declaration.id.type === "Identifier" && declaration.id.value === "Route");
|
|
58
|
-
});
|
|
59
|
-
} catch {
|
|
60
|
-
return false;
|
|
61
|
-
}
|
|
2
|
+
const require_watchRouter = require('./watchRouter-CfoP6FM2.cjs');
|
|
3
|
+
let node_path = require("node:path");
|
|
4
|
+
node_path = require_watchRouter.__toESM(node_path);
|
|
5
|
+
|
|
6
|
+
//#region src/next/index.ts
|
|
7
|
+
const REQUIRE_CONTEXT_IMPORT = /^(?:\.{1,2}\/)+(?:[^/]+\/)*require\.context(?:\.[^/]+)*$/;
|
|
8
|
+
const REQUIRE_CONTEXT_MODULE = /[\\/]require\.context(?:\.[^/\\]+)*$/;
|
|
9
|
+
function router() {
|
|
10
|
+
return [
|
|
11
|
+
routeGenerator(),
|
|
12
|
+
routeWatcher(),
|
|
13
|
+
pageImports()
|
|
14
|
+
];
|
|
62
15
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
//#endregion
|
|
73
|
-
//#region src/getPath.ts
|
|
74
|
-
function getPath(filePath) {
|
|
75
|
-
let path = filePath.replace(/^pages\//, "").replace(/\.[^/.]+$/, "");
|
|
76
|
-
if (path.endsWith("/index")) path = path.replace(/\/index$/, "");
|
|
77
|
-
else if (path === "index") path = "";
|
|
78
|
-
return `/${path}`;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
//#endregion
|
|
82
|
-
//#region src/template.ts
|
|
83
|
-
const NEW_ROUTE_FILE_TEMPLATE = `import React from 'react';
|
|
84
|
-
import { Text, View } from 'react-native';
|
|
85
|
-
import { createRoute } from '@granite-js/react-native';
|
|
86
|
-
|
|
87
|
-
export const Route = createRoute('%%path%%', {
|
|
88
|
-
validateParams: (params) => params,
|
|
89
|
-
component: %%componentName%%,
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
function %%componentName%%() {
|
|
93
|
-
return (
|
|
94
|
-
<View>
|
|
95
|
-
<Text>Hello %%componentName%%</Text>
|
|
96
|
-
</View>
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
`;
|
|
100
|
-
const ROUTER_GEN_TEMPLATE = `/* eslint-disable */
|
|
101
|
-
// This file is auto-generated by @granite-js/react-native. DO NOT EDIT.
|
|
102
|
-
%%pageImports%%
|
|
103
|
-
|
|
104
|
-
declare module '@granite-js/react-native' {
|
|
105
|
-
interface RegisterScreenInput {
|
|
106
|
-
%%pageInputRoutes%%
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
interface RegisterScreen {
|
|
110
|
-
%%pageRoutes%%
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
`;
|
|
114
|
-
const NEW_LAYOUT_FILE_TEMPLATE = `import React, { PropsWithChildren } from 'react';
|
|
115
|
-
|
|
116
|
-
export default function %%componentName%%Layout({ children }: PropsWithChildren) {
|
|
117
|
-
return (
|
|
118
|
-
<>
|
|
119
|
-
{children}
|
|
120
|
-
</>
|
|
121
|
-
);
|
|
122
|
-
}
|
|
123
|
-
`;
|
|
124
|
-
|
|
125
|
-
//#endregion
|
|
126
|
-
//#region src/utils/transformTemplate.ts
|
|
127
|
-
/**
|
|
128
|
-
* 템플릿 문자열에서 %%key%% 형식의 플레이스홀더를 values 객체의 값으로 대체합니다.
|
|
129
|
-
* 제네릭 타입 T를 통해 템플릿 문자열의 플레이스홀더 키를 자동으로 추론하여 타입 안정성을 보장합니다.
|
|
130
|
-
*
|
|
131
|
-
* @example
|
|
132
|
-
* const str = "안녕하세요 %%name%%님, 당신의 나이는 %%age%%살 입니다."
|
|
133
|
-
* const result = transformTemplate(str, { name: "홍길동", age: "20" })
|
|
134
|
-
* // 결과: "안녕하세요 홍길동님, 당신의 나이는 20살 입니다."
|
|
135
|
-
*/
|
|
136
|
-
function transformTemplate(templateString, values) {
|
|
137
|
-
let result = templateString;
|
|
138
|
-
for (const key in values) {
|
|
139
|
-
const placeholder = `%%${key}%%`;
|
|
140
|
-
result = result.replace(new RegExp(placeholder, "g"), values[key]);
|
|
141
|
-
}
|
|
142
|
-
return result;
|
|
16
|
+
function routeGenerator() {
|
|
17
|
+
return {
|
|
18
|
+
name: "granite:router:generate",
|
|
19
|
+
configResolved(config) {
|
|
20
|
+
require_watchRouter.generateRouterFile(config.root);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
143
23
|
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
if (file.isDirectory()) return [...acc, ...getPageFiles(`${dir}/${file.name}`, `${prefix}${file.name}/`)];
|
|
152
|
-
if (file.name.endsWith(".tsx") || file.name.endsWith(".ts")) {
|
|
153
|
-
const name = (0, path.parse)(file.name).name;
|
|
154
|
-
const ext = (0, path.parse)(file.name).ext;
|
|
155
|
-
return [...acc, `${prefix}${name}${ext}`];
|
|
156
|
-
}
|
|
157
|
-
return acc;
|
|
158
|
-
}, []);
|
|
159
|
-
}
|
|
160
|
-
const allPages = getPageFiles("pages");
|
|
161
|
-
const exportRouteMap = new Map(allPages.map((page) => [page, checkExportRoute((0, path.join)(cwd, "pages", page))]));
|
|
162
|
-
const pageFiles = allPages.filter((page) => !page.startsWith("_") && exportRouteMap.get(page));
|
|
163
|
-
const generatedContent = transformTemplate(ROUTER_GEN_TEMPLATE, {
|
|
164
|
-
pageImports: pageFiles.map((page) => {
|
|
165
|
-
return transformTemplate("import { Route as _%%componentName%%Route } from '../pages%%pagePath%%';", {
|
|
166
|
-
componentName: getComponentName(page),
|
|
167
|
-
pagePath: getPath(page)
|
|
168
|
-
});
|
|
169
|
-
}).join("\n"),
|
|
170
|
-
pageInputRoutes: pageFiles.map((page) => {
|
|
171
|
-
return transformTemplate(" '%%pagePath%%': (typeof _%%componentName%%Route)['_inputType'];", {
|
|
172
|
-
componentName: getComponentName(page),
|
|
173
|
-
pagePath: getPath(page)
|
|
174
|
-
});
|
|
175
|
-
}).join("\n"),
|
|
176
|
-
pageRoutes: pageFiles.map((page) => {
|
|
177
|
-
return transformTemplate(" '%%pagePath%%': (typeof _%%componentName%%Route)['_outputType'];", {
|
|
178
|
-
componentName: getComponentName(page),
|
|
179
|
-
pagePath: getPath(page)
|
|
24
|
+
function routeWatcher() {
|
|
25
|
+
return {
|
|
26
|
+
name: "granite:router:watch",
|
|
27
|
+
configureServer(server) {
|
|
28
|
+
const close = require_watchRouter.watchRouter(server.config.root);
|
|
29
|
+
server.instance.addHook("onClose", async () => {
|
|
30
|
+
await close();
|
|
180
31
|
});
|
|
181
|
-
}).join("\n")
|
|
182
|
-
});
|
|
183
|
-
const routerFilePath = (0, path.join)(cwd, "src", "router.gen.ts");
|
|
184
|
-
if ((0, fs.existsSync)(routerFilePath)) {
|
|
185
|
-
if ((0, fs.readFileSync)(routerFilePath, "utf-8") === generatedContent) return;
|
|
186
|
-
}
|
|
187
|
-
(0, fs.writeFileSync)(routerFilePath, generatedContent);
|
|
188
|
-
console.log("✅ Router file generated successfully!");
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
//#endregion
|
|
192
|
-
//#region src/getPageName.ts
|
|
193
|
-
function getPageName(filePath) {
|
|
194
|
-
const parts = filePath.split("/");
|
|
195
|
-
const indexOfPages = parts.indexOf("pages");
|
|
196
|
-
if (indexOfPages === -1) return "";
|
|
197
|
-
const relevantParts = parts.slice(indexOfPages + 1);
|
|
198
|
-
if (relevantParts.at(-1)?.startsWith("_")) relevantParts.pop();
|
|
199
|
-
const lastPart = relevantParts.at(-1);
|
|
200
|
-
if (!lastPart) return "";
|
|
201
|
-
return (0, es_toolkit.pascalCase)(lastPart);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
//#endregion
|
|
205
|
-
//#region src/transformNewRouteFile.ts
|
|
206
|
-
async function transformNewRouteFile(path) {
|
|
207
|
-
return transformTemplate(NEW_ROUTE_FILE_TEMPLATE, {
|
|
208
|
-
path: getPath(path),
|
|
209
|
-
componentName: getComponentName(path)
|
|
210
|
-
});
|
|
211
|
-
}
|
|
212
|
-
async function transformNewLayoutFile(path) {
|
|
213
|
-
return transformTemplate(NEW_LAYOUT_FILE_TEMPLATE, { componentName: getPageName(path) });
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
//#endregion
|
|
217
|
-
//#region src/watchRouter.ts
|
|
218
|
-
function watchRouter() {
|
|
219
|
-
const watcher = chokidar.default.watch("./pages", {
|
|
220
|
-
ignored: (path$1, stats) => {
|
|
221
|
-
return Boolean(stats?.isFile() && !path$1.endsWith(".ts") && !path$1.endsWith(".tsx"));
|
|
222
|
-
},
|
|
223
|
-
ignoreInitial: true,
|
|
224
|
-
persistent: true,
|
|
225
|
-
cwd: process.cwd()
|
|
226
|
-
});
|
|
227
|
-
const handleAdd = async (path$2) => {
|
|
228
|
-
if (await (0, fs_promises.readFile)((0, path.join)(process.cwd(), path$2), "utf8") !== "") return;
|
|
229
|
-
const filename = (0, path.parse)(path$2).name;
|
|
230
|
-
if (filename.startsWith("_")) switch (filename) {
|
|
231
|
-
case "_layout":
|
|
232
|
-
console.log("👀 Layout file has been added");
|
|
233
|
-
await (0, fs_promises.writeFile)(path$2, await transformNewLayoutFile(path$2));
|
|
234
|
-
return;
|
|
235
|
-
default: return;
|
|
236
32
|
}
|
|
237
|
-
const componentName = (0, es_toolkit.kebabCase)(filename);
|
|
238
|
-
if (componentName !== filename) {
|
|
239
|
-
console.log(`❌ File name should be in kebab-case format. Would you like to rename ${filename} to ${componentName}?`);
|
|
240
|
-
return;
|
|
241
|
-
}
|
|
242
|
-
console.log(`👀 File ${path$2} has been added`);
|
|
243
|
-
await (0, fs_promises.writeFile)(path$2, await transformNewRouteFile(path$2));
|
|
244
|
-
await generateRouterFile();
|
|
245
|
-
};
|
|
246
|
-
watcher.on("add", handleAdd);
|
|
247
|
-
watcher.on("change", generateRouterFile);
|
|
248
|
-
watcher.on("unlink", generateRouterFile);
|
|
249
|
-
return () => {
|
|
250
|
-
watcher.off("add", handleAdd);
|
|
251
|
-
watcher.off("change", generateRouterFile);
|
|
252
|
-
watcher.off("unlink", generateRouterFile);
|
|
253
|
-
watcher.close();
|
|
254
33
|
};
|
|
255
34
|
}
|
|
256
|
-
|
|
257
|
-
//#endregion
|
|
258
|
-
//#region src/routerPlugin.ts
|
|
259
|
-
const DEFAULT_OPTIONS = { watch: true };
|
|
260
|
-
const router = (options = DEFAULT_OPTIONS) => {
|
|
261
|
-
const resolvedOptions = {
|
|
262
|
-
...DEFAULT_OPTIONS,
|
|
263
|
-
...options
|
|
264
|
-
};
|
|
35
|
+
function pageImports() {
|
|
265
36
|
return {
|
|
266
|
-
name: "router-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
handler
|
|
270
|
-
|
|
37
|
+
name: "granite:router:page-imports",
|
|
38
|
+
resolveId: {
|
|
39
|
+
filter: { id: REQUIRE_CONTEXT_IMPORT },
|
|
40
|
+
handler(source, importer) {
|
|
41
|
+
return importer == null ? null : node_path.resolve(node_path.dirname(importer), source);
|
|
271
42
|
}
|
|
272
43
|
},
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
handler
|
|
276
|
-
|
|
277
|
-
|
|
44
|
+
load: {
|
|
45
|
+
filter: { id: REQUIRE_CONTEXT_MODULE },
|
|
46
|
+
handler() {
|
|
47
|
+
return `
|
|
48
|
+
const modules = import.meta.glob('./**/*.{js,jsx,ts,tsx}', { base: './pages', eager: true });
|
|
49
|
+
|
|
50
|
+
export const context = Object.assign(
|
|
51
|
+
(key) => {
|
|
52
|
+
if (!Object.prototype.hasOwnProperty.call(modules, key)) {
|
|
53
|
+
throw new Error(\`Unknown screen: \${key}\`);
|
|
54
|
+
}
|
|
55
|
+
return modules[key];
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
keys: () => Object.keys(modules),
|
|
59
|
+
resolve: (key) => key,
|
|
60
|
+
id: 'pages',
|
|
61
|
+
}
|
|
62
|
+
);
|
|
63
|
+
`;
|
|
278
64
|
}
|
|
279
65
|
}
|
|
280
66
|
};
|
|
281
|
-
}
|
|
67
|
+
}
|
|
282
68
|
|
|
283
69
|
//#endregion
|
|
284
70
|
exports.router = router;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PluginOption } from "rollipop";
|
|
2
2
|
|
|
3
|
-
//#region src/
|
|
4
|
-
|
|
5
|
-
watch?: boolean;
|
|
6
|
-
}
|
|
7
|
-
declare const router: (options?: RouterPluginOptions) => GranitePluginCore;
|
|
3
|
+
//#region src/next/index.d.ts
|
|
4
|
+
declare function router(): PluginOption;
|
|
8
5
|
//#endregion
|
|
9
6
|
export { router };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PluginOption } from "rollipop";
|
|
2
2
|
|
|
3
|
-
//#region src/
|
|
4
|
-
|
|
5
|
-
watch?: boolean;
|
|
6
|
-
}
|
|
7
|
-
declare const router: (options?: RouterPluginOptions) => GranitePluginCore;
|
|
3
|
+
//#region src/next/index.d.ts
|
|
4
|
+
declare function router(): PluginOption;
|
|
8
5
|
//#endregion
|
|
9
6
|
export { router };
|