Skip to content

Commit 664a09c

Browse files
authored
feat: Enable ESM support for hooks by using dynamic import() when package.json is set to type module (#7936)
1 parent 6c4d17c commit 664a09c

8 files changed

Lines changed: 31 additions & 14 deletions

File tree

.changeset/quick-rocks-cover.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"app-builder-lib": minor
3+
---
4+
5+
feat: Enable ESM support for hooks by using dynamic `import()` when `package.json` is set to type `module`.

packages/app-builder-lib/src/appInfo.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export function smarten(s: string): string {
2222
export class AppInfo {
2323
readonly description = smarten(this.info.metadata.description || "")
2424
readonly version: string
25+
readonly type: string | undefined
2526
readonly shortVersion: string | undefined
2627
readonly shortVersionWindows: string | undefined
2728

@@ -39,6 +40,7 @@ export class AppInfo {
3940
normalizeNfd = false
4041
) {
4142
this.version = info.metadata.version!
43+
this.type = info.metadata.type
4244

4345
if (buildVersion == null) {
4446
buildVersion = info.config.buildVersion

packages/app-builder-lib/src/codeSign/windowsCodeSign.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export async function sign(options: WindowsSignOptions, packager: WinPackager):
5252
hashes = Array.isArray(hashes) ? hashes : [hashes]
5353
}
5454

55-
const executor = resolveFunction(options.options.sign, "sign") || doSign
55+
const executor = (await resolveFunction(packager.appInfo.type, options.options.sign, "sign")) || doSign
5656
let isNest = false
5757
for (const hash of hashes) {
5858
const taskConfiguration: WindowsSignTaskConfiguration = { ...options, hash, isNest }

packages/app-builder-lib/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export function build(options: PackagerOptions & PublishOptions, packager: Packa
7676
process.once("SIGINT", sigIntHandler)
7777

7878
const promise = packager.build().then(async buildResult => {
79-
const afterAllArtifactBuild = resolveFunction(buildResult.configuration.afterAllArtifactBuild, "afterAllArtifactBuild")
79+
const afterAllArtifactBuild = await resolveFunction(packager.appInfo.type, buildResult.configuration.afterAllArtifactBuild, "afterAllArtifactBuild")
8080
if (afterAllArtifactBuild != null) {
8181
const newArtifacts = asArray(await Promise.resolve(afterAllArtifactBuild(buildResult)))
8282
if (newArtifacts.length === 0 || !publishManager.isPublish) {

packages/app-builder-lib/src/options/metadata.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ export interface Metadata {
4141
/** @private */
4242
readonly version?: string
4343
/** @private */
44+
readonly type?: string
45+
/** @private */
4446
readonly shortVersion?: string | null
4547
/** @private */
4648
readonly shortVersionWindows?: string | null

packages/app-builder-lib/src/packager.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ export class Packager {
257257
},
258258
"building"
259259
)
260-
const handler = resolveFunction(this.config.artifactBuildStarted, "artifactBuildStarted")
260+
const handler = await resolveFunction(this.appInfo.type, this.config.artifactBuildStarted, "artifactBuildStarted")
261261
if (handler != null) {
262262
await Promise.resolve(handler(event))
263263
}
@@ -271,7 +271,7 @@ export class Packager {
271271
}
272272

273273
async callArtifactBuildCompleted(event: ArtifactCreated): Promise<void> {
274-
const handler = resolveFunction(this.config.artifactBuildCompleted, "artifactBuildCompleted")
274+
const handler = await resolveFunction(this.appInfo.type, this.config.artifactBuildCompleted, "artifactBuildCompleted")
275275
if (handler != null) {
276276
await Promise.resolve(handler(event))
277277
}
@@ -280,14 +280,14 @@ export class Packager {
280280
}
281281

282282
async callAppxManifestCreated(path: string): Promise<void> {
283-
const handler = resolveFunction(this.config.appxManifestCreated, "appxManifestCreated")
283+
const handler = await resolveFunction(this.appInfo.type, this.config.appxManifestCreated, "appxManifestCreated")
284284
if (handler != null) {
285285
await Promise.resolve(handler(path))
286286
}
287287
}
288288

289289
async callMsiProjectCreated(path: string): Promise<void> {
290-
const handler = resolveFunction(this.config.msiProjectCreated, "msiProjectCreated")
290+
const handler = await resolveFunction(this.appInfo.type, this.config.msiProjectCreated, "msiProjectCreated")
291291
if (handler != null) {
292292
await Promise.resolve(handler(path))
293293
}
@@ -503,7 +503,7 @@ export class Packager {
503503
return
504504
}
505505

506-
const beforeBuild = resolveFunction(config.beforeBuild, "beforeBuild")
506+
const beforeBuild = await resolveFunction(this.appInfo.type, config.beforeBuild, "beforeBuild")
507507
if (beforeBuild != null) {
508508
const performDependenciesInstallOrRebuild = await beforeBuild({
509509
appDir: this.appDir,
@@ -532,7 +532,7 @@ export class Packager {
532532
}
533533

534534
async afterPack(context: AfterPackContext): Promise<any> {
535-
const afterPack = resolveFunction(this.config.afterPack, "afterPack")
535+
const afterPack = await resolveFunction(this.appInfo.type, this.config.afterPack, "afterPack")
536536
const handlers = this.afterPackHandlers.slice()
537537
if (afterPack != null) {
538538
// user handler should be last

packages/app-builder-lib/src/platformPackager.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>
204204
// Due to node-gyp rewriting GYP_MSVS_VERSION when reused across the same session, we must reset the env var: https://github.com/electron-userland/electron-builder/issues/7256
205205
delete process.env.GYP_MSVS_VERSION
206206

207-
const beforePack = resolveFunction(this.config.beforePack, "beforePack")
207+
const beforePack = await resolveFunction(this.appInfo.type, this.config.beforePack, "beforePack")
208208
if (beforePack != null) {
209209
await beforePack({
210210
appOutDir,
@@ -330,7 +330,7 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>
330330
electronPlatformName: platformName,
331331
}
332332
const didSign = await this.signApp(packContext, isAsar)
333-
const afterSign = resolveFunction(this.config.afterSign, "afterSign")
333+
const afterSign = await resolveFunction(this.appInfo.type, this.config.afterSign, "afterSign")
334334
if (afterSign != null) {
335335
if (didSign) {
336336
await Promise.resolve(afterSign(packContext))
@@ -753,7 +753,16 @@ export function normalizeExt(ext: string) {
753753
return ext.startsWith(".") ? ext.substring(1) : ext
754754
}
755755

756-
export function resolveFunction<T>(executor: T | string, name: string): T {
756+
async function resolveModule<T>(type: string | undefined, name: string): Promise<T> {
757+
const extension = path.extname(name).toLowerCase()
758+
const isModuleType = type === "module"
759+
if (extension === ".mjs" || (extension === ".js" && isModuleType)) {
760+
return await eval("import('" + name + "')")
761+
}
762+
return require(name)
763+
}
764+
765+
export async function resolveFunction<T>(type: string | undefined, executor: T | string, name: string): Promise<T> {
757766
if (executor == null || typeof executor !== "string") {
758767
return executor
759768
}
@@ -770,8 +779,7 @@ export function resolveFunction<T>(executor: T | string, name: string): T {
770779
p = path.resolve(p)
771780
}
772781

773-
// eslint-disable-next-line @typescript-eslint/no-var-requires
774-
const m = require(p)
782+
const m: any = await resolveModule(type, p)
775783
const namedExport = m[name]
776784
if (namedExport == null) {
777785
return m.default || m

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class NodeModuleCopyHelper extends FileCopyHelper {
4141
const filter = this.filter
4242
const metadata = this.metadata
4343

44-
const onNodeModuleFile = resolveFunction(this.packager.config.onNodeModuleFile, "onNodeModuleFile")
44+
const onNodeModuleFile = await resolveFunction(this.packager.appInfo.type, this.packager.config.onNodeModuleFile, "onNodeModuleFile")
4545

4646
const result: Array<string> = []
4747
const queue: Array<string> = []

0 commit comments

Comments
 (0)