先日に引き続き、Gradle から TypeScript のコンパイルができないかなと考えていたところ、以下の2つのGradleプラグインがありました。
- https://github.com/jzarnikov/gradle-typescript-plugin
- https://github.com/sothmann/typescript-gradle-plugin
試してみたところ、前者の方は、上手く動かせることができました。
Gradle + eclipse で開発している Java Web アプリに TypeScript のビルドを加えてみました。
build.gradle
apply plugin: 'java' apply plugin: 'war' apply plugin: 'eclipse-wtp' apply plugin: 'typescript' buildscript { repositories { mavenCentral() } dependencies { classpath 'at.irian.typescript:gradle-typescript-plugin:0.12' } } task wrapper(type: Wrapper) { gradleVersion = '1.12' } typescript { // 暗黙の any 型の宣言や式を警告します tscOptions = ['--noImplicitAny'] } // war 作成前に TypeScript のコンパイルをする war.dependsOn compileTypeScript war { // コンパイルされたJSファイルを war の中の ts 配下に入れる into("ts") { from typescript.generatedJsPath } } // Eclipse で TypeScript のコンパイルする設定を出力する(Eclipse 側の TypeScript プラグインの設定) eclipse { project { natures 'com.palantir.typescript.typeScriptNature' buildCommand 'com.palantir.typescript.typeScriptBuilder' } copy{ from 'eclipse/com.palantir.typescript.prefs' into '.settings' } }
tscコマンドには、PATHを通しておく必要がある。
eclipse/com.palantir.typescript.prefs
build.path.sourceFolder=src/main/typescript compiler.codeGenTarget=ECMASCRIPT3 compiler.compileOnSave=true compiler.generateDeclarationFiles=false compiler.mapSourceFiles=false compiler.moduleGenTarget=UNSPECIFIED compiler.noImplicitAny=true compiler.noLib=false compiler.outputDirOption=src/main/webapp/ts compiler.outputFileOption= compiler.removeComments=false eclipse.preferences.version=1 editor.indentSize=4 formatter.insertSpaceAfterCommaDelimiter=true formatter.insertSpaceAfterFunctionKeywordForAnonymousFunctions=false formatter.insertSpaceAfterKeywordsInControlFlowStatements=true formatter.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis=false formatter.insertSpaceAfterSemicolonInForStatements=true formatter.insertSpaceBeforeAndAfterBinaryOperators=true formatter.placeOpenBraceOnNewLineForControlBlocks=false formatter.placeOpenBraceOnNewLineForFunctions=false spacesForTabs=true tabWidth=4
src/main/typescript がTypeScriptのファイルを配置する場所、 src/main/webapp/ts がJSのファイルが生成されるディレクトリとなります。 そして、 compiler.noImplicitAny=true で、暗黙の any 型の宣言や式を警告するようにしています。
.gitignore
eclipse のビルドで生成されたJSファイルは、gitにコミットしないようにする。この場合、 /src/main/webapp/ts 。
/bin /build /.gradle /.settings /.project /.classpath /src/main/webapp/ts
こんな感じかなー。それより TypeScriptの文法を勉強していない私。