From 01081d96aff5916421d87b02b944ec142d4494c8 Mon Sep 17 00:00:00 2001 From: TheBuky Date: Fri, 29 May 2020 19:07:43 +0000 Subject: [PATCH] Add css-html-js-minify compressor --- AUTHORS | 3 ++- HISTORY.rst | 10 +++++++++- docs/compressors.rst | 21 ++++++++++++++++++++ pipeline/compressors/csshtmljsminify.py | 15 ++++++++++++++ setup.py | 4 ++-- tests/assets/compressors/csshtmljsminify.css | 1 + tests/assets/compressors/csshtmljsminify.js | 1 + tests/tests/test_compressor.py | 20 +++++++++---------- tox.ini | 1 + 9 files changed, 61 insertions(+), 15 deletions(-) create mode 100644 pipeline/compressors/csshtmljsminify.py create mode 100644 tests/assets/compressors/csshtmljsminify.css create mode 100644 tests/assets/compressors/csshtmljsminify.js diff --git a/AUTHORS b/AUTHORS index 0cbcb987..76730def 100644 --- a/AUTHORS +++ b/AUTHORS @@ -121,4 +121,5 @@ or just made Pipeline more awesome. * Wismill * Zachary Kazanski * Zenobius Jiricek - * Zeus Kronion \ No newline at end of file + * Zeus Kronion + \ No newline at end of file diff --git a/HISTORY.rst b/HISTORY.rst index 240b9a1d..fe55493f 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -3,6 +3,14 @@ History ======= +2.0.4 +====== + +* Adding **css-html-js-minify** support to compress JS and CSS. +* Update compressors documentation with css-html-js-minify. +* Create tests for css-html-js-minify compressor. +* Optimization by grouping the tests yuglify compressor. + 2.0.3 ====== @@ -36,7 +44,7 @@ History * Remove tests of uncovered versions of Python and Django. * Replace tests for Pypy by Pypy3. * Explicitly specify when files are read / write in binary mode. -* Set opening files for tests to deal with universal newlines. +* Set opening files for tests to deal with universal newlines. * Upgrade documentation version to 2.0 to follow the project version. 1.7.0 diff --git a/docs/compressors.rst b/docs/compressors.rst index 6387aa4b..b1efcd86 100644 --- a/docs/compressors.rst +++ b/docs/compressors.rst @@ -198,6 +198,7 @@ To us it for your stylesheets add this to your ``PIPELINE['CSS_COMPRESSOR']`` :: Default to ``'--template=highest'`` + CSSMin compressor ================= @@ -221,6 +222,26 @@ command to compress stylesheets. To use it, add this to your ``PIPELINE['CSS_COM Default to ``''`` + +css-html-js-minify compressor +============================= + +The css-html-js-minify is full Python compressor using `css-html-js-minify `_ +for compressing javascript and stylesheets. + +To use it for your stylesheets add this to your ``PIPELINE['CSS_COMPRESSOR']`` :: + + PIPELINE['CSS_COMPRESSOR'] = 'pipeline.compressors.csshtmljsminify.CssHtmlJsMinifyCompressor' + +To use it for your javascripts add this to your ``PIPELINE['JS_COMPRESSOR']`` :: + + PIPELINE['JS_COMPRESSOR'] = 'pipeline.compressors.csshtmljsminify.CssHtmlJsMinifyCompressor' + +Install the css-html-js-minify library with your favorite Python package manager :: + + pip install css-html-js-minify + + No-Op Compressors ================= diff --git a/pipeline/compressors/csshtmljsminify.py b/pipeline/compressors/csshtmljsminify.py new file mode 100644 index 00000000..b2afd84c --- /dev/null +++ b/pipeline/compressors/csshtmljsminify.py @@ -0,0 +1,15 @@ +from pipeline.compressors import CompressorBase + + +class CssHtmlJsMinifyCompressor(CompressorBase): + """ + CSS, HTML and JS compressor based on the Python library css-html-js-minify + (https://pypi.org/project/css-html-js-minify/). + """ + def compress_css(self, css): + from css_html_js_minify import css_minify + return css_minify(css) + + def compress_js(self, js): + from css_html_js_minify import js_minify + return js_minify(js) diff --git a/setup.py b/setup.py index 75a8913c..bb2cee98 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ setup( name='django-pipeline', - version='2.0.2', + version='2.0.4', description='Pipeline is an asset packaging library for Django.', long_description=io.open('README.rst', encoding='utf-8').read() + '\n\n' + io.open('HISTORY.rst', encoding='utf-8').read(), @@ -33,7 +33,7 @@ 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: 3.8', - 'Programming Language :: Python :: Implementation :: PyPy3', + 'Programming Language :: Python :: Implementation :: PyPy', 'Topic :: Utilities', 'Topic :: Software Development :: Libraries :: Python Modules', 'Topic :: Internet :: WWW/HTTP', diff --git a/tests/assets/compressors/csshtmljsminify.css b/tests/assets/compressors/csshtmljsminify.css new file mode 100644 index 00000000..ee38c52c --- /dev/null +++ b/tests/assets/compressors/csshtmljsminify.css @@ -0,0 +1 @@ +@charset "utf-8";.concat{display:none}.concatenate{display:block} \ No newline at end of file diff --git a/tests/assets/compressors/csshtmljsminify.js b/tests/assets/compressors/csshtmljsminify.js new file mode 100644 index 00000000..83d6f479 --- /dev/null +++ b/tests/assets/compressors/csshtmljsminify.js @@ -0,0 +1 @@ +(function(){(function(){window.concat=function(){console.log(arguments);}}());(function(){window.cat=function(){console.log("hello world");}}());}).call(this); \ No newline at end of file diff --git a/tests/tests/test_compressor.py b/tests/tests/test_compressor.py index bfe13dff..e63fb12b 100644 --- a/tests/tests/test_compressor.py +++ b/tests/tests/test_compressor.py @@ -230,20 +230,18 @@ def test_slimit(self): self._test_compressor('pipeline.compressors.slimit.SlimItCompressor', 'js', 'pipeline/compressors/slimit.js') + def test_csshtmljsminify(self): + self._test_compressor('pipeline.compressors.csshtmljsminify.CssHtmlJsMinifyCompressor', + 'css', 'pipeline/compressors/csshtmljsminify.css') + self._test_compressor('pipeline.compressors.csshtmljsminify.CssHtmlJsMinifyCompressor', + 'js', 'pipeline/compressors/csshtmljsminify.js') + @skipUnless(settings.HAS_NODE, "requires node") - def test_uglifyjs(self): - self._test_compressor('pipeline.compressors.uglifyjs.UglifyJSCompressor', - 'js', 'pipeline/compressors/uglifyjs.js') - - @skipUnless(settings.HAS_NODE, "requires node") - def test_yuglify_js(self): - self._test_compressor('pipeline.compressors.yuglify.YuglifyCompressor', - 'js', 'pipeline/compressors/yuglify.js') - - @skipUnless(settings.HAS_NODE, "requires node") - def test_yuglify_css(self): + def test_yuglify(self): self._test_compressor('pipeline.compressors.yuglify.YuglifyCompressor', 'css', 'pipeline/compressors/yuglify.css') + self._test_compressor('pipeline.compressors.yuglify.YuglifyCompressor', + 'js', 'pipeline/compressors/yuglify.js') @skipUnless(settings.HAS_NODE, "requires node") def test_cssmin(self): diff --git a/tox.ini b/tox.ini index 810fcf64..16ce68f6 100644 --- a/tox.ini +++ b/tox.ini @@ -22,6 +22,7 @@ deps = jsmin==2.2.0 ply==3.4 slimit==0.8.1 + css-html-js-minify==2.5.5 setenv = DJANGO_SETTINGS_MODULE = tests.settings PYTHONPATH = {toxinidir}