代码之家  ›  专栏  ›  技术社区  ›  Joseph K.

Webpack-使用不同的javascript脚本文件生成多个HTML文件

  •  0
  • Joseph K.  · 技术社区  · 7 年前

    我设置了多个条目,输出配置为 [name].bundle.js . 我也在使用多个 new HtmlWebpackPlugin()

    我确信这个配置正在做它应该做的事情,但是我不知道如何配置它,以便每个javascript都被单独引用到它自己的html文件中。我在网上和文件里找不到更多的信息。

    // 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>
    
    0 回复  |  直到 7 年前
        1
  •  0
  •   Phil.hsu    7 年前

    您可以使用多个页面来完成此操作,配置模块条目和条目

    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'],
        },
      },
    
    };
    

    然后配置html网页包插件

    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
      }));
    });
    

    然后配置模块和条目

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