Beispiel #1
0
def minify_sources(sources, ext, fs_root=''):
    import cssutils 

    if 'js' in ext:
        js_minify = JavascriptMinify()
    minified_sources = []

    for source in sources:
        # generate full path to source
        no_ext_source = os.path.splitext(source)[0]
        full_source = os.path.join(fs_root, (no_ext_source + ext).lstrip('/'))

        # generate minified source path
        full_source = os.path.join(fs_root, (source).lstrip('/'))
        no_ext_full_source = os.path.splitext(full_source)[0]
        minified = no_ext_full_source + ext

        f_minified_source = open(minified, 'w')

        # minify js source (read stream is auto-closed inside)
        if 'js' in ext:
            js_minify.minify(open(full_source, 'r'), f_minified_source)
        # minify css source
        if 'css' in ext:
            serializer = get_serializer()
            sheet = cssutils.parseFile(full_source)
            sheet.setSerializer(serializer)
            cssutils.ser.prefs.useMinified()
            f_minified_source.write(sheet.cssText)

        f_minified_source.close()
        minified_sources.append(no_ext_source + ext)

    return minified_sources
Beispiel #2
0
    def run(self):
        self.mkpath(self.build_dir)

        outfiles = []
        updated_files = []

        # consider replacing this with a better utility
        jsm = JavascriptMinify()

        for f in self.web_files:
            inf = convert_path(f)
            outf = os.path.join(self.build_dir, f)

            self.mkpath(os.path.dirname(outf))

            if self.compress:
                log.info("minifying %s -> %s" % (f, outf))

                input = open(inf, 'r')
                output = open(outf, 'wb')

                if f.endswith('.js'):
                    # eat the first line (JSLint directive)
                    input.readline()

                # copy 5 lines of the header
                for l in range(6):
                    output.write(input.readline())

                if f.endswith('.js'):
                    jsm.minify(input, output)
                elif f.endswith('.css'):
                    output.write(cssmin(input.read()))

                input.close()
                output.close()
            else:
                self.copy_file(inf, outf)

            self.outfiles.append(outf)

        # build a regular expression to substitute the HTML files
        regex = ';'.join([
            's/{{%s}}/%s/' % (re.escape(k), re.escape(v[self.compress]))
            for k, v in self.html_subst.iteritems()
        ])
        regex += ';s/{{VERSION}}/%s/' % (re.escape(self.version))

        for f in self.html_files:
            inf = convert_path(f)
            outf = os.path.join(self.build_dir, os.path.basename(f))

            log.info("substituting %s -> %s" % (f, outf))
            output = open(outf, 'wb')

            # use sed, because I like sed
            subprocess.check_call(['sed', regex, inf], stdout=output)
            output.close()

            self.outfiles.append(outf)
