webpack is a tool to build JavaScript modules in your application. To start using webpack from its cli or api, follow the Installation instructions.webpack simplifies your workflow by quickly constructing a dependency graph of your application and bundling them in the right order. webpack can be configured to customise optimisations to your code, to split vendor/css/js code for production, run a development server that hot-reloads your code without page refresh and many such cool features. Learn more about why you should use webpack.
Creating a bundle
Create a demo directory to try out webpack. Install webpack.
mkdir webpack-demo && cd webpack-demo
npm init -y
npm install --save-dev webpack
./node_modules/.bin/webpack --help # Shows a list of valid cli commands
.\node_modules\.bin\webpack --help # For windows users
webpack --help # If you installed webpack globally
Now create a subdirectory app with an index.js file.
app/index.js
function component () {
var element = document.createElement('div');
/* lodash is required for the next line to work */
element.innerHTML = _.join(['Hello','webpack'], ' ');
return element;
}
document.body.appendChild(component());
To run this piece of code, one usually has the below HTML
index.html
webpack 2 demo
In this example, there are implicit dependencies between the
- +