Eirik Langholm Vullum PRO
JavaScript fanatic that loves node.js and React.
A module bundler
Webpack
Script includes
<script src="js/jquery.js"></script>
<script src="js/jquery-ui.js"></script>
<script src="js/some-jquery-plugin.js"></script>
<script src="js/react.js"></script>
<script src="js/react-router.js"></script>
<script src="js/home.js"></script>
Webpack
Webpack
Concatenation..
grunt.initConfig({
concat: {
files: {
home: [
'js/jquery.js',
'js/jquery-ui.js',
'js/some-jquery-plugin.js',
'js/react.js',
'js/react-router.js',
'js/home.js',
]
}
}
});
Webpack
grunt.initConfig({
concat: {
files: {
home: [
'js/jquery.js',
'js/jquery-ui.js',
'js/some-jquery-plugin.js',
'js/react.js',
'js/react-router.js',
'js/home.js',
],
dashboard: [
'js/jquery.js',
'js/jquery-ui.js',
'js/some-other-jquery-plugin.js',
'js/react.js',
'js/react-router.js',
'js/dashboard.js',
]
}
}
});
Webpack
Webpack
grunt.initConfig({
concat: {
files: {
common: [
'js/jquery.js',
'js/jquery-ui.js',
'js/some-jquery-plugin.js',
'js/some-other-jquery-plugin.js',
'js/react.js',
'js/react-router.js'
],
home: [
'js/home.js',
],
dashboard: [
'js/dashboard.js',
]
}
}
});
Webpack
manual code sharing
Webpack
manual code sharing
Webpack
var Home = React.createClass({/* ... */});
Webpack
Webpack
Modules
+
bundling
Webpack
Webpack
var React = require('react');
var Home = React.createClass({/* ... */});
module.exports = Home;
Webpack
Webpack
Webpack
> npm install webpack --save-dev
> webpack js/home.js build/bundle.js
CLI
Webpack
# terminal
> webpack
Config file
// webpack.config.js
module.exports = {
entry: {
home: 'js/home',
dashboard: 'js/dashboard'
},
output: {
filename: 'build/[name].js'
}
};
Webpack
(easing the load)
Webpack
module.exports = {
entry: {
home: 'js/home',
hashboard: 'js/dashboard'
},
output: {
filename: 'build/[name].js'
},
plugins: [
new webpack.optimize.CommonsChunkPlugin('build/common.js')
]
};
Code splitting (common)
<!-- home.html -->
<script src="build/common.js"></script>
<script src="build/home.js"></script>
Code splitting (common)
Webpack
<!-- dashboard.html -->
<script src="build/common.js"></script>
<script src="build/dashboard.js"></script>
(easing the load)
Webpack
Webpack
// webpack.config.js
module.exports = {
// ...
module: {
loaders: [
{ test: /\.js$/, loader: 'babel' }
]
}
};
Webpack
// js/home.js (with es6 and jsx)
const React = require('react');
let foo = <div>hello world</div>;
let add = (x, y) => x + y;
Webpack
// bundle/home.js (bundled)
var React = __webpack_require__[0];
var foo = React.createElement(
"div",
null,
"hello world"
);
var add = function add(x, y) {
return x + y;
};
Webpack
module.exports = {
// ...
devtool: 'eval' // 'source-map'
};
// js/home.js
require('style!css!less!../styles/home.less');
var React = require('react');
var Home = React.createClass({/* ... */});
module.exports = Home;
Webpack
http://webpack.github.io/docs/
Webpack
Webpack
By Eirik Langholm Vullum