Beispiel #3
0
    def open(self):
        output = BytesIO()
        minify = JavascriptMinify(self._file.open(),
                                  output,
                                  quote_chars="'\"`")
        minify.minify()
        output.seek(0)

        if not self._verify_command:
            return output

        input_source = self._file.open().read()
        output_source = output.getvalue()

        with NamedTemporaryFile() as fh1, NamedTemporaryFile() as fh2:
            fh1.write(input_source)
            fh2.write(output_source)
            fh1.flush()
            fh2.flush()

            try:
                args = list(self._verify_command)
                args.extend([fh1.name, fh2.name])
                subprocess.check_output(args, stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as e:
                errors.warn('JS minification verification failed for %s:' %
                            (getattr(self._file, 'path', '<unknown>')))
                # Prefix each line with "Warning:" so mozharness doesn't
                # think these error messages are real errors.
                for line in e.output.splitlines():
                    errors.warn(line)

                return self._file.open()

        return output
Beispiel #4
0
def minify_sources(sources, ext, fs_root=''):
    import cssutils

    if 'js' in ext:
        js_minify = JavascriptMinify()
    minified_sources = []

    for source in sources:
        # generate full path to source
        no_ext_source = os.path.splitext(source)[0]
        full_source = os.path.join(fs_root, (no_ext_source + ext).lstrip('/'))

        # generate minified source path
        full_source = os.path.join(fs_root, (source).lstrip('/'))
        no_ext_full_source = os.path.splitext(full_source)[0]
        minified = no_ext_full_source + ext

        f_minified_source = open(minified, 'w')

        # minify js source (read stream is auto-closed inside)
        if 'js' in ext:
            js_minify.minify(open(full_source, 'r'), f_minified_source)
        # minify css source
        if 'css' in ext:
            serializer = get_serializer()
            sheet = cssutils.parseFile(full_source)
            sheet.setSerializer(serializer)
            cssutils.ser.prefs.useMinified()
            f_minified_source.write(sheet.cssText)

        f_minified_source.close()
        minified_sources.append(no_ext_source + ext)

    return minified_sources
Beispiel #5
0
    def open(self):
        output = BytesIO()
        minify = JavascriptMinify(self._file.open(), output, quote_chars="'\"`")
        minify.minify()
        output.seek(0)

        if not self._verify_command:
            return output

        input_source = self._file.open().read()
        output_source = output.getvalue()

        with NamedTemporaryFile() as fh1, NamedTemporaryFile() as fh2:
            fh1.write(input_source)
            fh2.write(output_source)
            fh1.flush()
            fh2.flush()

            try:
                args = list(self._verify_command)
                args.extend([fh1.name, fh2.name])
                subprocess.check_output(args, stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as e:
                errors.warn('JS minification verification failed for %s:' %
                    (getattr(self._file, 'path', '<unknown>')))
                # Prefix each line with "Warning:" so mozharness doesn't
                # think these error messages are real errors.
                for line in e.output.splitlines():
                    errors.warn(line)

                return self._file.open()

        return output
def main():
    parser = OptionParser(
        usage="usage: %prog [options] header source [input [input...]]")
    parser.add_option('--no-minify',
                      action='store_true',
                      help='Do not run the input files through jsmin')
    parser.add_option('-n', '--namespace', help='Namespace to use')
    (options, arguments) = parser.parse_args()
    if not options.namespace:
        print 'Error: must provide a namespace'
        parser.print_usage()
        exit(-1)
    if len(arguments) < 3:
        print 'Error: must provide at least 3 arguments'
        parser.print_usage()
        exit(-1)

    namespace = options.namespace
    headerPath = arguments[0]
    sourcePath = arguments[1]
    inputPaths = arguments[2:]

    headerFile = open(headerPath, 'w')
    print >> headerFile, 'namespace {0:s} {{'.format(namespace)

    sourceFile = open(sourcePath, 'w')
    print >> sourceFile, '#include "{0:s}"'.format(
        os.path.basename(headerPath))
    print >> sourceFile, 'namespace {0:s} {{'.format(namespace)

    jsm = JavascriptMinify()

    for inputFileName in inputPaths:
        inputStream = io.FileIO(inputFileName)
        outputStream = StringIO()

        if not options.no_minify:
            jsm.minify(inputStream, outputStream)
            characters = outputStream.getvalue()
        else:
            characters = inputStream.read()

        size = len(characters)
        variableName = os.path.splitext(os.path.basename(inputFileName))[0]

        print >> headerFile, 'extern const char {0:s}JavaScript[{1:d}];'.format(
            variableName, size)
        print >> sourceFile, 'const char {0:s}JavaScript[{1:d}] = {{'.format(
            variableName, size)

        codepoints = map(ord, characters)
        for codepointChunk in chunk(codepoints, 16):
            print >> sourceFile, '    {0:s},'.format(','.join(
                map(stringifyCodepoint, codepointChunk)))

        print >> sourceFile, '};'

    print >> headerFile, '}} // namespace {0:s}'.format(namespace)
    print >> sourceFile, '}} // namespace {0:s}'.format(namespace)
Beispiel #7
0
def minify(filename):
    """Given a filename, handle minifying it as -min.js"""
    if not is_min(filename):
        new_filename = re.sub(".js$", "-min.js", filename)

        with open(filename) as shrink_me:
            with open(new_filename, 'w') as tobemin:
                jsm = JavascriptMinify()
                jsm.minify(shrink_me, tobemin)
Beispiel #8
0
def minify(filename):
    """Given a filename, handle minifying it as -min.js"""
    if not is_min(filename):
        new_filename = re.sub(".js$", "-min.js", filename)

        with open(filename) as shrink_me:
            with open(new_filename, 'w') as tobemin:
                jsm = JavascriptMinify()
                jsm.minify(shrink_me, tobemin)
Beispiel #9
0
def compileString(code, minify=True):
	code = ElementMacro.fromCode(code)
	code = UriOfMacro.fromCode(code)
	code = Javascript.fromCode(code)
	
	if minify:
		jsm = JavascriptMinify()
		out = StringIO()
		jsm.minify(StringIO(code), out)
		code = out.getvalue()
	
	return code.strip()
def main():
    parser = OptionParser(usage="usage: %prog [options] header source [input [input...]]")
    parser.add_option('--no-minify', action='store_true', help='Do not run the input files through jsmin')
    parser.add_option('-n', '--namespace', help='Namespace to use')
    (options, arguments) = parser.parse_args()
    if not options.namespace:
        print('Error: must provide a namespace')
        parser.print_usage()
        exit(-1)
    if len(arguments) < 3:
        print('Error: must provide at least 3 arguments')
        parser.print_usage()
        exit(-1)

    namespace = options.namespace
    headerPath = arguments[0]
    sourcePath = arguments[1]
    inputPaths = arguments[2:]

    headerFile = open(headerPath, 'w')
    print('namespace {0:s} {{'.format(namespace), file=headerFile)

    sourceFile = open(sourcePath, 'w')
    print('#include "{0:s}"'.format(os.path.basename(headerPath)), file=sourceFile)
    print('namespace {0:s} {{'.format(namespace), file=sourceFile)

    jsm = JavascriptMinify()

    for inputFileName in inputPaths:
        inputStream = io.FileIO(inputFileName)
        outputStream = StringIO()

        if not options.no_minify:
            jsm.minify(inputStream, outputStream)
            characters = outputStream.getvalue()
        else:
            characters = inputStream.read()

        size = len(characters)
        variableName = os.path.splitext(os.path.basename(inputFileName))[0]

        print('extern const char {0:s}JavaScript[{1:d}];'.format(variableName, size), file=headerFile)
        print('const char {0:s}JavaScript[{1:d}] = {{'.format(variableName, size), file=sourceFile)

        codepoints = list(map(ord, characters))
        for codepointChunk in chunk(codepoints, 16):
            print('    {0:s},'.format(','.join(map(stringifyCodepoint, codepointChunk))), file=sourceFile)

        print('};', file=sourceFile)

    print('}} // namespace {0:s}'.format(namespace), file=headerFile)
    print('}} // namespace {0:s}'.format(namespace), file=sourceFile)
Beispiel #11
0
 def write_minify(cls, source, dest):
     from pecan import conf
     redis = redis_connector()
     buff = StringIO.StringIO()
     JavascriptMinify().minify(source, buff)
     redis = redis_connector()
     redis.set(dest.replace(conf.app.static_root, ''), buff.getvalue())
Beispiel #12
0
 def get_js_minified(self, codestr):
     ins = StringIO(codestr)
     outs = StringIO()
     JavascriptMinify().minify(ins, outs)
     min_js = outs.getvalue()
     if len(min_js) > 0 and min_js[0] == '\n':
         min_js = min_js[1:]
     return re.sub(r'(\n|\r)+', '', min_js)
Beispiel #13
0
def minify_js(file_path, src):
    """Returns a minified version of the given JavaScript source string."""
    in_str = StringIO(src)
    out_str = StringIO()
    JavascriptMinify().minify(in_str, out_str)
    src = out_str.getvalue()
    in_str.close()
    out_str.close()
    return src
Beispiel #14
0
    def open(self):
        output = six.StringIO()
        minify = JavascriptMinify(codecs.getreader("utf-8")(self._file.open()),
                                  output,
                                  quote_chars="'\"`")
        minify.minify()
        output.seek(0)
        output_source = six.ensure_binary(output.getvalue())
        output = BytesIO(output_source)

        if not self._verify_command:
            return output

        input_source = self._file.open().read()

        with NamedTemporaryFile("wb+") as fh1, NamedTemporaryFile(
                "wb+") as fh2:
            fh1.write(input_source)
            fh2.write(output_source)
            fh1.flush()
            fh2.flush()

            try:
                args = list(self._verify_command)
                args.extend([fh1.name, fh2.name])
                subprocess.check_output(args,
                                        stderr=subprocess.STDOUT,
                                        universal_newlines=True)
            except subprocess.CalledProcessError as e:
                errors.warn("JS minification verification failed for %s:" %
                            (getattr(self._file, "path", "<unknown>")))
                # Prefix each line with "Warning:" so mozharness doesn't
                # think these error messages are real errors.
                for line in e.output.splitlines():
                    errors.warn(line)

                return self._file.open()

        return output
    def get_minified_js_str(self, js):

        ins = StringIO(js)
        outs = StringIO()

        JavascriptMinify().minify(ins, outs)

        min_js = outs.getvalue()

        if len(min_js) > 0 and min_js[0] == '\n':
            min_js = min_js[1:]

        min_js = re.sub(r'(\n|\r)+', '', min_js)

        return min_js
Beispiel #16
0
    def on_minifier_js_activate(self, action):
        doc = self._window.get_active_document()
        if not doc:
            return

        doctxt = doc.get_text(doc.get_iter_at_line(0), doc.get_end_iter())

        ins = StringIO(doctxt)
        outs = StringIO()

        JavascriptMinify().minify(ins, outs)

        min_js = outs.getvalue()

        if len(min_js) > 0 and min_js[0] == '\n':
            min_js = min_js[1:]

        min_js = re.sub(r'(\n|\r)+', '', min_js)

        self.handle_new_output("Minified JS Copied to CLipboard", min_js)
Beispiel #17
0
 def write_minify(cls, source, dest):
     dest = open(dest, 'w')
     JavascriptMinify().minify(source, dest)
     dest.close()
if __name__ == '__main__':
    import sys, os

    if len(sys.argv) != 3:
      print "Usage: gen-tiall.py <path to tiall.js> <dir of source js>"
      sys.exit(1)

    tiall_js = sys.argv[1]
    src_dir = sys.argv[2]
    tilist_file = os.path.join(src_dir, "tilist.txt")
    l = open(tilist_file, "r")
    sources = []
    for n in l:
        n = n.strip()
        if len(n) > 0:
            sources.append(os.path.join(src_dir,n))

    l.close()

    print "Dest: %s" % (tiall_js)
    outf = open(tiall_js, "w+")

    for f in sources:
        inf = open(f,"r")
        print "Minifying: %s" % (f)
        jsm = JavascriptMinify()
        jsm.minify(inf, outf)

    outf.close()

Beispiel #19
0
 def apply(self, _in, out):
     JavascriptMinify().minify(_in, out)
Beispiel #20
0
# Copyright (c) 2013 Dave St.Germain
#·
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#·
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#·
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import sys, os, glob
from jsmin import JavascriptMinify

for f in sys.argv[1:]:
    with open(f, 'r') as js:
        minifier = JavascriptMinify(js, sys.stdout)
        minifier.minify()
    sys.stdout.write('\n')