Joseph K.
Joseph K.

Reputation: 1223

Webpack - Generating multiple HTML files with different javascript script files

I'm unable to create multiple Html files with separate javascript files in webpack.

I have set up multiple entries with output configured as [name].bundle.js. I'm also using multiple new HtmlWebpackPlugin() to create html files as well.

I am sure that this configuration is doing exactly what it is supposed to do, but I don't know how to configure it so that each javascript are referenced separately for its own html file. I couldn't find more information online and in documentations about this.

// webpack.config.js
module.exports = {
  ...
  entry: {
    background: './src/browser/chrome/background/index.js',
    panel: './src/browser/chrome/panel/index.js',
    popup: './src/browser/chrome/popup/index.js',
    devtools: './src/browser/chrome/devtools/index.js',
  },
  output: {
    path: path.resolve(__dirname, './build'),
    filename: 'js/[name].bundle.js',
    chunkFilename: '[id].chunk.js',
  },
  ...
  plugins: [
    new HtmlWebpackPlugin({
      template: './src/browser/chrome/popup/index.html',
      filename: 'popup.html',
    }),
    new HtmlWebpackPlugin({
      template: './src/browser/chrome/devtools/index.html',
      filename: 'devtools.html',
    }),
    ...
  ],
  ...
}
// devtools.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  This is the dev tools page
  <script type="text/javascript" src="js/background.bundle.js"></script>
  <script type="text/javascript" src="js/panel.bundle.js"></script>
  <script type="text/javascript" src="js/popup.bundle.js"></script>
  <script type="text/javascript" src="js/devtools.bundle.js"></script>
</body>
</html>

Upvotes: 3

Views: 3569

Answers (1)

Phil.hsu
Phil.hsu

Reputation: 86

You can do this with multiple pages, config the modules entry and entries

const appConfig = {
  entry: {
    background: './src/browser/chrome/background/index.js',
    panel: './src/browser/chrome/panel/index.js',
    popup: './src/browser/chrome/popup/index.js',
    devtools: './src/browser/chrome/devtools/index.js',
  },
  port: 3002,
  entries: {
    background: {
      title: 'background',
      template: './src/browser/chrome/background/index.html',
      chunks: ['background','vendors'],
    },
    popup: {
      title: 'background',
      template: './src/browser/chrome/popup/index.html',
      chunks: ['popup','vendors'],
    },
  },

};

Then config the html-webpack-plugin

let plugins = []
_.each(appConfig.entries, (entryInfo, entryName) => {
  plugins.push(new HtmlWebpackPlugin({
    title: entryInfo.title,
    template: entryInfo.template,
    chunks: entryInfo.chunks,
    chunksSortMode: 'manual',
    inject: 'body',
    favicon: entryInfo.favicon,
    resources: entryInfo.resources
  }));
});

Then config the module and entry

let entry = _.reduce(appConfig.entry, (entries, entry, entryName) => {
  entries[entryName] = entry;
  return entries;
}, {});

module.export = {
  entry,
  plugins,
}

Done!!!

Upvotes: 1

Related Questions