What is webpack?
Webpack is a powerful module bundler for JavaScript applications. It processes applications by internally building a dependency graph which maps every module your project needs and generates one or more bundles. It is highly extensible via loaders and plugins, and it's designed to manage, transform, and bundle frontend assets like JavaScript, CSS, and images.
What are webpack's main functionalities?
Module Bundling
Webpack bundles all the JavaScript files and other assets like CSS and images into a single output file. The code sample shows a basic webpack configuration defining an entry point and the output bundle.
module.exports = {
entry: './path/to/my/entry/file.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'my-first-webpack.bundle.js'
}
};
Loaders
Loaders allow webpack to process different types of files and convert them into modules that can be included in your bundle. The code sample demonstrates how to use loaders to handle .txt and .css files.
module.exports = {
module: {
rules: [
{ test: /\.txt$/, use: 'raw-loader' },
{ test: /\.css$/, use: ['style-loader', 'css-loader'] }
]
}
};
Plugins
Plugins can be leveraged to perform a wider range of tasks like bundle optimization, asset management, and environment variable injection. The code sample shows how to use the HtmlWebpackPlugin to generate an index.html file with the bundled assets injected.
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
plugins: [new HtmlWebpackPlugin({ template: './src/index.html' })]
};
Development Server
Webpack provides a development server that can be used to serve your application during development. It supports live reloading. The code sample configures the webpack development server to serve files from the 'dist' directory.
module.exports = {
devServer: {
contentBase: './dist',
open: true
}
};
Code Splitting
Code splitting allows you to split your code into various bundles which can then be loaded on demand or in parallel. The code sample shows how to split the application and vendor code into separate bundles.
module.exports = {
entry: {
app: './src/app.js',
vendor: './src/vendor.js'
},
output: {
filename: '[name].bundle.js',
path: __dirname + '/dist'
}
};
Other packages similar to webpack
rollup
Rollup is a module bundler for JavaScript which uses a flat bundle approach that's more efficient for libraries and applications with a complex module structure. It's known for its tree-shaking capabilities, which eliminate unused code.
parcel
Parcel is a web application bundler that offers a zero-configuration setup. It's known for its fast bundle times and out-of-the-box support for many file types without the need for additional plugins or loaders.
browserify
Browserify lets you require('modules') in the browser by bundling up all of your dependencies. It's been around longer than webpack and has a simpler approach, but it lacks some of the more advanced features and optimizations that webpack offers.
fuse-box
FuseBox is a bundler/module loader that combines the power of webpack, JSPM, and SystemJS. It introduces a streamlined workflow and has a powerful API. It's known for its speed and simplicity.
vm-browserify
Emulate node's vm module for the browser.
example
Just write some client-side javascript:
var vm = require('vm');
$(function () {
var res = vm.runInNewContext('a + 5', { a : 100 });
$('#res').text(res);
});
compile it with browserify:
browserify entry.js -o bundle.js
then whip up some html:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="/bundle.js"></script>
</head>
<body>
result = <span id="res"></span>
</body>
</html>
and when you load the page you should see:
result = 105
methods
vm.runInNewContext(code, context={})
Evaluate some code
in a new iframe with a context
.
Contexts are like wrapping your code in a with()
except slightly less terrible
because the code is sandboxed into a new iframe.
browser compatability
All modern browsers are supported.
If you have a browserling account,
from the testling/ directory just do:
$ ./test.sh substack@gmail.com
Enter host password for user 'substack@gmail.com':
chrome/17.0:
vmRunInNewContext ................................. 5/5
iexplore/9.0:
vmRunInNewContext ................................. 5/5
firefox/10.0:
vmRunInNewContext ................................. 5/5
safari/5.1:
vmRunInNewContext ................................. 5/5
opera/11.6:
vmRunInNewContext ................................. 5/5
total ............................................... 25/25
In IE8 and IE7, running vm.runInNewContext()
more than once can cause the
browsers to hang for some reason. Otherwise they work too.
install
This module is depended upon by browserify, so you should just be able to
require('vm')
and it will just work. However if you want to use this module
directly you can install it with npm:
npm install vm-browserify
license
MIT/X11