LoginSignup
9
8

More than 5 years have passed since last update.

Titanium + Alloy + TypeScript1.0RC のコンパイル

Last updated at Posted at 2014-04-02

概要

2014年02月25日に TypeScript1.0RC(compilerVersion 0.9.7) が公開されました。
Announcing TypeScript 1.0RC

Titanium + Alloy + TypeScript1.0RC のコンパイル方法を調べても情報を見つけられなかったので、自分でなんとかコンパイルできるようにしました。

環境

MacOS X 10.9.2
Titanium SDK 3.3.0
Alloy 1.3.1
TypeScript1.0RC

参考

以下の記事を参考にさせていただきました。
Titanium + Alloy + TypeScript で、iOS & Android アプリを作る

詳細

ファイルのコピー

Typescript インストールフォルダの tsc.js と lib.d.ts を
Alloy インストールフォルダ内の bin フォルダにコピーする。

私の環境では以下のpathです。
/usr/local/lib/node_modules/alloy/bin/

alloy.jmk の作成

alloy.jmk を編集して、tsc.js を読み込むようにする。

typescript.js ではなく、tsc.jsを使う。

wrench.readdirSyncRecursive のコードブロックの前にBatchCompilerのインスタンスの生成文を挿入。

alloy.jmk
var batch = new TypeScript.BatchCompiler(TypeScript.IO);

wrench.readdirSyncRecursive のコードブロックの中で、各ファイルのパスをBatchCompilerのinputFiles配列に追加する。

alloy.jmk
batch.inputFiles.push(filename);

wrench.readdirSyncRecursive のコードブロックの後にBatchCompilerのcompile実行文を追加。

alloy.jmk
batch.batchCompile(); 

完成した alloy.jmk は以下の通り。

alloy.jmk
task("pre:compile", function(event,logger) {
    var wrench = require("wrench"),
        fs = require("fs"),
        path = require("path");

    var code = [
        fs.readFileSync("/usr/local/lib/node_modules/alloy/bin/tsc.js"), // <- path to tsc.js in your environment
        "module.exports = TypeScript;"
    ].join("");
    fs.writeFileSync(process.env.TMPDIR + "tsc.js", code);
    var TypeScript = require(process.env.TMPDIR + "tsc.js");
    fs.unlinkSync(process.env.TMPDIR + "tsc.js");

    event.alloyConfig.tsc = [];

    var batch = new TypeScript.BatchCompiler(TypeScript.IO);
    wrench.readdirSyncRecursive(event.dir.home).forEach(function(target){
        if (target.match(/\.ts$/) && ! target.match(/\.d\.ts$/)) {
            var filename = path.join(event.dir.home + "/" + target);
            batch.inputFiles.push(filename);
            event.alloyConfig.tsc.push(target.replace(/.ts$/, ".js"));
        }
    });
    batch.batchCompile();
});

task("post:compile",function(event,logger){
  var fs = require("fs");

  event.alloyConfig.tsc.forEach(function(target){
    fs.unlinkSync(event.dir.home + "/" + target);
  });
});

tsc.js の編集

Alloy インストールフォルダ内の bin フォルダにコピーした tsc.js を編集する。

ファイルの末尾付近のBatchCompiler実行文をコメントアウトする。

tsc.js
//var batch = new TypeScript.BatchCompiler(TypeScript.IO);
//batch.batchCompile();

BatchCompilerクラスのparseOptionsメソッド定義文の後半 opts.parse(this.ioHost.arguments) の前に
1文追加して余分なoptionを除外する。

tsc.js
this.ioHost.arguments = this.ioHost.arguments.filter(function(arg) { 
    return arg !== '--config';
});

BatchCompilerクラスのresolveメソッド定義文の前半 var resolutionResults = ... の前に
1文追加して余分なargumentsを除外する。

tsc.js
this.inputFiles = this.inputFiles.filter(function(file) { 
    return file.match(/\.ts$/);
});

ログの出力をする場合は、CompilationSettingsクラスのコンストラクタ内 this.gatherDiagnostics = false; -> trueに変更する。
プロパティのget/setアクセサメソッドを使用する場合は
this.codeGenTarget = 0; -> 1に変更してECMAScript5をターゲットにする。

追記

Announcing TypeScript 1.0
TypeScript1.0 でも同様にコンパイルできました。

9
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
8