Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference a TypeScript file from a web page

Preamble

I can get this to work but I'm not sure I'm using the technology as Microsoft intended.

Context

A web page can refer to a JS file containing JavaScript by means of a reference tag like this:

<script src="Scripts/jquery.js"></script>

In the above example, the JS file is part of a Visual Studio ASPX.NET project. It was both placed in the project Scripts folder and added to the project by Nuget.

Having added a Typescript file to an existing ASP.NET project, I would like to use the generated JS from an ASPX web page.

Instructing Solution Explorer to show all files reveals that the expected JS file has indeed been generated on the expected path.

Am I using this correctly? What is the recommended method?

There are a couple of ways one might go about things.

  • Refer directly to the generated JS file

I was concerned that files not part of a project would be excluded from a web server deployment, but have since determined that they are deployed.

  • Add some kind of reference to the Typescript file

It would be great if there were some syntax for a reference to the TS file that will be transformed into a script tag referring to the generated JS file. Alas, if this exists it's not very well publicised.

like image 231
Peter Wone Avatar asked Oct 18 '25 15:10

Peter Wone


1 Answers

In VS2015, dragging the TS file onto an HTML or ASPX page in the editor will insert a link tag referring to the corresponding JS file.

DO NOT use server URL rewriting to remap references to the TS file to the JS file. This interferes with VS and browser debug support via source map files.

like image 132
Peter Wone Avatar answered Oct 21 '25 03:10

Peter Wone



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!