Esempio n. 1
0
    def optimizeFile(self,
                     file,
                     callback,
                     minimize=False,
                     new_path=None,
                     prepend="opt"):
        """optimizes a single file

        Arguments:
        file -- path to file
        callback -- function to run the file through
        minimize -- whether or not we should minimize the file contents (html)
        prepend -- what extension to prepend

        Returns:
        void

        """
        content = callback(file)
        if new_path is None:
            new_path = Util.prependExtension(prepend, file)
        if minimize is True:
            self.output("minimizing " + file)
            content = self.minimize(content)
        self.output("optimizing " + file + " to " + new_path)
        Util.filePutContents(new_path, content)

        if self.config.show_savings:
            SizeTracker.trackFile(file, new_path)
Esempio n. 2
0
    def optimizeJsManifest(self):
        contents = Util.fileGetContents(self.config.js_manifest)

        for key, value in self.manifest_ids.items():
            if "#" + value in self.id_map:
                contents = re.sub(r'((?<!\$)\${1}[A-Z0-9_]+\s?=\s?[\'|\"])(' + value + ')([\'|\"][,|;])', r'\1' + self.id_map["#" + value].replace("#", "") + r'\3', contents)

        for key, value in self.manifest_classes.items():
            if "." + value in self.class_map:
                contents = re.sub(r'(\${2}[A-Z0-9_]+\s?=\s?[\'|\"])(' + value + ')([\'|\"][,|;])', r'\1' + self.class_map["." + value].replace(".", "") + r'\3', contents)

        if self.config.rewrite_constants:
            constants = re.findall(r'(\s+?(var\s)?([A-Z0-9_]+)\s?=\s?[\'|\"](.*?)[\'|\"][,|;])', contents)
            new_constants = {}
            i = 0
            for constant in constants:
                # underscore variables are ignored
                if constant[2][0] == "_":
                    continue

                i += 1
                new_constant = re.sub(r'=(.*)([,|;])','= ' + str(i) + r'\2', constant[0])
                contents = contents.replace(constant[0], new_constant)

        new_manifest = Util.prependExtension("opt", self.config.js_manifest)
        Util.filePutContents(new_manifest, contents)

        if self.config.show_savings:
            SizeTracker.trackFile(self.config.js_manifest, new_manifest)
Esempio n. 3
0
    def optimizeJsManifest(self):
        contents = Util.fileGetContents(self.config.js_manifest)

        for key, value in self.manifest_ids.items():
            if "#" + value in self.id_map:
                contents = re.sub(r'((?<!\$)\${1}[A-Z0-9_]+\s?=\s?[\'|\"])(' + value + ')([\'|\"][,|;])', r'\1' + self.id_map["#" + value].replace("#", "") + r'\3', contents)

        for key, value in self.manifest_classes.items():
            if "." + value in self.class_map:
                contents = re.sub(r'(\${2}[A-Z0-9_]+\s?=\s?[\'|\"])(' + value + ')([\'|\"][,|;])', r'\1' + self.class_map["." + value].replace(".", "") + r'\3', contents)

        if self.config.rewrite_constants:
            constants = re.findall(r'(\s+?(var\s)?([A-Z0-9_]+)\s?=\s?[\'|\"](.*?)[\'|\"][,|;])', contents)
            new_constants = {}
            i = 0
            for constant in constants:
                # underscore variables are ignored
                if constant[2][0] == "_":
                    continue

                i += 1
                new_constant = re.sub(r'=(.*)([,|;])','= ' + str(i) + r'\2', constant[0])
                contents = contents.replace(constant[0], new_constant)

        new_manifest = Util.prependExtension("opt", self.config.js_manifest)
        Util.filePutContents(new_manifest, contents)

        if self.config.show_savings:
            SizeTracker.trackFile(self.config.js_manifest, new_manifest)
Esempio n. 4
0
    def optimizeFile(self, file, callback, minimize = False, new_path = None, prepend = "opt"):
        """optimizes a single file

        Arguments:
        file -- path to file
        callback -- function to run the file through
        minimize -- whether or not we should minimize the file contents (html)
        prepend -- what extension to prepend

        Returns:
        void

        """
        content = callback(file)
        if new_path is None:
            new_path = Util.prependExtension(prepend, file)
        if minimize is True:
            self.output("minimizing " + file)
            content = self.minimize(content)
        self.output("optimizing " + file + " to " + new_path)
        Util.filePutContents(new_path, content)

        if self.config.show_savings:
            SizeTracker.trackFile(file, new_path)