Skip to content

Commit fbb255d

Browse files
committed
example: add code splitting example
1 parent e632bc3 commit fbb255d

File tree

8 files changed

+56
-8
lines changed

8 files changed

+56
-8
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
*node_modules/
2-
example/bundle.js
3-
example/bundle.js.map
2+
example/build/

example/AsyncComponent.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React = require('react');
2+
3+
var AsyncComponent = React.createClass({
4+
5+
render() {
6+
return React.DOM.div({}, 'I AM ASYNC!');
7+
}
8+
});
9+
10+
export = AsyncComponent;

example/Button.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import React = require('react');
22

3-
export var Button = React.createClass({
3+
var Button = React.createClass({
44

55
render() {
66
return React.DOM.button({onClick: this.onClick}, 'Typed Button!');
77
},
88

99
componentDidMount() {
10-
throw new Error('xx');
10+
console.log('hello');
1111
},
1212

1313
onClick(e) {
1414
alert('Works!');
1515
}
1616
});
17+
18+
export = Button;

example/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ BIN = ./node_modules/.bin
33
build: install typings
44
$(BIN)/webpack
55

6+
watch: install typings
7+
$(BIN)/webpack --watch
8+
69
install:
710
@npm install
811

example/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<!doctype html>
22
<div id="main"></div>
3-
<script src="bundle.js"></script>
3+
<script src="build/bundle.js"></script>

example/index.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,29 @@ require('./index.css');
44

55
import React = require('react/addons');
66
import Button = require('./Button');
7+
import AsyncComponent = require('./AsyncComponent');
78

9+
var App = React.createClass({
10+
11+
render() {
12+
return React.DOM.div({},
13+
React.createElement(Button, {}),
14+
this.state.async ? React.createElement(this.state.async, {}) : null
15+
);
16+
},
17+
18+
getInitialState() {
19+
return {async: null};
20+
},
21+
22+
componentDidMount() {
23+
require.ensure(['./AsyncComponent'], (require) => {
24+
var async: typeof AsyncComponent = require('./AsyncComponent');
25+
this.setState({async: async});
26+
});
27+
}
28+
});
829

930
React.render(
10-
React.createElement(Button.Button, {}),
31+
React.createElement(App, {}),
1132
document.getElementById('main'));

example/webpack.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ module.exports = {
55
devtool: 'source-map',
66

77
output: {
8-
filename: __dirname + '/bundle.js'
8+
path: 'build',
9+
filename: 'bundle.js',
10+
chunkFilename: '[id].js',
11+
publicPath: '/build/'
912
},
1013

1114
resolve: {

webpack-runtime.d.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
/**
22
* Type declarations for Webpack runtime.
33
*/
4-
declare function require(x: string): any
4+
5+
interface WebpackRequireEnsureCallback {
6+
((id: string) => any) => any
7+
}
8+
9+
interface WebpackRequire {
10+
(id: string) => any;
11+
ensure(ids: string[], WebpackRequireEnsureCallback) => any;
12+
}
13+
14+
declare var require: WebpackRequire;

0 commit comments

Comments
 (0)