简体   繁体   English

如何在 Visual Studio React 项目中使用纱线代替 npm?

[英]How to use yarn instead of npm in Visual Studio React project?

I've created a VS 2022 project using the VS out of the box ASP.NET Core with React.js and Redux template.我使用带有 React.js 和 Redux 模板的 VS 开箱即用 ASP.NET 核心创建了一个 VS 2022 项目。

When I publish the project I see in the output window it uses npm install command.当我发布我在 output window 中看到的项目时,它使用npm install命令。

Question问题

How can I modify the build/publish process to use yarn build command instead of npm install ?如何修改构建/发布过程以使用yarn build命令而不是npm install

First, install yarn globally with npm.首先,使用 npm 全局安装 yarn。

npm install --global yarn
yarn --version

The installation method varies by OS depending on Windows and Mac.安装方法因操作系统而异,具体取决于 Windows 和 Mac。

Then, you can run the division that was run with npm with yarn.然后,您可以使用纱线运行使用 npm 运行的分区。 You can run it with 'yarn start', 'yarn bulid', etc. without 'run' such as 'npm run start'.您可以使用 'yarn start'、'yarn bulid' 等来运行它,而无需使用 'run',例如 'npm run start'。

npm run start

if use yarn如果使用纱线

yarn start

In the project's.csproj file I've changed the following lines:在项目的 .csproj 文件中,我更改了以下几行:

<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
    <!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build" />

to:至:

<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
    <!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
    <Exec WorkingDirectory="$(SpaRoot)" Command="yarn" />
    <Exec WorkingDirectory="$(SpaRoot)" Command="yarn build" />

(additionaly I've changed the build process accordingly:) (此外,我已经相应地更改了构建过程:)

<Message Importance="high" Text="Restoring dependencies using 'npm'. This may take several minutes..." />
<Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />

to:至:

<Message Importance="high" Text="Restoring dependencies using 'yarn'. This may take several minutes..." />
<Exec WorkingDirectory="$(SpaRoot)" Command="yarn" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM