【发布时间】:2020-07-27 13:46:37
【问题描述】:
我正在尝试将 css 文件注入到我的 index.html 中。目前我的网络包执行以下操作
- 将我的 SASS 转换为 CSS
- 用哈希命名 css 文件(例如 optionA-32d6cb.css)
- 放入 ./dist 文件夹
文件夹结构的输出:
- Dist
- OptionA
- optionA-hash.css
- OptionB
- optionB-hash.css
- index.html //How to inject above css into this file?
我是 HtmlWebpackPlugin 的新手,我的问题是如何将上述动态 css 文件注入 index.html?所以在头部我有以下内容:
<link href="optionA-32d6cb.css" rel="stylesheet">
<link href="optionB-w2d6cb.css" rel="stylesheet">
我的 webpack.config.js 文件:
module.exports ={
entry: ['path_to_scss_file'],// SASSfile which contains import statment with wildcard to get all sass in given directory
output: {
filename: "[name].min.js",
path: path.resolve(__dirname, 'dist/'),
},
plugins:[
new HtmlWebpackPlugin({
template: './src/index.html'
})
],
module:{
rules: [
{
test: /\.scss$/,
use: [
{
loader: 'file-loader',
options:{
name:'[name]/[name]-[contenthash].css',
}
},
{
loader: 'sass-loader',
options: {
sassOptions: {
importer: globImporter(),
//my sass files uses wildcard to import all sass files
},
},
}
]
}
]
}
【问题讨论】:
标签: webpack html-webpack-plugin