Skip to content

Commit ad282f9

Browse files
authored
Refactor internal closure_compiler method. NFC (#15761)
This refactoring is to allow for closure to be run in traspile-only mode in a followup PR.
1 parent 19eb495 commit ad282f9

File tree

1 file changed

+36
-23
lines changed

1 file changed

+36
-23
lines changed

tools/building.py

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -707,16 +707,8 @@ def isascii(s):
707707
return True
708708

709709

710-
@ToolchainProfiler.profile_block('closure_compiler')
711-
def closure_compiler(filename, pretty, advanced=True, extra_closure_args=None):
710+
def get_closure_compiler_and_env(user_args):
712711
env = shared.env_with_node_in_path()
713-
user_args = []
714-
env_args = os.environ.get('EMCC_CLOSURE_ARGS')
715-
if env_args:
716-
user_args += shlex.split(env_args)
717-
if extra_closure_args:
718-
user_args += extra_closure_args
719-
720712
closure_cmd = get_closure_compiler()
721713

722714
native_closure_compiler_works = check_closure_compiler(closure_cmd, user_args, env, allowed_to_fail=True)
@@ -737,6 +729,20 @@ def add_to_path(dirname):
737729
java_home = os.path.dirname(java_bin)
738730
env.setdefault('JAVA_HOME', java_home)
739731

732+
return closure_cmd, env
733+
734+
735+
@ToolchainProfiler.profile_block('closure_compiler')
736+
def closure_compiler(filename, pretty, advanced=True, extra_closure_args=None):
737+
user_args = []
738+
env_args = os.environ.get('EMCC_CLOSURE_ARGS')
739+
if env_args:
740+
user_args += shlex.split(env_args)
741+
if extra_closure_args:
742+
user_args += extra_closure_args
743+
744+
closure_cmd, env = get_closure_compiler_and_env(user_args)
745+
740746
# Closure externs file contains known symbols to be extern to the minification, Closure
741747
# should not minify these symbol names.
742748
CLOSURE_EXTERNS = [path_from_root('src/closure-externs/closure-externs.js')]
@@ -792,10 +798,23 @@ def add_to_path(dirname):
792798
# Tell closure never to inject the 'use strict' directive.
793799
args += ['--emit_use_strict=false']
794800

801+
if settings.IGNORE_CLOSURE_COMPILER_ERRORS:
802+
args.append('--jscomp_off=*')
803+
# Specify input file relative to the temp directory to avoid specifying non-7-bit-ASCII path names.
804+
for e in CLOSURE_EXTERNS:
805+
args += ['--externs', e]
806+
args += user_args
807+
808+
cmd = closure_cmd + args
809+
return run_closure_cmd(cmd, filename, env, pretty=pretty)
810+
811+
812+
def run_closure_cmd(cmd, filename, env, pretty):
813+
cmd += ['--js', filename]
814+
795815
# Closure compiler is unable to deal with path names that are not 7-bit ASCII:
796816
# https://github.com/google/closure-compiler/issues/3784
797817
tempfiles = configuration.get_temp_files()
798-
outfile = tempfiles.get('.cc.js').name # Safe 7-bit filename
799818

800819
def move_to_safe_7bit_ascii_filename(filename):
801820
if isascii(filename):
@@ -804,23 +823,17 @@ def move_to_safe_7bit_ascii_filename(filename):
804823
shutil.copyfile(filename, safe_filename)
805824
return os.path.relpath(safe_filename, tempfiles.tmpdir)
806825

807-
for e in CLOSURE_EXTERNS:
808-
args += ['--externs', move_to_safe_7bit_ascii_filename(e)]
826+
for i in range(len(cmd)):
827+
if cmd[i] == '--externs' or cmd[i] == '--js':
828+
cmd[i + 1] = move_to_safe_7bit_ascii_filename(cmd[i + 1])
809829

810-
for i in range(len(user_args)):
811-
if user_args[i] == '--externs':
812-
user_args[i + 1] = move_to_safe_7bit_ascii_filename(user_args[i + 1])
830+
outfile = tempfiles.get('.cc.js').name # Safe 7-bit filename
813831

814832
# Specify output file relative to the temp directory to avoid specifying non-7-bit-ASCII path names.
815-
args += ['--js_output_file', os.path.relpath(outfile, tempfiles.tmpdir)]
816-
817-
if settings.IGNORE_CLOSURE_COMPILER_ERRORS:
818-
args.append('--jscomp_off=*')
833+
cmd += ['--js_output_file', os.path.relpath(outfile, tempfiles.tmpdir)]
819834
if pretty:
820-
args += ['--formatting', 'PRETTY_PRINT']
821-
# Specify input file relative to the temp directory to avoid specifying non-7-bit-ASCII path names.
822-
args += ['--js', move_to_safe_7bit_ascii_filename(filename)]
823-
cmd = closure_cmd + args + user_args
835+
cmd += ['--formatting', 'PRETTY_PRINT']
836+
824837
logger.debug(f'closure compiler: {shared.shlex_join(cmd)}')
825838

826839
# Closure compiler does not work if any of the input files contain characters outside the

0 commit comments

Comments
 (0)