Example #1
0
    def css_with_sourcemap(self, content_import_rules):
        """Create the ir.attachment representing the not-minified content of the bundleCSS
        and create/modify the ir.attachment representing the linked sourcemap.

        :param content_import_rules: string containing all the @import rules to put at the beginning of the bundle
        :return ir.attachment representing the un-minified content of the bundleCSS
        """
        sourcemap_attachment = self.get_attachments('css.map') \
                                or self.save_attachment('css.map', '')
        debug_asset_url = self.get_debug_asset_url(
            name=self.name,
            extra='rtl/' if self.user_direction == 'rtl' else '')
        generator = SourceMapGenerator(source_root="/".join(
            [".."
             for i in range(0,
                            len(debug_asset_url.split("/")) - 2)]) + "/", )
        self.preprocess_css()

        # adds the @import rules at the beginning of the bundle
        content_bundle_list = [content_import_rules]
        content_line_count = len(content_import_rules.split("\n"))
        for asset in self.stylesheets:
            if asset.content:
                content = asset.with_header(asset.content)
                if asset.url:
                    generator.add_source(asset.url, content,
                                         content_line_count)
                # comments all @import rules that have been added at the beginning of the bundle
                content = re.sub(self.rx_css_import,
                                 lambda matchobj: f"/* {matchobj.group(0)} */",
                                 content)
                content_bundle_list.append(content)
                content_line_count += len(content.split("\n"))

        content_bundle = '\n'.join(
            content_bundle_list
        ) + f"\n//*# sourceMappingURL={sourcemap_attachment.url} */"
        css_attachment = self.save_attachment('css', content_bundle)

        generator._file = css_attachment.url
        sourcemap_attachment.write({
            "raw": generator.get_content(),
        })

        return css_attachment
Example #2
0
    def js_with_sourcemap(self):
        """Create the ir.attachment representing the not-minified content of the bundleJS
        and create/modify the ir.attachment representing the linked sourcemap.

        :return ir.attachment representing the un-minified content of the bundleJS
        """
        sourcemap_attachment = self.get_attachments('js.map') \
                                or self.save_attachment('js.map', '')
        generator = SourceMapGenerator(source_root="/".join([
            ".." for i in range(
                0,
                len(self.get_debug_asset_url(name=self.name).split("/")) - 2)
        ]) + "/", )

        content_bundle_list = []
        content_line_count = 0
        line_header = 6  # number of lines added by with_header()
        for asset in self.javascripts:
            if asset.is_transpiled:
                # '+ 3' corresponds to the 3 lines added at the beginning of the file during transpilation.
                generator.add_source(asset.url,
                                     asset._content,
                                     content_line_count,
                                     start_offset=line_header + 3)
            else:
                generator.add_source(asset.url,
                                     asset.content,
                                     content_line_count,
                                     start_offset=line_header)

            content_bundle_list.append(
                asset.with_header(asset.content, minimal=False))
            content_line_count += len(asset.content.split("\n")) + line_header

        content_bundle = ';\n'.join(
            content_bundle_list
        ) + "\n//# sourceMappingURL=" + sourcemap_attachment.url
        js_attachment = self.save_attachment('js', content_bundle)

        generator._file = js_attachment.url
        sourcemap_attachment.write({"raw": generator.get_content()})

        return js_attachment