如何在.NET 6中使用WebPack?

发布于 2025-02-12 21:12:57 字数 1708 浏览 2 评论 0原文

如何在.NET6 Razor页面中使用WebPack?我在互联网上发现的任何文档都使我更加困惑。

的file1.ts:

export function CCC(): string {
    return "AAAAAA";
}

我的文件

import { CCC } from "./file1"

function AAA(): string {
    return CCC();
}

@page
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

<script>
const str = AAA()
alert(str);
</script>

<!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>

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();

这是我

How can I use webpack in .net6 razor pages? Any documentation I've found on the internet about this just confuses me more.

this is my file1.ts:

export function CCC(): string {
    return "AAAAAA";
}

this is my file.ts:

import { CCC } from "./file1"

function AAA(): string {
    return CCC();
}

this is my Index.cshtml:

@page
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

<script>
const str = AAA()
alert(str);
</script>

this is my 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>

this is my program.cs file:

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();

I hope I have given enough explanation to present my problem

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゃ人海孤独症 2025-02-19 21:12:57

对于WebPack 5和NET 6,请按照以下步骤:

  1. 安装 node.js
  2. 创建ASP.NET Core 6应用程序
  3. 6配置文件

package.json configuration

{
  "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'),
  }
};

文件

{
  "compilerOptions": {
    "outDir": "./dist/",
    "noImplicitAny": true,
    "module": "es6",
    "target": "es5",
    "allowJs": true,
    "moduleResolution": "node"
  }
}

文件 您可以改用JavaScript。

在您的_Layout.cshtml中添加捆绑的脚本:

<script type="text/javascript" src="~/scripts/bundle.js"></script>

现在您可以使用&gt; npm run build命令。

For webpack 5 and net 6 follow these steps:

  1. install node.js
  2. create an asp.net core 6 application
  3. Add the following configuration files

package.json configuration file

{
  "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 file

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 file

{
  "compilerOptions": {
    "outDir": "./dist/",
    "noImplicitAny": true,
    "module": "es6",
    "target": "es5",
    "allowJs": true,
    "moduleResolution": "node"
  }
}

I assume typescript for these configs and you can use javascript instead.

Add bundled scripts in your _Layout.cshtml:

<script type="text/javascript" src="~/scripts/bundle.js"></script>

Now you can bundle project modules using > npm run build command.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文