[PYTHON] Use TypeScript with django-compressor

Premise

The template looks like this [^ 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 %}

How to do it was useless

settings.py


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

I get an error

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

Reason

tsc can only use .ts or .d.ts extensions. [^ 2]

Solution

Create a script that wraps tsc and specify it.

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

reference

[^ 1]: TypeScript code is Official Samples

Recommended Posts

Use TypeScript with django-compressor
Use RTX 3090 with PyTorch
Use ansible with cygwin
Use pipdeptree with virtualenv
[Python] Use JSON with Python
Use Mock with pytest
Use Gentelella with django
Use tensorboard with Chainer
Use DynamoDB with Python
Use pip with MSYS2
Use Python 3.8 with Anaconda
Use pyright with Spacemacs
Use python with docker
Use LESS with Django
Use MySQL with Django
Use Enums with SQLAlchemy
Use tensorboard with NNabla
Use GPS with Edison
Use nim with Jupyter
Use Trello API with python
Use shared memory with shared libraries
Use "$ in" operator with mongo-go-driver
Use directional graphs with networkx
Use TensorFlow with Intellij IDEA
Use Twitter API with Python
Use pip with Jupyter Notebook
Use DATE_FORMAT with SQLAlchemy filter
Use TUN / TAP with Python
Use sqlite3 with NAO (Pepper)
Use sqlite load_extensions with Pyramid
Use Windows 10 fonts with WSL
Use chainer with Jetson TK1
Use SSL with Celery + Redis
Use Maxout + CNN with Pylearn2
Use WDC-433SU2M2 with Manjaro Linux
Use OpenBLAS with numpy, scipy
Use subsonic API with python3
Use Sonicwall NetExtener with Systemd
Use prefetch_related conveniently with Django
Use AWS interpreter with Pycharm
Use Bokeh with IPython Notebook
Use Python-like range with Rust
Use MLflow with Databricks ④ --Call model -
Python: How to use async with
Use PointGrey camera with Python (PyCapture2)
Use vl53l0x with Raspberry Pi (python)
Use PX-S1UD / PX-Q1UD with Jetson nano
Use the preview feature with aws-cli
How to use virtualenv with PowerShell
[Python] Use Basic/Digest authentication with Flask
Use NAIF SPICE TOOLKIT with Python
Use rospy with virtualenv in Python3
Use markdown with jupyter notebook (with shortcut)
Use Python in pyenv with NeoVim
Use Tensorflow 2.1.0 with Anaconda on Windows 10!
How to use FTP with Python
Use Windows 10 speech synthesis with Python
Use curl / jq library with Go
I can't use Japanese with pyperclip
Use camera calibration file with OpenCvSharp4
Use MULTI_ORG function with re: dash