我设法通过利用html-webpack-plugin、 pug -loader和copy-webpack-plugin来完成这项工作。
1) 确保在您的顶部需要 2 个插件webpack.config.js
:
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
2)然后在您的文件夹中html-webpack-plugin
引用您的index.pug
模板/src
:
plugins: [
new HtmlWebpackPlugin({
template: './src/index.pug',
})
]
3)然后让pug-loader
模块处理您的.pug
文件:
module: {
rules: [
{
test: /\.pug$/,
use: 'pug-loader'
}
]
}
4) 然后使用copy-webpack-plugin
将已编译的 HTML 文件从您/dist
的文件夹移动到/Views/
Visual Studio 将要引用的文件夹中。将文件名更改为您喜欢的任何名称,并将文件扩展名更改为.cshtml
:
plugins: [
new CopyWebpackPlugin([
{
from: 'dist/index.html',
to: '../../Views/Shared/_layout.cshtml'
}
])
]
5)您可以将模板中的任何剃刀语法.pug
视为带有|
符号的纯文本:
| @Html.AntiForgeryToken();
可能有一种更清洁的方法,但是一旦设置它就非常简单并且现在适用于我的项目,因为我们过渡到更清洁的前端流程。