美文网首页
使用webpack配置热更新

使用webpack配置热更新

作者: 叶小七的真命天子 | 来源:发表于2018-07-10 11:25 被阅读20次

步骤:

  • 安装webpack、webpack-cli、webpack-dev-server、html-webpack-plugin
yarn add webpack webpack-cli webpack-dev-server  html-webpack-plugin
  • webpack.config.js配置plugins
const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const env = process.env.NODE_ENV
const isDev = process.env.NODE_ENV === 'development'
// console.log(process.env)
const config = {
  target: 'web',
  entry: ['./main.js'],
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  mode: env,
  plugins: [
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: 'index.html',
      inject: true
    })
  ]
}
if (isDev) {
  config.devServer = {
    port: 9000,
    host: 'localhost',
    overlay: {
      errors: true
    },
    open: true,
    hot: true
  }
// 添加热更新模块
  config.plugins.push(
    new webpack.DefinePlugin({
      'process.evn': '"development"'  //添加全局变量
    }),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoEmitOnErrorsPlugin()
  )
  config.devtool = '#cheap-module-eval-source-map'
}
module.exports = config
  • package.json文件命令调用
"scripts": {
    "dev": "webpack-dev-server --config webpack.config.js"
  },

相关文章

网友评论

      本文标题:使用webpack配置热更新

      本文链接:https://www.haomeiwen.com/subject/nvlfpftx.html