Skip to content

Commit

Permalink
Add terser (JS compressor for ES5 and ES6) (#696)
Browse files Browse the repository at this point in the history
* Add terser JS compressor

* Update authors
  • Loading branch information
felix-last authored Jan 18, 2021
1 parent bc0d0fb commit e7acf03
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ or just made Pipeline more awesome.
* Evan Myller <[email protected]>
* Fabian Büchler <[email protected]>
* Feanil Patel <[email protected]>
* Felix Last <[email protected]>
* Florent Messa <[email protected]>
* Frankie Dintino <[email protected]>
* Hannes Ljungberg <[email protected]>
Expand Down
29 changes: 29 additions & 0 deletions docs/compressors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,35 @@ Install the slimit library with your favorite Python package manager ::
pip install slimit


Terser compressor
===================

`Terser <https://github.com/terser/terser>`_ is a JavaScript parser and
mangler/compressor toolkit for ES6+. It has been designed as a successor of
``uglify-es`` and ``uglify-js``. The compressor works with ES5 and ES6 and
regular ``.js`` file endings.

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

PIPELINE['JS_COMPRESSOR'] = 'pipeline.compressors.terser.TerserCompressor'


``TERSER_BINARY``
----------------------------

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

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

``TERSER_ARGUMENTS``
-------------------------------

Additional arguments to use when terser is called.

Default to ``'--compress'``


CSSTidy compressor
==================

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"stylus": "latest",
"cssmin": "latest",
"google-closure-compiler": "latest",
"terser": "latest",
"uglify-js": "latest",
"yuglify": "1.0.x",
"yuicompressor": "latest"
Expand Down
12 changes: 12 additions & 0 deletions pipeline/compressors/terser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from __future__ import unicode_literals

from pipeline.conf import settings
from pipeline.compressors import SubProcessCompressor


class TerserCompressor(SubProcessCompressor):
def compress_js(self, js):
command = (settings.TERSER_BINARY, settings.TERSER_ARGUMENTS)
if self.verbose:
command += ' --verbose'
return self.execute_command(command, js)
3 changes: 3 additions & 0 deletions pipeline/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@

'UGLIFYJS_BINARY': '/usr/bin/env uglifyjs',
'UGLIFYJS_ARGUMENTS': '',

'TERSER_BINARY': '/usr/bin/env terser',
'TERSER_ARGUMENTS': '--compress',

'CSSMIN_BINARY': '/usr/bin/env cssmin',
'CSSMIN_ARGUMENTS': '',
Expand Down
1 change: 1 addition & 0 deletions tests/assets/compressors/terser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(function(){window.concat=function(){console.log(arguments)},window.cat=function(){console.log("hello world")}}).call(this);
1 change: 1 addition & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def node_exe_path(command):
'LIVE_SCRIPT_ARGUMENTS': ['--no-header'],
'YUGLIFY_BINARY': node_exe_path('yuglify'),
'UGLIFYJS_BINARY': node_exe_path('uglifyjs'),
'TERSER_BINARY': node_exe_path('terser'),
'CSSMIN_BINARY': node_exe_path('cssmin'),
})

Expand Down
7 changes: 6 additions & 1 deletion tests/tests/test_compressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,12 @@ def test_csshtmljsminify(self):
def test_uglifyjs(self):
self._test_compressor('pipeline.compressors.uglifyjs.UglifyJSCompressor',
'js', 'pipeline/compressors/uglifyjs.js')


@skipUnless(settings.HAS_NODE, "requires node")
def test_terser(self):
self._test_compressor('pipeline.compressors.terser.TerserCompressor',
'js', 'pipeline/compressors/terser.js')

@skipUnless(settings.HAS_NODE, "requires node")
def test_yuglify(self):
self._test_compressor('pipeline.compressors.yuglify.YuglifyCompressor',
Expand Down

0 comments on commit e7acf03

Please sign in to comment.