Webpack-dev-server not bundling even after showing bundle valid message

前端 未结 6 712
渐次进展
渐次进展 2021-02-02 08:22

I\'ve set up a basic react application with webpack but I couldn\'t get the webpack-dev-server running properly.

I\'ve installed webpack-dev-server

相关标签:
6条回答
  • 2021-02-02 08:51

    In my case I had to check where the webpack is serving the file.

    You can see it: http://localhost:8080/webpack-dev-server

    Then I could see my bundle.js path > http://localhost:8080/dist/bundle.js

    After that I used that /dist/bundle.js in my index.html <script src="/dist/bundle.js"></script>

    Now it refreshes any file changes.

    0 讨论(0)
  • 2021-02-02 08:52

    In my case I was using VS Code and I had to restart it. I have noticed that vs code bugs up sometimes. The changes you make in configuration files (package.json, webpack.config.js etc.) do not take effect sometimes. So, in case you encounter a situation where something doesn't work even with the correct settings, just restart vs code.

    I can't see any bug reports related to this. So that's strange. I'm thinking this bug is triggered if you change one of the configuration files some time later after you have already built the project multiple times.

    If this is actually what's happening then it's a huge bug. I'll try switching to Visual Studio instead of Code and see if this still happens. If it does then it's probably an issue with webpack.

    0 讨论(0)
  • 2021-02-02 08:53

    webpack-dev-server serves your bundle.js from memory. It won't generate the file when you run it. So bundle.js is not present as a file in this scenario.

    If you wan't to use bundle.js, for example to optimize it's size or test your production deployment, generate it with webpack using the webpack command and serve it in production mode.

    0 讨论(0)
  • 2021-02-02 08:56

    By the way, started from Webpack v4 it is possible to write in-memory assets to disk:

      devServer: {
        contentBase: "./",
        port: 8080,
        writeToDisk: true
      }
    

    See doc.

    0 讨论(0)
  • 2021-02-02 09:04

    Dev-server keeps the compiled file in memory, and I can't access it directly... BUT! THE SOLUTION is that you have no need to access it, in development(even when you run your project on node server or localhost) use localhost:8080 to access your page, and that's where webpack-dev-server is serving your page and you can feel all advantages of using it(live reload!), BUT! it doesn't compile your bundle.js, SO! before production, you need to manually run webpack build command, and that's it! there is no way for dev-server to compile your bundle.js file! Good Luck!

    0 讨论(0)
  • 2021-02-02 09:06

    I've got the issue resolved by myself. Silly as it sounds but the issue was with the publicPath under the output object. It should match the path property instead of just /build/, i.e.,

    output: {
        path: path.join(__dirname, 'build/js'),
        publicPath: '/build/js', // instead of publicPath: '/build/' 
        filename: '[name].js'
    },
    
    0 讨论(0)
提交回复
热议问题