Webpack-dev-server compiles files but does not refresh or make compiled javascript available to browser

前端 未结 11 1794
-上瘾入骨i
-上瘾入骨i 2020-12-04 16:51

I\'m trying to use webpack-dev-server to compile files and start up a dev web server.

In my package.json I have the script property set to:



        
相关标签:
11条回答
  • 2020-12-04 16:52

    I'll add my own special tale of Webpack --watch woe to the wall of suffering here.

    I was running

    webpack --watch
    

    in order to build a Typescript project. The compiled .js files would update, but the bundle that the browser was seeing would not. So I was basically in the same position as the OP.

    My problem came down to the watchOptions.ignored parameter. The original author of the build config had set up ignored as a filter function, which turns out to not be a valid value for that parameter. Replacing the filter function with an appropriate RegExp got the --watch build working again for me.

    0 讨论(0)
  • 2020-12-04 16:58

    After a long search I found the solution for my problem, in my case output path wasn't configured correctly.

    This configuration solved my problem:

    const path = require('path');
    
    module.exports = {
      "entry": ['./app/index.js'],
      "output": {
        path: path.join(__dirname, 'build'),
        publicPath: "/build/",
        "filename": "bundle.js"
      }....
    
    0 讨论(0)
  • 2020-12-04 16:58

    the right solution

    Tell dev-server to watch the files served by the devServer.watchContentBase option.

    It is disabled by default.

    When enabled, file changes will trigger a full page reload.

    Example:

    module.exports = {
      //...
      devServer: {
        // ...
        watchContentBase: true
      }
    };
    
    0 讨论(0)
  • 2020-12-04 16:59

    It can happen because of ExtractTextPlugin. Deactive the ExtractTextPlugin in development mode. Use it only for production build.

    0 讨论(0)
  • 2020-12-04 17:00

    Somehow, for my case, removing "--hot" makes it work. So, I removed hot: true

    webpack.dev.js

    module.exports = merge(common, {
      mode: 'development',
      devtool: 'inline-source-map',
      devServer: {
        publicPath: '/js/',
        contentBase: path.resolve(__dirname, 'docs'),
        watchContentBase: true,
      }
    });
    

    webpack.common.js

      output: {
        path: path.resolve(__dirname, 'docs/js'),
        filename: '[name].min.js',
        library: ['[name]']
      },
    
    0 讨论(0)
  • 2020-12-04 17:01

    My case was that I got so deep into experimenting with Webpack features, but totally forgot that I had set inject to be false the entire time like so...

     new HTMLWebpackPlugin({
            inject: false,
            ...
     }),
    

    Switching that on was my ticket.

    0 讨论(0)
提交回复
热议问题