Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

typescript - How to compile a specific file with tsc using the paths compiler option

I have several files I want to compile using a script. These files rely on the paths compile option to resolve its modules. As I want to target these files specifically I am bound to supplying them to tsc (as I don't want to create a separate tsconfig.json that targets these files for this task)

I've looked at the option to pass the --path parameter to tsc, but this is not allowed (error TS6064: Option 'paths' can only be specified in 'tsconfig.json' file.)

Can I somehow only compile specific .ts files while using the paths option?

Update (22-06-17)

As per request some specific examples:

The relevant settings in the tsconfig.json file are the following:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "Components/*": [
        "./src/Components/*"
      ]
    }
  }
}

so the relevant part is the paths setting, where you can create shorthands to import files from a certain directory. so you can import a file like import {header} from 'Components/header.ts' in stead of import {header} from '../../Components/header.ts'

But I need to compile specific files from the command line. But if I try:

tsc --project tsconfig.json entryFile.ts

it will give me the error:

error TS5042: Option 'project' cannot be mixed with source files on a command line.

and if I try to provide the paths option to the cli I get the following error:

error TS6064: Option 'paths' can only be specified in 'tsconfig.json' file.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Specifying the input files on the command line voids your tsconfig.json.

When input files are specified on the command line, tsconfig.json files are ignored.

From the TypeScript docs.

So, there's no other option than to use a tsconfig.json with the proper 'include' (or exclude) specification of the files. The good news is that in the same docs you will find:

A tsconfig.json file can inherit configurations from another file using the extends property.

So what you can do is overwrite the include property of your tsconfig with something like this:

{
  "extends": "./tsconfig.json",
  "include": [
      "src/root/index.ts"
  ]
}

So you could make a script that:

  1. Creates a temporary 'extends tsconfig' file with the desired 'include' property
  2. Calls tsc with --project specified as that temporary tsconfig

One could argue that this is very complicated and question why they made the decision to not support this scenario properly. However, the question remains why one would like to compile just one file of a project. While developing, you can use the watch (-w) option to compile changes files, and upon building a full project you'll probably want to compile exactly that, the full project.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...