Skip to content

Commit bd11cbd

Browse files
committed
Replace path-to-regexp with wayfarer
Trie-based, more sensible to read, smaller footprint, more concise implementation
1 parent 7779b55 commit bd11cbd

File tree

4 files changed

+47
-48
lines changed

4 files changed

+47
-48
lines changed

package-lock.json

+15-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"babel-plugin-transform-react-display-name": "6.25.0",
3939
"babel-plugin-transform-react-jsx": "6.24.1",
4040
"babel-plugin-transform-runtime": "6.23.0",
41+
"babel-plugin-unassert": "2.1.2",
4142
"babel-preset-env": "1.6.0",
4243
"babel-preset-stage-2": "6.24.1",
4344
"babel-register": "6.26.0",
@@ -73,14 +74,14 @@
7374
"flatpickr": "3.0.7",
7475
"lodash": "4.17.4",
7576
"memize": "1.0.4",
76-
"path-to-regexp": "2.0.0",
7777
"preact": "8.2.5",
7878
"preact-redux": "2.0.3",
7979
"querystringify": "1.0.0",
8080
"redux": "3.7.2",
8181
"redux-multi": "0.1.12",
8282
"refx": "2.1.0",
8383
"rememo": "2.3.3",
84-
"textarea-caret": "3.0.2"
84+
"textarea-caret": "3.0.2",
85+
"wayfarer": "6.6.2"
8586
}
8687
}

src/routes/index.js

+15-38
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/**
22
* External dependencies
33
*/
4-
import pathToRegexp from 'path-to-regexp';
5-
import { map } from 'lodash';
4+
import wayfarer from 'wayfarer';
65
import { parse } from 'querystringify';
76
import memoize from 'memize';
87

@@ -15,45 +14,23 @@ import TagsRoute from './tags';
1514
import TagRoute from './tag';
1615
import NotFoundRoute from './not-found';
1716

18-
export const routes = map( [
19-
[ '/', HomeRoute ],
20-
[ '/date/:date/', DateRoute ],
21-
[ '/tags/:tag/page/:page', TagRoute ],
22-
[ '/tags/:tag', TagRoute ],
23-
[ '/tags/', TagsRoute ],
24-
[ '*', NotFoundRoute ],
25-
], ( [ path, Route ] ) => {
26-
const keys = [];
17+
const withParams = ( Route ) => ( params ) => [ params, Route ];
2718

28-
return {
29-
path,
30-
keys,
31-
Route,
32-
regexp: pathToRegexp( path, keys ),
33-
};
34-
} );
19+
const router = wayfarer();
20+
router.on( '/', withParams( HomeRoute ) );
21+
router.on( '/date/:date', withParams( DateRoute ) );
22+
router.on( '/tags/:tag/page/:page', withParams( TagRoute ) );
23+
router.on( '/tags/:tag', withParams( TagRoute ) );
24+
router.on( '/tags', withParams( TagsRoute ) );
25+
router.on( '*', withParams( NotFoundRoute ) );
3526

3627
export const getRouteByPath = memoize( ( path ) => {
3728
const [ pathname, search = '' ] = path.split( '?' );
29+
const [ params, Route ] = router( pathname.replace( /\/$/, '' ) );
3830

39-
for ( let r = 0, rl = routes.length; r < rl; r++ ) {
40-
const { regexp, keys, Route } = routes[ r ];
41-
const match = pathname.match( regexp );
42-
if ( ! match ) {
43-
continue;
44-
}
45-
46-
const params = {};
47-
for ( let m = 1, ml = match.length; m < ml; m++ ) {
48-
params[ keys[ m - 1 ].name ] = decodeURIComponent( match[ m ] );
49-
}
50-
51-
return {
52-
params,
53-
Route,
54-
query: parse( search ),
55-
};
56-
}
57-
58-
return {};
31+
return {
32+
params,
33+
Route,
34+
query: parse( search ),
35+
};
5936
} );

webpack.config.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ const config = module.exports = {
1919
'fast-stable-stringify',
2020
'flatpickr',
2121
'memize',
22-
'path-to-regexp',
2322
'preact',
2423
'preact-redux',
2524
'refx',
2625
'textarea-caret',
26+
'wayfarer',
2727
],
2828
},
2929
output: {
@@ -72,6 +72,19 @@ if ( 'production' === NODE_ENV ) {
7272
include: __dirname + '/src/state',
7373
} );
7474

75+
// The Wayfarer module uses `assert` to validate incoming arguments. This
76+
// is a non-trivial dependency, and not desirable for production.
77+
config.module.rules.unshift( {
78+
test: /\.js$/,
79+
use: {
80+
loader: 'babel-loader',
81+
options: {
82+
plugins: [ 'unassert' ],
83+
},
84+
},
85+
include: __dirname + '/node_modules/wayfarer',
86+
} );
87+
7588
config.module.rules.push( {
7689
test: /\.s?css$/,
7790
use: ExtractTextPlugin.extract( {

0 commit comments

Comments
 (0)