Webpack.config how to just copy the index.html to the dist folder

后端 未结 9 1700
醉梦人生
醉梦人生 2020-12-07 08:14

I am trying to automate assets going into /dist. I have the following config.js:

module.exports = {
  context: __dirname + "/lib",
  entry: {
    m         


        
9条回答
  •  抹茶落季
    2020-12-07 09:01

    I will add an option to VitalyB's answer:

    Option 3

    Via npm. If you run your commands via npm, then you could add this setup to your package.json (check out also the webpack.config.js there too). For developing run npm start, no need to copy index.html in this case because the web server will be run from the source files directory, and the bundle.js will be available from the same place (the bundle.js will live in memory only but will available as if it was located together with index.html). For production run npm run build and a dist folder will contain your bundle.js and index.html gets copied with good old cp-command, as you can see below:

    "scripts": {
        "test": "NODE_ENV=test karma start",
        "start": "node node_modules/.bin/webpack-dev-server --content-base app",
        "build": "NODE_ENV=production node node_modules/.bin/webpack && cp app/index.html dist/index.html"
      }
    

    Update: Option 4

    There is a copy-webpack-plugin, as described in this Stackoverflow answer

    But generally, except for the very "first" file (like index.html) and larger assets (like large images or video), include the css, html, images and so on directly in your app via require and webpack will include it for you (well, after you set it up correctly with loaders and possibly plugins).

提交回复
热议问题