Skip to content

Commit

Permalink
fix(interopDefault): support mixed CJS + default
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Sep 21, 2021
1 parent 1fac753 commit 4392c6a
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,22 @@ export function isWritable (filename: string): boolean {
}
}

export function interopDefault (ex: any): any {
return (ex && (typeof ex === 'object') && 'default' in ex) ? ex.default : ex
export function interopDefault (sourceModule: any): any {
if (!(sourceModule && 'default' in sourceModule)) {
return sourceModule
}
const newModule = sourceModule.default
for (const key in sourceModule) {
if (key === 'default') { continue }
try {
Object.defineProperty(newModule, key, {
enumerable: true,
configurable: true,
get () { return sourceModule[key] }
})
} catch (_err) {}
}
return newModule
}

export function md5 (content: string, len = 8) {
Expand Down

0 comments on commit 4392c6a

Please sign in to comment.