(self, shell, minify_whitespace)
| 75 | self.profiling_funcs = False |
| 76 | |
| 77 | def minify_shell(self, shell, minify_whitespace): |
| 78 | # Run through acorn-optimizer.mjs to find and minify the global symbols |
| 79 | # We send it the globals, which it parses at the proper time. JS decides how |
| 80 | # to minify all global names, we receive a dictionary back, which is then |
| 81 | # used by the function processors |
| 82 | |
| 83 | shell = shell.replace('0.0', '13371337') # avoid optimizer doing 0.0 => 0 |
| 84 | |
| 85 | # Find all globals in the JS functions code |
| 86 | |
| 87 | if not self.profiling_funcs: |
| 88 | self.globs = [m.group(1) for m in func_sig.finditer(self.js)] |
| 89 | if len(self.globs) == 0: |
| 90 | self.globs = [m.group(1) for m in func_sig_json.finditer(self.js)] |
| 91 | else: |
| 92 | self.globs = [] |
| 93 | |
| 94 | with temp_files.get_file('.minifyglobals.js') as temp_file: |
| 95 | with open(temp_file, 'w', encoding='utf-8') as f: |
| 96 | f.write(shell) |
| 97 | f.write('\n') |
| 98 | f.write('// EXTRA_INFO:' + json.dumps(self.serialize())) |
| 99 | |
| 100 | cmd = [*get_acorn_cmd(), temp_file, 'minifyGlobals'] |
| 101 | if minify_whitespace: |
| 102 | cmd.append('--minify-whitespace') |
| 103 | output = utils.run_process(cmd, stdout=subprocess.PIPE).stdout |
| 104 | |
| 105 | assert len(output) and not output.startswith('Assertion failed'), 'Error in js optimizer: ' + output |
| 106 | code, metadata = output.split('// EXTRA_INFO:') |
| 107 | self.globs = json.loads(metadata) |
| 108 | |
| 109 | if self.symbols_file: |
| 110 | mapping = '\n'.join(f'{value}:{key}' for key, value in self.globs.items()) |
| 111 | utils.write_file(self.symbols_file, mapping + '\n') |
| 112 | print('wrote symbol map file to', self.symbols_file, file=sys.stderr) |
| 113 | |
| 114 | return code.replace('13371337', '0.0') |
| 115 | |
| 116 | def serialize(self): |
| 117 | return { |
no test coverage detected