Leverage the TypeScript compiler's output from a .NET library within a Blazor application by referencing it

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), an error related to Uint8Array iteration surfaced due to the requirement for at least es2015, despite setting "target" to es2020 in the tsconfig.json – implying potential misalignment with the tsconfig file?

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?

Answer №1

After exhaustive research across various websites and experiments, I finally cracked the code:

When accessing a file from a library, it won't be located at /myFile.js, but rather at /_content/My.Lib/myFile.js, following the standard practice for serving static files from libraries.
This principle also holds true when the library is integrated as a NuGet package.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I retrieve the express Application within a REST API?

After reviewing Massive's documentation and learning that saving the connection object to Express's application settings can help reduce database connection execution time, I encountered a problem. How can one access the Express app variable when ...

Different ways to separate an axios call into a distinct method with vuex and typescript

I have been working on organizing my code in Vuex actions to improve readability and efficiency. Specifically, I want to extract the axios call into its own method, but I haven't been successful so far. Below is a snippet of my code: async updateProf ...

Retrieve component information from the service

Incorporated within my component is a form that I'm utilizing with reactive form and my intention is to reach this form through my service. For example: const id = MyComponent.id and inside my component: @Output: public id: number = 7; ...

Parent observable method encountering an error while grouping multiple HTTP calls in Angular

I am facing an issue with managing multiple http calls and handling errors that may occur during their execution. I want to be able to identify which calls have failed so that I can retry them using a different method. However, without visibility at the co ...

Share a callback function with child components via props

My child container defines Ownprops like this: export interface OwnProps { prop1: string; prop2: "callback function" } I want to pass a callback function from the parent to this child in order to trigger a parent function from the child. However ...

Improving Image Loading Efficiency in Next.js - Enhancing Performance by Preloading Images

Currently, I am working on a project in Next.js that involves a scroll-based image loading component. The method I am using to fetch and load images dynamically based on the scroll position is causing performance problems and leading to a less than ideal u ...

Angular Form Validations - input values must not match the initial values

These are my current reactive form validations: ngOnInit(): void { this.userForm = this.formBuilder.group({ status: {checked: this.selectedUser.status == 1}, username: [this.selectedUser.username, [Validators.required, Validators.minLeng ...

Determine in React whether a JSX Element is a descendant of a specific class

I am currently working with TypeScript and need to determine if a JSX.Element instance is a subclass of another React component. For instance, if I have a Vehicle component and a Car component that extends it, then when given a JSX.Element generated from ...

Utilizing variables for Protractor command line parameters

I am struggling to make variables work when passing parameters as a string in my code. Conf.ts params: { testEnvironment: TestEnvironment.Prod, }, env.ts export enum TestEnvironment { Dev = 'dev', QA = 'qa', Prod ...

Facing a challenge with handling HTTP data in a TypeScript-based Angular web application

I am currently working on developing a web application using Angular and the SpringMVC Framework. One of the tasks I'm facing is loading a list of users (referred to as "consulenti" in the code). While the backend HTTP request works fine, I encounter ...

Adjusting table to include hashed passwords for migration

How can I convert a string password into a hash during migration? I've tried using the following code, but it seems the transaction completes after the selection: const users = await queryRunner.query('SELECT * FROM app_user;'); user ...

What is the best method for comparing arrays of objects in TypeScript for optimal efficiency?

Two different APIs are sending me arrays of order objects. I need to check if both arrays have the same number of orders and if the values of these orders match as well. An order object looks like this: class Order { id: number; coupon: Coupon; customer ...

Guide: Implementing material-ui theme with redux in gatsby

I'm currently utilizing the material-ui theme in conjunction with redux-toolkit within a gatsby project. This is my theme.ts file: import { createMuiTheme } from "@material-ui/core"; import { useSelector } from "react-redux"; import { State } from ". ...

Confirm your identity without using a Single Sign-On (SSO) system

My current situation involves a PHP-based website where users login using credentials stored in a database. Additionally, we have another SPA website with .NET CORE as the API layer. Unfortunately, we do not have the option of utilizing a central authent ...

Encountering a Typescript issue when linking a class component with the reducer

Within my class component that is linked to the redux rootReducer, I am encountering a TypeScript error specifically related to the mapPropsToState section. The error message reads: Property 'unit' does not exist on type 'DefaultRootState&ap ...

Is there a way for me to loop through an object without prior knowledge of its keys?

Upon receiving data from the server, it looks something like this: { "2021-10-13": { "1. open": "141.2350", "2. high": "141.4000", "3. low": "139.2000", "4. close& ...

Leveraging Global Variables in TypeScript with Node Express

Having issues using global variables in node express when working with typescript. Specifically, trying to use the same global array variable tokenList:tokList across multiple modules and middleware, but haven't been successful so far. Any suggestions ...

The timer functionality in the Angular 2+ component is malfunctioning

This situation is quite perplexing. I have a timer function that works perfectly on all my components except one. Strangely, I can't seem to figure out why this particular component is not cooperating, especially since there are no error messages appe ...

A guide on harnessing the power of Jest when integrating it with vuex-module-decor

In my Typescript-written Vue component, I am facing an issue while trying to write a unit test using vue-test-utils and jest. The problem arises when injecting the vuex store that was created with vuex-module-decorators. Below is a snippet from my Vue com ...

Incorporating a CSS Module into a conditional statement

Consider the following HTML structure <div className={ `${style.cell} ${cell === Player.Black ? "black" : cell === Player.White ? "white" : ""}`} key={colIndex}/> Along with the associated CSS styles .cell { ...