Skip to content

Commit

Permalink
fix: inline mlly.interopDefault (resolves #48)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Oct 18, 2021
1 parent ccc1493 commit 32e606f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/jiti.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import createRequire from 'create-require'
import semver from 'semver'
import { addHook } from 'pirates'
import objectHash from 'object-hash'
import { interopDefault } from 'mlly'
import { isDir, isWritable, md5, detectESMSyntax, detectLegacySyntax } from './utils'
import { interopDefault, isDir, isWritable, md5, detectESMSyntax, detectLegacySyntax } from './utils'
import { TransformOptions, JITIOptions } from './types'

const _EnvDebug = destr(process.env.JITI_DEBUG)
Expand Down
36 changes: 36 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,39 @@ export function detectESMSyntax (code: string) {
export function detectLegacySyntax (code: string) {
return code.match(/\?\.|\?\?/)
}

export function isObject (val: any) {
return val !== null && typeof val === 'object'
}

// TODO: Use mlly (https://github.com/unjs/jiti/issues/48)
export function interopDefault (sourceModule: any) {
if (!isObject(sourceModule) || !('default' in sourceModule)) {
return sourceModule
}
const newModule = sourceModule.default
for (const key in sourceModule) {
if (key === 'default') {
try {
if (!(key in newModule)) {
Object.defineProperty(newModule, key, {
enumerable: false,
configurable: false,
get () { return newModule }
})
}
} catch (_err) {}
} else {
try {
if (!(key in newModule)) {
Object.defineProperty(newModule, key, {
enumerable: true,
configurable: true,
get () { return sourceModule[key] }
})
}
} catch (_err) {}
}
}
return newModule
}

0 comments on commit 32e606f

Please sign in to comment.