Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack Dev Server Not Hot Reloading .vue files

Been working on a project and was sure HMR was working, any of my .js files if I would update them webpack would compile and the module would be replaced all hot like.

I was working on a .vue file and webpack would recompile, but there was no super fresh HMR.

Hoping someone can take a look and tell me if something is off:

The script I'm using in the cli looks like this.

webpack-dev-server --d --watch --output-path ./public --config ./_src/webpack.config.js --progress --env.dev

I'm guessing the most important bit to look at is this:

devServer: {
    contentBase: 'public',
    hot: true,
    filename: 'main.js',
    overlay: true,
    stats: { colors: true },
    port: 3000,
    proxy: {
      '/': {
        target: 'http://moment.local/',
        secure: false,
        changeOrigin: true
      }
    },
    historyApiFallback: true
  },

But here is my whole config if it helps.

const webpack = require('webpack')
const {resolve} = require('path')

const ExtractTextPlugin = require('extract-text-webpack-plugin')

module.exports = env => {
const addPlugin = (add, plugin) => add ? plugin : undefined
const ifProd = plugin => addPlugin(env.prod, plugin)
const removeEmpty = array => array.filter(i => !!i)

// Our Sass Extraction Plugin
const extractSass = new ExtractTextPlugin({
  filename: 'style.css',
  disable: env.dev
})

return {
  entry: {
    'vendor': ['jquery', 'KlaviyoSubscribe', 'learnq', 'select2', 'slick-carousel', 'moment', 'lodash'],
    'main': resolve(__dirname, './js/index.js')
  },
  output: {
    filename: '[name].js',
    path: resolve(__dirname, '../public/'),
    pathinfo: !env.prod
  },
  module: {
    loaders: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
            css: 'vue-style-loader!css-loader!postcss-loader!sass-loader', // <style lang='scss'>
            scss: 'vue-style-loader!css-loader!postcss-loader!sass-loader', // <style lang='scss'>
            sass: 'vue-style-loader!css-loader!postcss-loader!sass-loader?indentedSyntax' // <style lang='sass'>
          }
        }
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader'
      },
      { test: /\.s(c|a)ss$/,
        use: extractSass.extract({
          fallback: 'style-loader',
          use: [
            {
              loader: 'css-loader',
              options: {
                importLoaders: 1,
                sourceMap: true
              }
            },
            'postcss-loader?sourceMap',
            'sass-loader?sourceMap'
          ]
        })
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]?[hash]'
        }
      }
    ]
  },
  devtool: env.prod ? 'source-map' : 'eval',
  devServer: {
    contentBase: 'public',
    hot: true,
    filename: 'main.js',
    overlay: true,
    stats: { colors: true },
    port: 3000,
    proxy: {
      '/': {
        target: 'http://moment.local/',
        secure: false,
        changeOrigin: true
      }
    },
    historyApiFallback: true
  },
  bail: env.prod, // Fail out on the first error if production
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.common.js'
    }
  },
  plugins: removeEmpty([
    extractSass,
    new webpack.HotModuleReplacementPlugin(),
    new webpack.optimize.CommonsChunkPlugin({
      // Let's create js for our vendor scripts
      name: 'vendor',
      // (with more entries, this ensures that no other module
      //  goes into the vendor chunk)
      minChunks: Infinity
    }),
    new webpack.optimize.CommonsChunkPlugin({
      name: 'commons',
      filename: 'commons.js',
      // (Modules must be shared between 2 entries)
      minChunks: 2
    })
    ...
  ])
  }
}

Really struggling here so anything would be great.

like image 247
Devan Flaherty Avatar asked Mar 06 '18 00:03

Devan Flaherty


1 Answers

It sounds like you want the "hot" behaviour but you are missing the --hot option in the script you posted. The documentation for that option is here.

You have a lot of options already; just add --hot and that should do the trick.

UPDATE: I do see that you have the hot: true set in the devServer property of your webpack config, but if I don't use --hot in the cli, I get the following error in the console, so that is why I am saying to use it even though you would think it would be covered by the config - its not.

Uncaught Error: [HMR] Hot Module Replacement is disabled.
like image 86
Geek Stocks Avatar answered Oct 10 '22 03:10

Geek Stocks