Skip to content

Commit 455649d

Browse files
authored
Add TypeScript compiler support (#784)
1 parent 24050e1 commit 455649d

File tree

9 files changed

+79
-1
lines changed

9 files changed

+79
-1
lines changed

HISTORY.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Unreleased
77
==========
88

99
* Update README.rst and add Pipeline overview image
10+
* Add TypeScript compiler support
1011

1112

1213
2.0.9

docs/compilers.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,32 @@
44
Compilers
55
=========
66

7+
TypeScript compiler
8+
======================
9+
10+
The TypeScript compiler uses `TypeScript <https://www.typescriptlang.org/>`_
11+
to compile your TypeScript code to JavaScript.
12+
13+
To use it add this to your ``PIPELINE['COMPILERS']`` ::
14+
15+
PIPELINE['COMPILERS'] = (
16+
'pipeline.compilers.typescript.TypeScriptCompiler',
17+
)
18+
19+
``TYPE_SCRIPT_BINARY``
20+
---------------------------------
21+
22+
Command line to execute for TypeScript program.
23+
You will most likely change this to the location of ``tsc`` on your system.
24+
25+
Defaults to ``'/usr/bin/env tsc'``.
26+
27+
``TYPE_SCRIPT_ARGUMENTS``
28+
------------------------------------
29+
30+
Additional arguments to use when ``tsc`` is called.
31+
32+
Defaults to ``''``.
733

834
Coffee Script compiler
935
======================

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"terser": "latest",
2424
"uglify-js": "latest",
2525
"yuglify": "1.0.x",
26-
"yuicompressor": "latest"
26+
"yuicompressor": "latest",
27+
"typescript": "latest"
2728
}
2829
}

pipeline/compilers/typescript.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from pipeline.compilers import SubProcessCompiler
2+
from pipeline.conf import settings
3+
4+
5+
class TypeScriptCompiler(SubProcessCompiler):
6+
output_extension = 'js'
7+
8+
def match_file(self, path):
9+
return path.endswith('.ts')
10+
11+
def compile_file(self, infile, outfile, outdated=False, force=False):
12+
if not outdated and not force:
13+
return
14+
command = (
15+
settings.TYPE_SCRIPT_BINARY,
16+
settings.TYPE_SCRIPT_ARGUMENTS,
17+
infile,
18+
'--outFile',
19+
outfile,
20+
)
21+
return self.execute_command(command)

pipeline/conf.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@
6363
'LIVE_SCRIPT_BINARY': '/usr/bin/env lsc',
6464
'LIVE_SCRIPT_ARGUMENTS': '',
6565

66+
'TYPE_SCRIPT_BINARY': '/usr/bin/env tsc',
67+
'TYPE_SCRIPT_ARGUMENTS': '',
68+
6669
'SASS_BINARY': '/usr/bin/env sass',
6770
'SASS_ARGUMENTS': '',
6871

@@ -76,6 +79,7 @@
7679
(('text/coffeescript'), ('.coffee')),
7780
(('text/less'), ('.less')),
7881
(('text/javascript'), ('.js')),
82+
(('text/typescript'), ('.ts')),
7983
(('text/x-sass'), ('.sass')),
8084
(('text/x-scss'), ('.scss'))
8185
),
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function getName(u) {
2+
return "".concat(u.firstName, " ").concat(u.lastName);
3+
}
4+
var userName = getName({ firstName: "Django", lastName: "Pipeline" });
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
type FullName = string;
2+
3+
interface User {
4+
firstName: string;
5+
lastName: string;
6+
}
7+
8+
9+
function getName(u: User): FullName {
10+
return `${u.firstName} ${u.lastName}`;
11+
}
12+
13+
let userName: FullName = getName({firstName: "Django", lastName: "Pipeline"});

tests/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ def node_exe_path(command):
173173
'UGLIFYJS_BINARY': node_exe_path('uglifyjs'),
174174
'TERSER_BINARY': node_exe_path('terser'),
175175
'CSSMIN_BINARY': node_exe_path('cssmin'),
176+
'TYPE_SCRIPT_BINARY': node_exe_path('tsc'),
176177
})
177178

178179
if HAS_NODE and HAS_JAVA:

tests/tests/test_compiler.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,13 @@ def test_es6(self):
276276
'pipeline/compilers/es6/expected.js',
277277
)
278278

279+
def test_typescript(self):
280+
self._test_compiler(
281+
'pipeline.compilers.typescript.TypeScriptCompiler',
282+
'pipeline/compilers/typescript/input.ts',
283+
'pipeline/compilers/typescript/expected.js',
284+
)
285+
279286
def test_stylus(self):
280287
self._test_compiler(
281288
'pipeline.compilers.stylus.StylusCompiler',

0 commit comments

Comments
 (0)