Skip to content

Commit 051d745

Browse files
authored
Fix native Closure Compiler to work. (#995)
* Fix native Closure Compiler to work. Reverts my earlier PR #803 which was a great mistake, as it caused Emscripten to silently fall back to Java version of the Closure Compiler. After this PR, Closure Compiler will work on user platforms that do not have Java installed. Also forcibly remove Java version of Closure Compiler on systems where installing the native version succeeds, in order to save on code size. * Add note to bug * Improve google-closure-compiler-java uninstall. * Read Closure version from Emscripten repository * Skip native google-closure-compiler install when it is not present in the emscripten branch in package.json * Print error
1 parent 9498542 commit 051d745

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

emsdk.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,8 @@ def emscripten_npm_install(tool, directory):
14021402
env = os.environ.copy()
14031403
env["PATH"] = node_path + os.pathsep + env["PATH"]
14041404
print('Running post-install step: npm ci ...')
1405+
# Do a --no-optional install to avoid bloating disk size:
1406+
# https://github.com/emscripten-core/emscripten/issues/12406
14051407
try:
14061408
subprocess.check_output(
14071409
[npm, 'ci', '--production', '--no-optional'],
@@ -1411,6 +1413,66 @@ def emscripten_npm_install(tool, directory):
14111413
errlog('Error running %s:\n%s' % (e.cmd, e.output))
14121414
return False
14131415

1416+
# Manually install the appropriate native Closure Compiler package
1417+
# This is currently needed because npm ci would install the packages
1418+
# for Closure for all platforms, adding 180MB to the download size
1419+
# There are two problems here:
1420+
# 1. npm ci does not consider the platform of optional dependencies
1421+
# https://github.com/npm/cli/issues/558
1422+
# 2. A bug with the native compiler has bloated the packages from
1423+
# 30MB to almost 300MB
1424+
# https://github.com/google/closure-compiler-npm/issues/186
1425+
# If either of these bugs are fixed then we can remove this exception
1426+
# See also https://github.com/google/closure-compiler/issues/3925
1427+
closure_compiler_native = ''
1428+
if LINUX and ARCH in ('x86', 'x86_64'):
1429+
closure_compiler_native = 'google-closure-compiler-linux'
1430+
if MACOS and ARCH in ('x86', 'x86_64'):
1431+
closure_compiler_native = 'google-closure-compiler-osx'
1432+
if WINDOWS and ARCH == 'x86_64':
1433+
closure_compiler_native = 'google-closure-compiler-windows'
1434+
1435+
if closure_compiler_native:
1436+
# Check which version of native Closure Compiler we want to install via npm.
1437+
# (npm install command has this requirement that we must explicitly tell the pinned version)
1438+
try:
1439+
closure_version = json.load(open(os.path.join(directory, 'package.json')))['dependencies']['google-closure-compiler']
1440+
except KeyError as e:
1441+
# The target version of Emscripten does not (did not) have a package.json that would contain google-closure-compiler. (fastcomp)
1442+
# Skip manual native google-closure-compiler installation there.
1443+
print(str(e))
1444+
print('Emscripten version does not have a npm package.json with google-closure-compiler dependency, skipping native google-closure-compiler install step')
1445+
return True
1446+
1447+
closure_compiler_native += '@' + closure_version
1448+
print('Running post-install step: npm install', closure_compiler_native)
1449+
try:
1450+
subprocess.check_output(
1451+
[npm, 'install', '--production', '--no-optional', closure_compiler_native],
1452+
cwd=directory, stderr=subprocess.STDOUT, env=env,
1453+
universal_newlines=True)
1454+
1455+
# Installation of native Closure compiler was successful, so remove import of Java Closure Compiler module to avoid
1456+
# a Java dependency.
1457+
compiler_filename = os.path.join(directory, 'node_modules', 'google-closure-compiler', 'lib', 'node', 'closure-compiler.js')
1458+
if os.path.isfile(compiler_filename):
1459+
old_js = open(compiler_filename, 'r').read()
1460+
new_js = old_js.replace("require('google-closure-compiler-java')", "''/*require('google-closure-compiler-java') XXX Removed by Emsdk*/")
1461+
if old_js == new_js:
1462+
raise Exception('Failed to patch google-closure-compiler-java dependency away!')
1463+
open(compiler_filename, 'w').write(new_js)
1464+
1465+
# Now that we succeeded to install the native version and patch away the Java dependency, delete the Java version
1466+
# since that takes up ~12.5MB of installation space that is no longer needed.
1467+
# This process is currently a little bit hacky, see https://github.com/google/closure-compiler/issues/3926
1468+
remove_tree(os.path.join(directory, 'node_modules', 'google-closure-compiler-java'))
1469+
print('Removed google-closure-compiler-java dependency.')
1470+
else:
1471+
errlog('Failed to patch away google-closure-compiler Java dependency. ' + compiler_filename + ' does not exist.')
1472+
except subprocess.CalledProcessError as e:
1473+
errlog('Error running %s:\n%s' % (e.cmd, e.output))
1474+
return False
1475+
14141476
print('Done running: npm ci')
14151477
return True
14161478

0 commit comments

Comments
 (0)