For an ASP.NET Core 2.x web application, I'd have expected this to be super easy as its a standard pattern. I'm clearly doing this very wrong.
I have this folder structure.
projectRoot
├── node_modules
│ ├── jquery
│ └── // others you'd see in node_modules
├── wwwroot
│ ├── lib
│ │ ├── jquery
│ | │ └── dist
| │ | │ └── jquery.js
│ │ └── // others copied by specific Gulp tasks from node_modules
│ ├── js
│ │ └── site.js // generated from site.ts
│ ├── css
│ └── page.html // script tag to /js/site.js
├── ts
│ └── site.ts
├── gulpfile.js
└── tsconfig.json
Now my problem is that in my site.ts
I need to import from jquery
// site.ts
import $ from "???"
But I cannot figure out the path, considering:
It's eventually written into
site.js
It must be reachable from the browser.
It must be resolvable by the TypeScript compiler.
The TypeScript compiler appears to use
node_modules\@types\jquery
Here's my tsconfig.json
. Note that I use a Gulp task to compile my .ts
files though I do not add any extra configuration, so it seems to use the json
below.
{
"compilerOptions": {
"module": "es2015",
"baseUrl": "./wwwroot/js/", // This must be specified if "paths" is.
"paths": {
"*": [ "./node_modules/@types/*" ],
"../lib/jquery/dist/jquery.js": [ "./node_modules/jquery/dist/jquery" ] // This mapping is relative to "baseUrl"
},
"esModuleInterop": true,
"moduleResolution": "node"
}
}
I've been playing with baseUrl
and paths
to get it working.
Either TypeScript resolves it but its a bad path once in the browser - I assume this is because Chrome can't find the path:
import $ from "lib/jquery/dist/jquery";
Uncaught SyntaxError: Unexpected identifier
Or TypeScript doesn't resolve it and it doesn't compile.
Again, I'd have assumed this wasn't an unusual setup.