From 263db9126afbe60d8321e04531f99a87baf335d9 Mon Sep 17 00:00:00 2001 From: Chad Killingsworth Date: Sat, 24 May 2025 04:43:44 -0500 Subject: [PATCH 1/2] Avoid using `require` to import an ES module --- .../google-closure-compiler-java/package.json | 5 ++- packages/google-closure-compiler/lib/utils.js | 38 ++++++++----------- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/packages/google-closure-compiler-java/package.json b/packages/google-closure-compiler-java/package.json index 966b3861..60628681 100644 --- a/packages/google-closure-compiler-java/package.json +++ b/packages/google-closure-compiler-java/package.json @@ -4,7 +4,10 @@ "description": "Check, compile, optimize and compress Javascript with Closure-Compiler using Java", "type": "module", "main": "index.js", - "repository": "https://github.com/google/closure-compiler-npm/tree/master/packages/google-closure-compiler-java", + "repository": { + "type": "git", + "url": "git+https://github.com/google/closure-compiler-npm.git#master" + }, "homepage": "https://developers.google.com/closure/compiler/", "author": "Chad Killingsworth ", "license": "Apache-2.0", diff --git a/packages/google-closure-compiler/lib/utils.js b/packages/google-closure-compiler/lib/utils.js index 66f736d9..210f80a9 100644 --- a/packages/google-closure-compiler/lib/utils.js +++ b/packages/google-closure-compiler/lib/utils.js @@ -13,35 +13,27 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import fs from 'node:fs'; import {createRequire} from 'node:module'; const require = createRequire(import.meta.url); export const getNativeImagePath = () => { - if (process.platform === 'darwin') { - try { - return require('google-closure-compiler-macos').default; - } catch (e) { - return; - } - } - if (process.platform === 'win32') { - try { - return require('google-closure-compiler-windows').default; - } catch (e) { - return; - } + let compilerPath; + switch (process.platform) { + case 'darwin': + compilerPath = 'google-closure-compiler-macos/compiler'; + break; + case 'win32': + compilerPath = 'google-closure-compiler-windows/compiler.exe'; + break; + case 'linux': + compilerPath = `google-closure-compiler-linux${process.arch === 'arm64' ? '-arm64' : ''}/compiler`; + break; } - if (process.platform === 'linux' && ['x64','x32'].includes(process.arch)) { + if (compilerPath) { try { - return require('google-closure-compiler-linux').default; - } catch (e) { - } - } - if (process.platform === 'linux' && ['arm64'].includes(process.arch)) { - try { - return require('google-closure-compiler-linux-arm64').default; - } catch (e) { - } + return require.resolve(compilerPath); + } catch {} } }; From a4d758161dc2117c56b4193539675f054b4686c9 Mon Sep 17 00:00:00 2001 From: Chad Killingsworth Date: Sat, 24 May 2025 07:53:29 -0500 Subject: [PATCH 2/2] Refactor code to be simpler --- packages/google-closure-compiler/lib/utils.js | 40 +++++++++++-------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/packages/google-closure-compiler/lib/utils.js b/packages/google-closure-compiler/lib/utils.js index 210f80a9..8230c1ac 100644 --- a/packages/google-closure-compiler/lib/utils.js +++ b/packages/google-closure-compiler/lib/utils.js @@ -17,26 +17,34 @@ import fs from 'node:fs'; import {createRequire} from 'node:module'; const require = createRequire(import.meta.url); +/** @type {!Map} */ +const platformLookup = new Map([ + ['darwin', 'macos'], + ['win32', 'windows'], + ['linux', 'linux'], +]); + +/** @return {string|undefined} */ export const getNativeImagePath = () => { - let compilerPath; - switch (process.platform) { - case 'darwin': - compilerPath = 'google-closure-compiler-macos/compiler'; - break; - case 'win32': - compilerPath = 'google-closure-compiler-windows/compiler.exe'; - break; - case 'linux': - compilerPath = `google-closure-compiler-linux${process.arch === 'arm64' ? '-arm64' : ''}/compiler`; - break; - } - if (compilerPath) { - try { - return require.resolve(compilerPath); - } catch {} + let platform = platformLookup.get(process.platform); + let binarySuffix = ''; + if (!platform) { + return; + } else if (platform === 'linux' && process.arch === 'arm64') { + platform += '-arm64'; + } else if (platform === 'windows') { + binarySuffix = '.exe'; } + const compilerPath = `google-closure-compiler-${platform}/compiler${binarySuffix}`; + try { + return require.resolve(compilerPath); + } catch {} }; +/** + * @param {!Array} platforms + * @return {string} + */ export const getFirstSupportedPlatform = (platforms) => { const platform = platforms.find((platform, index) => { switch (platform.toLowerCase()) {