Skip to content

Commit e7acf03

Browse files
authored
Add terser (JS compressor for ES5 and ES6) (#696)
* Add terser JS compressor * Update authors
1 parent bc0d0fb commit e7acf03

File tree

8 files changed

+54
-1
lines changed

8 files changed

+54
-1
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ or just made Pipeline more awesome.
5454
* Evan Myller <[email protected]>
5555
* Fabian Büchler <[email protected]>
5656
* Feanil Patel <[email protected]>
57+
* Felix Last <[email protected]>
5758
* Florent Messa <[email protected]>
5859
* Frankie Dintino <[email protected]>
5960
* Hannes Ljungberg <[email protected]>

docs/compressors.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,35 @@ Install the slimit library with your favorite Python package manager ::
173173
pip install slimit
174174

175175

176+
Terser compressor
177+
===================
178+
179+
`Terser <https://github.com/terser/terser>`_ is a JavaScript parser and
180+
mangler/compressor toolkit for ES6+. It has been designed as a successor of
181+
``uglify-es`` and ``uglify-js``. The compressor works with ES5 and ES6 and
182+
regular ``.js`` file endings.
183+
184+
To use it add this to your ``PIPELINE['JS_COMPRESSOR']`` ::
185+
186+
PIPELINE['JS_COMPRESSOR'] = 'pipeline.compressors.terser.TerserCompressor'
187+
188+
189+
``TERSER_BINARY``
190+
----------------------------
191+
192+
Command line to execute for the terser program.
193+
You will most likely change this to the location of terser on your system.
194+
195+
Defaults to ``'/usr/bin/env terser'``.
196+
197+
``TERSER_ARGUMENTS``
198+
-------------------------------
199+
200+
Additional arguments to use when terser is called.
201+
202+
Default to ``'--compress'``
203+
204+
176205
CSSTidy compressor
177206
==================
178207

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"stylus": "latest",
2121
"cssmin": "latest",
2222
"google-closure-compiler": "latest",
23+
"terser": "latest",
2324
"uglify-js": "latest",
2425
"yuglify": "1.0.x",
2526
"yuicompressor": "latest"

pipeline/compressors/terser.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from __future__ import unicode_literals
2+
3+
from pipeline.conf import settings
4+
from pipeline.compressors import SubProcessCompressor
5+
6+
7+
class TerserCompressor(SubProcessCompressor):
8+
def compress_js(self, js):
9+
command = (settings.TERSER_BINARY, settings.TERSER_ARGUMENTS)
10+
if self.verbose:
11+
command += ' --verbose'
12+
return self.execute_command(command, js)

pipeline/conf.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848

4949
'UGLIFYJS_BINARY': '/usr/bin/env uglifyjs',
5050
'UGLIFYJS_ARGUMENTS': '',
51+
52+
'TERSER_BINARY': '/usr/bin/env terser',
53+
'TERSER_ARGUMENTS': '--compress',
5154

5255
'CSSMIN_BINARY': '/usr/bin/env cssmin',
5356
'CSSMIN_ARGUMENTS': '',

tests/assets/compressors/terser.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(function(){window.concat=function(){console.log(arguments)},window.cat=function(){console.log("hello world")}}).call(this);

tests/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ def node_exe_path(command):
170170
'LIVE_SCRIPT_ARGUMENTS': ['--no-header'],
171171
'YUGLIFY_BINARY': node_exe_path('yuglify'),
172172
'UGLIFYJS_BINARY': node_exe_path('uglifyjs'),
173+
'TERSER_BINARY': node_exe_path('terser'),
173174
'CSSMIN_BINARY': node_exe_path('cssmin'),
174175
})
175176

tests/tests/test_compressor.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,12 @@ def test_csshtmljsminify(self):
240240
def test_uglifyjs(self):
241241
self._test_compressor('pipeline.compressors.uglifyjs.UglifyJSCompressor',
242242
'js', 'pipeline/compressors/uglifyjs.js')
243-
243+
244+
@skipUnless(settings.HAS_NODE, "requires node")
245+
def test_terser(self):
246+
self._test_compressor('pipeline.compressors.terser.TerserCompressor',
247+
'js', 'pipeline/compressors/terser.js')
248+
244249
@skipUnless(settings.HAS_NODE, "requires node")
245250
def test_yuglify(self):
246251
self._test_compressor('pipeline.compressors.yuglify.YuglifyCompressor',

0 commit comments

Comments
 (0)