6
votes

I would like to have Hot Reloading when I run the npm run watch command. (I would like it to work as it is working when using the vue-cli - when you run the npm run dev command: https://vue-loader.vuejs.org/en/features/hot-reload.html).

So, I've installed webpack-dev-server and this what I've tried:

package.json:

{
  "name": "webpack-learning",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "dev": "cross-env NODE_ENV=development webpack",
    "watch": "cross-env NODE_ENV=development webpack-dev-server --open --hot",
    "build": "cross-env NODE_ENV=production webpack --progress --hide-modules"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.24.1",
    "babel-loader": "^7.0.0",
    "babel-preset-es2015": "^6.24.1",
    "cross-env": "^5.0.0",
    "css-loader": "^0.28.1",
    "extract-text-webpack-plugin": "^2.1.0",
    "file-loader": "^0.11.1",
    "node-sass": "^4.5.2",
    "purifycss-webpack": "^0.6.2",
    "sass-loader": "^6.0.5",
    "style-loader": "^0.17.0",
    "webpack": "^2.4.1",
    "webpack-dev-server": "^2.4.5"
  }
}

If you look at the "watch" script - it uses webpack-dev-server and it also has --open --hot - this is the same as in vue-cli's package.json.

Also, here is my webpack.config.js:

var webpack = require('webpack');
var path = require('path');
const glob = require('glob');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const PurifyCSSPlugin = require('purifycss-webpack');
var inProduction = (process.env.NODE_ENV === 'production');

module.exports = {
    entry: {
        app: [
            './src/js/main.js',
            './src/sass/app.scss'
        ]
    },
    output: {
        path: path.resolve(__dirname, './dist'),
        filename: '[name].js'
    },

    module: {
        rules: [
            {
                test: /\.s[ac]ss$/,
                use: ExtractTextPlugin.extract({
                    use: ['css-loader', 'sass-loader'],
                    fallback: 'style-loader'
                })
            },
            {
                test:  /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/,
                loader: 'file-loader',
                options: {
                    name: '[name].[ext]?[hash]'
                }
            },
            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader']
            },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: "babel-loader"
            }
        ]
    },

    plugins: [
        new ExtractTextPlugin('[name].css'),

        new PurifyCSSPlugin({
            paths: glob.sync(path.join(__dirname, 'index.html')),
            minimize: inProduction
        }),

        new webpack.LoaderOptionsPlugin({
            minimize: inProduction
        })
    ],

    devtool: '#eval-source-map'
};

if (inProduction) {
    module.exports.devtool = '#source-map';

    module.exports.plugins.push(
        new webpack.optimize.UglifyJsPlugin({
            sourceMap: true,
            compress: {
                warnings: false
            }
        })
    );
}

Note that I'm using the ExtractTextPlugin (I have a separate CSS file), file-loader plugin (for url/images) etc.

When I run the npm run watch command - there is no Hot Relaoding, nothing happens. There are no errors, but it doesn't do anything, it only opens index.html in the browser ( http://localhost:8080 ).

No changes are detected. For example, if I edit:

  • app.scss
  • index.html

... those changes are not loaded.

It doesn't even compile anything (if I remember well, in vue-cli if you have deleted the dist directory - when you run npm run dev - it will re-create the dist directory again and it will put compiled assets there... but here, if you have deleted the compiled assets - it will not compile/create the new ones).

1

1 Answers

1
votes

I have a template project that I've used to bootstrap my own projects, and it has hot reloading working. It's using gulp to run the webpack-dev-server, but you can take a look and add the relevant bits back to your own webpack.conf.js:

https://github.com/vhogemann/angular2-starter/blob/master/config/gulp.dev-server.js