如何在.net6 razor页面中使用webpack ?我在网上找到的任何关于这个的文档都让我更困惑。
这是我的文件:
export function CCC(): string {
return "AAAAAA";
}
这是我的文件。
import { CCC } from "./file1"
function AAA(): string {
return CCC();
}
this is my index .
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
<script>
const str = AAA()
alert(str);
</script>
这是我的layout.cshtml:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - TypeScriptLab5</title>
<link rel="stylesheet" href="~/TypeScriptLab5.styles.css" asp-append-version="true" />
<script src="~/ts/file1.js"></script>
<script src="~/ts/file.js"></script>
</head>
<body>
@RenderBody()
<script src="~/lib/jquery/dist/jquery.min.js"></script>
@await RenderSectionAsync("Scripts", required: false)
</body>
</html>
这是我的program.cs文件:
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.Run();
我希望我已经给出了足够的解释来提出我的问题
对于webpack 5和net 6,请遵循以下步骤:
- 安装node . js
- 创建asp.net core 6应用程序
- 添加以下配置文件
包。json配置文件
{
"name": "app_name",
"version": "1.0.0",
"description": "here is my app description",
"private": true,//make packages private and prevent an accidental publish of your code
"scripts": {
"build": "webpack --mode=development",
},
"devDependencies": {
"webpack": "^5.78.1",
"webpack-cli": "^5.0.1",
"ts-loader": "^9.2.5",
"typescript": "^4.4.3",
},
"dependencies": {
}
}
webpack.config.js文件const path = require('path');
module.exports = {
entry: './wwwroot/scripts/app.js',
module: {
rules: [
{
exclude: /node_modules/,
test: /.tsx?$/,
use: 'ts-loader',
},
],
},
resolve: {
extensions: ['.js','.tsx', '.ts'],
},
output: {
library: {
name: 'app_name',
type: 'var'
},
filename: 'bundle.js',
path: path.resolve(__dirname, '../scripts'),
}
};
tsconfig。json文件
{
"compilerOptions": {
"outDir": "./dist/",
"noImplicitAny": true,
"module": "es6",
"target": "es5",
"allowJs": true,
"moduleResolution": "node"
}
}
我假设这些配置是typescript
,你可以使用javascript代替。
在_Layout.cshtml
中添加捆绑脚本:
<script type="text/javascript" src="~/scripts/bundle.js"></script>
现在可以使用> npm run build
命令捆绑项目模块。