当我尝试推送我的 react .net 项目时,npx vite build失败了

有问题的错误信息:

There was an error exporting the HTTPS developer certificate to a file.
error during build:
Error: Could not create certificate.
    at file:///D:/a/1/s/name.client/vite.config.ts.timestamp-1730192939030-060bc963903a.mjs:24:11
    at ModuleJob.run (node:internal/modules/esm/module_job:234:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:473:24)
    at async loadConfigFromBundledFile (file:///D:/a/1/s/name.client/node_modules/vite/dist/node/chunks/dep-CDnG8rE7.js:66634:15)
    at async loadConfigFromFile (file:///D:/a/1/s/name.client/node_modules/vite/dist/node/chunks/dep-CDnG8rE7.js:66475:24)
    at async resolveConfig (file:///D:/a/1/s/name.client/node_modules/vite/dist/node/chunks/dep-CDnG8rE7.js:66083:24)
    at async build (file:///D:/a/1/s/name.client/node_modules/vite/dist/node/chunks/dep-CDnG8rE7.js:65180:18)
    at async CAC.<anonymous> (file:///D:/a/1/s/name.client/node_modules/vite/dist/node/cli.js:828:5)

管道:

pr:
  - Development
  - master

pool:
  vmImage: "windows-latest"

variables:
  buildConfiguration: "Release"
  buildPlatform: "any cpu"

steps:
  - checkout: self
    fetchDepth: 0
  - task: NuGetToolInstaller@1

  - task: UseDotNet@2
    inputs:
      packageType: 'sdk'
      version: '8.x' # Ensure this matches your project's .NET version
      installationPath: $(Agent.ToolsDirectory)/dotnet
    displayName: 'Install .NET SDK'

  - task: NuGetCommand@2
    displayName: "NuGet restore"
    inputs:
      restoreSolution: "name.sln"

  - task: SonarCloudPrepare@1
    inputs:
      SonarCloud: "namename"
      organization: "name-test1"
      scannerMode: "MSBuild"
      projectKey: "namenamepass"
      projectName: "name"


  - task: VSBuild@1
    displayName: 'Build solution **\*.sln'
    inputs:
      solution: "name.sln"
      platform: "$(BuildPlatform)"
      configuration: "$(BuildConfiguration)"

  - task: VSTest@2
    displayName: "VsTest - testAssemblies"
    inputs:
      testAssemblyVer2: |
        **\$(BuildConfiguration)\*Test*.dll
        !**\obj\**
      codeCoverageEnabled: true
      platform: "$(BuildPlatform)"
      configuration: "$(BuildConfiguration)"

  - task: SonarCloudAnalyze@1
    displayName: "Run SonarCloud analysis"

  - task: SonarCloudPublish@1
    displayName: "Publish results on build summary"

vite 配置:

import { fileURLToPath, URL } from "node:url";

import { defineConfig, UserConfig } from "vite";
import plugin from "@vitejs/plugin-react";
import fs from "fs";
import path from "path";
import child_process from "child_process";
import mkcert from "vite-plugin-mkcert";
const isCI = process.env.CI === 'true' || process.env.AZURE_PIPELINE === 'true';
const baseFolder =
    process.env.APPDATA !== undefined && process.env.APPDATA !== "" ? `${process.env.APPDATA}/ASP.NET/https` : `${process.env.HOME}/.aspnet/https`;

//@ts-ignore
const certificateArg = process.argv.map((arg) => arg.match(/--name=(?<value>.+)/i)).filter(Boolean)[0];
const certificateName = certificateArg ? certificateArg.groups.value : "name.client";

if (!certificateName) {
    console.error("Invalid certificate name. Run this script in the context of an npm/yarn script or pass --name=<<app>> explicitly.");
    process.exit(-1);
}

const certFilePath = path.join(baseFolder, `${certificateName}.pem`);
const keyFilePath = path.join(baseFolder, `${certificateName}.key`);

if (!fs.existsSync(certFilePath) || !fs.existsSync(keyFilePath)) {
    if (
        0 !==
        child_process.spawnSync("dotnet", ["dev-certs", "https", "--export-path", certFilePath, "--format", "Pem", "--no-password"], {
            stdio: "inherit",
        }).status
    ) {
        throw new Error("Could not create certificate.");
    }
}

const configLocal : UserConfig  = {
    plugins: [plugin(), mkcert()],
    resolve: {
        alias: {
            "@": fileURLToPath(new URL("./src", import.meta.url)),
        },
    },
    server: {
        proxy: {
            "^/weatherforecast": {
                target: "https://localhost:7293/",
                secure: false,
            },
        },
        port: 5173,
        https: {
            key: fs.readFileSync(keyFilePath),
            cert: fs.readFileSync(certFilePath),
        },
    },
}
const configServer : UserConfig  = {
    plugins: [plugin(), mkcert()],
    resolve: {
        alias: {
            "@": fileURLToPath(new URL("./src", import.meta.url)),
        },
    },
    server: {
        proxy: {
            "^/weatherforecast": {
                target: "https://localhost:7293/",
                secure: false,
            },
        },
        port: 5173,
    },
}

export default defineConfig(isCI ? configServer : configLocal);


最佳答案
2

当使用 vite + asp.net 项目时我可以重现同样的问题。

该问题可能与管道中使用的.NET8 版本有关。

在您的 Pipeline 定义中,它将使用 .net8 版本:8.0.403(通过 UseDotNet 任务设置版本)。创建证书时,如果该.aspnet/https文件夹不存在,则不会自动创建。

为了解决此问题,您可以将.Net 版本降级到8.0.402

例如:

steps:
  - checkout: self
    fetchDepth: 0
  - task: NuGetToolInstaller@1

  - task: UseDotNet@2
    inputs:
      packageType: 'sdk'
      version: '8.0.402' # Ensure this matches your project's .NET version
      installationPath: $(Agent.ToolsDirectory)/dotnet
    displayName: 'Install .NET SDK

结果:

有关更多详细信息,您可以参考此票:

1

  • 太好了,谢谢!


    – 

您可以将以下行添加到 vite.config.ts 文件中以解决此问题 –

fs.mkdirSync(baseFolder, { recursive: true });

在以下语句后添加此内容-

const baseFolder =
env.APPDATA !== undefined && env.APPDATA !== ''
    ? `${env.APPDATA}/ASP.NET/https`
    : `${env.HOME}/.aspnet/https`;