Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,47 @@ If you have nested or sibling Routes that you want to be loaded together, you ca
</Route>
```

This will cause the `user` chunk to be loaded if any of the three user pages is loaded. It will also mean that you won't need two separate calls for the base class and child class.
This will cause the `user` chunk to be loaded if any of the three user pages is loaded.
It will also mean that you won't need two separate calls for the base class and child class.

#### Named chunks with placeholders

You can also use the [standard Webpack placeholders](https://github.com/webpack/loader-utils#interpolatename) in the name of your chunks.

```js
<Route path="details" component={require('react-router-proxy?name=[name]!./UserDetails.jsx')}>
<Route path="settings" component={require('react-router-proxy?name=[name]!./UserSettings.jsx')}>
<Route path="other" component={require('react-router-proxy?name=[name]!./UserOther.jsx')}>
```

Would generate three chunks, exported in `userdetails.js`, `usersettings.js` and so on.
Using this approach allows you to setup your loader globally through an exclude/include rule in your `webpack.config.js`.
To avoid conflicts it may be best to prefix your `name` with a subfolder name, such as `routes/`:

```js
loaders: [
{
test: /\.js$/,
exclude: /src\/Pages/,
loader: 'babel',
},
{
test: /\.js$/,
include: /src\/Pages/,
loaders: ['react-router-proxy?name=routes/[name]', 'babel'],
}
],
```

This has the advantage of making your router a lot leaner:

```js
<Route path="details" component={require('./UserDetails.jsx')}>
<Route path="settings" component={require('./UserSettings.jsx')}>
<Route path="other" component={require('./UserOther.jsx')}>
```

The generated files would then go into `routes/userdetails`, `routes/usersettings` etc.

## Changelog

Expand Down
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ module.exports = function() {};
module.exports.pitch = function(remainingRequest) {
this.cacheable && this.cacheable();
var query = loaderUtils.parseQuery(this.query);
var chunkName = loaderUtils.interpolateName(this, query.name, {
context: this.options.context,
content: remainingRequest,
}).toLowerCase();

var moduleRequest = "!!" + remainingRequest;
return [
Expand All @@ -30,7 +34,7 @@ module.exports.pitch = function(remainingRequest) {
' else {',
' callback();',
' }',
' }' + (query.name ? ', ' + JSON.stringify(query.name) : '') + ');',
' }' + (query.name ? ', ' + JSON.stringify(chunkName) : '') + ');',
' },',
' willTransitionFrom: function(transition, component, callback) {',
' var componentClass = component && component.state ? component.state.component : null;',
Expand All @@ -53,7 +57,7 @@ module.exports.pitch = function(remainingRequest) {
' var module = require(' + JSON.stringify(moduleRequest) + ');',
' component = module.__esModule ? module["default"] : module;',
' if(callback) callback(component);',
' }' + (query.name ? ', ' + JSON.stringify(query.name) : '') + ');',
' }' + (query.name ? ', ' + JSON.stringify(chunkName) : '') + ');',
' } else if(callback) callback(component);',
' return component;',
' }',
Expand Down