javascript - Webpack 不会将 javascript 注入到 cshtml
问题描述
嗨,我有一个 MVC 5 项目,其中包含大量 JavaScript 前端代码。所以我决定模块化这些 javascript ==> 将它们注入 cshtml 模板 ==> 在我的 View My 配置中渲染部分它们:
webpack.config.js
module.exports = {
entry: {
layout: './Public/js/Layout.js',
index: './ViewModules/Home/Index.js',
noisoisieuam: './ViewModules/KetQua/NoiSoiSieuAm.js'
},
output: {
path: path.resolve(__dirname, './Public/Output'),
filename: '[name].bundle.[contenthash].js',
},
plugins: [
new HtmlWebpackPlugin({
inject: true,
templateContent: '',
filename: '../../../ViewModules/Shared/_Layout.temp.cshtml',
chunks: [ 'layout']
}),
new HtmlWebpackPlugin({
inject: true,
templateContent: '',
filename: '../../../ViewModules/Home/Index.temp.cshtml',
chunks: ['index']
}),
new HtmlWebpackPlugin({
inject: true,
templateContent: '',
filename: '../../../ViewModules/KetQua/NoiSoiSieuAm.temp.cshtml',
chunks: ['noisoisieuam']
})
],
// IMPORTANT NOTE: If you are using Webpack 2 or above, replace "loaders" with "rules"
module: {
rules: [
{
use: [
{
loader: 'babel-loader',
options: {
presets: ["@babel/preset-env"]
}
}
],
test: /\.js$/,
exclude: /node_modules/
},
{
test: /\.json$/,
type: 'javascript/auto',
use: [
{
loader: 'file-loader',
options: {
name: "[path][name].[ext]"
}
}
],
}
]
}
}
这些*.temp.cshtml
文件是空的 cshtml 文件,仅用于 javascript 注入。我各自*.cshtml
(我的观点)中的代码:
...
@Html.Partial("~/ViewModules/<path-to-template>/*.temp.cshtml")
...
问题是只有前 2 个 temp.cshtml 是由 webpack (_Layout
和我的主页)注入的。
npm run build
或在 Visual Studio 中构建不要给出任何错误。那些 javascript 包都创建了。该'noisoisieuam'
块未注入到我的 temp.cshtml
我怎样才能解决这个问题?非常感谢
解决方案
推荐阅读
- linux - 在 /etc/hosts 中使用不同的规范或别名引用相同的 IP
- postgresql - 处理异常和事务
- reactjs - 反应路由器嵌套,一旦有参数就打开新页面
- javascript - 使用 Javascript 数组 sort() 方法和比较函数的字母搜索
- javascript - 从 JSON 子对象计算总和
- rascal - MouseDown 事件时的图形信息
- postgresql - 如何为具有“timestamptz”数据类型的 PostgreSQL 列设置 NULL 值?
- node.js - Express.js 的状态
- html - 如何垂直对齐一行中的元素
- python - 根据登录用户过滤 Django Admin 用户列表