tsc --module always generates the same output no matter the argument
-
12-12-2019 - |
Question
I'm pretty new to Javascript, NodeJS and --obviously-- TypeScript. I'd like to experiment with the compiler services in src/services
(Windows) to provide intellisense, etc. for an editor.
As far as I can tell, I need to be able to require
the TypeScript services code in a NodeJS server and communicate with that from the editor. I couldn't find any NodeJS-specific TypeScript services code, so I think I'd have to compile the services code with --module amd
to make it available to NodeJS via RequireJS.
If the above is correct, my attempts to compile the services code with --module amd
yield exactly the same results as using the default module kind (see below).
$ tsc.cmd --module amd --out amd.js .\languageService.ts
$ tsc.cmd --out comm.js .\languageService.ts
$ diff.exe .\amd.js .\comm.js
$
Solution
This is admittedly confusing, but the code in languageService.ts isn't in a 'module' in the sense of AMD/RequireJS's definition of 'module'.
The spec refers to things inside a module
block as "internal modules". That's what you're seeing in languageService.ts. You can think of them more like containers or singletons that expose a top-level name (looking at the code gen for a simple internal module clarifies this quite a bit). You consume these like regular top-level objects without any module loader at all.
Producing external modules (as named by the spec) for require
is done by putting the export
directive on a top-level declaration in a .ts file (see spec section 9.2.1). There are some samples (e.g. imageboard) available for download that do this if you're interested.
OTHER TIPS
You have to have your modules defined as export module
, not your module
and then compile it with --m amd
or --module amd
.
Took me few hours to realize :)