(filename)
| 2559 | |
| 2560 | @ToolchainProfiler.profile() |
| 2561 | def minify_html(filename): |
| 2562 | if settings.DEBUG_LEVEL >= 2: |
| 2563 | return |
| 2564 | |
| 2565 | opts = [] |
| 2566 | # -g1 and greater retain whitespace and comments in source |
| 2567 | if settings.DEBUG_LEVEL == 0: |
| 2568 | opts += ['--collapse-whitespace', |
| 2569 | '--remove-comments', |
| 2570 | '--remove-tag-whitespace', |
| 2571 | '--sort-attributes', |
| 2572 | '--sort-class-name'] |
| 2573 | # -g2 and greater do not minify HTML at all |
| 2574 | if settings.DEBUG_LEVEL <= 1: |
| 2575 | opts += ['--decode-entities', |
| 2576 | '--collapse-boolean-attributes', |
| 2577 | '--remove-attribute-quotes', |
| 2578 | '--remove-redundant-attributes', |
| 2579 | '--remove-script-type-attributes', |
| 2580 | '--remove-style-link-type-attributes', |
| 2581 | '--use-short-doctype', |
| 2582 | '--minify-css', 'true', |
| 2583 | '--minify-js', 'true'] |
| 2584 | |
| 2585 | # html-minifier also has the following options, but they look unsafe for use: |
| 2586 | # '--collapse-inline-tag-whitespace': removes whitespace between inline tags in visible text, |
| 2587 | # causing words to be joined together. See |
| 2588 | # https://github.com/terser/html-minifier-terser/issues/179 |
| 2589 | # https://github.com/emscripten-core/emscripten/issues/22188 |
| 2590 | # '--remove-optional-tags': removes e.g. <head></head> and <body></body> tags from the page. |
| 2591 | # (Breaks at least browser.test_sdl2glshader) |
| 2592 | # '--remove-empty-attributes': removes all attributes with whitespace-only values. |
| 2593 | # (Breaks at least browser.test_asmfs_hello_file) |
| 2594 | # '--remove-empty-elements': removes all elements with empty contents. |
| 2595 | # (Breaks at least browser.test_asm_swapping) |
| 2596 | |
| 2597 | logger.debug(f'minifying HTML file {filename}') |
| 2598 | size_before = os.path.getsize(filename) |
| 2599 | shared.check_call([*shared.get_npm_cmd('html-minifier-terser'), filename, '-o', filename, *opts], env=shared.env_with_node_in_path()) |
| 2600 | |
| 2601 | # HTML minifier will turn all null bytes into an escaped two-byte sequence "\0". Turn those back to single byte sequences. |
| 2602 | def unescape_nulls(filename): |
| 2603 | data = read_file(filename) |
| 2604 | out = [] |
| 2605 | in_escape = False |
| 2606 | i = 0 |
| 2607 | while i < len(data): |
| 2608 | ch = data[i] |
| 2609 | i += 1 |
| 2610 | if ch == '\\': |
| 2611 | if in_escape: |
| 2612 | out.append('\\\\') |
| 2613 | in_escape = not in_escape |
| 2614 | elif in_escape: |
| 2615 | in_escape = False |
| 2616 | if ch == '0': |
| 2617 | out.append('\x00') # Convert '\\0' (5Ch 00h) into '\0' (00h) |
| 2618 | elif ch == 'x' and data[i] == '0' and data[i + 1] == '0': |
no test coverage detected