Search code examples
webpackvuejs2laravel-mixwebpack-hmr

HMR with Webpack/Laravel Mix/AdonisJS


I am having a lot of difficulty getting Webpack's Hot Module Replacement (HMR) working for my application stack, which is:

  • Adonisjs v4.0.28
  • Laravel Mix v2.1.11
  • Webpack v3.11.0
  • Vue v2.5.16

The frontend application assets are all loaded from the webpack-dev-server rather than Adonis's application server like so:

<script src="http://localhost:8080/js/manifest.js" />
<script src="http://localhost:8080/js/vendor.js" />
<script src="http://localhost:8080/js/app.js" />

My application loads and executes successfully, and when running the webpack-dev-server and I make source changes, it updates the mix-manifest.json file properly by adding "hot-update" files which are all accessible at their URLs.

{
    "/js/app.js": "/js/app.js",
    "/css/app.css": "/css/app.css",
    "/js/vendor.js": "/js/vendor.js",
    "/js/manifest.js": "/js/manifest.js",
    "/0.e28262e7e8d825cfaa1a.hot-update.js": "/0.e28262e7e8d825cfaa1a.hot-update.js",
    "/0.1a35d37d2386038f6c78.hot-update.js": "/0.1a35d37d2386038f6c78.hot-update.js"
}

I'm unsure how to troubleshoot further or what I am doing incorrectly in order to get HMR working.


Configuration

npm script:

node node_modules/cross-env/dist/bin/cross-env.js NODE_ENV=development node node_modules/webpack-dev-server/bin/webpack-dev-server.js --hot --config=node_modules/laravel-mix/setup/webpack.config.js

webpack.mix.js:

const Env = require('@adonisjs/framework/src/Env');
const mix = require('laravel-mix');
const webpack = require('webpack');

const env = new Env(__dirname);
const appUrl = JSON.stringify(env.get('APP_URL'));
mix.webpackConfig({
  plugins: [
    new webpack.DefinePlugin({ APP_URL: appUrl }),
  ],
  devServer: {
    inline: true,
    quiet: false,
  },
});

mix
  .setPublicPath('public')
  .js('resources/assets/js/app.js', 'public/js')
  .extract(['vue', 'vue-router', 'axios'])
  .sass('resources/assets/sass/app.scss', 'public/css')
  .sourceMaps()
  .disableNotifications();

Full Webpack configuration as generated by Laravel Mix:

https://gist.github.com/jarodhayes/3e2accb996f6884321671bcd4cc2b690


Solution

  • This appears to be an issue with Laravel Mix v2. The following code changes to the laravel-mix module fixed the issue for me:

    https://github.com/JeffreyWay/laravel-mix/issues/1437#issuecomment-365484126