0
如何在我的HTML模板中替換指向當前塊散列的變量?構建期間如何獲得webpack塊散列?
我的HTML模板看起來是這樣的:
<html>
<head>
<link href="$css" rel="stylesheet">
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
我試圖把它打造這樣的:
<html>
<head>
<link href="app.c824da43.css" rel="stylesheet">
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
我嘗試使用字符串替換[name].[chunkhash:8].css
,但呈現字面上:
<html>
<head>
<link href="[name].[chunkhash:8].css" rel="stylesheet">
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
我的項目:
.
├── dist
│ ├── app.c824da43.css
│ └── index.html
├── html
│ └── index.html
├── package.json
├── sass
│ └── main.scss
└── webpack.config.js
我webpack.config.js
var webpack = require('webpack');
var outdir = __dirname + '/dist';
const ExtractTextPlugin = require("extract-text-webpack-plugin");
module.exports = {
entry: {
app: [
'./sass/main.scss',
'./html/index.html',
]
},
output: {
path: outdir,
filename: '[name].[chunkhash:8].js'
},
module: {
rules: [
{
test: /\.s[ac]ss$/,
use: ExtractTextPlugin.extract({
use: ['css-loader', 'sass-loader']
})
},
{
test: /\.html$/,
use: [
{
loader: "file-loader",
options: {
name: "/[name].[ext]",
},
},
{
loader: 'string-replace-loader',
query: {
search: /\$css/,
// What do I need to put here?
replace: '/[name].[chunkhash:8].css'
}
},
{
loader: "extract-loader",
},
{
loader: 'html-loader',
options: {
minimize: true,
removeComments: true,
collapseWhitespace: true
}
}
]
}
]
},
plugins: [
new ExtractTextPlugin("[name].[chunkhash:8].css"),
]
};
我package.json
{
"devDependencies": {
"css-loader": "^0.28.4",
"extract-loader": "^0.1.0",
"extract-text-webpack-plugin": "^2.1.0",
"file-loader": "^0.11.1",
"html-loader": "^0.4.5",
"node-sass": "^4.5.3",
"sass-loader": "^6.0.5",
"string-replace-loader": "^1.2.0",
"webpack": "^2.6.1"
}
}
也許這會幫助:https://github.com/webpack/webpack/tree/master/examples/multiple-commons-chunks –