Webpack-dev-server does not place bundle in dist

后端 未结 4 1132
执念已碎
执念已碎 2021-01-12 03:39

I am trying to setup a Webpack configuration for a website I want to build. I want to compile SASS to CSS and place it into the dist folder. When I run npm run build

相关标签:
4条回答
  • 2021-01-12 03:50

    dev server have the option writeToDisk

    module.exports = { //... devServer: { writeToDisk: true } };

    check this : https://webpack.js.org/configuration/dev-server/#devserverwritetodisk-

    0 讨论(0)
  • 2021-01-12 03:53

    webpack-dev-server serves from memory. If you want to see the files on disk during development with webpack-dev-server, you'll need to run a standard webpack build concurrently. See this answer for details.

    0 讨论(0)
  • 2021-01-12 04:01

    You can look to the generated URLs by webpack-dev-server into this URL.

    http://localhost:8080/webpack-dev-server (change host and port as needed)

    webpack-dev-server is not going to write the files into disk, but you can see them there.

    0 讨论(0)
  • 2021-01-12 04:07

    You've noticed that dist/scripts is not updated.
    Like others have said, the webpack-dev-server only keeps it in memory.
    But you can take advantage of this.

    Add this to your webpack.config.js:

    module.exports = {
    ...
      devServer: {
        open: true,             // Automatically open the browser
        hot: true,              // Automatically refresh the page whenever bundle.js 
        publicPath: '/dist/scripts',
      },
    ...
    };
    

    publicPath: '/dist/scripts'
    This is where the magic is.
    By default, webpack-dev-server serves the config's output.filename on localhost:8080/ (eg. localhost:8080/bundle.js).
    We changed it so that it serves the content on localhost:8080/dist/scripts/ (eg. localhost:8080/dist/scripts/bundle.js).
    Now your index.html will be able successfully find <script src="dist/scripts/bundle.js"></script>.

    NOTE: You must visit your index.html via localhost:8080 for this work.
    eg. visiting from file://... or a different host/port will not work because it will look for the actual file on your system (which is not updated of course).


    Alternatively, if you have output: { filename: '/dist/scripts/bundles.js } } instead of output: { filename: 'bundles.js } } then publicPath will be unnecessary.

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