nuxt-typed-router 0.2.22 → 0.2.26

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/lib/module.js CHANGED
@@ -7,50 +7,57 @@ const lodash_1 = require("lodash");
7
7
  const chalk_1 = __importDefault(require("chalk"));
8
8
  const save_1 = require("./save");
9
9
  const utils_1 = require("./utils");
10
- const typedRouterModule = function (moduleOptions) {
11
- const { filePath = `${this.options.srcDir}/__routes.js`, routesObjectName = 'routerPagesNames', stripAtFromName = false, } = { ...this.options.typedRouter, ...moduleOptions };
12
- this.nuxt.hook('build:extendRoutes', async (existingRoutes) => {
13
- try {
14
- this.extendRoutes(async (routes) => {
15
- (0, utils_1.transformRouteNames)(routes, stripAtFromName);
16
- let routesObjectString = '{';
17
- let routeObjectJs = {};
18
- const recursiveTypedRoutes = (route, level, routeObject, siblings, parentName, hadMatching) => {
19
- const routeName = route.name;
20
- const matchingSiblings = (0, utils_1.extractMatchingSiblings)(route, siblings);
21
- const haveMatchingSiblings = !!(matchingSiblings === null || matchingSiblings === void 0 ? void 0 : matchingSiblings.length);
22
- if ((route.children && !haveMatchingSiblings) ||
23
- (!route.children && haveMatchingSiblings)) {
24
- let childrenChunks = haveMatchingSiblings ? matchingSiblings : route.children;
25
- const splittedPaths = route.path.split('/');
26
- const parentPath = splittedPaths[splittedPaths.length - 1];
27
- const nameKey = (0, lodash_1.camelCase)(parentPath || 'index');
28
- routesObjectString += `${nameKey}:{`;
29
- routeObject[nameKey] = {};
30
- childrenChunks === null || childrenChunks === void 0 ? void 0 : childrenChunks.map((r) => recursiveTypedRoutes(r, level + 1, routeObject[nameKey], (0, utils_1.extractUnMatchingSiblings)(route, siblings), nameKey, haveMatchingSiblings));
31
- routesObjectString += '},';
32
- }
33
- else if (routeName) {
34
- let splitted = routeName.split('-');
10
+ function routeHook(filePath, routesObjectName, stripAtFromName) {
11
+ try {
12
+ this.extendRoutes(async (routes) => {
13
+ (0, utils_1.transformRouteNames)(routes, stripAtFromName);
14
+ let routesObjectString = '{';
15
+ let routeObjectJs = {};
16
+ const recursiveTypedRoutes = (route, level, routeObject, siblings, parentName, hadMatching) => {
17
+ var _a, _b;
18
+ const matchingSiblings = (0, utils_1.extractMatchingSiblings)(route, siblings);
19
+ const haveMatchingSiblings = !!(matchingSiblings === null || matchingSiblings === void 0 ? void 0 : matchingSiblings.length) && route.path !== '/';
20
+ const chunkArray = (_b = (_a = route.chunkName) === null || _a === void 0 ? void 0 : _a.split('/')) !== null && _b !== void 0 ? _b : [];
21
+ const isRootSibling = chunkArray[(chunkArray === null || chunkArray === void 0 ? void 0 : chunkArray.length) - 1] === 'index';
22
+ if ((route.children && !haveMatchingSiblings) ||
23
+ (!route.children && haveMatchingSiblings && isRootSibling)) {
24
+ let childrenChunks = haveMatchingSiblings ? matchingSiblings : route.children;
25
+ const splittedPaths = route.path.split('/');
26
+ const parentPath = splittedPaths[splittedPaths.length - 1];
27
+ const nameKey = (0, lodash_1.camelCase)(parentPath || 'index');
28
+ routesObjectString += `${nameKey}:{`;
29
+ routeObject[nameKey] = {};
30
+ childrenChunks === null || childrenChunks === void 0 ? void 0 : childrenChunks.map((r) => recursiveTypedRoutes(r, level + 1, routeObject[nameKey], (0, utils_1.extractUnMatchingSiblings)(route, siblings), nameKey, haveMatchingSiblings));
31
+ routesObjectString += '},';
32
+ }
33
+ else {
34
+ let splitted = [];
35
+ if (route.name) {
36
+ splitted = route.name.split('-');
35
37
  splitted = splitted.slice(level, splitted.length);
36
38
  if (splitted[0] === parentName) {
37
39
  splitted.splice(0, 1);
38
40
  }
39
- const keyName = route.path === '' ? 'index' : (0, lodash_1.camelCase)(splitted.join('-')) || 'index';
40
- routesObjectString += `'${keyName}': '${routeName}',`;
41
- routeObject[keyName] = routeName;
42
41
  }
43
- };
44
- routes.map((r) => recursiveTypedRoutes(r, 0, routeObjectJs, routes === null || routes === void 0 ? void 0 : routes.filter((f) => f.path !== r.path)));
45
- routesObjectString += '}';
46
- const templateRoutes = `export const ${routesObjectName} = ${routesObjectString};`;
47
- await (0, save_1.saveRoutesFiles)(filePath, templateRoutes);
48
- });
49
- }
50
- catch (e) {
51
- console.error(chalk_1.default.red('Error while generating routes definitions model'), '\n' + e);
52
- }
53
- });
42
+ const keyName = route.path === '' ? 'index' : (0, lodash_1.camelCase)(splitted.join('-')) || 'index';
43
+ routesObjectString += `'${keyName}': '${route.name}',`;
44
+ routeObject[keyName] = route.name;
45
+ }
46
+ };
47
+ routes.map((r) => recursiveTypedRoutes(r, 0, routeObjectJs, routes === null || routes === void 0 ? void 0 : routes.filter((f) => f.path !== r.path)));
48
+ routesObjectString += '}';
49
+ const templateRoutes = `export const ${routesObjectName} = ${routesObjectString} as const;`;
50
+ await (0, save_1.saveRoutesFiles)(filePath, templateRoutes);
51
+ });
52
+ }
53
+ catch (e) {
54
+ console.error(chalk_1.default.red('Error while generating routes definitions model'), '\n' + e);
55
+ }
56
+ }
57
+ const typedRouterModule = function (moduleOptions) {
58
+ const { filePath = `${this.options.srcDir}/__routes.js`, routesObjectName = 'routerPagesNames', stripAtFromName = false, } = { ...this.options.typedRouter, ...moduleOptions };
59
+ this.nuxt.hook('build:before', () => routeHook.call(this, filePath, routesObjectName, stripAtFromName));
60
+ this.nuxt.hook('build:extendRoutes', () => routeHook.call(this, filePath, routesObjectName, stripAtFromName));
54
61
  };
55
62
  module.exports = typedRouterModule;
56
63
  module.exports.meta = require('../package.json');
package/lib/utils.js CHANGED
@@ -56,7 +56,7 @@ exports.extractChunkRouteName = extractChunkRouteName;
56
56
  function extractMatchingSiblings(mainRoute, siblingRoutes) {
57
57
  return siblingRoutes === null || siblingRoutes === void 0 ? void 0 : siblingRoutes.filter((s) => {
58
58
  const chunkName = extractChunkMain(mainRoute.chunkName);
59
- if (chunkName) {
59
+ if (chunkName && s.name) {
60
60
  const siblingChunkName = extractChunkMain(s.chunkName);
61
61
  if (!siblingChunkName)
62
62
  return false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-typed-router",
3
- "version": "0.2.22",
3
+ "version": "0.2.26",
4
4
  "description": "Provide autocompletion for pages route names generated by Nuxt router",
5
5
  "main": "lib/module.js",
6
6
  "keywords": [
@@ -15,7 +15,9 @@
15
15
  "dev": "tsc -p ./tsconfig.json --pretty --watch",
16
16
  "build": "rimraf lib && rimraf types && tsc -p ./tsconfig.json --pretty",
17
17
  "copy-files": "copyfiles -u 1 ./src/**/*.js ./lib",
18
- "prepublish": "yarn build"
18
+ "prepublish": "yarn build",
19
+ "test": "jest",
20
+ "test:reset": "yarn test --updateSnapshot"
19
21
  },
20
22
  "publishConfig": {
21
23
  "access": "public"
@@ -41,17 +43,27 @@
41
43
  "typings": "types/index.d.ts",
42
44
  "dependencies": {
43
45
  "chalk": "^4.1.2",
46
+ "jest": "^27.2.4",
44
47
  "lodash": "^4.17.21",
45
48
  "log-symbols": "^5.0.0",
46
49
  "prettier": "^2.4.1"
47
50
  },
48
51
  "devDependencies": {
52
+ "@babel/plugin-transform-runtime": "^7.15.8",
53
+ "@babel/preset-env": "^7.15.8",
54
+ "@nuxt/test-utils": "^0.2.2",
49
55
  "@nuxt/types": "^2.15.8",
56
+ "@types/jest": "^27.0.2",
50
57
  "@types/lodash": "^4.14.173",
51
58
  "@types/node": "^15.12.4",
52
59
  "@types/prettier": "^2.3.2",
60
+ "babel-jest": "^27.2.4",
53
61
  "copyfiles": "^2.4.0",
62
+ "core-js": "^3.18.2",
63
+ "nuxt": "^2.15.8",
64
+ "playwright": "^1.15.2",
54
65
  "rimraf": "^3.0.2",
66
+ "ts-jest": "^27.0.5",
55
67
  "typescript": "^4.4.3",
56
68
  "vue-router": "^3.5.2"
57
69
  }