I am currently facing an issue with three different levels:
Main Issue: I have developed a Blazor WebAssembly (WASM) application that requires JavaScript, but I prefer to use TypeScript. To address this, I have added a tsconfig file and the TypeScript code from the wwwroot folder is now compiled into JavaScript and included in the wwwroot output. This enables me to load it within the WASM running in the browser.
Library Problem: Here comes the first hurdle
My intention is to relocate the TypeScript and its associated module to a separate library project.
I proceeded by creating a new class library project, establishing a wwwroot folder there, moving the ts file to the wwwroot directory, relocating the tsconfig file to the root of the new library, installing Microsoft.JSInterop within the library, and referencing it from the Blazor app.
Regrettably, the js file isn't being included in the app's output and figuring out the next steps remains uncertain. Should a tsconfig be incorporated into the app? It seems illogical, as the app should simply utilize all resources provided by the library; the onus should lie on the library to offer functionalities without necessitating compilation by the app.
Development Update:
Initially overlooked adding the Microsoft.TypeScript.MSBuild
package to the library project. Upon adding it and including
<TypeScriptCompile Include="wwwroot\myFile.ts" />
(mimicking the original Blazor WASM project setup),Continued Progress:
To ensure successful compilation of TypeScript to JavaScript in the library, it must be designed as a Razor Class Library (.Sdk.Razor). Consequently, the ts now compiles to js. The copy property was then configured to PreserveNewest, as advised in the linked answer below. Subsequently, the js file is now effectively integrated into the app's output.
New Challenge:
Encountering a 404 error as the Blazor WASM struggles to load the integrated JavaScript file.
Future Plans: Into NuGet Package Territory
Once resolved, the goal is to distribute the library as a NuGet package, simplifying installation for other Blazor WebAssembly projects sans the need for a tsconfig or manual inclusion of the js file.
An alternative solution on Stack Overflow guided the OP to designate the js file as "content" within the csproj file and instruct nuget to transfer said file to the app's wwwroot during installation. Yet, no direct js file exists in my library, solely the ts file which converts to js post-compilation. Where exactly should I specify the js file copying process when leveraging NuGet?