Skip to content

Commit

Permalink
Add TypeScript compiler support (#784)
Browse files Browse the repository at this point in the history
  • Loading branch information
peymanslh authored Feb 20, 2023
1 parent 24050e1 commit 455649d
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 1 deletion.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Unreleased
==========

* Update README.rst and add Pipeline overview image
* Add TypeScript compiler support


2.0.9
Expand Down
26 changes: 26 additions & 0 deletions docs/compilers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,32 @@
Compilers
=========

TypeScript compiler
======================

The TypeScript compiler uses `TypeScript <https://www.typescriptlang.org/>`_
to compile your TypeScript code to JavaScript.

To use it add this to your ``PIPELINE['COMPILERS']`` ::

PIPELINE['COMPILERS'] = (
'pipeline.compilers.typescript.TypeScriptCompiler',
)

``TYPE_SCRIPT_BINARY``
---------------------------------

Command line to execute for TypeScript program.
You will most likely change this to the location of ``tsc`` on your system.

Defaults to ``'/usr/bin/env tsc'``.

``TYPE_SCRIPT_ARGUMENTS``
------------------------------------

Additional arguments to use when ``tsc`` is called.

Defaults to ``''``.

Coffee Script compiler
======================
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"terser": "latest",
"uglify-js": "latest",
"yuglify": "1.0.x",
"yuicompressor": "latest"
"yuicompressor": "latest",
"typescript": "latest"
}
}
21 changes: 21 additions & 0 deletions pipeline/compilers/typescript.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from pipeline.compilers import SubProcessCompiler
from pipeline.conf import settings


class TypeScriptCompiler(SubProcessCompiler):
output_extension = 'js'

def match_file(self, path):
return path.endswith('.ts')

def compile_file(self, infile, outfile, outdated=False, force=False):
if not outdated and not force:
return
command = (
settings.TYPE_SCRIPT_BINARY,
settings.TYPE_SCRIPT_ARGUMENTS,
infile,
'--outFile',
outfile,
)
return self.execute_command(command)
4 changes: 4 additions & 0 deletions pipeline/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
'LIVE_SCRIPT_BINARY': '/usr/bin/env lsc',
'LIVE_SCRIPT_ARGUMENTS': '',

'TYPE_SCRIPT_BINARY': '/usr/bin/env tsc',
'TYPE_SCRIPT_ARGUMENTS': '',

'SASS_BINARY': '/usr/bin/env sass',
'SASS_ARGUMENTS': '',

Expand All @@ -76,6 +79,7 @@
(('text/coffeescript'), ('.coffee')),
(('text/less'), ('.less')),
(('text/javascript'), ('.js')),
(('text/typescript'), ('.ts')),
(('text/x-sass'), ('.sass')),
(('text/x-scss'), ('.scss'))
),
Expand Down
4 changes: 4 additions & 0 deletions tests/assets/compilers/typescript/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function getName(u) {
return "".concat(u.firstName, " ").concat(u.lastName);
}
var userName = getName({ firstName: "Django", lastName: "Pipeline" });
13 changes: 13 additions & 0 deletions tests/assets/compilers/typescript/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type FullName = string;

interface User {
firstName: string;
lastName: string;
}


function getName(u: User): FullName {
return `${u.firstName} ${u.lastName}`;
}

let userName: FullName = getName({firstName: "Django", lastName: "Pipeline"});
1 change: 1 addition & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ def node_exe_path(command):
'UGLIFYJS_BINARY': node_exe_path('uglifyjs'),
'TERSER_BINARY': node_exe_path('terser'),
'CSSMIN_BINARY': node_exe_path('cssmin'),
'TYPE_SCRIPT_BINARY': node_exe_path('tsc'),
})

if HAS_NODE and HAS_JAVA:
Expand Down
7 changes: 7 additions & 0 deletions tests/tests/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,13 @@ def test_es6(self):
'pipeline/compilers/es6/expected.js',
)

def test_typescript(self):
self._test_compiler(
'pipeline.compilers.typescript.TypeScriptCompiler',
'pipeline/compilers/typescript/input.ts',
'pipeline/compilers/typescript/expected.js',
)

def test_stylus(self):
self._test_compiler(
'pipeline.compilers.stylus.StylusCompiler',
Expand Down

0 comments on commit 455649d

Please sign in to comment.