sv-router 0.2.0 → 0.3.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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sv-router",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Modern Svelte Routing",
5
5
  "keywords": [
6
6
  "svelte",
package/src/cli/index.js CHANGED
@@ -18,10 +18,13 @@ function arg(option) {
18
18
  return args[pathArgIndex];
19
19
  }
20
20
 
21
- const pathArg = arg('path');
22
- if (pathArg) genConfig.routesPath = pathArg;
21
+ const allLazyArg = arg('allLazy');
22
+ if (allLazyArg) genConfig.allLazy = true;
23
23
 
24
24
  const jsArg = arg('js');
25
25
  if (jsArg) genConfig.routesInJs = true;
26
26
 
27
+ const pathArg = arg('path');
28
+ if (pathArg) genConfig.routesPath = pathArg;
29
+
27
30
  writeRouterCode();
@@ -46,7 +46,7 @@ export function createRouter(r) {
46
46
  return params.value;
47
47
  },
48
48
  get pathname() {
49
- return location.pathname;
49
+ return /** @type {import('./index.d.ts').Path<T>} */ (location.pathname);
50
50
  },
51
51
  get search() {
52
52
  return location.search;
package/src/gen/config.js CHANGED
@@ -1,7 +1,8 @@
1
1
  /**
2
2
  * @type {{
3
- * routesPath: string;
3
+ * allLazy: boolean;
4
4
  * routesInJs: boolean;
5
+ * routesPath: string;
5
6
  * readonly genCodeDirPath: string;
6
7
  * readonly routerPath: string;
7
8
  * readonly tsconfigPath: string;
@@ -9,8 +10,9 @@
9
10
  * }}
10
11
  */
11
12
  export const genConfig = {
12
- routesPath: 'src/routes',
13
+ allLazy: false,
13
14
  routesInJs: false,
15
+ routesPath: 'src/routes',
14
16
  genCodeDirPath: '.router',
15
17
  get routerPath() {
16
18
  return '.router/router.' + (this.routesInJs ? 'js' : 'ts');
@@ -17,16 +17,17 @@ const HOOKS_FILENAME_REGEX = /(?<=[/.]|^)(hooks)(\.svelte)?\.(js|ts)$/; // hooks
17
17
 
18
18
  /**
19
19
  * @param {string} routesPath
20
+ * @param {{ allLazy?: boolean }} [options]
20
21
  * @returns {string}
21
22
  */
22
- export function generateRouterCode(routesPath) {
23
+ export function generateRouterCode(routesPath, options) {
23
24
  const absoluteRoutesPath = path.join(process.cwd(), routesPath);
24
25
  if (!fs.existsSync(absoluteRoutesPath)) {
25
26
  throw new Error(`Routes directory not found at \`${routesPath}\``);
26
27
  }
27
28
  const fileTree = buildFileTree(absoluteRoutesPath);
28
29
  const routeMap = createRouteMap(fileTree);
29
- return createRouterCode(routeMap, path.posix.join('..', routesPath));
30
+ return createRouterCode(routeMap, path.posix.join('..', routesPath), options);
30
31
  }
31
32
 
32
33
  /**
@@ -119,9 +120,10 @@ function filePathToRoute(filename) {
119
120
  /**
120
121
  * @param {GeneratedRoutes} routes
121
122
  * @param {string} routesPath
123
+ * @param {{ allLazy?: boolean }} [options]
122
124
  * @returns {string}
123
125
  */
124
- export function createRouterCode(routes, routesPath) {
126
+ export function createRouterCode(routes, routesPath, { allLazy = false } = {}) {
125
127
  if (!routesPath.endsWith('/')) {
126
128
  routesPath += '/';
127
129
  }
@@ -135,7 +137,7 @@ export function createRouterCode(routes, routesPath) {
135
137
  for (const [key, value] of Object.entries(routes)) {
136
138
  if (typeof value === 'object') {
137
139
  result[key] = handleImports(value, routesPath);
138
- } else if (key === 'hooks' || !value.endsWith('.lazy.svelte')) {
140
+ } else if (key === 'hooks' || (!value.endsWith('.lazy.svelte') && !allLazy)) {
139
141
  const variableName = pathToCorrectCasing(value);
140
142
  importsMap.set(variableName, routesPath + value);
141
143
  result[key] = variableName;
@@ -32,7 +32,7 @@ export function writeRouterCode() {
32
32
  writeFileIfDifferent(genConfig.tsconfigPath, JSON.stringify(tsConfig, undefined, 2));
33
33
 
34
34
  // Write `.router/router.ts` file
35
- const routerCode = generateRouterCode(genConfig.routesPath);
35
+ const routerCode = generateRouterCode(genConfig.routesPath, { allLazy: genConfig.allLazy });
36
36
  const written = writeFileIfDifferent(genConfig.routerPath, routerCode);
37
37
 
38
38
  if (written) {
@@ -61,6 +61,8 @@ export function matchRoute(pathname, routes) {
61
61
  }
62
62
  if (breakFromLayouts) {
63
63
  routePart = `(${routePart})`;
64
+ } else if ('layout' in routes && routes.layout) {
65
+ layouts.push(routes.layout);
64
66
  }
65
67
  const resolvedPath = /** @type {keyof Routes} */ (
66
68
  (index ? '/' : '') + routeParts.join('/')
package/src/index.d.ts CHANGED
@@ -134,7 +134,7 @@ export type RouterApi<T extends Routes> = {
134
134
  */
135
135
  params: AllParams<T>;
136
136
  /** The reactive pathname of the URL. */
137
- pathname: string;
137
+ pathname: Path<T>;
138
138
  /** The reactive query string part of the URL. */
139
139
  search: string;
140
140
  /** The reactive history state that can be passed to the `navigate` function. */
@@ -2,17 +2,23 @@ import type { Plugin } from 'vite';
2
2
 
3
3
  export type RouterOptions = {
4
4
  /**
5
- * The path to the routes folder.
5
+ * If true, all routes will be lazy loaded by default.
6
6
  *
7
- * @default 'src/routes'
7
+ * @default false
8
8
  */
9
- path?: string;
9
+ allLazy?: boolean;
10
10
  /**
11
11
  * If true, generates the routes in a .js file instead of a .ts file.
12
12
  *
13
13
  * @default false
14
14
  */
15
15
  js?: boolean;
16
+ /**
17
+ * The path to the routes folder.
18
+ *
19
+ * @default 'src/routes'
20
+ */
21
+ path?: string;
16
22
  };
17
23
 
18
24
  export const router: (options?: RouterOptions) => Plugin;
@@ -7,8 +7,9 @@ import { writeRouterCode } from '../gen/write-router-code.js';
7
7
  * @returns {import('vite').Plugin}
8
8
  */
9
9
  export function router(options) {
10
- if (options?.path) genConfig.routesPath = options.path;
10
+ if (options?.allLazy) genConfig.allLazy = options.allLazy;
11
11
  if (options?.js) genConfig.routesInJs = options.js;
12
+ if (options?.path) genConfig.routesPath = options.path;
12
13
 
13
14
  return {
14
15
  name: 'sv-router',