darkpool
darkpool

Reputation: 14641

Django compressor does not uglify javascript

I wish to use Django Compressor to both minify and uglify my css and javascript. I have got it working such that I do an offline compression and all required javascript is compressed correctly.

The problem is that it does not uglify the code. My settings:

STATICFILES_FINDERS = (
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
    "compressor.finders.CompressorFinder",
)
COMPRESS_ENABLED = True
COMPRESS_OFFLINE = True
COMPRESS_YUGLIFY_BINARY = "yuglify"
COMPRESS_YUGLIFY_JS_ARGUMENTS = "--mangle"

yuglify is on my path. I tried running it manually on a file to test, ie:yuglify file.js --mangle. It turns out that yuglify (which is a wrapper around uglify-js) does not support es6 and above.

I found another uglifier called terser which works perfectly from the terminal with es6 code. I therefore tried to replace the above settings with terser, ie:

COMPRESS_YUGLIFY_BINARY = "terser"
COMPRESS_YUGLIFY_JS_ARGUMENTS = "--mangle"

This also does not work in django-compressor. The result is that the files get minified but not uglified.

I would appreciate any suggestions on getting this working either with django-compressor or with an alternative package.

Upvotes: 4

Views: 1771

Answers (1)

Krukas
Krukas

Reputation: 677

If you look at the docs the default setting for COMPRESS_JS_FILTERS is

COMPRESS_JS_FILTERS = ['compressor.filters.jsmin.JSMinFilter']

So you need to add the 'compressor.filters.yuglify.YUglifyJSFilter' filter to it.

COMPRESS_JS_FILTERS = [
  'compressor.filters.jsmin.JSMinFilter',
  'compressor.filters.yuglify.YUglifyJSFilter',
]

Upvotes: 4

Related Questions