I've read tones of articles, but none of them works. The thing is that - I have a .net core WebApplication with some typescript code (instead of javascript) and:
I want to be able to debug it in Visual Studio
I want to be able to use other modules
I want to be able to call JS functions from withing my html files, for example:
<button onclick="myTypeScriptFunction()">OK</button>
This is my project tree (some folders and files were deleted for clarification):
- wwwroot
+ css
+ imgs
- js <-- these files are created by compiling TypeScript
app.js
ajaxmanager.js
etc...
+ node_modules
- Scripts <-- this is folder with my typescript files
ajaxmanager.ts
app.ts
package.json
tsconfig.json
No, my package.json looks like that:
{
"version": "1.0.0",
"name": "asp.net",
"private": true,
"devDependencies": {
"@types/jquery": "3.5.0",
"@types/bootstrap": "4.5.0",
"@types/jquery-validation-unobtrusive": "3.2.32",
"@syncfusion/ej2": "*",
"autonumeric": "4.6.0"
}
}
My tsconfig.json:
{
"compilerOptions": {
"noImplicitAny": false,
"noEmitOnError": true,
"removeComments": false,
"sourceMap": true,
"target": "ES5",
"outDir": "wwwroot/js",
"esModuleInterop": true,
"module": "CommonJS"
},
"compileOnSave": true,
"include": [
"scripts/**/*"
],
"exclude": [
"node_modules"
]
}
And that is all I have. All I get while loading my website are bunch of errors.
The legendaryL "exports is not defined", but I was able to resolve this by just declaring exports on main page:
<script>var exports = {}</script>
But then I was getting other errors like require is not defined. I have read that this comes from node.js. But I am not using node.js, so why it compiles that way?
On some configurations (I mean "tests" with my tsconfig.json) I was able to run my page, but it wasn't working. For example. I have a button:
<button onclick="btnClick(this)">Click me</button>
<script>
function btnClick(btn)
{
exports.MyCustomPageClass.foo(btn);
}
</script>
MyCustomPageClass is just my class written in typescript. I noticed that TypeScript compiles to JavaScript in such way that it creates objects in exports variable. So this one should work. Instead MyCustomPageClass was undefined. Sometimes (in some configurations) exports had only AutoNumeric object (from AutoNumeric module), sometimes it had some of my classes. Not all of them.
So, right now, I am very close to shooting myself in head. It's been a week since I try to configure everything properly. So, can anyone tell me step after step, what should I do to make it work?
I tried also webpack and gulp. But the errors were rather the same, or none was working.
.tsto.jsbefore you provide to the browser. You can debug with the help of map files so you know which line of.jsmatches which line in.tsfile