sv-router 0.15.0 → 0.16.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 +2 -2
- package/src/Router.svelte +1 -0
- package/src/attachments.svelte.js +0 -22
- package/src/gen/generate-router-code.js +8 -2
- package/src/gen/write-router-code.js +20 -4
- package/src/index.d.ts +0 -13
- package/src/index.js +1 -1
- package/src/search-params.svelte.js +2 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sv-router",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.0",
|
|
4
4
|
"description": "Modern Svelte Routing",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"svelte",
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"@vitest/coverage-v8": "^4.1.0",
|
|
47
47
|
"eslint": "^10.0.3",
|
|
48
48
|
"eslint-config-prettier": "^10.1.8",
|
|
49
|
-
"eslint-plugin-simple-import-sort": "^
|
|
49
|
+
"eslint-plugin-simple-import-sort": "^13.0.0",
|
|
50
50
|
"eslint-plugin-svelte": "^3.15.2",
|
|
51
51
|
"eslint-plugin-unicorn": "^64.0.0",
|
|
52
52
|
"globals": "^17.4.0",
|
package/src/Router.svelte
CHANGED
|
@@ -23,25 +23,3 @@ export function isActiveLink({ className = 'is-active', startsWith = false } = {
|
|
|
23
23
|
});
|
|
24
24
|
};
|
|
25
25
|
}
|
|
26
|
-
|
|
27
|
-
/** @type {import('./index.d.ts').IsActiveLinkAction} */
|
|
28
|
-
export function isActiveLinkAction(node, { className = 'is-active', startsWith = false } = {}) {
|
|
29
|
-
if (node.tagName !== 'A') {
|
|
30
|
-
throw new Error('isActiveLink can only be used on <a> elements');
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
$effect(() => {
|
|
34
|
-
let pathname;
|
|
35
|
-
if (base.name === '#') {
|
|
36
|
-
pathname = new URL(node.href).hash.slice(1);
|
|
37
|
-
} else {
|
|
38
|
-
pathname = new URL(node.href).pathname;
|
|
39
|
-
}
|
|
40
|
-
const tokens = className.split(' ').filter(Boolean) ?? [];
|
|
41
|
-
if (startsWith ? location.pathname.startsWith(pathname) : location.pathname === pathname) {
|
|
42
|
-
node.classList.add(...tokens);
|
|
43
|
-
} else {
|
|
44
|
-
node.classList.remove(...tokens);
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
}
|
|
@@ -165,8 +165,14 @@ function mergeRouteGroup(result, childMap) {
|
|
|
165
165
|
} else if (!Array.isArray(val)) {
|
|
166
166
|
routeWithGroupFiles = { ...val };
|
|
167
167
|
}
|
|
168
|
-
if (layout)
|
|
169
|
-
|
|
168
|
+
if (layout) {
|
|
169
|
+
if (routeWithGroupFiles.layout) {
|
|
170
|
+
routeWithGroupFiles = { '/': routeWithGroupFiles, layout: layout };
|
|
171
|
+
} else {
|
|
172
|
+
routeWithGroupFiles.layout = layout;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
if (hooks) routeWithGroupFiles.hooks = hooks;
|
|
170
176
|
if (mergedMeta) routeWithGroupFiles.meta = /** @type {string | string[]} */ (mergedMeta);
|
|
171
177
|
if (result[key]) {
|
|
172
178
|
throw new Error(`Route conflict at \`${key}\``);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/* eslint-disable no-console */
|
|
2
2
|
import fs from 'node:fs';
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
3
4
|
import { genConfig } from './config.js';
|
|
4
5
|
import { generateRouterCode } from './generate-router-code.js';
|
|
5
6
|
|
|
@@ -10,14 +11,18 @@ export function writeRouterCode() {
|
|
|
10
11
|
}
|
|
11
12
|
|
|
12
13
|
// Write `.router/tsconfig.json` file
|
|
14
|
+
const tsMajor = getTypeScriptMajorVersion();
|
|
15
|
+
const useBaseUrl = tsMajor === undefined || tsMajor < 6;
|
|
16
|
+
let alias = genConfig.routerPath;
|
|
17
|
+
if (!useBaseUrl) {
|
|
18
|
+
alias = alias.replace(genConfig.genCodeDirPath, '.');
|
|
19
|
+
}
|
|
13
20
|
const tsConfig = {
|
|
14
21
|
compilerOptions: {
|
|
15
22
|
module: 'preserve',
|
|
16
23
|
moduleResolution: 'bundler',
|
|
17
|
-
baseUrl: '..',
|
|
18
|
-
paths: {
|
|
19
|
-
[genConfig.genCodeAlias]: [genConfig.routerPath],
|
|
20
|
-
},
|
|
24
|
+
...(useBaseUrl ? { baseUrl: '..' } : {}),
|
|
25
|
+
paths: { [genConfig.genCodeAlias]: [alias] },
|
|
21
26
|
},
|
|
22
27
|
include: [
|
|
23
28
|
'../src/**/*.js',
|
|
@@ -62,3 +67,14 @@ function writeFileIfDifferent(filePath, content) {
|
|
|
62
67
|
return true;
|
|
63
68
|
}
|
|
64
69
|
}
|
|
70
|
+
|
|
71
|
+
function getTypeScriptMajorVersion() {
|
|
72
|
+
try {
|
|
73
|
+
const require = createRequire(process.cwd() + '/package.json');
|
|
74
|
+
const tsPackagePath = require.resolve('typescript/package.json');
|
|
75
|
+
const tsPackage = JSON.parse(fs.readFileSync(tsPackagePath, 'utf8'));
|
|
76
|
+
return Number(tsPackage.version.split('.')[0]);
|
|
77
|
+
} catch {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
}
|
package/src/index.d.ts
CHANGED
|
@@ -2,19 +2,6 @@ import type { Component, Snippet } from 'svelte';
|
|
|
2
2
|
import type { Action } from 'svelte/action';
|
|
3
3
|
import type { Attachment } from 'svelte/attachments';
|
|
4
4
|
|
|
5
|
-
/**
|
|
6
|
-
* @deprecated Use the `isActiveLink` [attachment](https://svelte.dev/docs/svelte/@attach) instead.
|
|
7
|
-
*
|
|
8
|
-
* A Svelte action that will add a class to the anchor if its `href` matches the current route. It
|
|
9
|
-
* can have an optional `className` parameter to specify the class to add, otherwise it will
|
|
10
|
-
* default to `is-active`, and an optional `startsWith` parameter.
|
|
11
|
-
*
|
|
12
|
-
* ```svelte
|
|
13
|
-
* <a href={p('/about')} use:isActiveLink={{ className: 'active-link' }}>
|
|
14
|
-
* ```
|
|
15
|
-
*/
|
|
16
|
-
export const isActiveLinkAction: IsActiveLinkAction;
|
|
17
|
-
|
|
18
5
|
/**
|
|
19
6
|
* A Svelte attachment that will add a class to the anchor if its `href` matches the current route.
|
|
20
7
|
* It can have an optional `className` parameter to specify the class to add, otherwise it will
|
package/src/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { isActiveLink
|
|
1
|
+
export { isActiveLink } from './attachments.svelte.js';
|
|
2
2
|
export { blockNavigation, createRouter } from './create-router.svelte.js';
|
|
3
3
|
export { serializeSearch } from './helpers/utils.js';
|
|
4
4
|
export { Navigation } from './navigation.js';
|
|
@@ -66,7 +66,8 @@ export function syncSearchParams(search) {
|
|
|
66
66
|
for (const [key, value] of newSearch) {
|
|
67
67
|
searchParams.set(key, value);
|
|
68
68
|
}
|
|
69
|
-
|
|
69
|
+
// eslint-disable-next-line unicorn/no-useless-spread
|
|
70
|
+
for (const key of [...searchParams.keys()]) {
|
|
70
71
|
if (!newSearch.has(key)) {
|
|
71
72
|
searchParams.delete(key);
|
|
72
73
|
}
|