Webpack essentials#
Background#
Based on this video
Start with a npm project
- Install
- webpack and webpack-cli
- Loaders - transform various types of source files ready for bundling
- Plugins - transform the bundle after it's created
- Configure
- The entry point - into the source code
- Loaders (from webpack)
- Individual loader configuration (e.g. babel)
- Output - where the bundle goes
- Mode - production or dev
- How to run webpack: npm run build OR npm run start
Installation#
- Initialise the project - npm init
- npm install webpack webpack-cli --save-dev
- Loaders
- npm install svg-inline-loader --save-dev
- npm install css-loader style-loader --save-dev
- npm install babel-loader --save-dev
- npm install html-webpack-plugin --save-dev
- Dev server
- npm install webpack-dev-server --save-dev
- babel https://www.robinwieruch.de/webpack-babel-setup-tutorial
- npm i -D @babel/core @babel/present-env
- npm i -D babel-loader (the webpack loader for babel)
Configuration file#
// webpack.config.js
const HtmlWebPackPlugin = require('html-webpack-plugin')
const webpack = require('webpack')
module.exports = {
entry: './app/index.js',
module: {
rules: [
{ test: /\.svg$/, use: 'svg-inline-loader' },
{ test: /\.css$/, use: [ 'style-loader', 'css-loader'] },
{ test: /\.js$/, use: 'babel-loader' }
]
},
plugins: [
new HtmlWebPackPlugin(),
],
mode: process.env.NODE_ENV === 'production' ? 'production':'development',
"scripts": {
"build": "NODE_ENV='production' webpack",
},
output: {
path: path.resolve( __dirname, 'dist'),
filename: 'index_bundle.js'
}
}