Compile TypeScript in Grunt
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
###noLib
type: boolean
Do not include a default lib.d.ts with global declarations
###target
type: string
Specify ECMAScript target version: 'ES3' (default), 'ES5', or 'ES6' (experimental)
###module
type: string
Specify module code generation: "commonjs" (default) or "amd"
###sourceMap
type: boolean
Generates corresponding .map files
###declaration
type: boolean
Generates corresponding .d.ts file
###removeComments
type: boolean
Do not emit comments to output.
###noImplicitAny
type: boolean
Warn on expressions and declarations with an implied 'any' type.
###noResolve
type: boolean
Skip resolution and preprocessing.
###preserveConstEnums
type: boolean
Do not erase const enum declarations in generated code.
###noEmitOnError
type: 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
type: boolean
Suppress noImplicitAny errors for indexing objects lacking index signatures.
##Original Options
###ignoreError(obsolete)
type: boolean
If the compiler can create a file, the task ignores the error. Default value is false.
This option is now obsolete. Please use noEmitOnError
option.
###basePath
type: string
Path component to cut off when mapping the source files to dest files.
###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
}
}
}
},
...
});
※I'm sorry for poor English