如何通过Microsoft.typescript.msbuild将Visual Studio复制JS文件复制JS文件?

发布于 2025-01-31 18:44:36 字数 1788 浏览 4 评论 0 原文

我在 Visual Studio 2022 中有一个非WEB项目(WPF),UI使用HTML/CSS/JavaScript。我也想在“构建”上编译打字稿,然后将编译的JS文件复制到构建文件夹中。

这是我的 tsconfig.json 文件,因此它将 scripts 文件夹中的文件编译到 webapp \ js 文件夹中(脚本文件夹不会复制,并且是不是Neccessary)

{
    "compileOnSave": true,
    "compilerOptions": {
        "outDir": "WebApp/js/",
        "rootDir": "Scripts/"
        // Other options
    },
    "exclude": [
        "WebApp/"
    ]
}

这是我的 csproj 文件,以包含并复制 webApp 文件夹中的所有文件(请注意 tsconfig.json 是内容,所以> Microsoft.typescript.msbuild 遵循该文件中的说明):

    <ItemGroup>
        <None Include="WebApp\**">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </None>
    </ItemGroup>

    <ItemGroup>
        <Content Include="tsconfig.json" />
    </ItemGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.TypeScript.MSBuild" Version="4.6.4">
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
            <PrivateAssets>all</PrivateAssets>
        </PackageReference>
    </ItemGroup>

    <!-- Other items -->

问题是,显然,在 typescript构建之前,副本已执行,因此除非我两次构建项目,否则我的文件永远不会复制。

我尝试添加a 任务以及 AfterTargets 设置为 build> build ,但即使是复制的文件也在打字稿汇编之前。

如何在复制之前制作打字稿汇编任务?

I have a non-web project (WPF) in Visual Studio 2022 and the UI uses HTML/CSS/Javascript. I want to compile TypeScript on built as well and copy the compiled JS files into built folder.

This is my tsconfig.json file so it compile files in Scripts folder into WebApp\js folder (Scripts folder won't be copied and is not neccessary)

{
    "compileOnSave": true,
    "compilerOptions": {
        "outDir": "WebApp/js/",
        "rootDir": "Scripts/"
        // Other options
    },
    "exclude": [
        "WebApp/"
    ]
}

This is my csproj file to include and copy all files in WebApp folder (note that tsconfig.json is Content so Microsoft.TypeScript.MSBuild follows the instruction in that file):

    <ItemGroup>
        <None Include="WebApp\**">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </None>
    </ItemGroup>

    <ItemGroup>
        <Content Include="tsconfig.json" />
    </ItemGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.TypeScript.MSBuild" Version="4.6.4">
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
            <PrivateAssets>all</PrivateAssets>
        </PackageReference>
    </ItemGroup>

    <!-- Other items -->

The problem is, apparently the copy is executed before the TypeScript build so my files are never copied unless I build the project twice.

I tried adding a <Copy> task as well with AfterTargets set to Build but even so the copied files are before TypeScript compilation.

How do I make TypeScript compilation task to run before the copy?

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

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

发布评论

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

评论(1

蘸点软妹酱 2025-02-07 18:44:36

我发现我创建的目标应该具有 aftertargets , compileTypescriptwithtsconfig 值。由于运行 dotnet build -verbosity:正常获得了实际目标的所有名称。

    <Target Name="CopyTypeScriptCompiledFiles" AfterTargets="CompileTypeScriptWithTSConfig">
        <ItemGroup>
            <CompiledTypeScriptJs Include="WebApp\js\**" />
        </ItemGroup>
        
        <Copy SourceFiles="@(CompiledTypeScriptJs)"
              DestinationFiles="$(OutputPath)WebApp\js\%(RecursiveDir)%(Filename)%(Extension)" />
    </Target>

我还在复制的 sourcefiles 属性之前使用了错误的符号( $ 而不是@),以便没有复制文件。

I found out the target I create should have AfterTargets with CompileTypeScriptWithTSConfig value. I got the value thanks to running dotnet build -verbosity:normal to get all the name of actual targets.

    <Target Name="CopyTypeScriptCompiledFiles" AfterTargets="CompileTypeScriptWithTSConfig">
        <ItemGroup>
            <CompiledTypeScriptJs Include="WebApp\js\**" />
        </ItemGroup>
        
        <Copy SourceFiles="@(CompiledTypeScriptJs)"
              DestinationFiles="$(OutputPath)WebApp\js\%(RecursiveDir)%(Filename)%(Extension)" />
    </Target>

I also used a wrong symbol ($ instead of @) in the Copy's SourceFiles attribute before so no file got copied.

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