Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document custom COMPRESS_FILTER implementation #1027

Open
dalescher opened this issue Nov 28, 2020 · 1 comment
Open

Document custom COMPRESS_FILTER implementation #1027

dalescher opened this issue Nov 28, 2020 · 1 comment

Comments

@dalescher
Copy link

dalescher commented Nov 28, 2020

I am trying to use terser https://github.com/terser/terser with django-compressor. It's working quite fine except that I have an issue I do not understand.

Once compressed, some of my output files are missing some data compared to the originals (some functions disappear especially). But when I run terser from the command line (outside of django) with the same argument, everything is fine. Here is what I use from the command line to get the right results:

terser <input_file> -c -m --keep-fnames --toplevel -o <output_file>

and here the equivalent arguments in settings.py :

COMPRESS_TERSER_JS_ARGUMENTS = "-c -m --keep-fnames --toplevel"

Here are the settings I have:

settings.py

COMPRESS_ENABLED = True
COMPRESS_OFFLINE = True
COMPRESS_CSS_HASHING_METHOD = None
COMPRESS_FILTERS = {
    'js': [
        'appName.compressor.filters.terser.TerserJSFilter',
    ]
}
COMPRESS_TERSER_BINARY = "terser"
COMPRESS_TERSER_JS_ARGUMENTS = "-c -m --keep-fnames --toplevel"
COMPRESS_URL = STATIC_URL
COMPRESS_OUTPUT_DIR = 'CACHE'

and here is the custom TerserJSFilter I have created (inspired from ClosureCompilerFilter):

terser.py in appName/compressor/filters folder

from compressor.conf import settings
from compressor.filters import CompilerFilter

class TerserJSFilter(CompilerFilter):
    command = "{binary} {args}"
    options = (
        ("binary", settings.COMPRESS_TERSER_BINARY),
        ("args", settings.COMPRESS_TERSER_JS_ARGUMENTS),
    )

Am I implementing terser correctly or missing something?
Is there a way to get more detailed logs from django-compressor in order to have a better understanding of what's going on?
Does someone have an idea about why I have some data not taken into account/deleted in my output compressed files?

Thank you in advance

@dalescher
Copy link
Author

dalescher commented Nov 28, 2020

ok, my bad. Looks like my issue comes from terser, not from django-compressor. Looks like using --toplevel argument has some nice effects I do not understand yet.

I prefer to not delete my post since I have not found any information about how to implement a custom filter like terser with django-compressor. So it might useful to others.

If you have any hint about terser behavior with --toplevel and why some functions and parts of code are deleted please let me know.

EDIT: Finally found why some code was remove. "-c" argument, which stands for "--compress" is also performing dead code removal. Since I am using some helper functions for other scripts, they were considered as dead code because not used in the scope of the analyzed file. nothing to do with --toplevel directly. To solve this, I passed the following option to compress: 'dead_code=false, unused=false'. My final arguments setting for TERSER in settings.py is now:

COMPRESS_TERSER_JS_ARGUMENTS = "-c dead_code=false,unused=false -m --keep-fnames --toplevel --ecma 2020"

@karyon karyon changed the title terser support and custom COMPRESS_FILTER implementation Document custom COMPRESS_FILTER implementation Jan 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants