sv-router 0.11.1 → 0.12.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/package.json
CHANGED
package/src/cli/index.js
CHANGED
|
@@ -1,13 +1,76 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
/* eslint-disable no-console */
|
|
2
3
|
|
|
4
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
3
6
|
import { genConfig } from '../gen/config.js';
|
|
4
7
|
import { writeRouterCode } from '../gen/write-router-code.js';
|
|
5
8
|
|
|
6
9
|
const args = process.argv.slice(2).flatMap((arg) => arg.split('='));
|
|
7
10
|
|
|
11
|
+
if (args.length > 0) {
|
|
12
|
+
parseArgs();
|
|
13
|
+
} else {
|
|
14
|
+
parseViteConfig();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
writeRouterCode();
|
|
18
|
+
|
|
19
|
+
function parseViteConfig() {
|
|
20
|
+
const viteConfig = readViteConfig('ts') || readViteConfig('js');
|
|
21
|
+
if (!viteConfig) return;
|
|
22
|
+
const routerConfig = extractRouterConfig(viteConfig);
|
|
23
|
+
if (!routerConfig) return;
|
|
24
|
+
console.log('ℹ️ Using router plugin options from Vite config');
|
|
25
|
+
if (routerConfig.allLazy) genConfig.allLazy = routerConfig.allLazy;
|
|
26
|
+
if (routerConfig.js) genConfig.routesInJs = routerConfig.js;
|
|
27
|
+
if (routerConfig.path) genConfig.routesPath = routerConfig.path;
|
|
28
|
+
if (routerConfig.ignore) genConfig.ignore = routerConfig.ignore;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @param {'js' | 'ts'} extension
|
|
33
|
+
* @returns {string | undefined}
|
|
34
|
+
*/
|
|
35
|
+
function readViteConfig(extension) {
|
|
36
|
+
const vitePath = path.join(process.cwd(), 'vite.config.' + extension);
|
|
37
|
+
if (existsSync(vitePath)) {
|
|
38
|
+
return readFileSync(vitePath, 'utf8');
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @param {string} viteConfig
|
|
44
|
+
* @returns {import('../vite-plugin/index.d.ts').RouterOptions | undefined}
|
|
45
|
+
*/
|
|
46
|
+
function extractRouterConfig(viteConfig) {
|
|
47
|
+
const regex = /router\(\s*([^)]+)\)/;
|
|
48
|
+
const match = viteConfig.match(regex);
|
|
49
|
+
if (!match) return;
|
|
50
|
+
try {
|
|
51
|
+
return new Function(`return ${match[1]}`)();
|
|
52
|
+
} catch (error) {
|
|
53
|
+
console.error('⚠️ Error parsing router config:', error);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function parseArgs() {
|
|
58
|
+
const allLazyArg = arg('allLazy');
|
|
59
|
+
if (allLazyArg) genConfig.allLazy = true;
|
|
60
|
+
|
|
61
|
+
const jsArg = arg('js');
|
|
62
|
+
if (jsArg) genConfig.routesInJs = true;
|
|
63
|
+
|
|
64
|
+
const pathArg = arg('path');
|
|
65
|
+
if (pathArg) genConfig.routesPath = pathArg;
|
|
66
|
+
|
|
67
|
+
const ignoreArg = arg('ignore');
|
|
68
|
+
if (ignoreArg) genConfig.ignore = ignoreArg.split(',').map((ignore) => new RegExp(ignore, 'gu'));
|
|
69
|
+
}
|
|
70
|
+
|
|
8
71
|
/**
|
|
9
72
|
* @param {keyof import('../vite-plugin/index.d.ts').RouterOptions} option
|
|
10
|
-
* @returns
|
|
73
|
+
* @returns {string | undefined}
|
|
11
74
|
*/
|
|
12
75
|
function arg(option) {
|
|
13
76
|
const pathArgIndex = args.indexOf('--' + option);
|
|
@@ -17,17 +80,3 @@ function arg(option) {
|
|
|
17
80
|
}
|
|
18
81
|
return args[pathArgIndex];
|
|
19
82
|
}
|
|
20
|
-
|
|
21
|
-
const allLazyArg = arg('allLazy');
|
|
22
|
-
if (allLazyArg) genConfig.allLazy = true;
|
|
23
|
-
|
|
24
|
-
const jsArg = arg('js');
|
|
25
|
-
if (jsArg) genConfig.routesInJs = true;
|
|
26
|
-
|
|
27
|
-
const pathArg = arg('path');
|
|
28
|
-
if (pathArg) genConfig.routesPath = pathArg;
|
|
29
|
-
|
|
30
|
-
const ignoreArg = arg('ignore');
|
|
31
|
-
if (ignoreArg) genConfig.ignore = ignoreArg.split(',').map((ignore) => new RegExp(ignore, 'gu'));
|
|
32
|
-
|
|
33
|
-
writeRouterCode();
|
|
@@ -10,6 +10,7 @@ import path from 'node:path';
|
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
const FILENAME_REGEX = /(?<=[/.]|^)\(?([\w-]+)\)?(\.lazy)?\.svelte$/; // any.svelte, any.lazy.svelte, (any).svelte
|
|
13
|
+
const INDEX_FILENAME_REGEX = /(?<=[/.]|^)\(?index\)?(\.lazy)?\.svelte$/; // index.svelte, index.lazy.svelte, (index).svelte
|
|
13
14
|
const PARAM_FILENAME_REGEX = /(?<=[/.]|^)\(?\[([\w-]+)\]\)?(\.lazy)?\.svelte$/; // [any].svelte, [any].lazy.svelte, ([any]).svelte
|
|
14
15
|
const CATCH_ALL_FILENAME_REGEX = /(?<=[/.]|^)\(?\[\.\.\.([\w-]+)\]\)?(\.lazy)?\.svelte$/; // [...any].svelte, [...any].lazy.svelte, ([...any]).svelte
|
|
15
16
|
const OUT_OF_LAYOUT_FILENAME_REGEX = /(?<=[/.]|^)\(\[\.?\.?\.?([\w-]+)\]\)(\.lazy)?\.svelte$/; // ([any]).svelte, ([...any]).lazy.svelte
|
|
@@ -81,8 +82,9 @@ export function createRouteMap(fileTree, prefix = '') {
|
|
|
81
82
|
continue;
|
|
82
83
|
}
|
|
83
84
|
|
|
84
|
-
if (
|
|
85
|
-
const
|
|
85
|
+
if (INDEX_FILENAME_REGEX.test(entry)) {
|
|
86
|
+
const replacement = /\.?\(index\)(\.lazy)?\.svelte/.test(entry) ? '()' : '';
|
|
87
|
+
const indexEntry = entry.replace(/\.?\(?index\)?(\.lazy)?\.svelte/, replacement);
|
|
86
88
|
result['/' + (indexEntry ? filePathToRoute(indexEntry) : '')] = prefix + entry;
|
|
87
89
|
continue;
|
|
88
90
|
}
|
|
@@ -40,13 +40,13 @@ export function writeRouterCode() {
|
|
|
40
40
|
const written = writeFileIfDifferent(genConfig.routerPath, routerCode);
|
|
41
41
|
|
|
42
42
|
if (written) {
|
|
43
|
-
console.log('
|
|
43
|
+
console.log('❇️ Routes generated');
|
|
44
44
|
} else {
|
|
45
45
|
console.log('✅️ Routes already up to date');
|
|
46
46
|
}
|
|
47
47
|
} catch (error) {
|
|
48
48
|
console.error(
|
|
49
|
-
'Error during routes generation:',
|
|
49
|
+
'⚠️ Error during routes generation:',
|
|
50
50
|
error instanceof Error ? error.message : String(error),
|
|
51
51
|
);
|
|
52
52
|
}
|
|
@@ -81,7 +81,7 @@ export function matchRoute(pathname, routes) {
|
|
|
81
81
|
);
|
|
82
82
|
match = /** @type {RouteComponent} */ (routes[resolvedPath]);
|
|
83
83
|
break outer;
|
|
84
|
-
} else if (routePart !== pathPart?.toLowerCase()) {
|
|
84
|
+
} else if (routePart.toLowerCase() !== pathPart?.toLowerCase()) {
|
|
85
85
|
break;
|
|
86
86
|
}
|
|
87
87
|
|
|
@@ -93,6 +93,10 @@ export function matchRoute(pathname, routes) {
|
|
|
93
93
|
routes[/** @type {keyof Routes} */ ('/' + routeParts.join('/'))]
|
|
94
94
|
);
|
|
95
95
|
|
|
96
|
+
if (typeof routeMatch === 'function' && routeParts.length !== pathParts.length) {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
|
|
96
100
|
if (!breakFromLayouts && 'layout' in routes && routes.layout) {
|
|
97
101
|
layouts.push(routes.layout);
|
|
98
102
|
}
|