Skip to content

Commit b614fab

Browse files
committed
Support ES6 modules in setting url-pattern compiler
1 parent 22b51fd commit b614fab

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

docs/override-url-pattern.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The author of `url-pattern`, which does the underlying conversion in RRC from ro
88
For example:
99

1010
var Router = require('react-router-component');
11+
var URLPattern = require('url-pattern');
1112

1213
// Create a compiler that only matches alphabetical characters in urls.
1314
//
@@ -24,3 +25,14 @@ Overriding the compiler allows you to write very powerful route definitions.
2425
Note that this override is global. At this time, there is no way to define a compiler per Router. If you have
2526
more advanced needs, we recommend using `url-pattern` directly to generate regular expressions to use as route paths,
2627
or using regular expressions directly.
28+
29+
A note on ES6; ES6 modules can't modify `module.exports` directly. Instead, use `setCreateURLPatternCompilerFactory`:
30+
31+
import * as Router from 'react-router-component';
32+
import * as URLPattern from 'url-pattern';
33+
34+
Router.setCreateURLPatternCompilerFactory((routeProps) => {
35+
var compiler = new URLPattern.Compiler();
36+
compiler.segmentValueCharset = 'a-zA-Z';
37+
return compiler;
38+
})

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,10 @@ module.exports = {
3838
// The fn used to create a compiler for "/user/:id"-style routes is exposed here so it can be overridden.
3939
createURLPatternCompiler: function() {
4040
return new URLPattern.Compiler();
41+
},
42+
43+
// For ES6 imports, which can't modify module.exports directly
44+
setCreateURLPatternCompilerFactory: function(fn) {
45+
this.createURLPatternCompiler = fn;
4146
}
4247
};

tests/matchRoutes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ describe('matchRoutes', function() {
8888
handler: React.createElement('div', {name: 'parseDomain'})});
8989

9090
// Lifted from url-pattern docs
91-
Router.createURLPatternCompiler = function(routeProps) {
91+
Router.setCreateURLPatternCompilerFactory(function(routeProps) {
9292
// Somebody might use this, make sure it works.
9393
assert.strictEqual(routeProps.path, route.props.path);
9494

@@ -102,7 +102,7 @@ describe('matchRoutes', function() {
102102
compiler.optionalSegmentEndChar = ']';
103103
compiler.wildcardChar = '?';
104104
return compiler;
105-
};
105+
});
106106

107107
var match = matchRoutes([route], 'https://www.github.com/strml/react-router-component');
108108
assert(match.route);

0 commit comments

Comments
 (0)