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

后端 未结 9 1691
醉梦人生
醉梦人生 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:16

    You can add the index directly to your entry config and using a file-loader to load it

    module.exports = {
    
      entry: [
        __dirname + "/index.html",
        .. other js files here
      ],
    
      module: {
        rules: [
          {
            test: /\.html/, 
            loader: 'file-loader?name=[name].[ext]', 
          },
          .. other loaders
        ]
      }
    
    }
    
    0 讨论(0)
  • 2020-12-07 09:20

    To copy an already existing index.html file into the dist directory you can simply use the HtmlWebpackPlugin by specifying the source index.html as a template.

    const HtmlWebpackPlugin = require('html-webpack-plugin');
    
    module.exports = {
      // ...
      plugins: [    
        new HtmlWebpackPlugin({
          template: './path/to/index.html',
        })
      ],
      // ...
    };
    

    The created dist/index.html file will be basically the same as your source file with the difference that bundled resources like .js files are injected with <script> tags by webpack. Minification and further options can be configured and are documented on github.

    0 讨论(0)
  • 2020-12-07 09:20

    I also found it easy and generic enough to put my index.html file in dist/ directory and add <script src='main.js'></script> to index.html to include my bundled webpack files. main.js seems to be default output name of our bundle if no other specified in webpack's conf file. I guess it's not good and long-term solution, but I hope it can help to understand how webpack works.

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