Skip to content

Commit

Permalink
fix: skip interop for esm
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jan 18, 2022
1 parent 1a3e696 commit 7bf0016
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
55 changes: 35 additions & 20 deletions packages/vite-node/src/client.ts
Expand Up @@ -56,7 +56,7 @@ export class ViteNodeRunner {

const { code: transformed, externalize } = await this.options.fetchModule(id)
if (externalize) {
const mod = await interpretedImport(externalize, this.options.interpretDefault ?? true)
const mod = await this.interopedImport(externalize)
this.setCache(fsPath, { exports: mod })
return mod
}
Expand Down Expand Up @@ -117,10 +117,41 @@ export class ViteNodeRunner {
else
Object.assign(this.moduleCache.get(id), mod)
}
}

function hasNestedDefault(target: any) {
return '__esModule' in target && target.__esModule && 'default' in target.default
/**
* Define if a module should be interop-ed
* This function mostly for the ability to override by subclass
*/
shouldInterop(path: string, mod: any) {
if (this.options.interpretDefault === false)
return false
// never interop ESM modules
// TODO: should also skip for `.js` with `type="module"`
return !path.endsWith('.mjs') && 'default' in mod
}

/**
* Import a module and interop it
*/
async interopedImport(path: string) {
const mod = await import(path)

if (this.shouldInterop(path, mod)) {
const tryDefault = this.hasNestedDefault(mod)
return new Proxy(mod, {
get: proxyMethod('get', tryDefault),
set: proxyMethod('set', tryDefault),
has: proxyMethod('has', tryDefault),
deleteProperty: proxyMethod('deleteProperty', tryDefault),
})
}

return mod
}

hasNestedDefault(target: any) {
return '__esModule' in target && target.__esModule && 'default' in target.default
}
}

function proxyMethod(name: 'get' | 'set' | 'has' | 'deleteProperty', tryDefault: boolean) {
Expand All @@ -134,22 +165,6 @@ function proxyMethod(name: 'get' | 'set' | 'has' | 'deleteProperty', tryDefault:
}
}

async function interpretedImport(path: string, interpretDefault: boolean) {
const mod = await import(path)

if (interpretDefault && 'default' in mod) {
const tryDefault = hasNestedDefault(mod)
return new Proxy(mod, {
get: proxyMethod('get', tryDefault),
set: proxyMethod('set', tryDefault),
has: proxyMethod('has', tryDefault),
deleteProperty: proxyMethod('deleteProperty', tryDefault),
})
}

return mod
}

function exportAll(exports: any, sourceModule: any) {
// eslint-disable-next-line no-restricted-syntax
for (const key in sourceModule) {
Expand Down
4 changes: 2 additions & 2 deletions packages/vite-node/src/server.ts
Expand Up @@ -63,8 +63,8 @@ export class ViteNodeServer {

const mode = this.getTransformMode(id)
if (mode === 'web') {
// for components like Vue, we want to use the client side
// plugins but then covert the code to be consumed by the server
// for components like Vue, we want to use the client side
// plugins but then covert the code to be consumed by the server
result = await this.server.transformRequest(id)
if (result)
result = await this.server.ssrTransform(result.code, result.map, id)
Expand Down

0 comments on commit 7bf0016

Please sign in to comment.