Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

grunt-typescript

k-maru13.9k0.8.0

compile typescript to javascript

gruntplugin, typescript

readme

grunt-typescript

Build Status NPM version

NPM

Compile TypeScript in Grunt

Release Note

Important!

BasePath option has been deprecated. Method for determining an output directory has been changed in the same way as the TSC. Please re-set output directory with the new rootDir option or use keepDirectoryHierachy option.However, keepDirectoryHierachy option would not be available long.

Documentation

You'll need to install grunt-typescript first:

npm install grunt-typescript --save-dev

or add the following line to devDependencies in your package.json

"grunt-typescript": "",

Then modify your Gruntfile.js file by adding the following line:

grunt.loadNpmTasks('grunt-typescript');

Then add some configuration for the plugin like so:

grunt.initConfig({
  ...
  typescript: {
    base: {
      src: ['path/to/typescript/files/**/*.ts'],
      dest: 'where/you/want/your/js/files',
      options: {
        module: 'amd', //or commonjs
        target: 'es5', //or es3
        basePath: 'path/to/typescript/files',
        sourceMap: true,
        declaration: true
      }
    }
  },
  ...
});

If you want to create a js file that is a concatenation of all the ts file (like -out option from tsc), you should specify the name of the file with the '.js' extension to dest option.

grunt.initConfig({
  ...
  typescript: {
    base: {
      src: ['path/to/typescript/files/**/*.ts'],
      dest: 'where/you/want/your/js/file.js',
      options: {
        module: 'amd', //or commonjs
      }
    }
  },
  ...
});

Options

typescript options

name type description
noLib boolean Do not include a default lib.d.ts with global declarations
target string Specify ECMAScript target version: 'ES3' (default), 'ES5', or 'ES6'
module string Specify module code generation: "commonjs" (default), "amd", "system" or "umd"
sourceMap boolean Generates corresponding .map files
declaration boolean Generates corresponding .d.ts file
removeComments boolean Do not emit comments to output.
noImplicitAny boolean Warn on expressions and declarations with an implied 'any' type.
noResolve boolean Skip resolution and preprocessing.
preserveConstEnums boolean Do not erase const enum declarations in generated code.
noEmitOnError boolean Do not emit outputs if any type checking errors were reported.The default for this option is set to true for backwards compatibility.
suppressImplicitAnyIndexErrors boolean Suppress noImplicitAny errors for indexing objects lacking index signatures.
experimentalDecorators boolean
emitDecoratorMetadata boolean
newLine string
inlineSourceMap boolean
inlineSources boolean
noEmitHelpers boolean

original options

generateTsConfig

type: string | boolean

generateTsConfig option will generate the content and equivalent tsconfig.json that are specified in the option. The value specify the directory name to be output. It is output to the current directory when you specify true.

references

type: string | string[]

Set auto reference libraries.

grunt.initConfig({
  ...
  typescript: {
    base: {
      src: ['path/to/typescript/files/**/*.ts'],
      options: {
        references: [
          "core",       //lib.core.d.ts
          "dom",        //lib.dom.d.ts
          "scriptHost", //lib.scriptHost.d.ts
          "webworker",  //lib.webworker.d.ts
          "path/to/reference/files/**/*.d.ts"
        ]
      }
    }
  },
  ...
});

watch

