B
B
boga-net2018-06-25 20:19:26
Node.js
boga-net, 2018-06-25 20:19:26

How to set up automatic html update in webpack?

Hello. After 6 hours of headache, I managed to compile .sass to css and automatically reload the page. Now, for complete happiness, there would be an automatic page refresh when the html file changes. What modules need to be installed and how to configure? How difficult is it?
package.json

"devDependencies": {
    "clean-webpack-plugin": "^0.1.19",
    "css-loader": "^0.28.11",
    "extract-text-webpack-plugin": "^4.0.0-beta.0",
    "file-loader": "^1.1.11",
    "node-sass": "^4.9.0",
    "path": "^0.12.7",
    "sass-loader": "^6.0.7",
    "style-loader": "^0.20.3",
    "webpack": "^4.9.1",
    "webpack-cli": "^2.1.4",
    "webpack-dev-server": "^3.1.4"
  }

webpack.config.js
const path = require('path');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const CleanWebpackPlugin = require('clean-webpack-plugin');
const cleanDist = new CleanWebpackPlugin(['dist']);

let conf = {
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, './dist/'),
    filename: 'main.js',
    publicPath: 'dist/'
  },
  devServer: {
    overlay: true
  },
  module: {
    rules: [
      {
        test: /\.sass$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: ['css-loader', 'sass-loader']
        })
      },
      {
        test: /\.(png|svg|jpg|gif)$/,
        use: ['file-loader']
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/,
        use: ['file-loader']
      }
    ]
  },
  plugins: [
    cleanDist,
    new ExtractTextPlugin('style.css')
  ]
};

module.exports = (env, options) => {
  let production = options.mode === 'production';

  conf.devtool 	= production 
                ? false
                : 'eval-sourcemap';

  return conf;
}

structure :
  • index.html
  • webpack.config.js
  • package.json
  • src
    • index.js
    • css
      • style.sass


  • node_modules

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita Egorov, 2018-06-26
@boga-net

It seems like only through the html preprocessor - pug.
Connection instructions:
https://www.npmjs.com/package/pug-plain-loader
Documentation:
https://pugjs.org/api/getting-started.html
And more. ExtractTextPlugin is not officially supported as of webpack 4. MiniCssExtractTextPlugin is recommended. It is from the same developers, but without bugs and other things + of. supported.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question