antonio-leblanc / webpack-tutorial

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Webpack-tutorial

img

Steps

  1. Init: npm init
  2. Install dependencies: npm i -D webpack webpack-cli
  3. Add build in scripts
  "scripts": {
    "build": "webpack"
  },
  1. To render HTML npm i -D html-webpack-plugin html-loader
  2. Create webpack.config.js
const HtmlWebPackPlugin = require('html-webpack-plugin');

module.exports = {
  module: {
    rules: [
      {
        test: /\.html$/,
        use: [
          {
            loader: 'html-loader',
            options: {minimize:true}
          },
        ]
      },
    ]
  },
  plugins: [
    new HtmlWebPackPlugin({
      template: './src/index.html',
      filename: './index.html',
    })
  ]
}
  1. Build npm rum build -> open index.html from /dist folder

  2. Dev serve -> npm i -D webpack-dev-server. Then add to scripts in package.json

  "scripts": {
    "build": "webpack",
    "dev": "webpack serve"
  }

and npm run dev

  1. Bundler Analyser:
  • run npm install --save-dev webpack-bundle-analyzer
  • follow the docs on the npm package to use it as a plugin in webpack.config.js
  • run npm run build --prod --stats-json (or add as a script in package.json)
"scripts": {
  "build": "webpack",
  "dev": "webpack serve",
  "bundle-analyser": "npm run build --prod --stats-json"
},
  1. SCSS loaders and plugins:
  • install libs npm i -D node-sass style-loader css-loader sass-loader mini-css-extract-plugin
  • add rule to module.exports in webpack.config
...
{
  test: /\.scss$/,
  use: [
    'style-loader',
    'css-loader',
    'sass-loader',
  ]
},
```
- add `MiniCssExtractPlugin` in module.exports

About


Languages

Language:JavaScript 76.0%Language:HTML 20.6%Language:SCSS 3.4%