webplus / blog

前端开发日志
63 stars 6 forks source link

vue-cli4 热更新失效无法自动刷新的问题 #51

Open webplus opened 4 years ago

webplus commented 4 years ago

devServe配置没毛病,但就是没法热更新

// devServe配置没毛病
return {
    disableHostCheck: true,
    open: false,
    host: '0.0.0.0',
    port: ENV.VUE_APP_PORT,
    https: false,
    hotOnly: true,
    clientLogLevel: 'warn',
    overlay: {
      warnings: true,
      errors: true,
    },
    before: (app) => {
      VUE_APP_USE_MOCK === 'TRUE' && app.use(mockMiddleware());
    },
    historyApiFallback: {
      rewrites: [
        {
          from: /.*/,
          to: path.posix.join('/', 'index.html'),
        },
      ],
    },

解决方法:config.resolve.symlinks(true);

chainWebpack: config => {
    // 修复HMR
    config.resolve.symlinks(true);

    // config.plugins.delete('preload');
    // config.plugins.delete('prefetch');

    config
      .plugin("ignore")
      .use(
        new webpack.ContextReplacementPlugin(/moment[/\\]locale$/, /zh-cn$/)
      );

    // 添加别名
    config.resolve.alias
      ....

vue.config.js其它配置可以参考,全面配置