示例#1
0
def minify_website(args):
    css_in = ' '.join(get_css_in(args))
    css_out = f'{args.output_dir}/css/base.css'
    if args.minify:
        command = f"purifycss -w '*algolia*' --min {css_in} '{args.output_dir}/*.html' " \
            f"'{args.output_dir}/docs/en/**/*.html' '{args.website_dir}/js/**/*.js' > {css_out}"
    else:
        command = f'cat {css_in} > {css_out}'
    logging.info(command)
    output = subprocess.check_output(command, shell=True)
    logging.debug(output)
    with open(css_out, 'rb') as f:
        css_digest = hashlib.sha3_224(f.read()).hexdigest()[0:8]

    js_in = get_js_in(args)
    js_out = f'{args.output_dir}/js/base.js'
    if args.minify:
        js_in = [js[1:-1] for js in js_in]
        closure_args = [
            '--js', *js_in, '--js_output_file', js_out, '--compilation_level',
            'SIMPLE', '--dependency_mode', 'NONE', '--third_party',
            '--use_types_for_optimization', '--isolation_mode', 'IIFE'
        ]
        logging.info(closure_args)
        if closure.run(*closure_args):
            raise RuntimeError('failed to run closure compiler')

    else:
        logging.info(command)
        js_in = ' '.join(js_in)
        output = subprocess.check_output(f'cat {js_in} > {js_out}', shell=True)
        logging.debug(output)
    with open(js_out, 'rb') as f:
        js_digest = hashlib.sha3_224(f.read()).hexdigest()[0:8]
        logging.info(js_digest)

    if args.minify:
        logging.info('Minifying website')
        for root, _, filenames in os.walk(args.output_dir):
            for filename in filenames:
                path = os.path.join(root, filename)
                if not (filename.endswith('.html')
                        or filename.endswith('.css')):
                    continue

                logging.info('Minifying %s', path)
                with open(path, 'rb') as f:
                    content = f.read().decode('utf-8')
                if filename.endswith('.html'):
                    content = htmlmin.minify(content, remove_empty_space=False)
                    content = content.replace('base.css?css_digest',
                                              f'base.css?{css_digest}')
                    content = content.replace('base.js?js_digest',
                                              f'base.js?{js_digest}')
                elif filename.endswith('.css'):
                    content = cssmin.cssmin(content)
                elif filename.endswith('.js'):
                    content = jsmin.jsmin(content)
                with open(path, 'wb') as f:
                    f.write(content.encode('utf-8'))
示例#2
0
def minify_website(args):
    css_in = ' '.join(get_css_in(args))
    css_out = f'{args.output_dir}/css/base.css'
    if args.minify:
        command = f"purifycss -w '*algolia*' --min {css_in} '{args.output_dir}/*.html' " \
            f"'{args.output_dir}/docs/en/**/*.html' '{args.website_dir}/js/**/*.js' > {css_out}"
    else:
        command = f'cat {css_in} > {css_out}'

    logging.info(command)
    output = subprocess.check_output(command, shell=True)
    logging.debug(output)
    with open(css_out, 'rb') as f:
        css_digest = hashlib.sha3_224(f.read()).hexdigest()[0:8]

    js_in = get_js_in(args)
    js_out = f'{args.output_dir}/js/base.js'
    if args.minify:
        js_in = [js[1:-1] for js in js_in]
        closure_args = [
            '--js', *js_in, '--js_output_file', js_out,
            '--compilation_level', 'SIMPLE',
            '--dependency_mode', 'NONE',
            '--third_party', '--use_types_for_optimization',
            '--isolation_mode', 'IIFE'
        ]
        logging.info(closure_args)
        if closure.run(*closure_args):
            raise RuntimeError('failed to run closure compiler')
        with open(js_out, 'r') as f:
            js_content = jsmin.jsmin(f.read())
        with open(js_out, 'w') as f:
            f.write(js_content)

    else:
        js_in = ' '.join(js_in)
        command = f'cat {js_in} > {js_out}'
        logging.info(command)
        output = subprocess.check_output(command, shell=True)
        logging.debug(output)
    with open(js_out, 'rb') as f:
        js_digest = hashlib.sha3_224(f.read()).hexdigest()[0:8]
        logging.info(js_digest)

    if args.minify:
        logging.info('Minifying website')
        with concurrent.futures.ThreadPoolExecutor() as executor:
            futures = []
            for root, _, filenames in os.walk(args.output_dir):
                for filename in filenames:
                    path = os.path.join(root, filename)
                    futures.append(executor.submit(minify_file, path, css_digest, js_digest))
            for future in futures:
                exc = future.exception()
                if exc:
                    logging.error(exc)
                    sys.exit(1)
示例#3
0
def minify_website(args):
    css_in = " ".join(get_css_in(args))
    css_out = f"{args.output_dir}/docs/css/base.css"
    os.makedirs(f"{args.output_dir}/docs/css")

    if args.minify and False:  # TODO: return closure
        command = (
            f"purifycss -w '*algolia*' --min {css_in} '{args.output_dir}/*.html' "
            f"'{args.output_dir}/docs/en/**/*.html' '{args.website_dir}/js/**/*.js' > {css_out}"
        )
        logging.info(css_in)
        logging.info(command)
        output = subprocess.check_output(command, shell=True)
        logging.debug(output)

    else:
        command = f"cat {css_in}"
        output = subprocess.check_output(command, shell=True)
        with open(css_out, "wb+") as f:
            f.write(output)

    with open(css_out, "rb") as f:
        css_digest = hashlib.sha3_224(f.read()).hexdigest()[0:8]

    js_in = " ".join(get_js_in(args))
    js_out = f"{args.output_dir}/docs/js/base.js"
    os.makedirs(f"{args.output_dir}/docs/js")

    if args.minify and False:  # TODO: return closure
        js_in = [js[1:-1] for js in js_in]
        closure_args = [
            "--js",
            *js_in,
            "--js_output_file",
            js_out,
            "--compilation_level",
            "SIMPLE",
            "--dependency_mode",
            "NONE",
            "--third_party",
            "--use_types_for_optimization",
            "--isolation_mode",
            "IIFE",
        ]
        logging.info(closure_args)
        if closure.run(*closure_args):
            raise RuntimeError("failed to run closure compiler")
        with open(js_out, "r") as f:
            js_content = jsmin.jsmin(f.read())
        with open(js_out, "w") as f:
            f.write(js_content)

    else:
        command = f"cat {js_in}"
        output = subprocess.check_output(command, shell=True)
        with open(js_out, "wb+") as f:
            f.write(output)

    with open(js_out, "rb") as f:
        js_digest = hashlib.sha3_224(f.read()).hexdigest()[0:8]
        logging.info(js_digest)

    if args.minify:
        logging.info("Minifying website")
        with concurrent.futures.ThreadPoolExecutor() as executor:
            futures = []
            for root, _, filenames in os.walk(args.output_dir):
                for filename in filenames:
                    path = os.path.join(root, filename)
                    futures.append(
                        executor.submit(minify_file, path, css_digest, js_digest)
                    )
            for future in futures:
                exc = future.exception()
                if exc:
                    logging.error(exc)
                    sys.exit(1)