最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - A clean way to loop HTMLWebpackPlugin with Webpack? - Stack Overflow

programmeradmin0浏览0评论

I'm using Webpack 4 with the HTMLWebPackPlugin. I'm currently at the point where I am dealing with close to 30 different pages and more moving forward. Here is a sample of what I have in code...

        new HTMLWebpackPlugin({
            template: './src/game11/index.html',
            mViewPort: 'width=device-width, initial-scale=1.0',
            favicon: './src/game11/media/favicon-16x16.png',
            title: 'Game 11 Training',
            filename: 'game11.htm',
            chunks: ['game11']
        }),
        new HTMLWebpackPlugin({
            template: './src/game12/index.html',
            mViewPort: 'width=device-width, initial-scale=1.0',
            favicon: './src/game12/media/favicon-16x16.png',
            title: 'Game 12 Training',
            filename: 'game12.htm',
            chunks: ['game12']
        }),

I have 30 of these so far in my webpack.config.js file. But I would prefer to do something like this instead...

 ['game11','game12','something-else','etc'].forEach((event) => {
       new HtmlWebpackPlugin({
           template: './src/${event}/index.html',
           mViewPort: 'width=device-width, initial-scale=1.0',
           favicon: './src/${event}/media/favicon-16x16.png',
           title: '${event} Training',
           filename: '${event}.htm',
           chunks: ['${event}']
       }),
   }),

The above code does not work and it only a sketch. But is it possible to do something like that today without adding additional plugins or modifying my outputs? I simply want to add array values which will create a new instance in itself.

Many thanks!

I'm using Webpack 4 with the HTMLWebPackPlugin. I'm currently at the point where I am dealing with close to 30 different pages and more moving forward. Here is a sample of what I have in code...

        new HTMLWebpackPlugin({
            template: './src/game11/index.html',
            mViewPort: 'width=device-width, initial-scale=1.0',
            favicon: './src/game11/media/favicon-16x16.png',
            title: 'Game 11 Training',
            filename: 'game11.htm',
            chunks: ['game11']
        }),
        new HTMLWebpackPlugin({
            template: './src/game12/index.html',
            mViewPort: 'width=device-width, initial-scale=1.0',
            favicon: './src/game12/media/favicon-16x16.png',
            title: 'Game 12 Training',
            filename: 'game12.htm',
            chunks: ['game12']
        }),

I have 30 of these so far in my webpack.config.js file. But I would prefer to do something like this instead...

 ['game11','game12','something-else','etc'].forEach((event) => {
       new HtmlWebpackPlugin({
           template: './src/${event}/index.html',
           mViewPort: 'width=device-width, initial-scale=1.0',
           favicon: './src/${event}/media/favicon-16x16.png',
           title: '${event} Training',
           filename: '${event}.htm',
           chunks: ['${event}']
       }),
   }),

The above code does not work and it only a sketch. But is it possible to do something like that today without adding additional plugins or modifying my outputs? I simply want to add array values which will create a new instance in itself.

Many thanks!

Share Improve this question asked Oct 10, 2018 at 18:11 klewisklewis 8,43816 gold badges69 silver badges112 bronze badges 2
  • From your idea, you could use Array.prototype.map instead: ['game11','game12','something-else','etc'].map((event) => { return new HtmlWebpackPlugin({... – lenilsondc Commented Oct 10, 2018 at 18:18
  • love it!!! I didn't know that existed. But even if I used it, how would I handle having ma-separated instances in a loop process. That's one of things I don't know how to handle yet... – klewis Commented Oct 10, 2018 at 18:21
Add a ment  | 

1 Answer 1

Reset to default 8

Following the same logic you suggested on your question, you could use map instead of forEach to build the plugins array like so:

webpack.config.js

{
  plugins: [
    new MiniCSSExtractPlugin(),
    ...['game11', 'game12', 'something-else', 'etc'].map((event) => {
      return new HtmlWebpackPlugin({
        template: `./src/${event}/index.html`,
        mViewPort: `width=device-width, initial-scale=1.0`,
        favicon: `./src/${event}/media/favicon-16x16.png`,
        title: `${event} Training`,
        filename: `${event}.htm`,
        chunks: [`${event}`]
      })
    })
  ]
}
发布评论

评论列表(0)

  1. 暂无评论