Skip to content

Commit 3e7fbab

Browse files
Merge pull request #325 from google/avoid-require
Avoid using `require` to import an ES module
2 parents dc757df + a4d7581 commit 3e7fbab

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

packages/google-closure-compiler-java/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
"description": "Check, compile, optimize and compress Javascript with Closure-Compiler using Java",
55
"type": "module",
66
"main": "index.js",
7-
"repository": "https://github.com/google/closure-compiler-npm/tree/master/packages/google-closure-compiler-java",
7+
"repository": {
8+
"type": "git",
9+
"url": "git+https://github.com/google/closure-compiler-npm.git#master"
10+
},
811
"homepage": "https://developers.google.com/closure/compiler/",
912
"author": "Chad Killingsworth <[email protected]>",
1013
"license": "Apache-2.0",

packages/google-closure-compiler/lib/utils.js

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,38 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
import fs from 'node:fs';
1617
import {createRequire} from 'node:module';
1718
const require = createRequire(import.meta.url);
1819

20+
/** @type {!Map<string, string>} */
21+
const platformLookup = new Map([
22+
['darwin', 'macos'],
23+
['win32', 'windows'],
24+
['linux', 'linux'],
25+
]);
26+
27+
/** @return {string|undefined} */
1928
export const getNativeImagePath = () => {
20-
if (process.platform === 'darwin') {
21-
try {
22-
return require('google-closure-compiler-macos').default;
23-
} catch (e) {
24-
return;
25-
}
26-
}
27-
if (process.platform === 'win32') {
28-
try {
29-
return require('google-closure-compiler-windows').default;
30-
} catch (e) {
31-
return;
32-
}
33-
}
34-
if (process.platform === 'linux' && ['x64','x32'].includes(process.arch)) {
35-
try {
36-
return require('google-closure-compiler-linux').default;
37-
} catch (e) {
38-
}
39-
}
40-
if (process.platform === 'linux' && ['arm64'].includes(process.arch)) {
41-
try {
42-
return require('google-closure-compiler-linux-arm64').default;
43-
} catch (e) {
44-
}
29+
let platform = platformLookup.get(process.platform);
30+
let binarySuffix = '';
31+
if (!platform) {
32+
return;
33+
} else if (platform === 'linux' && process.arch === 'arm64') {
34+
platform += '-arm64';
35+
} else if (platform === 'windows') {
36+
binarySuffix = '.exe';
4537
}
38+
const compilerPath = `google-closure-compiler-${platform}/compiler${binarySuffix}`;
39+
try {
40+
return require.resolve(compilerPath);
41+
} catch {}
4642
};
4743

44+
/**
45+
* @param {!Array<string>} platforms
46+
* @return {string}
47+
*/
4848
export const getFirstSupportedPlatform = (platforms) => {
4949
const platform = platforms.find((platform, index) => {
5050
switch (platform.toLowerCase()) {

0 commit comments

Comments
 (0)