A module bundler
@TehranJS
<script src="js/jquery.js"></script>
<script src="js/react.js"></script>
<script src="js/react-router.js"></script>
<script src="js/app.js"></script>
grunt.initConfig({
concat: {
files: {
home: [
'js/jquery.js',
'js/jquery-ui.js',
'js/react.js',
'js/react-router.js',
'js/home.js',
]
}
}
});grunt.initConfig({
concat: {
files: {
home: [
'js/jquery.js',
'js/jquery-ui.js',
'js/react.js',
'js/react-router.js',
'js/app.js',
],
dashboard: [
'js/jquery.js',
'js/some-other-jquery-plugin.js',
'js/react.js',
'js/react-router.js',
'js/dashboard.js',
]
}
}
});Re-downloading the vendor code for every page (no code-sharing)
Can be solved (somewhat) by separating the common from the page specific
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',
]
}
}
});Modules
+
bundling
The State of Front-End Tooling – 2015
module.exports = {
entry: './about',
output: {
filename: 'build.js'
}
};entry: The entry point of your bundle (the initial file where your application starts). Can be a string, an array or an object (for multiple entries).
Entry points: The first JS code executed when a page is loaded. Dependencies are analyzed from this first module.
Loaders: Transformations on a resource file. Each module is loaded through a loader
Plugins: Injects themselves into the build process to make all sort of crazy things.
"use strict";
var path = require("path");
var Webpack = require("webpack");
// Our configuration
module.exports = {
// Define the entry point
entry: path.resolve(__dirname, "js", "app.js"),
// Output configuration
output: {
path: path.resolve(__dirname, "assets"),
filename: "bundle.js"
},
module: {
loaders: [
// Inform about CSS loaders so that it can be bundled too
{ test: /\.css$/, loader: "style-loader!css-loader" }
]
},
// Generate source map files
devtool: "source-map"
};body {
background: #eeeeee;
}style.css
content.js
module.exports = "TehranJS";require("!style!css!../css/style.css");
var content = require("./content.js");
document.write(content);app.js
> webpack app.js bundle.jsbundle.js
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script type="text/javascript" src="bundle.js"></script>
</body>
</html>Note, the CSS is included in the bundle but it is not applied until the bundle is loaded :(
Easy integration with bower.
@TehranJS