sv-router 0.0.2 → 0.0.4
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 +69 -74
- package/{dist → src}/RecursiveComponentTree.svelte +4 -4
- package/{dist → src}/Router.svelte +2 -2
- package/src/cli/index.js +13 -0
- package/src/create-router.svelte.js +67 -0
- package/src/gen/config.js +16 -0
- package/src/gen/generate-router-code.js +102 -0
- package/src/gen/write-router-code.js +52 -0
- package/src/helpers/match-route.js +112 -0
- package/src/helpers/utils.js +46 -0
- package/src/helpers/validate-routes.js +46 -0
- package/src/index.d.ts +77 -0
- package/src/index.js +2 -0
- package/src/vite-plugin/index.d.ts +12 -0
- package/src/vite-plugin/index.js +1 -0
- package/src/vite-plugin/plugin.js +42 -0
- package/dist/RecursiveComponentTree.svelte.d.ts +0 -8
- package/dist/cli/index.d.ts +0 -2
- package/dist/cli/index.js +0 -3
- package/dist/common.d.ts +0 -5
- package/dist/common.js +0 -6
- package/dist/gen/generate-router-code.d.ts +0 -12
- package/dist/gen/generate-router-code.js +0 -67
- package/dist/gen/generate-router-code.test.d.ts +0 -1
- package/dist/gen/generate-router-code.test.js +0 -103
- package/dist/gen/write-router-code.d.ts +0 -1
- package/dist/gen/write-router-code.js +0 -36
- package/dist/helpers/match-route.d.ts +0 -7
- package/dist/helpers/match-route.js +0 -66
- package/dist/helpers/match-route.test.d.ts +0 -1
- package/dist/helpers/match-route.test.js +0 -146
- package/dist/helpers/utils.d.ts +0 -8
- package/dist/helpers/utils.js +0 -28
- package/dist/helpers/validate-routes.d.ts +0 -3
- package/dist/helpers/validate-routes.js +0 -34
- package/dist/helpers/validate-routes.test.d.ts +0 -1
- package/dist/helpers/validate-routes.test.js +0 -67
- package/dist/index.d.ts +0 -2
- package/dist/index.js +0 -2
- package/dist/router.svelte.d.ts +0 -13
- package/dist/router.svelte.js +0 -49
- package/dist/types/test-types.d.ts +0 -1
- package/dist/types/test-types.js +0 -1
- package/dist/types/types.d.ts +0 -26
- package/dist/types/types.js +0 -1
- package/dist/vite-plugin/index.d.ts +0 -1
- package/dist/vite-plugin/index.js +0 -1
- package/dist/vite-plugin/plugin.d.ts +0 -2
- package/dist/vite-plugin/plugin.js +0 -31
package/package.json
CHANGED
|
@@ -1,75 +1,70 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
"peerDependencies": {
|
|
72
|
-
"svelte": "^5"
|
|
73
|
-
},
|
|
74
|
-
"packageManager": "pnpm@9.15.0"
|
|
75
|
-
}
|
|
2
|
+
"name": "sv-router",
|
|
3
|
+
"version": "0.0.4",
|
|
4
|
+
"description": "Modern Svelte routing",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"svelte",
|
|
7
|
+
"router",
|
|
8
|
+
"spa"
|
|
9
|
+
],
|
|
10
|
+
"homepage": "https://github.com/colinlienard/sv-router",
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/colinlienard/sv-router/issues"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/colinlienard/sv-router.git"
|
|
17
|
+
},
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"type": "module",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"import": "./src/index.js",
|
|
23
|
+
"svelte": "./src/index.js",
|
|
24
|
+
"types": "./src/index.d.ts"
|
|
25
|
+
},
|
|
26
|
+
"./vite-plugin": {
|
|
27
|
+
"import": "./src/vite-plugin/index.js",
|
|
28
|
+
"types": "./src/vite-plugin/index.d.ts"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"bin": "./src/cli/index.js",
|
|
32
|
+
"files": [
|
|
33
|
+
"src"
|
|
34
|
+
],
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"esm-env": "^1.2.1"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@changesets/cli": "^2.27.10",
|
|
40
|
+
"@eslint/js": "^9.16.0",
|
|
41
|
+
"@types/node": "^22.10.1",
|
|
42
|
+
"eslint-config-prettier": "^9.1.0",
|
|
43
|
+
"eslint-plugin-simple-import-sort": "^12.1.1",
|
|
44
|
+
"eslint-plugin-svelte": "^2.46.1",
|
|
45
|
+
"eslint-plugin-unicorn": "^56.0.1",
|
|
46
|
+
"globals": "^15.13.0",
|
|
47
|
+
"prettier": "^3.4.2",
|
|
48
|
+
"prettier-plugin-jsdoc": "^1.3.0",
|
|
49
|
+
"prettier-plugin-svelte": "^3.3.2",
|
|
50
|
+
"type-testing": "^0.2.0",
|
|
51
|
+
"typescript": "^5.7.2",
|
|
52
|
+
"typescript-eslint": "^8.18.0",
|
|
53
|
+
"vite": "^6.0.3",
|
|
54
|
+
"vitest": "^2.1.8"
|
|
55
|
+
},
|
|
56
|
+
"peerDependencies": {
|
|
57
|
+
"svelte": "^5"
|
|
58
|
+
},
|
|
59
|
+
"scripts": {
|
|
60
|
+
"ex:basic": "pnpm --filter basic-example",
|
|
61
|
+
"ex:file-based": "pnpm --filter file-based-example",
|
|
62
|
+
"test": "vitest",
|
|
63
|
+
"check": "tsc --noEmit && pnpm -r check",
|
|
64
|
+
"lint": "eslint .",
|
|
65
|
+
"lint:fix": "eslint . --fix",
|
|
66
|
+
"format": "prettier . --check",
|
|
67
|
+
"format:fix": "prettier . --write",
|
|
68
|
+
"changeset": "changeset"
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
<script
|
|
2
|
-
import
|
|
1
|
+
<script>
|
|
2
|
+
import { paramsStore } from './create-router.svelte.js';
|
|
3
3
|
import RecursiveComponentTree from './RecursiveComponentTree.svelte';
|
|
4
|
-
import { paramsStore } from './router.svelte.js';
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
/** @type {{ tree: import('svelte').Component[] }} */
|
|
6
|
+
let { tree } = $props();
|
|
7
7
|
|
|
8
8
|
const FirstComponent = $derived(tree[0]);
|
|
9
9
|
const restTree = $derived(tree.slice(1));
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
<script
|
|
1
|
+
<script>
|
|
2
2
|
import { on } from 'svelte/events';
|
|
3
|
+
import { componentTree, onGlobalClick, onNavigate } from './create-router.svelte.js';
|
|
3
4
|
import RecursiveComponentTree from './RecursiveComponentTree.svelte';
|
|
4
|
-
import { componentTree, onGlobalClick, onNavigate } from './router.svelte.js';
|
|
5
5
|
|
|
6
6
|
onNavigate();
|
|
7
7
|
|
package/src/cli/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { genConfig } from '../gen/config.js';
|
|
4
|
+
import { writeRouterCode } from '../gen/write-router-code.js';
|
|
5
|
+
|
|
6
|
+
const args = process.argv.slice(2).flatMap((arg) => arg.split('='));
|
|
7
|
+
|
|
8
|
+
const pathArgIndex = args.indexOf('--path');
|
|
9
|
+
if (pathArgIndex !== -1 && pathArgIndex + 1 < args.length) {
|
|
10
|
+
genConfig.routesPath = args[pathArgIndex + 1];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
writeRouterCode();
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { BROWSER, DEV } from 'esm-env';
|
|
2
|
+
import { matchRoute } from './helpers/match-route.js';
|
|
3
|
+
import { constructPath, resolveRouteComponents } from './helpers/utils.js';
|
|
4
|
+
|
|
5
|
+
/** @type {import('./index.d.ts').Routes} */
|
|
6
|
+
export let routes;
|
|
7
|
+
|
|
8
|
+
/** @type {import('svelte').Component[]} */
|
|
9
|
+
export const componentTree = $state([]);
|
|
10
|
+
|
|
11
|
+
/** @type {Record<string, string>} */
|
|
12
|
+
export const paramsStore = $state({});
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @template {import('./index.d.ts').Routes} T
|
|
16
|
+
* @param {T} r
|
|
17
|
+
* @returns {import('./index.d.ts').RouterMethods<T>}
|
|
18
|
+
*/
|
|
19
|
+
export function createRouter(r) {
|
|
20
|
+
routes = r;
|
|
21
|
+
|
|
22
|
+
if (DEV && BROWSER) {
|
|
23
|
+
import('./helpers/validate-routes.js').then(({ validateRoutes }) => {
|
|
24
|
+
validateRoutes(routes);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
path: constructPath,
|
|
30
|
+
goto(...args) {
|
|
31
|
+
const path = constructPath(args[0], args[1]);
|
|
32
|
+
globalThis.history.pushState({}, '', path);
|
|
33
|
+
onNavigate();
|
|
34
|
+
},
|
|
35
|
+
params() {
|
|
36
|
+
const readonly = $derived(paramsStore);
|
|
37
|
+
return readonly;
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function onNavigate() {
|
|
43
|
+
if (!routes) {
|
|
44
|
+
throw new Error('Router not initialized: `createRouter` was not called.');
|
|
45
|
+
}
|
|
46
|
+
const { match, layouts, params } = matchRoute(globalThis.location.pathname, routes);
|
|
47
|
+
resolveRouteComponents(match ? [...layouts, match] : layouts).then((components) => {
|
|
48
|
+
Object.assign(componentTree, components);
|
|
49
|
+
});
|
|
50
|
+
Object.assign(paramsStore, params);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** @param {Event} event */
|
|
54
|
+
export function onGlobalClick(event) {
|
|
55
|
+
const anchor = /** @type {HTMLElement} */ (event.target).closest('a');
|
|
56
|
+
if (!anchor) return;
|
|
57
|
+
|
|
58
|
+
if (anchor.hasAttribute('target') || anchor.hasAttribute('download')) return;
|
|
59
|
+
|
|
60
|
+
const url = new URL(anchor.href);
|
|
61
|
+
const currentOrigin = globalThis.location.origin;
|
|
62
|
+
if (url.origin !== currentOrigin) return;
|
|
63
|
+
|
|
64
|
+
event.preventDefault();
|
|
65
|
+
globalThis.history.pushState({}, '', anchor.href);
|
|
66
|
+
onNavigate();
|
|
67
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @type {{
|
|
3
|
+
* routesPath: string;
|
|
4
|
+
* readonly genCodeDirPath: string;
|
|
5
|
+
* readonly routerPath: string;
|
|
6
|
+
* readonly tsconfigPath: string;
|
|
7
|
+
* readonly genCodeAlias: string;
|
|
8
|
+
* }}
|
|
9
|
+
*/
|
|
10
|
+
export const genConfig = {
|
|
11
|
+
routesPath: 'src/routes',
|
|
12
|
+
genCodeDirPath: '.router',
|
|
13
|
+
routerPath: '.router/router.ts',
|
|
14
|
+
tsconfigPath: '.router/tsconfig.json',
|
|
15
|
+
genCodeAlias: 'sv-router/generated',
|
|
16
|
+
};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
/** @typedef {(string | { name: string; tree: FileTree })[]} FileTree */
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {{
|
|
8
|
+
* [key: string]: string | GeneratedRoutes;
|
|
9
|
+
* }} GeneratedRoutes
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const PARAM_FILENAME_REGEX = /\[(.*)\].svelte/g;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @param {string} routesPath
|
|
16
|
+
* @returns {string}
|
|
17
|
+
*/
|
|
18
|
+
export function generateRouterCode(routesPath) {
|
|
19
|
+
const fileTree = buildFileTree(path.join(process.cwd(), routesPath));
|
|
20
|
+
const routeMap = createRouteMap(fileTree);
|
|
21
|
+
return createRouterCode(routeMap, path.join('..', routesPath));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @param {string} routesPath
|
|
26
|
+
* @returns {FileTree}
|
|
27
|
+
*/
|
|
28
|
+
export function buildFileTree(routesPath) {
|
|
29
|
+
const entries = fs.readdirSync(routesPath);
|
|
30
|
+
/** @type {FileTree} */
|
|
31
|
+
const result = [];
|
|
32
|
+
for (const entry of entries) {
|
|
33
|
+
const stat = fs.lstatSync(path.join(routesPath, entry));
|
|
34
|
+
if (stat.isDirectory()) {
|
|
35
|
+
result.push({ name: entry, tree: buildFileTree(path.join(routesPath, entry)) });
|
|
36
|
+
} else if (entry.endsWith('.svelte')) {
|
|
37
|
+
result.push(entry);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @param {FileTree} fileTree
|
|
45
|
+
* @param {string} prefix
|
|
46
|
+
* @returns {GeneratedRoutes}
|
|
47
|
+
*/
|
|
48
|
+
export function createRouteMap(fileTree, prefix = '') {
|
|
49
|
+
/** @type {GeneratedRoutes} */
|
|
50
|
+
const result = {};
|
|
51
|
+
for (const entry of fileTree) {
|
|
52
|
+
if (typeof entry === 'string') {
|
|
53
|
+
switch (entry) {
|
|
54
|
+
case 'index.svelte': {
|
|
55
|
+
result['/'] = prefix + entry;
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
case '*.svelte': {
|
|
59
|
+
result['*'] = prefix + entry;
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
case '_layout.svelte': {
|
|
63
|
+
result['layout'] = prefix + entry;
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
default: {
|
|
67
|
+
if (PARAM_FILENAME_REGEX.test(entry)) {
|
|
68
|
+
result['/' + entry.replaceAll(PARAM_FILENAME_REGEX, ':$1')] = prefix + entry;
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
result['/' + entry.replace('.svelte', '')] = prefix + entry;
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
} else {
|
|
76
|
+
result['/' + entry.name] = createRouteMap(entry.tree, prefix + entry.name + '/');
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @param {GeneratedRoutes} routes
|
|
84
|
+
* @param {string} routesPath
|
|
85
|
+
* @returns {string}
|
|
86
|
+
*/
|
|
87
|
+
export function createRouterCode(routes, routesPath) {
|
|
88
|
+
if (!routesPath.endsWith('/')) {
|
|
89
|
+
routesPath += '/';
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const jsonRoutes = JSON.stringify(routes, undefined, 2);
|
|
93
|
+
const withImports = jsonRoutes.replaceAll(
|
|
94
|
+
/"(.*)": "(.*)",?/g,
|
|
95
|
+
`"$1": () => import("${routesPath}$2"),`,
|
|
96
|
+
);
|
|
97
|
+
return [
|
|
98
|
+
'import { createRouter } from "sv-router";',
|
|
99
|
+
'\n\n',
|
|
100
|
+
`export const { path, goto, params } = createRouter(${withImports});`,
|
|
101
|
+
].join('');
|
|
102
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import { genConfig } from './config.js';
|
|
4
|
+
import { generateRouterCode } from './generate-router-code.js';
|
|
5
|
+
|
|
6
|
+
export function writeRouterCode() {
|
|
7
|
+
try {
|
|
8
|
+
if (!fs.existsSync(genConfig.genCodeDirPath)) {
|
|
9
|
+
fs.mkdirSync(genConfig.genCodeDirPath);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// Write `.router/router.ts` file
|
|
13
|
+
const routerCode = generateRouterCode(genConfig.routesPath);
|
|
14
|
+
writeFileIfDifferent(genConfig.routerPath, routerCode);
|
|
15
|
+
|
|
16
|
+
// Write `.router/tsconfig.json` file
|
|
17
|
+
const tsConfig = {
|
|
18
|
+
compilerOptions: {
|
|
19
|
+
module: 'preserve',
|
|
20
|
+
moduleResolution: 'bundler',
|
|
21
|
+
baseUrl: '..',
|
|
22
|
+
paths: {
|
|
23
|
+
[genConfig.genCodeAlias]: [genConfig.routerPath],
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
include: [
|
|
27
|
+
'../src/**/*.js',
|
|
28
|
+
'../src/**/*.ts',
|
|
29
|
+
'../src/**/*.svelte',
|
|
30
|
+
'../tests/**/*.js',
|
|
31
|
+
'../tests/**/*.ts',
|
|
32
|
+
'../tests/**/*.svelte',
|
|
33
|
+
'./router.ts',
|
|
34
|
+
],
|
|
35
|
+
};
|
|
36
|
+
writeFileIfDifferent(genConfig.tsconfigPath, JSON.stringify(tsConfig, undefined, 2));
|
|
37
|
+
|
|
38
|
+
console.log('✅️ Routes generated');
|
|
39
|
+
} catch (error) {
|
|
40
|
+
console.error('Error during routes generation:', error);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @param {string} filePath
|
|
46
|
+
* @param {string} content
|
|
47
|
+
*/
|
|
48
|
+
function writeFileIfDifferent(filePath, content) {
|
|
49
|
+
if (!fs.existsSync(filePath) || fs.readFileSync(filePath, 'utf8') !== content) {
|
|
50
|
+
fs.writeFileSync(filePath, content);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {import('../index.d.ts').LayoutComponent} LayoutComponent
|
|
3
|
+
*
|
|
4
|
+
* @typedef {import('../index.d.ts').RouteComponent} RouteComponent
|
|
5
|
+
*
|
|
6
|
+
* @typedef {import('../index.d.ts').Routes} Routes
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @param {string} pathname
|
|
11
|
+
* @param {Routes} routes
|
|
12
|
+
* @returns {{
|
|
13
|
+
* match: RouteComponent | undefined;
|
|
14
|
+
* layouts: LayoutComponent[];
|
|
15
|
+
* params: Record<string, string>;
|
|
16
|
+
* breakFromLayouts: boolean;
|
|
17
|
+
* }}
|
|
18
|
+
*/
|
|
19
|
+
export function matchRoute(pathname, routes) {
|
|
20
|
+
// Remove trailing slash
|
|
21
|
+
if (pathname.length > 1 && pathname.endsWith('/')) {
|
|
22
|
+
pathname = pathname.slice(0, -1);
|
|
23
|
+
}
|
|
24
|
+
const pathParts = pathname.split('/');
|
|
25
|
+
const allRouteParts = sortRoutes(Object.keys(routes)).map((route) => route.split('/'));
|
|
26
|
+
|
|
27
|
+
/** @type {RouteComponent | undefined} */
|
|
28
|
+
let match;
|
|
29
|
+
|
|
30
|
+
/** @type {LayoutComponent[]} */
|
|
31
|
+
let layouts = [];
|
|
32
|
+
|
|
33
|
+
/** @type {Record<string, string>} */
|
|
34
|
+
let params = {};
|
|
35
|
+
|
|
36
|
+
let breakFromLayouts = false;
|
|
37
|
+
|
|
38
|
+
outer: for (const routeParts of allRouteParts) {
|
|
39
|
+
for (let [index, routePart] of sortRoutes(routeParts).entries()) {
|
|
40
|
+
breakFromLayouts = routePart.startsWith('(') && routePart.endsWith(')');
|
|
41
|
+
if (breakFromLayouts) {
|
|
42
|
+
routePart = routePart.slice(1, -1);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const pathPart = pathParts[index];
|
|
46
|
+
if (routePart.startsWith(':')) {
|
|
47
|
+
params[routePart.slice(1)] = pathPart;
|
|
48
|
+
} else if (routePart === '*') {
|
|
49
|
+
match = /** @type {RouteComponent} */ (
|
|
50
|
+
routes[/** @type {keyof Routes} */ (routeParts.join('/'))]
|
|
51
|
+
);
|
|
52
|
+
break outer;
|
|
53
|
+
} else if (routePart !== pathPart) {
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (index !== routeParts.length - 1) {
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (!breakFromLayouts && 'layout' in routes && routes.layout) {
|
|
62
|
+
layouts.push(routes.layout);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const routeMatch = /** @type {RouteComponent} */ (
|
|
66
|
+
routes[/** @type {keyof Routes} */ (routeParts.join('/'))]
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
if (typeof routeMatch === 'function') {
|
|
70
|
+
if (routeParts.length === pathParts.length) {
|
|
71
|
+
match = routeMatch;
|
|
72
|
+
} else {
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
} else if (routeMatch) {
|
|
76
|
+
const nestedPathname = '/' + pathParts.slice(index + 1).join('/');
|
|
77
|
+
const result = matchRoute(nestedPathname, routeMatch);
|
|
78
|
+
if (result) {
|
|
79
|
+
match = result.match;
|
|
80
|
+
params = { ...params, ...result.params };
|
|
81
|
+
if (result.breakFromLayouts) {
|
|
82
|
+
layouts = [];
|
|
83
|
+
} else {
|
|
84
|
+
layouts.push(...result.layouts);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
break outer;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return { match, layouts, params, breakFromLayouts };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* @param {string[]} routes
|
|
97
|
+
* @returns {string[]}
|
|
98
|
+
*/
|
|
99
|
+
export function sortRoutes(routes) {
|
|
100
|
+
return routes.toSorted((a, b) => getRoutePriority(a) - getRoutePriority(b));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* @param {string} route
|
|
105
|
+
* @returns {number}
|
|
106
|
+
*/
|
|
107
|
+
function getRoutePriority(route) {
|
|
108
|
+
if (route === '' || route === '/') return 1;
|
|
109
|
+
if (route === '*') return 4;
|
|
110
|
+
if (route.includes(':')) return 3;
|
|
111
|
+
return 2;
|
|
112
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {string} path
|
|
3
|
+
* @param {Record<string, string>} [params]
|
|
4
|
+
* @returns {string}
|
|
5
|
+
*/
|
|
6
|
+
export function constructPath(path, params) {
|
|
7
|
+
if (!params) return path;
|
|
8
|
+
|
|
9
|
+
let result = path;
|
|
10
|
+
for (const key in params) {
|
|
11
|
+
result = result.replace(`:${key}`, params[key]);
|
|
12
|
+
}
|
|
13
|
+
return result;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @param {import('../index.d.ts').RouteComponent[]} input
|
|
18
|
+
* @returns {Promise<import('svelte').Component[]>}
|
|
19
|
+
*/
|
|
20
|
+
export function resolveRouteComponents(input) {
|
|
21
|
+
return Promise.all(input.map((c) => resolveRouteComponent(c)));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @param {import('../index.d.ts').RouteComponent} input
|
|
26
|
+
* @returns {Promise<import('svelte').Component>}
|
|
27
|
+
*/
|
|
28
|
+
export function resolveRouteComponent(input) {
|
|
29
|
+
return new Promise((resolve) => {
|
|
30
|
+
if (isLazyImport(input)) {
|
|
31
|
+
Promise.resolve(input()).then((module) => {
|
|
32
|
+
resolve(module.default);
|
|
33
|
+
});
|
|
34
|
+
} else {
|
|
35
|
+
resolve(input);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @param {unknown} input
|
|
42
|
+
* @returns {input is import('../index.d.ts').LazyRouteComponent}
|
|
43
|
+
*/
|
|
44
|
+
export function isLazyImport(input) {
|
|
45
|
+
return typeof input === 'function' && !!/\(\)\s?=>\s?import\(.*\)/g.test(String(input));
|
|
46
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/** @param {import('../index.d.ts').Routes} routes */
|
|
2
|
+
export function validateRoutes(routes) {
|
|
3
|
+
const paths = getRoutePaths(routes);
|
|
4
|
+
const wildcardPaths = paths.filter((path) => path.endsWith('*'));
|
|
5
|
+
for (const wildcardPath of wildcardPaths) {
|
|
6
|
+
const parentPath = wildcardPath.slice(0, -1);
|
|
7
|
+
const dynamicPath = paths.find(
|
|
8
|
+
(p) =>
|
|
9
|
+
p !== '/' &&
|
|
10
|
+
!p.endsWith('*') &&
|
|
11
|
+
p.startsWith(parentPath === '' ? '/:' : parentPath) &&
|
|
12
|
+
p.match(/:[^/]*$/g), // Match dynamic paths without slashes after the colon
|
|
13
|
+
);
|
|
14
|
+
if (dynamicPath) {
|
|
15
|
+
console.warn(
|
|
16
|
+
`Router warning: Wildcard route \`${wildcardPath}\` should not be at the same level as dynamic route \`${dynamicPath}\`.`,
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @param {import('../index.d.ts').Routes} routes
|
|
24
|
+
* @returns {string[]}
|
|
25
|
+
*/
|
|
26
|
+
export function getRoutePaths(routes) {
|
|
27
|
+
const paths = [];
|
|
28
|
+
for (const [key, value] of Object.entries(routes)) {
|
|
29
|
+
if (typeof value === 'object') {
|
|
30
|
+
paths.push(
|
|
31
|
+
...getRoutePaths(value).map((path) => {
|
|
32
|
+
if (path === '*') {
|
|
33
|
+
return key + '/*';
|
|
34
|
+
}
|
|
35
|
+
if (path === '/') {
|
|
36
|
+
return key;
|
|
37
|
+
}
|
|
38
|
+
return key + path;
|
|
39
|
+
}),
|
|
40
|
+
);
|
|
41
|
+
} else {
|
|
42
|
+
paths.push(key);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return paths;
|
|
46
|
+
}
|