type: string | boolean | { path?:<string | string[]`>; before?: <string | string[]`>; after?: <string | string[]`>; atBegin: boolean }

Watch .ts files. It runs very quickly the second time since the compilation. It is because you only want to read and output file is limited.

Specify the directory where you want to monitor in the options.

grunt.initConfig({
  ...
  typescript: {
    base: {
      src: ['path/to/typescript/files/**/*.ts'],
      options: {
        watch: 'path/to/typescript/files' //or ['path/to/typescript/files1', 'path/to/typescript/files2']
      }
    }
  },
  ...
});

If you specify the true, then automatically detects the directory.

grunt.initConfig({
  ...
  typescript: {
    base: {
      src: ['path/to/typescript/files/**/*.ts'],
      options: {
        watch: true //Detect all target files root. eg: 'path/to/typescript/files/'
      }
    }
  },
  ...
});

For expansion of the future, You can also be specified 'object'.

grunt.initConfig({
  ...
  typescript: {
    base: {
      src: ['path/to/typescript/files/**/*.ts'],
      options: {
        watch: {
          path: 'path/to/typescript/files', //or ['path/to/typescript/files1', 'path/to/typescript/files2']
          before: ['beforetasks'],   //Set before tasks. eg: clean task
          after: ['aftertasks']      //Set after tasks.  eg: minify task
          atBegin: true              //Run tasks when watcher starts. default false
        }
      }
    }
  },
  ...
});

basePath(obsolete)

type: string

Path component to cut off when mapping the source files to dest files.

keepDirectoryHierarchy(obsolete)

type: boolean

Path component to cut off when mapping the source files to dest files.

grunt.initConfig({
  ...
  typescript: {
    base: {
      src: ['path/to/typescript/files/**/*.ts'],
      dest: 'bin'
      options: {
        keepDirectoryHierarchy: true
      }
    }
  },
  ...
});

If keepDirectoryHierarchy option is true, it is output as follows.

/bin
- /path
--- /to
----- /typescript
------- /files
--------- *.ts 

If keepDirectoryHierarchy option is false or not set, it is output as follows. It is same way as the tsc.

/bin
- *.ts 

※I'm sorry for poor English

changelog

Released

0.8

  • Typescript 1.6.2 に対応
    • experimentalAsyncFunctions オプションに対応
    • jsx オプションに対応

0.7

  • Typescript 1.5.3 に対応
    • module の system と umd に対応
    • emitDecoratorMetadata オプションに対応
    • experimentalDecorators オプションに対応
    • newLine オプションに対応
    • inlineSourceMap オプションに対応
    • inlineSources オプションに対応
    • noEmitHelpers オプションに対応

0.6.2

  • コンパイルのコードファイルをtypescriptService.js から typescript.js に変更
  • Typescript 1.5-beta に対応
    • dest でディレクトリを指定した際のディレクトリ階層の保持方法をtscと同じ動きに変更
    • 以前のディレクトリ階層の保持ができるように keepDirectoryHierarchy オプションを追加
    • basePath オプションをobsoluteに変更
    • rootDir オプションに対応
    • generateTsConfig オプションを追加

0.6.1

  • 不具合修正
    • 内部処理でScriptTargetが設定されていない場合に例外が発生することがある不具合?に初期値を設定しておくことで対応

0.6.0

  • Typescript 1.4.1 に対応
    • preserveConstEnums オプションの追加
    • noEmitOnError オプションの追加と ignoreError オプションの obsolete 化
    • suppressImplicitAnyIndexErrors オプションの追加
    • target オプションに ES6 を追加
  • comment オプションの削除
  • コンパイルのコードファイルをコマンドライン用の tsc.js から typescriptService.js に変更

0.4.7 / 0.4.8

  • watch の時に reference で指定されているファイルが更新されていても内容が反映されない不具合を修正

0.4.6

  • taskにdescription を追加
    • grunt-jsmin-sourcemap と競合?して grunt が task を実行できていなかった挙動の修正

0.4.5

  • README の option の記述を修正

0.4.4

  • TypeScript 1.3.0 に対応

0.4.3

  • 不具合の修正
    • 同じファイルが何回もemit実行されてしまうため "lib.d.ts を Emmit 時の対象外に変更" を取りやめ

0.4.2

  • 不具合の修正
    • 例外を握りつぶしてしまっていたのを表示するように修正
  • パフォーマンス対応
    • lib.d.ts を TypeCheck の対象外に変更
    • lib.d.ts を Emmit 時の対象外に変更
  • grunt の --verbose に対応
  • grunt の実行時に --showtsc オプションを付加することで処理内容と近い tsc コマンドを表示するように対応

0.4.1

  • 不具合の修正
    • watch のコンパイル失敗時の次回コンパイル対象
  • 参照するファイルを設定する references オプションを追加
    • lib.core.d.ts や lib.dom.d.ts の参照にも対応

0.4.0

  • All rewrite 全部書き直しを実施
  • Support TypeScript version 1.1.0-1 TypeScript の ver 1.1.0-1 で動作するように対応

0.3.7

  • Added support to include the files that fail to file the next target of the compilation failure. watchのコンパイルが失敗した時に、次回のコンパイル対象に含めるように対応
  • Fixed a bug that it had failed to check the update date and time in the file deletion. watchでファイル削除時に更新日時の取得に失敗していた不具合を修正

0.3.6

  • Add support TypeScript version 1.0.0 and 1.0.1

0.3.5

  • Added watch.atBegin option to run tasks when watcher starts watch.atBeginオプションを追加。watchが開始された時にコンパイルを実行します。
  • Corresponding to delete files from the cache when a file is deleted ファイルが削除された時に、ファイルキャッシュからも削除してコンパイル対象にならないように対応

0.3.3 / 0.3.4

  • Compilation with watch option is more fast.

0.3.1 / 0.3.2

  • Added watch option

0.3.0

  • Update to TypeScript 1.0.0.
  • Remove base_path, sourcemap and nolib options. (Changed to basePath, sourceMap and noLib)
  • Remove ignoreTypeCheck option.
  • Breaking Changes: ignoreError:true is now the default.