Typescript compiler recreates entire directory structure for AMD project

We have an AMD Typescript project, its directory structure is similar to this:

project_root/ /scripts /ts module.ts /js (generated from the TS files) /tests /ts moduleTest.ts (imports module.ts) /js (generated from the TS files) 

The problem is when we compile a file that imports another module (file) from a directory that is not a descendant of the importer's directory, the TS compiler reconstructs the entire tree from the direct common parent of the two directories, js .

So, for example, compiling tests/ts/moduleTest.ts (which imports scripts/ts/module.ts ) into the tests/js directory will give this directory tests/js :

 project_root/tests/js /scripts/ts module1.js /tests/ts module1test.js 

instead of just module1test.js in project_root/tests/js . In real life, this will be even worse, since module1.ts itself will import more modules from subdirectories, all of which will be created in project_root/tests/js .
In addition to importing created .js files and links to .d.ts files, not importing .ts files. Is there a solution to create an entire tree? Preferably, is there a way to tell the compiler not to compile the imported TS files, but to use them only as a reference?

I made a basic example / test repository to use as a playground. To find out what I'm talking about, from the root of the project, run:

 tsc --module amd -t ES5 --outDir tests/js/ tests/ts/module1spec.ts 

This project uses requireAdapters.d.ts to avoid using relative module names in the import statement (in other words, it translates TS names and require.js).

+6
source share
2 answers

Is there a solution to create an entire tree? Preferably, is there a way to tell the compiler not to compile the imported TS files, but to use them only as a reference?

You can use the recently added rootDir option rootDir See https://github.com/Microsoft/TypeScript/pull/2772

+1
source

Why not move on from this structure? Having js and ts folders sounds like an error. You should have scripts and tests next to each other and a dist folder in which javascript results files will be stored.

Thus, you separate the source code from the "binary code" (Javascript) in the same way as when compiling C ++ in exe. the dist folder will also save the source maps and get a mini-code.

This is also good practice for version control, just add the dist folder to your .gitignore file.

To summarize: go to a better structure.

+1
source

Source: https://habr.com/ru/post/987136/


All Articles