Vue-cli 的热更新无效问题

我使用vue init webpack-simple ProjectName创建了一个简单的Vue-cli工程,但是无法实现热更新。每次一点点改动就需要重新编译运行才能看到效果,十分困扰。
我的webpack配置如下:

const path = require('path')
const webpack = require('webpack');
console.log(path.resolve(__dirname, './src/main.js'))

module.exports = {
  //热部署
  entry:[
    path.resolve(__dirname, './src/main.js'),
    'webpack-dev-server/client?http://localhost:8080/',
  ],
  output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: './dist/',
    filename: 'build.js'
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          'vue-style-loader',
          'css-loader'
        ],
      },      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
          }
          // other vue-loader options go here
        }
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]?[hash]'
        }
      }
    ]
  },
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    },
    extensions: ['*', '.js', '.vue', '.json']
  },
  devServer: {  //这里可以配置端口号
//  port:8080,
//  host: 'localhost',
    //重定向404
    historyApiFallback: true,
    noInfo: true,
    //编译出错的时候,在浏览器页面上显示错误
    overlay: true,
    hot:true,
    inline:true,
    //该目录不填默认是项目目录
//  contentBase: path.resolve(__dirname),
  },
  performance: {
    hints: false
  },
  devtool: '#eval-source-map',
    //配置webpack插件
  plugins:[
      //热加载插件
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NamedModulesPlugin(),
  ]
}

if (process.env.NODE_ENV === 'production') {
  module.exports.devtool = '#source-map'
  // http://vue-loader.vuejs.org/en/workflow/production.html
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      sourceMap: true,
      compress: {
        warnings: false
      }
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    })
  ])
}

package.json如下:

{
  "name": "srms",
  "description": "A Vue.js project",
  "version": "1.0.0",
  "author": "Snile <826245622@qq.com>",
  "license": "",
  "private": true,
  "scripts": {
    "clean": "rd /s /q dist",
    "build": "cross-env NODE_ENV=production webpack --progress --hide-modules",
    "dev": "npm run build && cross-env NODE_ENV=development webpack-dev-server --open --inline --hot"
  },
  "dependencies": {
    "vue": "^2.5.11",
    "vue-router": "^3.0.2"
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not ie <= 8"
  ],
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-env": "^1.6.0",
    "babel-preset-stage-3": "^6.24.1",
    "cross-env": "^5.0.5",
    "css-loader": "^0.28.7",
    "file-loader": "^1.1.4",
    "vue-loader": "^14.2.3",
    "vue-template-compiler": "^2.4.4",
    "webpack": "^3.12.0",
    "webpack-dev-server": "^2.9.1"
  }
}

该配置似乎能检测到文件的变化,每次改动后,控制台都会输出:
client:77 [WDS] Hot Module Replacement enabled.
2client:80 [WDS] App updated. Recompiling...
client:223 [WDS] App hot update...

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