How to configure tsconfig.json to output files from multiple source folders to single flat outDir?

女生的网名这么多〃 提交于 2019-12-01 07:57:24

问题


I have multiple typescript projects (e.g. client and server), which share some common functionality (located in a common folder). Consider this folder structure:

+ client
|  - tsconfig.json
|  + src
|     - client.ts
+ common
   + src
      - util.ts

Where the client.ts imports the utility this way:

import { Util } from '../../common/src/util';

For brevity I am omitting the server folder, which is at the same level as client. After the transpilation into the out folder, I would like to get this simple flat folder structure:

+ client
|  + out
|     - client.js
|     - util.js

... and ideally I could simply import the common utility agnostic of the actual folder:

import { Util } from './util';

How do I configure the tsconfig.json to achieve that?

I tried different combinations of rootDirs, baseUrl and paths, but I am still getting the client and common subfolders (both even including their src subfolder) under out.


回答1:


And now we have the support for project references in Typescript 3.0 (i.e. cascade build of multiple Typescript projects). We can also control the output location of the tsc builds.

https://blogs.msdn.microsoft.com/typescript/2018/07/30/announcing-typescript-3-0/#project-references

If a Typescript project 'bar' uses project 'foo', here is how you chain them:

// ./src/bar/tsconfig.json
{
    "compilerOptions": {
        // Needed for project references.
        "composite": true,
        "declaration": true,

        // Other options...
        "outDir": "../../lib/bar",
        "strict": true, "module": "esnext", "moduleResolution": "node",
    },
    "references": [
        { "path": "../foo" }
    ]
}

Thanks, TS guys!




回答2:


I've solved this problem in my current project (which has the same setup) by putting a link to the common lib into both sources.

common/
  src/
    lib/
client/
  src/
    lib/ -> $PROJECT_PATH/common/src/lib/
server/
  src/
    lib/ -> $PROJECT_PATH/common/src/lib/

Compilation and bundling become transparent as the tools treat that directory as a local one.




回答3:


As of now, you can't.

I am in a situation similar to yours, I have looked around but it seems like there is no way to change this behaviour with TypeScipt configuration.

The best you can do is use something else (a Grunt task, a shell script, ...) for moving the js files and deleting the generated folders, after every build.



来源:https://stackoverflow.com/questions/46995801/how-to-configure-tsconfig-json-to-output-files-from-multiple-source-folders-to-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!