..is an application that automates time consuming, repetitive and boring tasks.
Allows to define steps, what they must do and how they are combined or chained.
Most of them (all?) uses an imperative notation defining what tasks must do and how they must do.
// Concatenate JS Files
gulp.task('scripts', function() {
return gulp.src('src/js/*.js')
.pipe(concat('main.js'))
.pipe(gulp.dest('build/js'));
});
// Build CSS from SASS
gulp.task('sass', function() {
return sass('src/scss/style.scss', {style: 'compressed'})
.pipe(rename({suffix: '.min'}))
.pipe(gulp.dest('build/css'));
});
// Complex task
gulp.task('default', ['scripts', 'sass']);
Webpack is a module bundler and not a task runner.
Analyzes dependencies among your modules (not only JS but also CSS, HTML, etc) and generates assets.
Understands multiple standards for how to define dependencies and export values: AMD, CommonJS, ES6 modules, ...
Webpack can be configured through:
module.exports = {
// configuration
};
Not an exhaustive list of options !!!
"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: "budle.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"
};
Use require("path") to reference files and folders.
require("!style!css!../css/style.css");
var content = require("./content.js");
document.write(content);
app.js
module.exports = "It works !!!";
content.js
body {
background: yellow;
}
style.css
bundle.js
> webpack app.js bundle.js
Simply include the bundle.js in your HTML page
<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 :(
require("!style!css!./style.css");
h1 {
color: green;
}
var cffile = require("coffee!./file.coffee");
var content = require("./content.js");
// Execute exported method con coffescript
cffile.someMethod();
document.write(content);
var $ = require("jquery");
$("p").css("color", "red");
npm install jquery --save
"use strict";
var path = require("path");
var Webpack = require("webpack");
var ExtractTextPlugin = require("extract-text-webpack-plugin");
// Our configuration
module.exports = {
// Define the entry point
entry: {
app1: path.resolve(__dirname, "js", "app1.js"),
app2: path.resolve(__dirname, "js", "app2.js")
},
// Output configuration
output: {
path: path.resolve(__dirname, "assets"),
filename: "[name].js",
chunkFilename: "[id].js"
},
module: {
loaders: [
// Inform CSS modules must be bundled in another file.
{
test: /\.css$/,
loader: ExtractTextPlugin.extract("style-loader", "css-loader")
}
]
},
plugins: [
// Extract all CSS content to a file
new ExtractTextPlugin("[name].css")
]
};
require("../css/style.css");
console.log("Running app1...");
var content = require("./content.js");
var log = require("./log.js");
log("At app1...");
document.write(content.app1);
app1.js
module.exports = {
app1: "It works for app1 !!!",
app2: "It works for app2 !!!"
};
content.js
body {
background: green;
color: white;
}
style.css
module.exports = {
app1: "It works for app1 !!!",
app2: "It works for app2 !!!"
};
content.js
body {
background: green;
color: white;
}
style.css
body {
background: green;
color: white;
}
app2.css
require("../css/style.css");
require("../css/app2.css");
console.log("Running app2...");
var content = require("./content.js");
var log = require("./log.js");
log("At app2...");
document.write(content.app2);
app2.js
app1.js + app1.css
app2.js + app2.css