formacao
This commit is contained in:
+45
-28
@@ -2,7 +2,7 @@ import { isBuiltin, createRequire } from 'node:module';
|
||||
import { pathToFileURL, fileURLToPath } from 'node:url';
|
||||
import vm from 'node:vm';
|
||||
import { ssrModuleExportsKey, ssrImportMetaKey, ssrImportKey, ssrDynamicImportKey, ssrExportAllKey } from 'vite/module-runner';
|
||||
import { T as Traces } from './chunks/traces.CCmnQaNT.js';
|
||||
import { T as Traces } from './chunks/traces.DT5aQ62U.js';
|
||||
|
||||
const performanceNow = performance.now.bind(performance);
|
||||
class ModuleDebug {
|
||||
@@ -60,7 +60,7 @@ class VitestModuleEvaluator {
|
||||
convertIdToImportUrl(id) {
|
||||
// TODO: vitest returns paths for external modules, but Vite returns file://
|
||||
// REMOVE WHEN VITE 6 SUPPORT IS OVER
|
||||
// unfortunetly, there is a bug in Vite where ID is resolved incorrectly, so we can't return files until the fix is merged
|
||||
// unfortunately, there is a bug in Vite where ID is resolved incorrectly, so we can't return files until the fix is merged
|
||||
// https://github.com/vitejs/vite/pull/20449
|
||||
if (!isWindows || isBuiltin(id) || /^(?:node:|data:|http:|https:|file:)/.test(id)) return id;
|
||||
const [filepath, query] = id.split("?");
|
||||
@@ -106,8 +106,7 @@ class VitestModuleEvaluator {
|
||||
async runInlinedModule(context, code, module) {
|
||||
return this._otel.$("vitest.module.inline", (span) => this._runInlinedModule(context, code, module, span));
|
||||
}
|
||||
async _runInlinedModule(context, code, module, span) {
|
||||
context.__vite_ssr_import_meta__.env = this.env;
|
||||
_createCJSGlobals(context, module, span) {
|
||||
const { Reflect, Proxy, Object } = this.primitives;
|
||||
const exportsObject = context[ssrModuleExportsKey];
|
||||
const SYMBOL_NOT_DEFINED = Symbol("not defined");
|
||||
@@ -143,46 +142,57 @@ class VitestModuleEvaluator {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
const moduleProxy = {
|
||||
set exports(value) {
|
||||
span.addEvent("`module.exports` is assigned directly, copying all properties to `exports`");
|
||||
exportAll(cjsExports, value);
|
||||
exportsObject.default = value;
|
||||
moduleExports = value;
|
||||
return {
|
||||
exports: cjsExports,
|
||||
module: {
|
||||
set exports(value) {
|
||||
span.addEvent("`module.exports` is assigned directly, copying all properties to `exports`");
|
||||
exportAll(cjsExports, value);
|
||||
exportsObject.default = value;
|
||||
moduleExports = value;
|
||||
},
|
||||
get exports() {
|
||||
return cjsExports;
|
||||
}
|
||||
},
|
||||
get exports() {
|
||||
return cjsExports;
|
||||
}
|
||||
require: this.createRequire(context[ssrImportMetaKey].url),
|
||||
__filename: context[ssrImportMetaKey].filename,
|
||||
__dirname: context[ssrImportMetaKey].dirname
|
||||
};
|
||||
}
|
||||
async _runInlinedModule(context, code, module, span) {
|
||||
const meta = context[ssrImportMetaKey];
|
||||
meta.env = this.env;
|
||||
if (this.options.getCurrentTestFilepath?.() === module.file) {
|
||||
const globalNamespace = this.vm?.context || globalThis;
|
||||
Object.defineProperty(meta, "vitest", { get: () => globalNamespace.__vitest_index__ });
|
||||
}
|
||||
const filename = meta.filename;
|
||||
const dirname = meta.dirname;
|
||||
span.setAttributes({ "code.file.path": filename });
|
||||
const require = this.createRequire(meta.url);
|
||||
span.setAttribute("code.file.path", meta.filename);
|
||||
const argumentsList = [
|
||||
ssrModuleExportsKey,
|
||||
ssrImportMetaKey,
|
||||
ssrImportKey,
|
||||
ssrDynamicImportKey,
|
||||
ssrExportAllKey,
|
||||
"__vite_ssr_exportName__",
|
||||
"__vite_ssr_exportName__"
|
||||
];
|
||||
const cjsGlobals = this._createCJSGlobals(context, module, span);
|
||||
argumentsList.push(
|
||||
// TODO@discuss deprecate in Vitest 5, remove in Vitest 6(?)
|
||||
// backwards compat for vite-node
|
||||
"__filename",
|
||||
"__dirname",
|
||||
"module",
|
||||
"exports",
|
||||
"require"
|
||||
];
|
||||
);
|
||||
if (this.compiledFunctionArgumentsNames) argumentsList.push(...this.compiledFunctionArgumentsNames);
|
||||
span.setAttribute("vitest.module.arguments", argumentsList);
|
||||
// add 'use strict' since ESM enables it by default
|
||||
const codeDefinition = `'use strict';async (${argumentsList.join(",")})=>{{`;
|
||||
const wrappedCode = `${codeDefinition}${code}\n}}`;
|
||||
const options = {
|
||||
filename: module.id,
|
||||
filename: module.id.startsWith("mock:") ? module.id.slice(5) : module.id,
|
||||
lineOffset: 0,
|
||||
columnOffset: -codeDefinition.length
|
||||
};
|
||||
@@ -200,16 +210,16 @@ class VitestModuleEvaluator {
|
||||
context[ssrDynamicImportKey],
|
||||
context[ssrExportAllKey],
|
||||
// vite 7 support, remove when vite 7+ is supported
|
||||
context.__vite_ssr_exportName__ || ((name, getter) => Object.defineProperty(exportsObject, name, {
|
||||
context.__vite_ssr_exportName__ || ((name, getter) => Object.defineProperty(context[ssrModuleExportsKey], name, {
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
get: getter
|
||||
})),
|
||||
filename,
|
||||
dirname,
|
||||
moduleProxy,
|
||||
cjsExports,
|
||||
require,
|
||||
cjsGlobals.__filename,
|
||||
cjsGlobals.__dirname,
|
||||
cjsGlobals.module,
|
||||
cjsGlobals.exports,
|
||||
cjsGlobals.require,
|
||||
...this.compiledFunctionArgumentsValues
|
||||
);
|
||||
} finally {
|
||||
@@ -218,8 +228,15 @@ class VitestModuleEvaluator {
|
||||
this.options.moduleExecutionInfo?.set(options.filename, finishModuleExecutionInfo());
|
||||
}
|
||||
}
|
||||
createRequire(filename) {
|
||||
return this.vm ? this.vm.externalModulesExecutor.createRequire(filename) : createRequire(filename);
|
||||
createRequire(url) {
|
||||
if (url.startsWith("data:")) {
|
||||
const _require = (id) => {
|
||||
throw new SyntaxError(`require() is not supported in virtual modules. Trying to call require("${id}") in ${url}`);
|
||||
};
|
||||
_require.resolve = _require;
|
||||
return _require;
|
||||
}
|
||||
return this.vm ? this.vm.externalModulesExecutor.createRequire(url) : createRequire(url);
|
||||
}
|
||||
shouldInterop(path, mod) {
|
||||
if (this.options.interopDefault === false) return false;
|
||||
|
||||
Reference in New Issue
Block a user