Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

typescript

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. 
like image 255
Jasper Schulte Avatar asked Jun 21 '17 13:06

Jasper Schulte


People also ask

How does TSC compiler work?

The TypeScript compiler compiles these files and outputs the JavaScript with . js extension by keeping the same file name as the individual input file. The TypeScript compiler also preserves the original file path, hence the . js output file will be generated where the input file was in the directory structure.

Which of the following command is used to compile a TypeScript file?

As you know, TypeScript files can be compiled using the tsc <file name>. ts command.


2 Answers

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.

like image 195
Bram Avatar answered Oct 05 '22 13:10

Bram


I was in a similar need for making sure the project will compile using lint-staged. If the combination of tsconfig.json and file paths were allowed, you could use "tsc" directly as a lint-staged command.

As a workaround I created the following helper script scripts/tsc-lint.sh:

#!/bin/bash -e  TMP=.tsconfig-lint.json cat >$TMP <<EOF {   "extends": "./tsconfig.json",   "include": [ EOF for file in "$@"; do   echo "    \"$file\"," >> $TMP done cat >>$TMP <<EOF     "unused"   ] } EOF tsc --project $TMP --skipLibCheck --noEmit 

Then the lint-staged config contains:

  "lint-staged": {     "{src,tests}/**/*.ts": [       "scripts/tsc-lint.sh"     ]   }, 

The file .tsconfig-lint.json is added to .gitignore.

like image 20
Sampo Avatar answered Oct 05 '22 14:10

Sampo