Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript compiler recreates the whole directory structure for AMD project

Tags:

typescript

amd

We have an AMD Typescript project it's 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 which isn't a descendant of the importer's directory the TS compiler reconstructs the whole tree from the immediate shared parent of the two directories, into the js directory.

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

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

instead of just the module1test.js alone in project_root/tests/js. In real life it would even be worse as module1.tswould itself import more modules from subdirectories, all of them being created into project_root/tests/js.
Apart from importing the generated .js files and referncing the corecponding .d.ts files instead of importing .ts files. Is there a solution to the creation of the entire tree? Preferably, is there's a way to tell the compiler not to compile imported TS files but only use them as reference?

I've made a basic example/testing repository to use as a playground. To see what I'm talking about, from the project root, run:

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

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

like image 917
Lior Avatar asked May 13 '15 13:05

Lior


2 Answers

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

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

like image 144
basarat Avatar answered Sep 20 '22 17:09

basarat


Why not move on from this structure? Having a js and ts folders sounds like a mistake. You should have your scripts and tests folders next to each other, and a dist folder that will hold the result javascript files.

This way, you are separating the source-code from the "binary code" (Javascript) same as you would when compiling a C++ into an exe. the dist folder will also hold the source maps, and get minified.

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

To sum it up: move to a better structure.

like image 32
gilamran Avatar answered Sep 22 '22 17:09

gilamran