From e7acf0337abd2bf9865c05fe7e1113d9c4f84f81 Mon Sep 17 00:00:00 2001 From: Felix Last Date: Mon, 18 Jan 2021 17:42:25 +0100 Subject: [PATCH] Add terser (JS compressor for ES5 and ES6) (#696) * Add terser JS compressor * Update authors --- AUTHORS | 1 + docs/compressors.rst | 29 +++++++++++++++++++++++++++++ package.json | 1 + pipeline/compressors/terser.py | 12 ++++++++++++ pipeline/conf.py | 3 +++ tests/assets/compressors/terser.js | 1 + tests/settings.py | 1 + tests/tests/test_compressor.py | 7 ++++++- 8 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 pipeline/compressors/terser.py create mode 100644 tests/assets/compressors/terser.js diff --git a/AUTHORS b/AUTHORS index 76730def..adabf94c 100644 --- a/AUTHORS +++ b/AUTHORS @@ -54,6 +54,7 @@ or just made Pipeline more awesome. * Evan Myller * Fabian Büchler * Feanil Patel + * Felix Last * Florent Messa * Frankie Dintino * Hannes Ljungberg diff --git a/docs/compressors.rst b/docs/compressors.rst index b1efcd86..1d9d8b3a 100644 --- a/docs/compressors.rst +++ b/docs/compressors.rst @@ -173,6 +173,35 @@ Install the slimit library with your favorite Python package manager :: pip install slimit +Terser compressor +=================== + +`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 ================== diff --git a/package.json b/package.json index cbb0ff03..b36a7256 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "stylus": "latest", "cssmin": "latest", "google-closure-compiler": "latest", + "terser": "latest", "uglify-js": "latest", "yuglify": "1.0.x", "yuicompressor": "latest" diff --git a/pipeline/compressors/terser.py b/pipeline/compressors/terser.py new file mode 100644 index 00000000..d8ea61b3 --- /dev/null +++ b/pipeline/compressors/terser.py @@ -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) diff --git a/pipeline/conf.py b/pipeline/conf.py index b089ac14..a851d2f1 100644 --- a/pipeline/conf.py +++ b/pipeline/conf.py @@ -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': '', diff --git a/tests/assets/compressors/terser.js b/tests/assets/compressors/terser.js new file mode 100644 index 00000000..63c76ce5 --- /dev/null +++ b/tests/assets/compressors/terser.js @@ -0,0 +1 @@ +(function(){window.concat=function(){console.log(arguments)},window.cat=function(){console.log("hello world")}}).call(this); diff --git a/tests/settings.py b/tests/settings.py index b142dedd..f6822436 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -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'), }) diff --git a/tests/tests/test_compressor.py b/tests/tests/test_compressor.py index 3729e07c..1c49e004 100644 --- a/tests/tests/test_compressor.py +++ b/tests/tests/test_compressor.py @@ -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',