@shuvi/router 0.0.1-pre.3 → 0.0.1-pre.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.
@@ -4,7 +4,7 @@ const BASE_PATH_PARSER_OPTIONS = {
4
4
  sensitive: false,
5
5
  strict: false,
6
6
  start: true,
7
- end: true,
7
+ end: true
8
8
  };
9
9
  // Special Regex characters that must be escaped in static tokens
10
10
  const REGEX_CHARS_RE = /[.+*?^${}()[\]/\\]/g;
@@ -46,7 +46,7 @@ export function tokensToParser(segments, extraOptions) {
46
46
  keys.push({
47
47
  name: value,
48
48
  repeatable,
49
- optional,
49
+ optional
50
50
  });
51
51
  const re = regexp ? regexp : BASE_PARAM_PATTERN;
52
52
  // the user provided a custom regexp /:id(\\d+)
@@ -75,7 +75,7 @@ export function tokensToParser(segments, extraOptions) {
75
75
  subPattern += '?';
76
76
  pattern += subPattern;
77
77
  if (!options.end)
78
- pattern += '(?=\/|$)';
78
+ pattern += '(?=/|$)';
79
79
  subSegmentScore += 20 /* Dynamic */;
80
80
  if (optional)
81
81
  subSegmentScore += -8 /* BonusOptional */;
@@ -116,7 +116,7 @@ export function tokensToParser(segments, extraOptions) {
116
116
  }
117
117
  return {
118
118
  match: match[0],
119
- params,
119
+ params
120
120
  };
121
121
  }
122
122
  function stringify(params) {
@@ -139,7 +139,9 @@ export function tokensToParser(segments, extraOptions) {
139
139
  if (param === undefined && !optional) {
140
140
  throw new Error(`Missing required param "${value}"`);
141
141
  }
142
- const text = Array.isArray(param) ? param.join('/') : (param || '');
142
+ const text = Array.isArray(param)
143
+ ? param.join('/')
144
+ : param || '';
143
145
  if (!text && optional) {
144
146
  // if we have more than one optional param like /:a?-static we
145
147
  // don't need to care about the optional param
@@ -163,7 +165,7 @@ export function tokensToParser(segments, extraOptions) {
163
165
  score,
164
166
  keys,
165
167
  parse,
166
- stringify,
168
+ stringify
167
169
  };
168
170
  }
169
171
  /**
@@ -0,0 +1 @@
1
+ export {};
@@ -1 +1,2 @@
1
1
  export * from './history';
2
+ export * from './router';
@@ -0,0 +1 @@
1
+ export {};
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createRoutesFromArray = void 0;
3
4
  function createRoutesFromArray(array) {
4
5
  return array.map(partialRoute => {
5
6
  let route = Object.assign(Object.assign({}, partialRoute), { caseSensitive: !!partialRoute.caseSensitive, path: partialRoute.path || '/' });
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getRedirectFromRoutes = void 0;
3
4
  function getRedirectFromRoutes(appRoutes) {
4
5
  return appRoutes.reduceRight((redirectPath, { route: { redirect } }) => {
5
6
  if (!redirectPath && redirect) {
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ACTION_REPLACE = exports.ACTION_PUSH = exports.ACTION_POP = void 0;
3
4
  const utils_1 = require("../utils");
4
5
  /**
5
6
  * A POP indicates a change to an arbitrary index in the history stack, such
@@ -23,10 +24,10 @@ exports.ACTION_REPLACE = 'REPLACE';
23
24
  class BaseHistory {
24
25
  constructor() {
25
26
  this.action = exports.ACTION_POP;
26
- this.location = utils_1.createLocation('/');
27
+ this.location = (0, utils_1.createLocation)('/');
27
28
  this.doTransition = () => void 0;
28
29
  this._index = 0;
29
- this._blockers = utils_1.createEvents();
30
+ this._blockers = (0, utils_1.createEvents)();
30
31
  }
31
32
  back() {
32
33
  this.go(-1);
@@ -35,15 +36,15 @@ class BaseHistory {
35
36
  this.go(1);
36
37
  }
37
38
  resolve(to, from) {
38
- const toPath = utils_1.resolvePath(to, from);
39
+ const toPath = (0, utils_1.resolvePath)(to, from);
39
40
  return {
40
41
  path: toPath,
41
- href: utils_1.pathToString(toPath)
42
+ href: (0, utils_1.pathToString)(toPath)
42
43
  };
43
44
  }
44
45
  transitionTo(to, { onTransition, onAbort, action = exports.ACTION_PUSH, state = null, redirectedFrom }) {
45
46
  const { path } = this.resolve(to, this.location.pathname);
46
- const nextLocation = utils_1.createLocation(path, { state, redirectedFrom });
47
+ const nextLocation = (0, utils_1.createLocation)(path, { state, redirectedFrom });
47
48
  // check transition
48
49
  if (this._blockers.length) {
49
50
  this._blockers.call({
@@ -1,9 +1,21 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
2
14
  var __importStar = (this && this.__importStar) || function (mod) {
3
15
  if (mod && mod.__esModule) return mod;
4
16
  var result = {};
5
- if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
6
- result["default"] = mod;
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
7
19
  return result;
8
20
  };
9
21
  Object.defineProperty(exports, "__esModule", { value: true });
@@ -24,7 +36,7 @@ class BrowserHistory extends base_1.default {
24
36
  state,
25
37
  redirectedFrom,
26
38
  onTransition({ state, url }) {
27
- utils_1.pushState(state, url);
39
+ (0, utils_1.pushState)(state, url);
28
40
  }
29
41
  });
30
42
  }
@@ -34,7 +46,7 @@ class BrowserHistory extends base_1.default {
34
46
  action: base_1.ACTION_REPLACE,
35
47
  redirectedFrom,
36
48
  onTransition({ state, url }) {
37
- utils_1.replaceState(state, url);
49
+ (0, utils_1.replaceState)(state, url);
38
50
  }
39
51
  });
40
52
  }
@@ -42,7 +54,7 @@ class BrowserHistory extends base_1.default {
42
54
  this._history.go(delta);
43
55
  }
44
56
  block(blocker) {
45
- return utils_1.addBlocker(this._blockers, blocker);
57
+ return (0, utils_1.addBlocker)(this._blockers, blocker);
46
58
  }
47
59
  setup() {
48
60
  let blockedPopTx = null;
@@ -74,7 +86,7 @@ class BrowserHistory extends base_1.default {
74
86
  else {
75
87
  // Trying to POP to a location with no index. We did not create
76
88
  // this location, so we can't effectively block the navigation.
77
- utils_1.warning(false,
89
+ (0, utils_1.warning)(false,
78
90
  // TODO: Write up a doc that explains our blocking strategy in
79
91
  // detail and link to it here so people can understand better what
80
92
  // is going on and how to avoid it.
@@ -100,7 +112,7 @@ class BrowserHistory extends base_1.default {
100
112
  const state = this._history.state || {};
101
113
  return [
102
114
  state.idx,
103
- utils_1.createLocation({
115
+ (0, utils_1.createLocation)({
104
116
  pathname,
105
117
  search,
106
118
  hash
@@ -1,9 +1,21 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
2
14
  var __importStar = (this && this.__importStar) || function (mod) {
3
15
  if (mod && mod.__esModule) return mod;
4
16
  var result = {};
5
- if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
6
- result["default"] = mod;
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
7
19
  return result;
8
20
  };
9
21
  Object.defineProperty(exports, "__esModule", { value: true });
@@ -22,7 +34,7 @@ function getBaseHref() {
22
34
  function createHref(to) {
23
35
  return (getBaseHref() +
24
36
  '#' +
25
- (typeof to === 'string' ? to : utils_1.pathToString(utils_1.resolvePath(to))));
37
+ (typeof to === 'string' ? to : (0, utils_1.pathToString)((0, utils_1.resolvePath)(to))));
26
38
  }
27
39
  class HashHistory extends base_1.default {
28
40
  constructor() {
@@ -39,7 +51,7 @@ class HashHistory extends base_1.default {
39
51
  state,
40
52
  redirectedFrom,
41
53
  onTransition({ state, url }) {
42
- utils_1.pushState(state, url);
54
+ (0, utils_1.pushState)(state, url);
43
55
  }
44
56
  });
45
57
  }
@@ -49,7 +61,7 @@ class HashHistory extends base_1.default {
49
61
  action: base_1.ACTION_REPLACE,
50
62
  redirectedFrom,
51
63
  onTransition({ state, url }) {
52
- utils_1.replaceState(state, url);
64
+ (0, utils_1.replaceState)(state, url);
53
65
  }
54
66
  });
55
67
  }
@@ -57,10 +69,10 @@ class HashHistory extends base_1.default {
57
69
  this._history.go(delta);
58
70
  }
59
71
  block(blocker) {
60
- return utils_1.addBlocker(this._blockers, blocker);
72
+ return (0, utils_1.addBlocker)(this._blockers, blocker);
61
73
  }
62
74
  resolve(to, from) {
63
- const toPath = utils_1.resolvePath(to, from);
75
+ const toPath = (0, utils_1.resolvePath)(to, from);
64
76
  return {
65
77
  path: toPath,
66
78
  href: createHref(toPath)
@@ -96,7 +108,7 @@ class HashHistory extends base_1.default {
96
108
  else {
97
109
  // Trying to POP to a location with no index. We did not create
98
110
  // this location, so we can't effectively block the navigation.
99
- utils_1.warning(false,
111
+ (0, utils_1.warning)(false,
100
112
  // TODO: Write up a doc that explains our blocking strategy in
101
113
  // detail and link to it here so people can understand better what
102
114
  // is going on and how to avoid it.
@@ -121,17 +133,17 @@ class HashHistory extends base_1.default {
121
133
  window.addEventListener('hashchange', () => {
122
134
  const [, nextLocation] = this.getIndexAndLocation();
123
135
  // Ignore extraneous hashchange events.
124
- if (utils_1.pathToString(nextLocation) !== utils_1.pathToString(this.location)) {
136
+ if ((0, utils_1.pathToString)(nextLocation) !== (0, utils_1.pathToString)(this.location)) {
125
137
  handlePop();
126
138
  }
127
139
  });
128
140
  }
129
141
  getIndexAndLocation() {
130
- const { pathname, search, hash } = utils_1.resolvePath(window.location.hash.substr(1));
142
+ const { pathname, search, hash } = (0, utils_1.resolvePath)(window.location.hash.substr(1));
131
143
  const state = this._history.state || {};
132
144
  return [
133
145
  state.idx,
134
- utils_1.createLocation({
146
+ (0, utils_1.createLocation)({
135
147
  pathname,
136
148
  search,
137
149
  hash
@@ -3,6 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createMemoryHistory = exports.createHashHistory = exports.createBrowserHistory = exports.MemoryHistory = void 0;
6
7
  const memory_1 = __importDefault(require("./memory"));
7
8
  exports.MemoryHistory = memory_1.default;
8
9
  const browser_1 = __importDefault(require("./browser"));
@@ -1,9 +1,21 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
2
14
  var __importStar = (this && this.__importStar) || function (mod) {
3
15
  if (mod && mod.__esModule) return mod;
4
16
  var result = {};
5
- if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
6
- result["default"] = mod;
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
7
19
  return result;
8
20
  };
9
21
  Object.defineProperty(exports, "__esModule", { value: true });
@@ -17,8 +29,8 @@ class MemoryHistory extends base_1.default {
17
29
  super();
18
30
  this._entries = [];
19
31
  this._entries = initialEntries.map(entry => {
20
- let location = utils_1.createLocation(Object.assign({ pathname: '/', search: '', hash: '' }, (typeof entry === 'string' ? utils_1.resolvePath(entry) : entry)));
21
- utils_1.warning(location.pathname.charAt(0) === '/', `Relative pathnames are not supported in createMemoryHistory({ initialEntries }) (invalid entry: ${JSON.stringify(entry)})`);
32
+ let location = (0, utils_1.createLocation)(Object.assign({ pathname: '/', search: '', hash: '' }, (typeof entry === 'string' ? (0, utils_1.resolvePath)(entry) : entry)));
33
+ (0, utils_1.warning)(location.pathname.charAt(0) === '/', `Relative pathnames are not supported in createMemoryHistory({ initialEntries }) (invalid entry: ${JSON.stringify(entry)})`);
22
34
  return location;
23
35
  });
24
36
  this._index = clamp(initialIndex == null ? this._entries.length - 1 : initialIndex, 0, this._entries.length - 1);
@@ -71,10 +83,10 @@ class MemoryHistory extends base_1.default {
71
83
  return this._blockers.push(blocker);
72
84
  }
73
85
  resolve(to, from) {
74
- const toPath = utils_1.resolvePath(to, from);
86
+ const toPath = (0, utils_1.resolvePath)(to, from);
75
87
  return {
76
88
  path: toPath,
77
- href: utils_1.pathToString(toPath)
89
+ href: (0, utils_1.pathToString)(toPath)
78
90
  };
79
91
  }
80
92
  getIndexAndLocation() {
package/lib/index.js CHANGED
@@ -1,22 +1,30 @@
1
1
  "use strict";
2
- function __export(m) {
3
- for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
4
- }
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
5
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.createRedirector = exports.createLocation = exports.resolvePath = exports.parseQuery = exports.pathToString = exports.rankRouteBranches = exports.matchRoutes = exports.matchStringify = exports.matchPathname = exports.createRoutesFromArray = void 0;
6
14
  var createRoutesFromArray_1 = require("./createRoutesFromArray");
7
- exports.createRoutesFromArray = createRoutesFromArray_1.createRoutesFromArray;
15
+ Object.defineProperty(exports, "createRoutesFromArray", { enumerable: true, get: function () { return createRoutesFromArray_1.createRoutesFromArray; } });
8
16
  var matchPathname_1 = require("./matchPathname");
9
- exports.matchPathname = matchPathname_1.matchPathname;
10
- exports.matchStringify = matchPathname_1.matchStringify;
17
+ Object.defineProperty(exports, "matchPathname", { enumerable: true, get: function () { return matchPathname_1.matchPathname; } });
18
+ Object.defineProperty(exports, "matchStringify", { enumerable: true, get: function () { return matchPathname_1.matchStringify; } });
11
19
  var matchRoutes_1 = require("./matchRoutes");
12
- exports.matchRoutes = matchRoutes_1.matchRoutes;
13
- exports.rankRouteBranches = matchRoutes_1.rankRouteBranches;
20
+ Object.defineProperty(exports, "matchRoutes", { enumerable: true, get: function () { return matchRoutes_1.matchRoutes; } });
21
+ Object.defineProperty(exports, "rankRouteBranches", { enumerable: true, get: function () { return matchRoutes_1.rankRouteBranches; } });
14
22
  var utils_1 = require("./utils");
15
- exports.pathToString = utils_1.pathToString;
16
- exports.parseQuery = utils_1.parseQuery;
17
- exports.resolvePath = utils_1.resolvePath;
18
- exports.createLocation = utils_1.createLocation;
19
- exports.createRedirector = utils_1.createRedirector;
20
- __export(require("./types"));
21
- __export(require("./history"));
22
- __export(require("./router"));
23
+ Object.defineProperty(exports, "pathToString", { enumerable: true, get: function () { return utils_1.pathToString; } });
24
+ Object.defineProperty(exports, "parseQuery", { enumerable: true, get: function () { return utils_1.parseQuery; } });
25
+ Object.defineProperty(exports, "resolvePath", { enumerable: true, get: function () { return utils_1.resolvePath; } });
26
+ Object.defineProperty(exports, "createLocation", { enumerable: true, get: function () { return utils_1.createLocation; } });
27
+ Object.defineProperty(exports, "createRedirector", { enumerable: true, get: function () { return utils_1.createRedirector; } });
28
+ __exportStar(require("./types"), exports);
29
+ __exportStar(require("./history"), exports);
30
+ __exportStar(require("./router"), exports);
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.matchStringify = exports.matchPathname = void 0;
3
4
  const pathParserRanker_1 = require("./pathParserRanker");
4
5
  const pathTokenizer_1 = require("./pathTokenizer");
5
6
  function safelyDecodeURIComponent(value, paramName, optional) {
@@ -30,7 +31,7 @@ function matchPathname(pattern, pathname) {
30
31
  pattern = { path: pattern };
31
32
  }
32
33
  const { path, caseSensitive = false, end = true } = pattern;
33
- const pathParser = pathParserRanker_1.tokensToParser(pathTokenizer_1.tokenizePath(path), { end, sensitive: caseSensitive });
34
+ const pathParser = (0, pathParserRanker_1.tokensToParser)((0, pathTokenizer_1.tokenizePath)(path), { end, sensitive: caseSensitive });
34
35
  ;
35
36
  const matchResult = pathParser.parse(pathname);
36
37
  if (!matchResult)
@@ -52,7 +53,7 @@ exports.matchPathname = matchPathname;
52
53
  * @param options
53
54
  */
54
55
  function matchStringify(path, params, options) {
55
- const pathParser = pathParserRanker_1.tokensToParser(pathTokenizer_1.tokenizePath(path), options);
56
+ const pathParser = (0, pathParserRanker_1.tokensToParser)((0, pathTokenizer_1.tokenizePath)(path), options);
56
57
  return pathParser.stringify(params);
57
58
  }
58
59
  exports.matchStringify = matchStringify;
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.matchRoutes = exports.rankRouteBranches = void 0;
3
4
  const matchPathname_1 = require("./matchPathname");
4
5
  const utils_1 = require("./utils");
5
6
  const pathParserRanker_1 = require("./pathParserRanker");
@@ -14,14 +15,14 @@ function matchRouteBranch(branch, pathname) {
14
15
  let remainingPathname = matchedPathname === '/'
15
16
  ? pathname
16
17
  : pathname.slice(matchedPathname.length) || '/';
17
- let routeMatch = matchPathname_1.matchPathname({
18
+ let routeMatch = (0, matchPathname_1.matchPathname)({
18
19
  path: route.path,
19
20
  caseSensitive: route.caseSensitive,
20
21
  end: i === routes.length - 1
21
22
  }, remainingPathname);
22
23
  if (!routeMatch)
23
24
  return null;
24
- matchedPathname = utils_1.joinPaths([matchedPathname, routeMatch.pathname]);
25
+ matchedPathname = (0, utils_1.joinPaths)([matchedPathname, routeMatch.pathname]);
25
26
  matchedParams = Object.assign(Object.assign({}, matchedParams), routeMatch.params);
26
27
  matches.push({
27
28
  route,
@@ -37,10 +38,10 @@ function rankRouteBranches(branches) {
37
38
  }
38
39
  const normalizedPaths = branches.map((branch, index) => {
39
40
  const [path] = branch;
40
- return Object.assign(Object.assign({}, pathParserRanker_1.tokensToParser(pathTokenizer_1.tokenizePath(path))), { path,
41
+ return Object.assign(Object.assign({}, (0, pathParserRanker_1.tokensToParser)((0, pathTokenizer_1.tokenizePath)(path))), { path,
41
42
  index });
42
43
  });
43
- normalizedPaths.sort((a, b) => pathParserRanker_1.comparePathParserScore(a, b));
44
+ normalizedPaths.sort((a, b) => (0, pathParserRanker_1.comparePathParserScore)(a, b));
44
45
  const newBranches = [];
45
46
  // console.log(
46
47
  // normalizedPaths
@@ -56,7 +57,7 @@ function rankRouteBranches(branches) {
56
57
  exports.rankRouteBranches = rankRouteBranches;
57
58
  function flattenRoutes(routes, branches = [], parentPath = '', parentRoutes = [], parentIndexes = []) {
58
59
  routes.forEach((route, index) => {
59
- let path = utils_1.joinPaths([parentPath, route.path]);
60
+ let path = (0, utils_1.joinPaths)([parentPath, route.path]);
60
61
  let routes = parentRoutes.concat(route);
61
62
  let indexes = parentIndexes.concat(index);
62
63
  // Add the children before adding this route to the array so we traverse the
@@ -71,7 +72,7 @@ function flattenRoutes(routes, branches = [], parentPath = '', parentRoutes = []
71
72
  }
72
73
  function matchRoutes(routes, location, basename = '') {
73
74
  if (typeof location === 'string') {
74
- location = utils_1.resolvePath(location);
75
+ location = (0, utils_1.resolvePath)(location);
75
76
  }
76
77
  let pathname = location.pathname || '/';
77
78
  if (basename) {
@@ -1,12 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.comparePathParserScore = exports.tokensToParser = void 0;
3
4
  // default pattern for a param: non greedy everything but /
4
5
  const BASE_PARAM_PATTERN = '[^/]+?';
5
6
  const BASE_PATH_PARSER_OPTIONS = {
6
7
  sensitive: false,
7
8
  strict: false,
8
9
  start: true,
9
- end: true,
10
+ end: true
10
11
  };
11
12
  // Special Regex characters that must be escaped in static tokens
12
13
  const REGEX_CHARS_RE = /[.+*?^${}()[\]/\\]/g;
@@ -48,7 +49,7 @@ function tokensToParser(segments, extraOptions) {
48
49
  keys.push({
49
50
  name: value,
50
51
  repeatable,
51
- optional,
52
+ optional
52
53
  });
53
54
  const re = regexp ? regexp : BASE_PARAM_PATTERN;
54
55
  // the user provided a custom regexp /:id(\\d+)
@@ -77,7 +78,7 @@ function tokensToParser(segments, extraOptions) {
77
78
  subPattern += '?';
78
79
  pattern += subPattern;
79
80
  if (!options.end)
80
- pattern += '(?=\/|$)';
81
+ pattern += '(?=/|$)';
81
82
  subSegmentScore += 20 /* Dynamic */;
82
83
  if (optional)
83
84
  subSegmentScore += -8 /* BonusOptional */;
@@ -118,7 +119,7 @@ function tokensToParser(segments, extraOptions) {
118
119
  }
119
120
  return {
120
121
  match: match[0],
121
- params,
122
+ params
122
123
  };
123
124
  }
124
125
  function stringify(params) {
@@ -141,7 +142,9 @@ function tokensToParser(segments, extraOptions) {
141
142
  if (param === undefined && !optional) {
142
143
  throw new Error(`Missing required param "${value}"`);
143
144
  }
144
- const text = Array.isArray(param) ? param.join('/') : (param || '');
145
+ const text = Array.isArray(param)
146
+ ? param.join('/')
147
+ : param || '';
145
148
  if (!text && optional) {
146
149
  // if we have more than one optional param like /:a?-static we
147
150
  // don't need to care about the optional param
@@ -165,7 +168,7 @@ function tokensToParser(segments, extraOptions) {
165
168
  score,
166
169
  keys,
167
170
  parse,
168
- stringify,
171
+ stringify
169
172
  };
170
173
  }
171
174
  exports.tokensToParser = tokensToParser;
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.tokenizePath = void 0;
3
4
  const ROOT_TOKEN = {
4
5
  type: 0 /* Static */,
5
6
  value: ''
package/lib/router.js CHANGED
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createRouter = void 0;
3
4
  const defer_1 = require("@shuvi/utils/lib/defer");
4
5
  const matchRoutes_1 = require("./matchRoutes");
5
6
  const createRoutesFromArray_1 = require("./createRoutesFromArray");
@@ -23,13 +24,13 @@ class Router {
23
24
  constructor({ basename = '', history, routes }) {
24
25
  this._pending = null;
25
26
  this._ready = false;
26
- this._readyDefer = defer_1.Defer();
27
- this._listeners = utils_1.createEvents();
28
- this._beforeEachs = utils_1.createEvents();
29
- this._afterEachs = utils_1.createEvents();
30
- this._basename = utils_1.normalizeBase(basename);
27
+ this._readyDefer = (0, defer_1.Defer)();
28
+ this._listeners = (0, utils_1.createEvents)();
29
+ this._beforeEachs = (0, utils_1.createEvents)();
30
+ this._afterEachs = (0, utils_1.createEvents)();
31
+ this._basename = (0, utils_1.normalizeBase)(basename);
31
32
  this._history = history;
32
- this._routes = createRoutesFromArray_1.createRoutesFromArray(routes);
33
+ this._routes = (0, createRoutesFromArray_1.createRoutesFromArray)(routes);
33
34
  this._current = START;
34
35
  this._history.doTransition = this._doTransition.bind(this);
35
36
  const setup = () => this._history.setup();
@@ -75,7 +76,7 @@ class Router {
75
76
  return this._afterEachs.push(listener);
76
77
  }
77
78
  resolve(to, from) {
78
- return this._history.resolve(to, from ? utils_1.joinPaths([this._basename, from]) : this._basename);
79
+ return this._history.resolve(to, from ? (0, utils_1.joinPaths)([this._basename, from]) : this._basename);
79
80
  }
80
81
  /*
81
82
  The Full Navigation Resolution Flow for shuvi/router
@@ -90,14 +91,14 @@ class Router {
90
91
  const nextRoute = this._getNextRoute(to);
91
92
  const current = this._current;
92
93
  const nextMatches = nextRoute.matches || [];
93
- const routeRedirect = getRedirectFromRoutes_1.getRedirectFromRoutes(nextMatches);
94
+ const routeRedirect = (0, getRedirectFromRoutes_1.getRedirectFromRoutes)(nextMatches);
94
95
  if (routeRedirect) {
95
96
  return this._history.replace(routeRedirect, {
96
97
  redirectedFrom: routeRedirect
97
98
  });
98
99
  }
99
100
  const routeContext = new Map();
100
- const queue = [].concat(this._beforeEachs.toArray(), extract_hooks_1.extractHooks(nextMatches, 'resolve', routeContext));
101
+ const queue = [].concat(this._beforeEachs.toArray(), (0, extract_hooks_1.extractHooks)(nextMatches, 'resolve', routeContext));
101
102
  const abort = () => {
102
103
  onAbort && onAbort();
103
104
  // fire ready cbs once
@@ -116,7 +117,7 @@ class Router {
116
117
  if (to === false) {
117
118
  abort();
118
119
  }
119
- else if (error_1.isError(to)) {
120
+ else if ((0, error_1.isError)(to)) {
120
121
  abort();
121
122
  }
122
123
  else if (typeof to === 'string' ||
@@ -144,7 +145,7 @@ class Router {
144
145
  console.error('Uncaught error during navigation:', err);
145
146
  }
146
147
  };
147
- async_1.runQueue(queue, iterator, () => {
148
+ (0, async_1.runQueue)(queue, iterator, () => {
148
149
  if (this._pending !== to) {
149
150
  return abort();
150
151
  }
@@ -167,7 +168,7 @@ class Router {
167
168
  _getCurrent(routeContext) {
168
169
  var _a;
169
170
  const { _routes: routes, _basename: basename, _history: { location } } = this;
170
- const matches = matchRoutes_1.matchRoutes(routes, location, basename);
171
+ const matches = (0, matchRoutes_1.matchRoutes)(routes, location, basename);
171
172
  let params;
172
173
  if (matches) {
173
174
  params = matches[matches.length - 1].params;
@@ -197,13 +198,14 @@ class Router {
197
198
  }
198
199
  _getNextRoute(to) {
199
200
  const { _routes: routes, _basename: basename } = this;
200
- const matches = matchRoutes_1.matchRoutes(routes, to, basename);
201
+ const matches = (0, matchRoutes_1.matchRoutes)(routes, to, basename);
201
202
  const params = matches ? matches[matches.length - 1].params : {};
202
- const parsedPath = utils_1.resolvePath(to);
203
+ const parsedPath = (0, utils_1.resolvePath)(to);
203
204
  return Object.assign(Object.assign({ matches,
204
205
  params }, parsedPath), { key: '', state: null });
205
206
  }
206
207
  }
207
- exports.createRouter = (options) => {
208
+ const createRouter = (options) => {
208
209
  return new Router(options);
209
210
  };
211
+ exports.createRouter = createRouter;
@@ -1,6 +1,14 @@
1
1
  "use strict";
2
- function __export(m) {
3
- for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
4
- }
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
5
12
  Object.defineProperty(exports, "__esModule", { value: true });
6
- __export(require("./history"));
13
+ __exportStar(require("./history"), exports);
14
+ __exportStar(require("./router"), exports);
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.runQueue = void 0;
3
4
  function runQueue(queue, fn, cb) {
4
5
  const step = (index) => {
5
6
  if (index >= queue.length) {
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createRedirector = void 0;
3
4
  function createRedirector() {
4
5
  const redirector = {
5
6
  redirected: false,
package/lib/utils/dom.js CHANGED
@@ -1,3 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.inBrowser = void 0;
3
4
  exports.inBrowser = typeof window !== 'undefined';
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isError = void 0;
3
4
  function isError(err) {
4
5
  return Object.prototype.toString.call(err).indexOf('Error') > -1;
5
6
  }
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.extractHooks = void 0;
3
4
  function extractHooks(matched, method, // can add more method later on,
4
5
  routeContext) {
5
6
  const guards = [];
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.addBlocker = exports.replaceState = exports.pushState = exports.createLocation = void 0;
3
4
  const misc_1 = require("./misc");
4
5
  const path_1 = require("./path");
5
6
  const BeforeUnloadEventType = 'beforeunload';
@@ -13,7 +14,7 @@ function createKey() {
13
14
  return Math.random().toString(36).substr(2, 8);
14
15
  }
15
16
  function createLocation(to, { state = null, key, redirectedFrom } = {}) {
16
- return misc_1.readOnly(Object.assign(Object.assign({}, path_1.resolvePath(to)), { redirectedFrom,
17
+ return (0, misc_1.readOnly)(Object.assign(Object.assign({}, (0, path_1.resolvePath)(to)), { redirectedFrom,
17
18
  state, key: key || createKey() }));
18
19
  }
19
20
  exports.createLocation = createLocation;
@@ -1,9 +1,16 @@
1
1
  "use strict";
2
- function __export(m) {
3
- for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
4
- }
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
5
12
  Object.defineProperty(exports, "__esModule", { value: true });
6
- __export(require("./history"));
7
- __export(require("./misc"));
8
- __export(require("./path"));
9
- __export(require("./createRedirector"));
13
+ __exportStar(require("./history"), exports);
14
+ __exportStar(require("./misc"), exports);
15
+ __exportStar(require("./path"), exports);
16
+ __exportStar(require("./createRedirector"), exports);
package/lib/utils/misc.js CHANGED
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.warning = exports.readOnly = exports.createEvents = exports.isDev = void 0;
3
4
  exports.isDev = process.env.NODE_ENV === 'development';
4
5
  function createEvents() {
5
6
  let handlers = [];
package/lib/utils/path.js CHANGED
@@ -3,12 +3,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.resolvePath = exports.pathToString = exports.parseQuery = exports.normalizeBase = exports.splitPath = exports.joinPaths = exports.normalizeSlashes = exports.trimTrailingSlashes = void 0;
6
7
  const query_string_1 = __importDefault(require("query-string"));
7
8
  const dom_1 = require("./dom");
8
- exports.trimTrailingSlashes = (path) => path.replace(/\/+$/, '');
9
- exports.normalizeSlashes = (path) => path.replace(/\/\/+/g, '/');
10
- exports.joinPaths = (paths) => exports.normalizeSlashes(paths.join('/'));
11
- exports.splitPath = (path) => exports.normalizeSlashes(path).split('/');
9
+ const trimTrailingSlashes = (path) => path.replace(/\/+$/, '');
10
+ exports.trimTrailingSlashes = trimTrailingSlashes;
11
+ const normalizeSlashes = (path) => path.replace(/\/\/+/g, '/');
12
+ exports.normalizeSlashes = normalizeSlashes;
13
+ const joinPaths = (paths) => (0, exports.normalizeSlashes)(paths.join('/'));
14
+ exports.joinPaths = joinPaths;
15
+ const splitPath = (path) => (0, exports.normalizeSlashes)(path).split('/');
16
+ exports.splitPath = splitPath;
12
17
  function normalizeBase(base) {
13
18
  if (!base) {
14
19
  if (dom_1.inBrowser) {
@@ -43,8 +48,8 @@ function pathToString({ pathname = '/', search = '', hash = '', query = {} }) {
43
48
  }
44
49
  exports.pathToString = pathToString;
45
50
  function resolvePathname(toPathname, fromPathname) {
46
- let segments = exports.splitPath(exports.trimTrailingSlashes(fromPathname));
47
- let relativeSegments = exports.splitPath(toPathname);
51
+ let segments = (0, exports.splitPath)((0, exports.trimTrailingSlashes)(fromPathname));
52
+ let relativeSegments = (0, exports.splitPath)(toPathname);
48
53
  relativeSegments.forEach(segment => {
49
54
  if (segment === '..') {
50
55
  // Keep the root "" segment so the pathname starts at /
@@ -55,7 +60,7 @@ function resolvePathname(toPathname, fromPathname) {
55
60
  segments.push(segment);
56
61
  }
57
62
  });
58
- return segments.length > 1 ? exports.joinPaths(segments) : '/';
63
+ return segments.length > 1 ? (0, exports.joinPaths)(segments) : '/';
59
64
  }
60
65
  /**
61
66
  * Parses a string URL path into its separate pathname, search, and hash components.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shuvi/router",
3
- "version": "0.0.1-pre.3",
3
+ "version": "0.0.1-pre.4",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/shuvijs/shuvi.git",
@@ -28,8 +28,8 @@
28
28
  "node": ">= 12.0.0"
29
29
  },
30
30
  "dependencies": {
31
- "@shuvi/utils": "0.0.1-pre.3",
31
+ "@shuvi/utils": "0.0.1-pre.4",
32
32
  "query-string": "6.13.8"
33
33
  },
34
- "gitHead": "71af6fe3620aab0a9dd900e6527b4f04a31a8757"
34
+ "gitHead": "ca02a6733c7dc63e7d69a174bdd116f8f54b98ee"
35
35
  }