def _build_app_script(self): script_name = self.app_file('js') output = StringIO() self._concatenate_application_script(output) write_file(join(self.output_dir, script_name), minify_js(output.getvalue())) output.close()
def concatenate_worker(module_name, descriptors, application_dir, output_dir, minify): descriptor = descriptors.modules[module_name] scripts = descriptor.get('scripts') if not scripts: return worker_dir = path.join(application_dir, module_name) output_file_path = path.join(output_dir, module_name + '_module.js') output = StringIO() output.write('/* Worker %s */\n' % module_name) output.write('/* Runtime.js */\n') output.write(read_file(path.join(application_dir, 'Runtime.js'))) dependencies = descriptors.sorted_dependencies_closure(module_name) dep_descriptors = [] for dep_name in dependencies: dep_descriptor = descriptors.modules[dep_name] dep_descriptors.append(dep_descriptor) scripts = dep_descriptor.get('scripts') if scripts: output.write('\n/* Module %s */\n' % dep_name) modular_build.concatenate_scripts( scripts, path.join(application_dir, dep_name), output_dir, output) write_file(output_file_path, minify_if_needed(output.getvalue(), minify)) output.close()
def main(argv): try: input_path_flag_index = argv.index('--input_path') input_path = argv[input_path_flag_index + 1] output_path_flag_index = argv.index('--output_path') output_path = argv[output_path_flag_index + 1] rollup_plugin_index = argv.index('--rollup_plugin') rollup_plugin = argv[rollup_plugin_index + 1] file_names_with_sizes = to_pairs(argv[1:input_path_flag_index]) for filename, max_size in file_names_with_sizes: max_size = int(max_size) if filename.endswith(".js"): rollup(input_path, output_path, filename, max_size, rollup_plugin) if filename.endswith(".css"): css_file = read_file(join(input_path, filename)) check_size(filename, css_file, max_size) write_file(join(output_path, filename), css_file) except: print( 'Usage: %s filename_1 max_size_1 filename_2 max_size_2 ... filename_N max_size_N --input_path <input_path> --output_path <output_path>' % argv[0]) raise
def concatenate_application_script(application_name, descriptors, application_dir, output_dir, minify): application_loader_name = application_name + '.js' output = StringIO() runtime_contents = read_file(path.join(application_dir, 'Runtime.js')) runtime_contents = re.sub( 'var allDescriptors = \[\];', 'var allDescriptors = %s;' % release_module_descriptors(descriptors.modules).replace("\\", "\\\\"), runtime_contents, 1) output.write('/* Runtime.js */\n') output.write(runtime_contents) output.write('\n/* Autostart modules */\n') concatenate_autostart_modules(descriptors, application_dir, output_dir, output) output.write('/* Application descriptor %s */\n' % (application_name + '.json')) output.write('applicationDescriptor = ') output.write(descriptors.application_json) output.write(';\n/* Application loader */\n') output.write(read_file(path.join(application_dir, application_loader_name))) write_file(path.join(output_dir, application_loader_name), minify_if_needed(output.getvalue(), minify)) output.close()
def _rollup_module(self, module_name, modules): js_entrypoint = join(self.application_dir, module_name, module_name + '.js') out = '' if self.use_rollup: rollup_process = subprocess.Popen( [devtools_paths.node_path(), devtools_paths.rollup_path()] + ROLLUP_ARGS + ['--input', js_entrypoint], stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, error = rollup_process.communicate() else: out = read_file(js_entrypoint) write_file(join(self.output_dir, module_name, module_name + '.js'), minify_js(out)) legacyFileName = module_name + '-legacy.js' if legacyFileName in modules: write_file( join(self.output_dir, module_name, legacyFileName), minify_js( read_file( join(self.application_dir, module_name, legacyFileName))))
def _rollup_module(self, module_name, modules, skip_rollup): legacyFileName = module_name + '-legacy.js' if legacyFileName in modules: write_file( join(self.output_dir, module_name, legacyFileName), minify_js( read_file( join(self.application_dir, module_name, legacyFileName)))) # Temporary hack, as we use `devtools_entrypoint` for this module now # TODO(crbug.com/1101738): remove once all folders are migrated if skip_rollup: return js_entrypoint = join(self.application_dir, module_name, module_name + '.js') out = '' if self.use_rollup: rollup_process = subprocess.Popen([ devtools_paths.node_path(), devtools_paths.rollup_path(), '--config', join(FRONT_END_DIRECTORY, 'rollup.config.js'), '--input', js_entrypoint ], stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, error = rollup_process.communicate() if rollup_process.returncode != 0: print(error) sys.exit(rollup_process.returncode) else: out = read_file(js_entrypoint) write_file(join(self.output_dir, module_name, module_name + '.js'), minify_js(out))
def _rollup_module(self, module_name, modules): js_entrypoint = join(self.application_dir, module_name, module_name + '.js') out = '' if self.use_rollup: rollup_process = subprocess.Popen([ devtools_paths.node_path(), devtools_paths.rollup_path(), '--config', join(FRONT_END_DIRECTORY, 'rollup.config.js'), '--input', js_entrypoint, '--external', EXTERNAL_MODULE_LIST ], stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, error = rollup_process.communicate() if rollup_process.returncode != 0: print(error) sys.exit(rollup_process.returncode) else: out = read_file(js_entrypoint) write_file(join(self.output_dir, module_name, module_name + '.js'), minify_js(out)) legacyFileName = module_name + '-legacy.js' if legacyFileName in modules: write_file( join(self.output_dir, module_name, legacyFileName), minify_js( read_file( join(self.application_dir, module_name, legacyFileName))))
def concatenate_dynamic_module(module_name, descriptors, application_dir, output_dir, minify): scripts = descriptors.modules[module_name].get('scripts') if not scripts: return module_dir = path.join(application_dir, module_name) output = StringIO() modular_build.concatenate_scripts(scripts, module_dir, output_dir, output) output_file_path = concatenated_module_filename(module_name, output_dir) write_file(output_file_path, minify_if_needed(output.getvalue(), minify)) output.close()
def build_app(self): if self.descriptors.has_html: html_entrypoint = self.app_file('html') write_file(join(self.output_dir, html_entrypoint), read_file(join(self.application_dir, html_entrypoint))) self._build_app_script() for module in filter( lambda desc: (not desc.get('type') or desc.get('type') == 'remote'), self.descriptors.application.values()): self._concatenate_dynamic_module(module['name'])
def _concatenate_dynamic_module(self, module_name): module = self.descriptors.modules[module_name] modules = module.get('modules') resources = self.descriptors.module_resources(module_name) module_dir = join(self.application_dir, module_name) output = StringIO() if resources: output.write("import * as RootModule from '../root/root.js';") self._write_module_resources(resources, output) output_file_path = concatenated_module_filename(module_name, self.output_dir) write_file(output_file_path, minify_js(output.getvalue())) output.close()
def _rollup_module( self, module_name, modules, ): legacyFileName = module_name + '-legacy.js' if legacyFileName in modules: write_file( join(self.output_dir, module_name, legacyFileName), minify_js( read_file( join(self.application_dir, module_name, legacyFileName))))
def _build_html(self): html_name = self.app_file('html') output = StringIO() with open(join(self.application_dir, html_name), 'r') as app_input_html: for line in app_input_html: if '<script ' in line or '<link ' in line: continue if '</head>' in line: output.write(self._generate_include_tag(self.app_file('js'))) output.write(line) write_file(join(self.output_dir, html_name), output.getvalue()) output.close()
def _concatenate_dynamic_module(self, module_name): module = self.descriptors.modules[module_name] scripts = module.get('scripts') resources = self.descriptors.module_resources(module_name) module_dir = join(self.application_dir, module_name) output = StringIO() if scripts: modular_build.concatenate_scripts(scripts, module_dir, self.output_dir, output) if resources: self._write_module_resources(resources, output) output_file_path = concatenated_module_filename(module_name, self.output_dir) write_file(output_file_path, minify_js(output.getvalue())) output.close()
def rollup(input_path, output_path, filename, max_size, rollup_plugin): target = join(input_path, filename) rollup_process = subprocess.Popen( [devtools_paths.node_path(), devtools_paths.rollup_path()] + ['--format', 'iife', '-n', 'InspectorOverlay'] + ['--input', target] + ['--plugin', rollup_plugin, '--plugin', 'terser'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, error = rollup_process.communicate() if not out: raise Exception("rollup failed: " + error) check_size(filename, out, max_size) write_file(join(output_path, filename), out)
def main(argv): try: input_path_flag_index = argv.index('--input_path') input_path = argv[input_path_flag_index + 1] output_path_flag_index = argv.index('--output_path') output_path = argv[output_path_flag_index + 1] devtools_modules = argv[1:input_path_flag_index] except: print('Usage: %s module_1 module_2 ... module_N --input_path <input_path> --output_path <output_path>' % argv[0]) raise for file_name in devtools_modules: file_content = read_file(join(input_path, file_name)) minified = rjsmin.jsmin(file_content) write_file(join(output_path, relpath(file_name, 'front_end')), minified)
def _concatenate_dynamic_module(self, module_name): module = self.descriptors.modules[module_name] scripts = module.get('scripts') stylesheets = self.descriptors.module_stylesheets(module_name) if not scripts and not stylesheets: return module_dir = join(self.application_dir, module_name) output = StringIO() if scripts: modular_build.concatenate_scripts(scripts, module_dir, self.output_dir, output) if stylesheets: self._write_module_css_styles(stylesheets, output) output_file_path = concatenated_module_filename(module_name, self.output_dir) write_file(output_file_path, minify_js(output.getvalue())) output.close()
def _build_html(self): html_name = self.app_file('html') output = StringIO() with open(join(self.application_dir, html_name), 'r') as app_input_html: for line in app_input_html: if ('<script ' in line and 'type="module"' not in line) or '<link ' in line: continue if '</head>' in line: self._write_include_tags(self.descriptors, output) js_file = join(self.application_dir, self.app_file('js')) if path.exists(js_file): output.write(' <script type="module">%s</script>\n' % minify_js(read_file(js_file))) output.write(line) write_file(join(self.output_dir, html_name), output.getvalue()) output.close()
def unclosure_injected_script(sourceFileName, outFileName): source = read_file(sourceFileName) def replace_function(matchobj): return re.sub(r'@param', 'param', matchobj.group(1) or '') + '\n//' + matchobj.group(2) # Comment out the closure function and its jsdocs source = re.sub(r'(/\*\*(?:[\s\n]*\*\s*@param[^\n]+\n)+\s*\*/\s*)?\n(\(function)', replace_function, source, count=1) # Comment out its return statement source = re.sub(r'\n(\s*return\s+[^;]+;\s*\n\}\)\s*)$', '\n/*\\1*/', source) # Replace the "var Object" override with a "self.Object" one source = re.sub(r'\nvar Object =', '\nself.Object =', source, count=1) write_file(outFileName, source)
def concatenate_application_script(application_name, descriptors, application_dir, output_dir, minify): application_loader_name = application_name + '.js' output = StringIO() runtime_contents = read_file(path.join(application_dir, 'Runtime.js')) runtime_contents = re.sub('var allDescriptors = \[\];', 'var allDescriptors = %s;' % release_module_descriptors(descriptors.modules).replace("\\", "\\\\"), runtime_contents, 1) output.write('/* Runtime.js */\n') output.write(runtime_contents) output.write('\n/* Autostart modules */\n') concatenate_autostart_modules(descriptors, application_dir, output_dir, output) output.write('/* Application descriptor %s */\n' % (application_name + '.json')) output.write('applicationDescriptor = ') output.write(descriptors.application_json) output.write(';\n/* Application loader */\n') output.write(read_file(path.join(application_dir, application_loader_name))) write_file(path.join(output_dir, application_loader_name), minify_if_needed(output.getvalue(), minify)) output.close()
def _build_html(self): html_name = self.app_file('html') output = StringIO() with open(join(self.application_dir, html_name), 'r') as app_input_html: for line in app_input_html: if '<script ' in line or '<link ' in line: continue if '</head>' in line: self._write_include_tags(self.descriptors, output) js_file = join(self.application_dir, self.app_file('js')) if path.exists(js_file): boot_js_file = self.app_file('boot.js') minified_js = minify_js(read_file(js_file)) output.write(' <script type="text/javascript" src="%s"></script>\n' % boot_js_file) write_file(join(self.output_dir, boot_js_file), minified_js) output.write(line) write_file(join(self.output_dir, html_name), output.getvalue()) output.close()
def _concatenate_worker(self, module_name): descriptor = self.descriptors.modules[module_name] scripts = descriptor.get('scripts') if not scripts: return output = StringIO() output.write('/* Worker %s */\n' % module_name) dep_descriptors = [] for dep_name in self.descriptors.sorted_dependencies_closure(module_name): dep_descriptor = self.descriptors.modules[dep_name] dep_descriptors.append(dep_descriptor) scripts = dep_descriptor.get('scripts') if scripts: output.write('\n/* Module %s */\n' % dep_name) modular_build.concatenate_scripts(scripts, join(self.application_dir, dep_name), self.output_dir, output) output_file_path = concatenated_module_filename(module_name, self.output_dir) write_file(output_file_path, minify_js(output.getvalue())) output.close()
def _concatenate_dynamic_module(self, module_name): module = self.descriptors.modules[module_name] modules = module.get('modules') resources = self.descriptors.module_resources(module_name) module_dir = join(self.application_dir, module_name) output = StringIO() if resources: relative_file_name = '../core/root/root.js' if "/" in module_name: relative_file_name = ( '../' * module_name.count('/')) + relative_file_name output.write("import * as RootModule from '%s';" % relative_file_name) self._write_module_resources(resources, output) minified_content = minify_js(output.getvalue()) write_file( concatenated_module_filename(module_name, self.output_path_gen_dir), minified_content) output.close()
def concatenate_worker(module_name, descriptors, application_dir, output_dir, minify): descriptor = descriptors.modules[module_name] scripts = descriptor.get('scripts') if not scripts: return worker_dir = path.join(application_dir, module_name) output_file_path = concatenated_module_filename(module_name, output_dir) output = StringIO() output.write('/* Worker %s */\n' % module_name) dependencies = descriptors.sorted_dependencies_closure(module_name) dep_descriptors = [] for dep_name in dependencies: dep_descriptor = descriptors.modules[dep_name] dep_descriptors.append(dep_descriptor) scripts = dep_descriptor.get('scripts') if scripts: output.write('\n/* Module %s */\n' % dep_name) modular_build.concatenate_scripts(scripts, path.join(application_dir, dep_name), output_dir, output) write_file(output_file_path, minify_if_needed(output.getvalue(), minify)) output.close()
def main(argv): try: file_list_arg_index = argv.index('--file_list') file_list_filename = argv[file_list_arg_index + 1] input_path_flag_index = argv.index('--input_path') input_path = argv[input_path_flag_index + 1] output_path_flag_index = argv.index('--output_path') output_path = argv[output_path_flag_index + 1] file_list_file = open(file_list_filename, 'r') file_list_contents = file_list_file.read() devtools_modules = shlex.split(file_list_contents) except: print( 'Usage: %s --file_list <response_file_path> --input_path <input_path> --output_path <output_path>' % argv[0]) raise for file_name in devtools_modules: file_content = read_file(join(input_path, file_name)) write_file(join(output_path, relpath(file_name, 'front_end')), file_content)
def concatenate_application_script(application_name, descriptors, application_dir, output_dir, minify): application_loader_name = application_name + '.js' output = StringIO() output.write('/* Runtime.js */\n') output.write(read_file(path.join(application_dir, 'Runtime.js'))) output.write('\n/* Autostart modules */\n') concatenate_autostart_modules(descriptors, application_dir, output_dir, output) output.write('/* Application descriptor %s */\n' % (application_name + '.json')) output.write('applicationDescriptor = ') output.write(descriptors.application_json) output.write(';\n/* Module descriptors */\n') output.write('allDescriptors = ') output.write(json.dumps(descriptors.modules.values())) output.write(';\n/* Application loader */\n') output.write(read_file(path.join(application_dir, application_loader_name))) write_file(path.join(output_dir, application_loader_name), minify_if_needed(output.getvalue(), minify)) output.close()
def copy_file(file_name): write_file(join(output_path, file_name), minify_js(read_file(join(input_path, file_name))))