简体   繁体   English

使用webpack和html-webpack-plugin输出许多html文件

[英]Using webpack and html-webpack-plugin to output many html files

I'm using webpack to output JS, SASS/CSS and HTML and some other operations, including babel. 我正在使用webpack输出JS,SASS / CSS和HTML以及其他一些操作,包括babel。

I also have a series of static HTML files that have several operations run on them as well. 我也有一系列静态HTML文件,这些文件上也运行了一些操作。 While most examples show how to output one, two or three HTML files, using this: 尽管大多数示例都显示了如何使用此输出一个,两个或三个HTML文件:

    new HtmlWebpackPlugin({
        filename: 'html/index.html',
        template: 'html/index.html'
    }),
    new HtmlWebpackPlugin({
        filename: 'html/about.html',
        template: 'html/about.html'
    }),
    new HtmlWebpackPlugin({
        filename: 'html/settings.html',
        template: 'html/settings.html'
    }),

What if I can't predict how many pages there will be? 如果我无法预测会有多少页怎么办? What if there's 40+? 如果有40岁以上怎么办? How can I approach this better? 我怎样才能更好地做到这一点?

Still relatively new to webpack, but I've had experience with with gulp and grunt. 对于webpack来说还是相对较新的东西,但是我对gulp和grunt有一定的经验。

Try to: 尝试:

  1. Find the files with the extension that you want. 查找具有所需扩展名的文件。

  2. Create new HtmlWebpackPlugin object configuration for each one. 为每个创建新的HtmlWebpackPlugin对象配置。

To find the files with the extension, we can use the function findFilesInDir adapted by the user Nicolas: 要查找具有扩展名的文件,我们可以使用用户Nicolas改编的函数findFilesInDir

...

let findFilesInDir = require('./findFilesInDir.js')

let templateFilesConf = findFilesInDir('src/html', '.html')
 .map((templateFileName) =>  
   new HtmlWebpackPlugin({
     filename: templateFileName,
     template: templateFileName
   })
)

let webpackConf = {
  entry: './path/to/my/entry/file.js',
  ...
  plugins : [...]
}  

webpackConf.plugins = webpackConf.plugins.concat(templateFilesConf)

module.exports = webpackConf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM