# 10. 配置多個 HTML 文件
之前我們只寫了一個 html 文件,就是 `src/index.html`,但是有時候我們是需要多個的,這個時候,怎么辦呢?
之前我們是這么做的,用了 html-webpack-plugin 這個插件來輸出 html 文件。
**webpack.config.js**
```
...
new HtmlWebpackPlugin({
template: './src/index.html',
filename: 'index.html',
minify: {
collapseWhitespace: true,
},
hash: true,
})
...
```
之前怎么做,現在還是怎么做,我們復制一下,改個名字不就好嗎?
**webpack.config.js**
```
new HtmlWebpackPlugin({
template: './src/index.html',
filename: 'index.html',
minify: {
collapseWhitespace: true,
},
hash: true,
}),
new HtmlWebpackPlugin({
template: './src/contact.html',
filename: 'contact.html',
minify: {
collapseWhitespace: true,
},
hash: true,
})
```
**src/contact.html**
```
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Contact</title>
</head>
<body>
<p>Contact page</p>
</body>
</html>
```


有個問題,**`contact.html` 使用的 js 和 css 跟 `index.html` 是一模一樣的**
如果我要讓 `contact.html` 使用跟 `index.html` 不同的 js,如何做呢?(只要保證 js 不同,css 也會不同的,因為 css 也是由 js 里 import 的嘛)
那我們來改造一下:
```
...
module.exports = {
entry: {
"app.bundle": './src/app.js',
// 這行是新增的。
"contact": './src/contact.js'
},
...
plugins: [
new CleanWebpackPlugin(pathsToClean),
new HtmlWebpackPlugin({
template: './src/index.html',
filename: 'index.html',
minify: {
collapseWhitespace: true,
},
hash: true,
// 這行是新增的。
excludeChunks: ['contact']
}),
new HtmlWebpackPlugin({
template: './src/contact.html',
filename: 'contact.html',
minify: {
collapseWhitespace: true,
},
hash: true,
// 這行是新增的。
chunks: ['contact']
}),
new ExtractTextPlugin('style.css')
],
...
};
```
上面的 `excludeChunks` 指的是不包含, `chunks` 代表的是包含。
**src/contact.js**
```
console.log('hi from contact js')
```
結果:


這樣就 OK 了。
先說這么多。
- 0. 開始
- 1. 介紹
- 2. 安裝
- 3. 實現 hello world
- 4. webpack 的配置文件 webpack.config.js
- 5. 使用第一個 webpack 插件 html-webpack-plugin
- 6. 使用 loader 處理 CSS 和 Sass
- 7. 初識 webpack-dev-server
- 8. 用 webpack 和 babel 配置 react 開發環境
- 9. 用 clean-webpack-plugin 來清除文件
- 10. 配置多個 HTML 文件
- 11. 如何使用 pug (jade) 作為 HTML 的模板
- 12. 如何使用模塊熱替換 HMR 來處理 CSS
- 13. 生產環境 vs 開發環境
- 14. 如何打包圖片
- 15. 加載和打包 Twitter Bootstrap 框架
- 16. 使用 ProvidePlugin 插件來處理像 jQuery 這樣的第三方包
- 17. 輕松通過兩個實例來理解 devtool: 'source-map' 是什么意思
- 18. 構建開發和生產環境-分離配置文件