Sass热更新webpack配置

需要安装的包

模块:

  • webpack
  • webpack-cli
  • webpack-dev-server
  • node-sass

loader:

  • style-loader 用作fallback
  • css-loader
  • sass-loader
  • file-loader 解决js css引入图片问题
  • html-loader 解决html引入图片问题

plugins:

  • html-webpack-plugin 将html也打包
  • clean-webpack-plugin
  • extract-text-webpack-plugin
yarn install webpack webpack-cli webpack-dev-server style-loader css-loader sass-loader node-sass html-webpack-plugin clean-webpack-plugin extract-text-webpack-plugin -D

webpack.config.js

const path = require('path')
const webpack = require('webpack')
const htmlplugin = require('html-webpack-plugin')
const cleanplugin = require('clean-webpack-plugin')

module.exports = {
  mode: 'development',
  entry: './src/index.js',
  output: {
    filename: '[hash:4]-boundle.js',
    path: path.join(__dirname, 'dist')
  },
  module: {
    rules: [
      {
        test: /\.scss$/,
        use: ['style-loader', 'css-loader', 'sass-loader']
      },
      {
        test: /\.(jpg|png|gif|svg|webp)$/,
        use: ['file-loader']
      },
      {
        test: /\.(html|ejs)$/,
        use: ['html-loader']
      }
    ]
  },
  devServer: {
    contentBase: path.join(__dirname, 'dist'),
    port: 3000,
    historyApiFallback: true,
    overlay: true,
    hot: true
  },
  devtool: 'inline-source-map',
  plugins: [
    new cleanplugin(),
    new htmlplugin({
      template: path.join(__dirname, 'src', 'template.ejs'),
      title: 'hello sass'
    })
  ]
}

package.json

"scripts": {
    "build": "webpack",
    "build:prod": "webpack --production",
    "start": "webpack-dev-server"
  }

你可能感兴趣的:(Sass热更新webpack配置)