|
4 | 4 |
|
5 | 5 | ## TypeScript Compilation Task for GruntJS
|
6 | 6 |
|
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). |
8 | 8 |
|
9 | 9 | ### 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. |
12 | 11 |
|
13 | 12 | [Full changelog is here](CHANGELOG.md).
|
14 | 13 |
|
@@ -993,47 +992,85 @@ grunt.initConfig({
|
993 | 992 |
|
994 | 993 | #### tsconfig
|
995 | 994 |
|
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. |
997 | 1014 |
|
998 | 1015 | ```js
|
999 | 1016 | grunt.initConfig({
|
1000 | 1017 | ts: {
|
1001 | 1018 | 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 |
1003 | 1021 | }
|
1004 | 1022 | }
|
1005 | 1023 | });
|
1006 | 1024 | ```
|
1007 | 1025 |
|
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`. |
1009 | 1028 |
|
1010 | 1029 | ```js
|
1011 | 1030 | grunt.initConfig({
|
1012 | 1031 | ts: {
|
1013 | 1032 | default: {
|
| 1033 | + // specifying tsconfig as a string will use the specified `tsconfig.json` file. |
1014 | 1034 | tsconfig: './some/path/to/tsconfig.json'
|
1015 | 1035 | }
|
1016 | 1036 | }
|
1017 | 1037 | });
|
1018 | 1038 | ```
|
1019 | 1039 |
|
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 | + |
1021 | 1049 |
|
1022 | 1050 | ```js
|
1023 | 1051 | grunt.initConfig({
|
1024 | 1052 | ts: {
|
1025 | 1053 | default: {
|
| 1054 | + // specifying tsconfig as an object allows detailed configuration overrides... |
1026 | 1055 | tsconfig: {
|
1027 | 1056 | tsconfig: './SomeOtherFolder/tsconfig.json',
|
1028 |
| - ignoreFiles: true |
| 1057 | + ignoreFiles: false, |
| 1058 | + ignoreSettings: false, |
| 1059 | + overwriteFilesGlob: false, |
| 1060 | + updateFiles: true, |
| 1061 | + passThrough: false |
1029 | 1062 | }
|
1030 | 1063 | }
|
1031 | 1064 | }
|
1032 | 1065 | });
|
1033 |
| -``` |
1034 | 1066 |
|
1035 |
| -Documentation for how this feature will work when fully implemented is here: https://github.com/TypeStrong/grunt-ts/issues/202#issuecomment-125992243 |
1036 | 1067 |
|
| 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. |
1037 | 1074 |
|
1038 | 1075 | #### verbose
|
1039 | 1076 |
|
|
0 commit comments