Skip to content

Commit

Permalink
fix: Attempt dynamically importing hook as a module if package.json `…
Browse files Browse the repository at this point in the history
…type=module`, if fail, fallback to default `require` (#8042)
  • Loading branch information
mmaietta authored Feb 9, 2024
1 parent 94677f3 commit 63a0044
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-rats-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"app-builder-lib": patch
---

Attempt dynamically importing hook as a module if package.json `type=module`, if fail, fallback to default `require`
8 changes: 6 additions & 2 deletions packages/app-builder-lib/src/platformPackager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -756,8 +756,12 @@ export function normalizeExt(ext: string) {
async function resolveModule<T>(type: string | undefined, name: string): Promise<T> {
const extension = path.extname(name).toLowerCase()
const isModuleType = type === "module"
if (extension === ".mjs" || (extension === ".js" && isModuleType)) {
return await eval("import('" + name + "')")
try {
if (extension === ".mjs" || (extension === ".js" && isModuleType)) {
return await eval("import('" + name + "')")
}
} catch (error) {
log.debug({ moduleName: name }, "Unable to dynamically import hook, falling back to `require`")
}
return require(name)
}
Expand Down

0 comments on commit 63a0044

Please sign in to comment.