vite-node
Advanced tools
Comparing version 0.1.20 to 0.1.21
@@ -17,3 +17,3 @@ declare type FetchFunction = (id: string) => Promise<{ | ||
moduleCache?: Map<string, ModuleCache>; | ||
interpretDefault?: boolean; | ||
interopDefault?: boolean; | ||
requestStubs?: Record<string, any>; | ||
@@ -29,2 +29,12 @@ } | ||
declare const DEFAULT_REQUEST_STUBS: { | ||
'/@vite/client': { | ||
injectQuery: (id: string) => string; | ||
createHotContext(): { | ||
accept: () => void; | ||
prune: () => void; | ||
}; | ||
updateStyle(): void; | ||
}; | ||
}; | ||
declare class ViteNodeRunner { | ||
@@ -42,4 +52,14 @@ options: ViteNodeRunnerOptions; | ||
setCache(id: string, mod: Partial<ModuleCache>): void; | ||
/** | ||
* Define if a module should be interop-ed | ||
* This function mostly for the ability to override by subclass | ||
*/ | ||
shouldInterop(path: string, mod: any): boolean; | ||
/** | ||
* Import a module and interop it | ||
*/ | ||
interopedImport(path: string): Promise<any>; | ||
hasNestedDefault(target: any): any; | ||
} | ||
export { ViteNodeRunner }; | ||
export { DEFAULT_REQUEST_STUBS, ViteNodeRunner }; |
@@ -180,2 +180,17 @@ import minimist from 'minimist'; | ||
const DEFAULT_REQUEST_STUBS = { | ||
"/@vite/client": { | ||
injectQuery: (id) => id, | ||
createHotContext() { | ||
return { | ||
accept: () => { | ||
}, | ||
prune: () => { | ||
} | ||
}; | ||
}, | ||
updateStyle() { | ||
} | ||
} | ||
}; | ||
class ViteNodeRunner { | ||
@@ -219,7 +234,8 @@ constructor(options) { | ||
}; | ||
if (this.options.requestStubs && id in this.options.requestStubs) | ||
return this.options.requestStubs[id]; | ||
const requestStubs = this.options.requestStubs || DEFAULT_REQUEST_STUBS; | ||
if (id in requestStubs) | ||
return requestStubs[id]; | ||
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 }); | ||
@@ -272,6 +288,24 @@ return mod; | ||
} | ||
shouldInterop(path, mod) { | ||
if (this.options.interopDefault === false) | ||
return false; | ||
return !path.endsWith(".mjs") && "default" in mod; | ||
} | ||
async interopedImport(path) { | ||
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) { | ||
return "__esModule" in target && target.__esModule && "default" in target.default; | ||
} | ||
} | ||
function hasNestedDefault(target) { | ||
return "__esModule" in target && target.__esModule && "default" in target.default; | ||
} | ||
function proxyMethod(name, tryDefault) { | ||
@@ -287,15 +321,2 @@ return function(target, key, ...args) { | ||
} | ||
async function interpretedImport(path, interpretDefault) { | ||
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, sourceModule) { | ||
@@ -302,0 +323,0 @@ for (const key in sourceModule) { |
@@ -25,2 +25,17 @@ import { builtinModules, createRequire } from 'module'; | ||
const DEFAULT_REQUEST_STUBS = { | ||
"/@vite/client": { | ||
injectQuery: (id) => id, | ||
createHotContext() { | ||
return { | ||
accept: () => { | ||
}, | ||
prune: () => { | ||
} | ||
}; | ||
}, | ||
updateStyle() { | ||
} | ||
} | ||
}; | ||
class ViteNodeRunner { | ||
@@ -64,7 +79,8 @@ constructor(options) { | ||
}; | ||
if (this.options.requestStubs && id in this.options.requestStubs) | ||
return this.options.requestStubs[id]; | ||
const requestStubs = this.options.requestStubs || DEFAULT_REQUEST_STUBS; | ||
if (id in requestStubs) | ||
return requestStubs[id]; | ||
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 }); | ||
@@ -117,6 +133,24 @@ return mod; | ||
} | ||
shouldInterop(path, mod) { | ||
if (this.options.interopDefault === false) | ||
return false; | ||
return !path.endsWith(".mjs") && "default" in mod; | ||
} | ||
async interopedImport(path) { | ||
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) { | ||
return "__esModule" in target && target.__esModule && "default" in target.default; | ||
} | ||
} | ||
function hasNestedDefault(target) { | ||
return "__esModule" in target && target.__esModule && "default" in target.default; | ||
} | ||
function proxyMethod(name, tryDefault) { | ||
@@ -132,15 +166,2 @@ return function(target, key, ...args) { | ||
} | ||
async function interpretedImport(path, interpretDefault) { | ||
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, sourceModule) { | ||
@@ -163,2 +184,2 @@ for (const key in sourceModule) { | ||
export { ViteNodeRunner }; | ||
export { DEFAULT_REQUEST_STUBS, ViteNodeRunner }; |
@@ -26,3 +26,3 @@ interface DepsHandlingOptions { | ||
moduleCache?: Map<string, ModuleCache>; | ||
interpretDefault?: boolean; | ||
interopDefault?: boolean; | ||
requestStubs?: Record<string, any>; | ||
@@ -29,0 +29,0 @@ } |
{ | ||
"name": "vite-node", | ||
"version": "0.1.20", | ||
"version": "0.1.21", | ||
"description": "Vite as Node.js runtime", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/vitest-dev/vitest#readme", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
64000
1751