Skip to content

Commit ce07784

Browse files
authored
Automatic Code Splitting By Route (#168)
* Implement code splitting by route using the projects route configuration * package-lock.json * Update dependencies * Fix up unit tests * Add README for code splitting
1 parent 6db7a6b commit ce07784

File tree

12 files changed

+239
-2654
lines changed

12 files changed

+239
-2654
lines changed

README.md

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
[![codecov](https://codecov.io/gh/dojo/cli-build-app/branch/master/graph/badge.svg)](https://codecov.io/gh/dojo/cli-build-app)
66
[![npm version](https://badge.fury.io/js/%40dojo%2Fcli-build-app.svg)](https://badge.fury.io/js/%40dojo%2Fcli-build-app)
77

8-
The official CLI command for building Dojo applications.
8+
The official CLI command for building optimized Dojo applications.
99

1010
- [Usage](#usage)
1111
- [Features](#features)
1212
- [Building](#building)
13+
- [Code Splitting By Route](#code-splitting-by-route)
1314
- [Serving](#serving-the-application)
1415
- [Watching](#watching)
1516
- [Eject](#eject)
@@ -59,6 +60,59 @@ The `unit` mode creates bundles that can be used to run the unit tests of the ap
5960

6061
The `functional` mode creates bundles that can be used to run the functional tests of the application.
6162

63+
### Code Splitting By Route
64+
65+
The build command will automatically code split your application based on it's dojo routing configuration.
66+
67+
To enable the code automatic splitting by route:
68+
69+
1) The dojo routing configuration needs to be the default export from a `routes.ts` module in the `src` directory.
70+
2) Widgets must by the default export of their module.
71+
3) When defining the `Outlet`, the `renderer` function must be defined inline.
72+
73+
```ts
74+
// routes.ts
75+
export default [
76+
{
77+
path: 'foo',
78+
outlet: 'foo',
79+
children: [
80+
{
81+
path: 'bar',
82+
outlet: 'bar'
83+
}
84+
]
85+
},
86+
{
87+
path: 'bar',
88+
outlet: 'bar'
89+
}
90+
];
91+
```
92+
93+
```ts
94+
// widget
95+
import WidgetBase from '@dojo/framework/widget-core/WidgetBase';
96+
import { v, w } from '@dojo/framework/widget-core/d';
97+
import Outlet from '@dojo/framework/routing/Outlet';
98+
99+
import FooWidget from './FooWidget';
100+
import BarWidget from './BarWidget';
101+
102+
export default class App extends WidgetBase {
103+
protected render() {
104+
return v('div', [
105+
w(Outlet, { id: 'foo', renderer: () => w(FooWidget, {})}),
106+
w(Outlet, { id: 'bar', renderer: () => w(BarWidget, {})})
107+
]);
108+
}
109+
}
110+
```
111+
112+
The output will result in a separate bundle for each of the application's top level routes. In this example, there will be a main application bundle and two further bundles for `src/FooWidget` and `src/BarWidget`.
113+
114+
**Note:** The configuration can be further refined using the bundle configuration in the `.dojorc`, see [bundles configuration](#bundles:-object).
115+
62116
### Serving the Application
63117

64118
A web server can be started with the `--serve` flag while running in `dev` or `dist` modes. By default, the application is served on port 9999, but this can be changed with the `--port` (`-p`) flag:

package-lock.json

Lines changed: 88 additions & 46 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
"sinon": "~4.5.0"
101101
},
102102
"dependencies": {
103-
"@dojo/webpack-contrib": "4.0.0-alpha.1",
103+
"@dojo/webpack-contrib": "next",
104104
"brotli-webpack-plugin": "0.5.0",
105105
"chalk": "2.4.1",
106106
"clean-webpack-plugin": "0.1.17",
@@ -135,6 +135,7 @@
135135
"strip-ansi": "3.0.1",
136136
"style-loader": "0.19.0",
137137
"ts-loader": "3.1.1",
138+
"ts-node": "7.0.1",
138139
"tslint": "5.8.0",
139140
"tslint-loader": "3.5.3",
140141
"typed-css-modules": "0.3.1",

0 commit comments

Comments
 (0)