ASP.NET SPA将TypeScript转换为JavaScript文件

问题描述 投票:0回答:1

我目前正在使用ASP.NET spa和Vue.js创建一个项目。我从Vue.js clientapp静态提供dist文件夹,这是clientapp / src(所有.Vue文件和.ts文件所在的位置)编译的结果。我目前正在使用以下代码作为startup.cs的设置:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public static void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
        services.AddSpaStaticFiles(configuration => configuration.RootPath = "ClientApp/dist");
        services.AddMvc();

        // Add Singletons for the providers
        services.AddSingleton<IActionDescriptorChangeProvider>(ControllerChangeProvider.Instance);
        services.AddSingleton(ControllerChangeProvider.Instance);

    }

    public static void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseRouting();
        app.UseSpaStaticFiles();
        app.UseAuthorization();
        app.UseEndpoints(endpoints => endpoints.MapControllers());
        app.UseSpa(spa => { });
    }
}

。csproj:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <RootNamespace>MyApplication</RootNamespace>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis" Version="3.5.0" />
    <PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.0.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
    <PackageReference Include="System.Text.Json" Version="4.7.1" />
    <PackageReference Include="VueCliMiddleware" Version="3.0.0" />
  </ItemGroup>

</Project>

现在,我的/ src中有一些TypeScript文件(例如/router/index.ts),当在dist文件夹上运行静态服务器(例如,使用npm)时,它们可以正常工作。但是,由于某些原因,当我使用ISS运行ASP.NET服务器时,它会将.ts文件转换为/ src文件夹中的.js文件。这不会影响任何内容,但是它是不必要的代码。

我已经在应用程序的最开始处(主方法)使用断点运行了我的应用程序,并且在主方法之前进行了转储。所以我认为这是一个配置。

所以我的问题是,是否可以选择在asp.net中关闭.ts文件的编译?

欢迎任何反馈,我将在需要时提供任何必要的信息。 :)

asp.net asp.net-mvc typescript vue.js single-page-application
1个回答
0
投票

出于某种原因,我没有Vue模板,所以我使用了React模板-步骤应该相同。

asp.net核心的构建过程在.csproj文件中连接在一起。从中删除以下部分,以在构建应用程序时禁止自动构建spa。

  <Target Name="DebugEnsureNodeEnv" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)node_modules') ">
    <!-- Ensure Node.js is installed -->
    <Exec Command="node --version" ContinueOnError="true">
      <Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
    </Exec>
    <Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. To continue, please install Node.js from https://nodejs.org/, and then restart your command prompt or IDE." />
    <Message Importance="high" Text="Restoring dependencies using 'npm'. This may take several minutes..." />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
  </Target>

  <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" />

    <!-- Include the newly-built files in the publish output -->
    <ItemGroup>
      <DistFiles Include="$(SpaRoot)build\**" />
      <ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
        <RelativePath>%(DistFiles.Identity)</RelativePath>
        <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
        <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
      </ResolvedFileToPublish>
    </ItemGroup>
  </Target>

第一部分确保NodeJs实际上已经安装,而第二部分调用npm脚本。

© www.soinside.com 2019 - 2024. All rights reserved.