"""Minify Javascript using `UglifyJS <https://github.com/mishoo/UglifyJS/>`_.

UglifyJS is an external tool written for NodeJS; this filter assumes that
the ``uglifyjs`` executable is in the path. Otherwise, you may define
a ``UGLIFYJS_BIN`` setting. Additional options may be passed to ``uglifyjs``
by setting ``UGLIFYJS_EXTRA_ARGS``, which expects a list of strings.
"""

import subprocess
from webassets.exceptions import FilterError
from webassets.filter import Filter


__all__ = ('UglifyJSFilter',)


class UglifyJSFilter(Filter):

    name = 'uglifyjs'

    def setup(self):
        self.binary = self.get_config(
            'UGLIFYJS_BIN', require=False) or 'uglifyjs'
        self.extra_args = self.get_config('UGLIFYJS_EXTRA_ARGS',
                                          require=False)

    def output(self, _in, out, **kw):
        args = [self.binary]
        if self.extra_args:
            args.extend(self.extra_args)
        proc = subprocess.Popen(
            args, stdin=subprocess.PIPE,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
        stdout, stderr = proc.communicate(_in.read())

        if proc.returncode != 0:
            raise FilterError(('uglifyjs: subprocess had error: stderr=%s, '+
                               'stdout=%s, returncode=%s') % (
                                    stderr, stdout, proc.returncode))
        out.write(stdout)

@@ -1,11 +1,183 @@
-%22%22%22
+import subprocess%0Afrom webassets.exceptions import FilterError%0Afrom webassets.filter import Filter%0A%0A%0A__all__ = ('UglifyJSFilter',)%0A%0A%0Aclass UglifyJSFilter(Filter):%0A    %22%22%22%0A    
 Minify J
@@ -244,16 +244,20 @@
 S/%3E%60_.%0A%0A
+    
 UglifyJS
@@ -322,16 +322,20 @@
 es that%0A
+    
 the %60%60ug
@@ -396,16 +396,20 @@
  define%0A
+    
 a %60%60UGLI
@@ -427,16 +427,21 @@
 setting.
+%0A%0A   
  Additio
@@ -485,19 +485,26 @@
 js%60%60
-%0Aby
+ using the
  setting
  %60%60U
@@ -499,16 +499,20 @@
  setting
+%0A   
  %60%60UGLIF
@@ -566,175 +566,15 @@
 gs.%0A
-%22%22%22%0A%0Aimport subprocess%0Afrom webassets.exceptions import FilterError%0Afrom webassets.filter import Filter%0A%0A%0A__all__ = ('UglifyJSFilter',)%0A%0A%0Aclass UglifyJSFilter(Filter):
+    %22%22%22
 %0A%0A  

Make UglifyJSFilter docstring more consistent with other filters