Skip to content

Commit 9a81531

Browse files
author
Didier Franc
committed
Initial commit
0 parents  commit 9a81531

File tree

8 files changed

+1049
-0
lines changed

8 files changed

+1049
-0
lines changed

.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["es2015", "react", "stage-0"],
3+
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Didier Franc
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# react-code-splitting
2+
3+
You're working on a great app powered by React, bundled with webpack and your bundle size increases ... You're in the right place to solve this modern JS apps nightmare.
4+
5+
6+
## Prerequisite
7+
8+
- You're using [Webpack 2](https://webpack.js.org/)
9+
- You've polyfilled ***Promise*** to support old browser
10+
11+
## How-to
12+
13+
#### Without code splitting
14+
15+
`<Login />` + `<Home />` are loaded at the first start
16+
```jsx
17+
import Login from './Login'
18+
import Home from './Home'
19+
20+
const App = ({ user }) => (
21+
<Body>
22+
{user.loggedIn ? <Route path="/" component={Home} /> : <Redirect to="/login" />}
23+
<Route path="/login" component={Login} />
24+
</Body>
25+
)
26+
```
27+
28+
#### With code splitting
29+
30+
You're not logged in ? `<Login />` component is the only loaded, `<Home />` will be loaded when the user will be logged in.
31+
```jsx
32+
import Async from 'react-code-splitting'
33+
34+
import Login from './Login'
35+
const Home = () => <Async load={import('./Home')} />
36+
37+
const App = ({ user }) => (
38+
<Body>
39+
{user.loggedIn ? <Route path="/" component={Home} /> : <Redirect to="/login" />}
40+
<Route path="/login" component={Login} />
41+
</Body>
42+
)
43+
```
44+
45+
You can view this snippets in context [here](https://github.com/didierfranc/redux-react-starter/blob/master/src/components/App.js#L11) !
46+
47+
## More
48+
49+
Webpack examples with some code splitting techniques https://github.com/webpack/webpack/tree/master/examples

dist/react-code-splitting.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
var _react = require('react');
8+
9+
var _react2 = _interopRequireDefault(_react);
10+
11+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12+
13+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
14+
15+
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
16+
17+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
18+
19+
var Async = function (_React$Component) {
20+
_inherits(Async, _React$Component);
21+
22+
function Async() {
23+
var _ref,
24+
_this2 = this;
25+
26+
var _temp, _this, _ret;
27+
28+
_classCallCheck(this, Async);
29+
30+
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
31+
args[_key] = arguments[_key];
32+
}
33+
34+
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Async.__proto__ || Object.getPrototypeOf(Async)).call.apply(_ref, [this].concat(args))), _this), _this.componentWillMount = function () {
35+
_this.props.load.then(function (c) {
36+
_this.C = c;_this.forceUpdate();
37+
});
38+
}, _this.render = function () {
39+
return _this.C ? _react2.default.createElement(_this2.C.default, null) : null;
40+
}, _temp), _possibleConstructorReturn(_this, _ret);
41+
}
42+
43+
return Async;
44+
}(_react2.default.Component);
45+
46+
exports.default = Async;
47+
48+
49+
Async.propTypes = {
50+
load: _react2.default.PropTypes.instanceOf(Promise).isRequired
51+
};

package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "react-code-splitting",
3+
"version": "1.0.3",
4+
"description": "Code splitting won't be a nightmare anymore",
5+
"main": "./dist/react-code-splitting.js",
6+
"repository": "https://github.com/didierfranc/react-code-splitting",
7+
"author": "Didier Franc",
8+
"license": "MIT",
9+
"scripts": {
10+
"build": "babel ./src/index.js --out-file ./dist/react-code-splitting.js"
11+
},
12+
"devDependencies": {
13+
"babel-core": "^6.22.1",
14+
"babel-preset-es2015": "^6.22.0",
15+
"babel-preset-react": "^6.22.0",
16+
"babel-preset-stage-0": "^6.22.0"
17+
}
18+
}

src/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react'
2+
3+
export default class Async extends React.Component {
4+
componentWillMount = () => {
5+
this.props.load.then((c) => { this.C = c; this.forceUpdate() })
6+
}
7+
8+
render = () => this.C ? <this.C.default /> : null
9+
}
10+
11+
Async.propTypes = {
12+
load: React.PropTypes.instanceOf(Promise).isRequired,
13+
}

0 commit comments

Comments
 (0)