Skip to content

Commit 9f06a85

Browse files
authored
fix: utilize a js helper dynamic-import.js for trying await import and require (#9299)
1 parent 85cb4d0 commit 9f06a85

File tree

7 files changed

+38
-14
lines changed

7 files changed

+38
-14
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"app-builder-lib": patch
3+
---
4+
5+
fix: removes dynamic eval and utilizes a js helper file for trying `await import` and `require` that is then copied into the final npm package

eslint.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export default [{
2020
"**/out",
2121
// used for CLI
2222
"**/main.js",
23+
"packages/app-builder-lib/helpers",
2324
"packages/electron-builder/cli.js",
2425
"packages/electron-builder/install-app-deps.js"
2526
],
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export declare function dynamicImport(path: string): Promise<any>;
2+
/** Like {@link dynamicImport()}, except it tries out {@link require()} first. */
3+
export declare function dynamicImportMaybe(path: string): Promise<any>;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const url = require("url")
2+
const fs = require("fs")
3+
4+
exports.dynamicImport = async function dynamicImport(path) {
5+
try {
6+
return await import(fs.existsSync(path) ? url.pathToFileURL(path).href : path)
7+
} catch (error) {
8+
return Promise.reject(error)
9+
}
10+
}
11+
12+
exports.dynamicImportMaybe = async function dynamicImportMaybe(path) {
13+
try {
14+
return require(path)
15+
} catch (e1) {
16+
try {
17+
return await exports.dynamicImport(path)
18+
} catch (e2) {
19+
e1.message = "\n1. " + e1.message + "\n2. " + e2.message
20+
throw e1
21+
}
22+
}
23+
}

packages/app-builder-lib/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"files": [
77
"out",
88
"templates",
9+
"helpers",
910
"scheme.json",
1011
"certs/root_certs.keychain"
1112
],

packages/app-builder-lib/src/util/resolve.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,14 @@
11
import { log } from "builder-util/out/log"
22
import debug from "debug"
33
import * as path from "path"
4-
import { pathToFileURL } from "url"
4+
import * as requireMaybe from "../../helpers/dynamic-import"
55

66
export async function resolveModule<T>(type: string | undefined, name: string): Promise<T> {
7-
const extension = path.extname(name).toLowerCase()
8-
const isModuleType = type === "module"
97
try {
10-
if (extension === ".mjs" || (extension === ".js" && isModuleType)) {
11-
const fileUrl = pathToFileURL(name).href
12-
return await eval("import ('" + fileUrl + "')")
13-
}
8+
return requireMaybe.dynamicImportMaybe(name)
149
} catch (error: any) {
15-
log.debug({ moduleName: name, message: error.message ?? error.stack }, "Unable to dynamically import , falling back to `require`")
16-
}
17-
try {
18-
return require(name)
19-
} catch (error: any) {
20-
log.error({ moduleName: name, message: error.message ?? error.stack }, "Unable to `require`")
21-
throw new Error(error.message ?? error.stack)
10+
log.error({ moduleName: name, message: error.message ?? error.stack }, "Unable to dynamically `import` or `require`")
11+
throw error
2212
}
2313
}
2414

tsconfig-base.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"noUnusedLocals": true,
1111
"noFallthroughCasesInSwitch": true,
1212
"noImplicitReturns": true,
13+
"allowJs": true,
1314

1415
"inlineSources": true,
1516
"sourceMap": true,

0 commit comments

Comments
 (0)