I always wondered how I can properly add the clientsided javascript in my express project. I use Typescript and I would also like to take advantage of the Typescript typings (for jquery for instance) when writing my clientside javascripts.
My project structure looks like this:
- root
- dist
- src
- helpers
- models
- registration
- router.ts
- form.pug
- profile
- router.ts
- profile.pug
- wwwroot
- css
- js
- images
What I have done until today:
I created all clientsided javascript files in wwwroot/js (e.g. jquery.min.js, registration-form.js) and I loaded them into the header of the appropriate pages.
Disadvantages:
- I had to write ES5 javascript which is compatible with the browsers we would like to support
- I couldn't put the javascript files where they logically belong to (e. g. I'd rather put my
registration-form.jsintosrc/registration/instead of the wwwroot) - No Typescript possible :(. No typescript typings, no transpiling to ES5 etc.
In some tutorials I saw they would simply run npm install --save jquery and import it in their clientsided files. So I feel like I must have missing some pretty important stuff, but I couldn't find any tutorials about it.
My question:
What is the "right way / best practice" to write clientsided javascript in Typescript / Express applications (which should also elliminate also the mentioned disadvantages)?