3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

django-compressorでTypeScriptを使う

Posted at

前提

テンプレートはこんな感じ1

{% load compress %}
{% compress js %}
<script type="text/typescript">
class Greeter {
    constructor(public greeting: string) { }
    greet() {
        return "<h1>" + this.greeting + "</h1>";
    }
};
var greeter = new Greeter("Hello, world!");
var str = greeter.greet();
document.body.innerHTML = str;
</script>
{% endcompress %}

ダメだったやりかた

settings.py
COMPRESS_PRECOMPILED = ('text/typescript', 'tsc --out {outfile} {infile}')

エラーが出る

error TS6053: File '/tmp/tmpxxt3rmzl.ts' not found.

理由

tscは拡張子に.tsまたは.d.tsしか使用できない。2

解決方法

tscをラップしたスクリプトを作ってそれを指定する。

settings.py
COMPRESS_PRECOMPILED = ('text/typescript', '/path/to/my-tsc {infile} {outfile}')
my-tsc
#!/bin/sh

cp $1 $1.ts
tsc $1 --out $2
rm $1.ts

参考

  1. TypeScriptのコードは公式のサンプル

  2. https://github.com/Microsoft/TypeScript/blob/ed7b07dce1e66e56fdc9ca9a8b1f818a9ab92080/src/compiler/core.ts#L705

3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?