Skip to content

Commit 3faaa20

Browse files
committed
Update docs for tsconfig.
1 parent 8c23523 commit 3faaa20

File tree

1 file changed

+47
-10
lines changed

1 file changed

+47
-10
lines changed

README.md

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44

55
## TypeScript Compilation Task for GruntJS
66

7-
Grunt-ts is an npm package that handles TypeScript compilation work in GruntJS build scripts. It provides a [Grunt-compatible wrapper](#support-for-tsc-switches) for the `tsc` command-line compiler, and provides some [additional functionality](#grunt-ts-gruntfilejs-options) that improves the TypeScript development workflow. Grunt-ts even supports compiling against a [Visual Studio project](#vs) directly. Grunt-ts is itself written in [TypeScript](./tasks/ts.ts).
7+
Grunt-ts is an npm package that handles TypeScript compilation work in GruntJS build scripts. It provides a [Grunt-compatible wrapper](#support-for-tsc-switches) for the `tsc` command-line compiler, and provides some [additional functionality](#grunt-ts-gruntfilejs-options) that improves the TypeScript development workflow. Grunt-ts supports compiling against [tsconfig.json](#tsconfig) or even a [Visual Studio project](#vs) directly. Grunt-ts is itself written in [TypeScript](./tasks/ts.ts).
88

99
### Latest Changes
10-
Latest test release is `5.0.0-beta.1` which includes tsconfig support.
11-
Current major release is 4.2, which includes TypeScript 1.5.
10+
Latest test release is `5.0.0`, which includes tsconfig.json and TypeScript 1.5 support, among many other improvements.
1211

1312
[Full changelog is here](CHANGELOG.md).
1413

@@ -993,47 +992,85 @@ grunt.initConfig({
993992

994993
#### tsconfig
995994

996-
Grunt-ts supports using a tsconfig.json file. Here are some example Gruntfile initConfig sections:
995+
Grunt-ts can integrate with a `tsconfig.json` file in three ways which offer different behavior:
996+
* As a `boolean`: simplest way for default behavior.
997+
* As a `string`: still uses defaults, but allows specifying a specific path to the `tsconfig.json` file or the containing folder.
998+
* As an `object`: allows detailed control over how grunt-ts works with `tsconfig.json`
999+
1000+
**When specifying tsconfig as a boolean**
1001+
In this scenario, grunt-ts will use all settings from the `tsconfig.json` file in the same folder as `Gruntfile.js`.
1002+
* If a `filesGlob` property is present in the `tsconfig.json` file:
1003+
* It will be evaluated, and any identified files will be added to the compilation context.
1004+
* If a `files` property is present, it will be modified with the result from evaluating the `filesGlob` that is present **inside** `tsconfig.json` (the `files` element will **not** be updated with the results from any glob inside `Gruntfile.js`).
1005+
* If `exclude` is present, it will be ignored.
1006+
* If a `filesGlob` property is NOT present, but `files` is present:
1007+
* Any files specified in `files` will be added to the compilation context.
1008+
* If `exclude` is present, it will be ignored.
1009+
* If neither `filesGlob` nor `files` is present:
1010+
* All \*.ts and \*.tsx files in all subfolders will be added to the compilation context, **excluding** any subfolders specified in the optional `exclude` property.
1011+
* If a glob is also specified in the `Gruntfile.js`, grunt-ts will NOT update the `filesGlob` in the `tsconfig.json` file with it nor will those files be added to the `tsconfig.json` `files` element.
1012+
* The `tsconfig` property should function correctly as either a task option or a target property.
1013+
* If the `tsconfig.json` file does not exist or there is a parse error, compilation will be aborted with an error.
9971014

9981015
```js
9991016
grunt.initConfig({
10001017
ts: {
10011018
default: {
1002-
tsconfig: true // Value must be a string referencing a file path such as "./somefolder/tsconfig.json", OR `true` to use the 'tsconfig.json' in same folder as Gruntfile.js
1019+
// specifying tsconfig as a boolean will use the 'tsconfig.json' in same folder as Gruntfile.js
1020+
tsconfig: true
10031021
}
10041022
}
10051023
});
10061024
```
10071025

1008-
or
1026+
**When specifying tsconfig as a string**
1027+
This scenario follows the same behavior as specifying `tsconfig.json` as a boolean, except that it is possible to use an explicit file name. If a directory name is provided instead, grunt-ts will use `tsconfig.json` in that directory. The path to `tsconfig.json` (or the directory that contains it) is relative to `Gruntfile.js`.
10091028

10101029
```js
10111030
grunt.initConfig({
10121031
ts: {
10131032
default: {
1033+
// specifying tsconfig as a string will use the specified `tsconfig.json` file.
10141034
tsconfig: './some/path/to/tsconfig.json'
10151035
}
10161036
}
10171037
});
10181038
```
10191039

1020-
also:
1040+
**When specifying tsconfig as an object**
1041+
This provides the most control over how grunt-ts integrates with `tsconfig.json`. Supported properties are:
1042+
* `tsconfig`: `string` (optional) - if absent, will default to `tsconfig.json` in same folder as `Gruntfile.js`. If a folder is passed, will use `tsconfig.json` in that folder.
1043+
* `ignoreFiles`: `boolean` (optional) - default is `false`. If true, will not inlcude files in `files` array from `tsconfig.json` in the compilation context.
1044+
* `ignoreSettings`: `boolean` (optional) - default is `false`. If true, will ignore `compilerOptions` section in `tsconfig.json` (will only use settings from `Gruntfile.js` or grunt-ts defaults)
1045+
* `overwriteFilesGlob`: `boolean` (optional) - default is `false`. If true, will overwrite the contents of the `filesGlob` array with the contents of the `src` glob from grunt-ts.
1046+
* `updateFiles`: `boolean` (optional) - default is `true`. Will modify the `files` array in `tsconfig.json` to match the result of evaluating a `filesGlob` that is present **inside** `tsconfig.json` (the `files` element will **not** be updated with the results from any glob inside `Gruntfile.js` unless `overwriteFilesGlob` is also `true`).
1047+
* `passThrough`: `boolean` (optional) - default is `false`. If `passThrough` is `true`, grunt-ts will run TypeScript (`tsc`) with the specified tsconfig folder, passing the `--project` option only (and anything in `additionalFlags`). This provides support for custom compilers with custom implementations of `tsconfig.json` support. Note: Since this entirely depends on support from `tsc`, the `tsconfig` option must be a directory (not a file) as of TypeScript 1.6.
1048+
10211049

10221050
```js
10231051
grunt.initConfig({
10241052
ts: {
10251053
default: {
1054+
// specifying tsconfig as an object allows detailed configuration overrides...
10261055
tsconfig: {
10271056
tsconfig: './SomeOtherFolder/tsconfig.json',
1028-
ignoreFiles: true
1057+
ignoreFiles: false,
1058+
ignoreSettings: false,
1059+
overwriteFilesGlob: false,
1060+
updateFiles: true,
1061+
passThrough: false
10291062
}
10301063
}
10311064
}
10321065
});
1033-
```
10341066

1035-
Documentation for how this feature will work when fully implemented is here: https://github.com/TypeStrong/grunt-ts/issues/202#issuecomment-125992243
10361067

1068+
```
1069+
### Important notes:
1070+
* Globs in `filesGlob` in `tsconfig.json` are relative to the `tsconfig.json`, **not** the `Gruntfile.js`.
1071+
* `tsconfig` has a restriction when used with `files` in the Grunt task configuration: `overwriteFilesGlob` is NOT supported if `files` has more than one element. This will abort compilation.
1072+
* If `files` is absent in `tsconfig.json`, but `filesGlob` is present, grunt-ts will create and update the `files` array in `tsconfig.json` as long as `updateFiles` is `true` (the default). Since `files` will be created in this scenario, any values in the `exclude` array will be ignored.
1073+
* This feature may be used along with the `vs` keyword. Any settings found in `tsconfig.json` will override any settings found in the Visual Studio project file. Any files referenced in the Visual Studio file that are not also referenced in tsconfig.json *will* be included in the compilation context after any files from `tsconfig.json` (any files from `src` but not in `vs` or `tsconfig` will be included after that). The order of the files in `tsconfig.json` will override the order of the files in the VS project file.
10371074

10381075
#### verbose
10391076

0 commit comments

Comments
 (0)