vite-node 0.17.0 → 0.18.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,269 +0,0 @@
1
- 'use strict';
2
-
3
- var module$1 = require('module');
4
- var url = require('url');
5
- var vm = require('vm');
6
- var pathe = require('pathe');
7
- var mlly = require('mlly');
8
- var createDebug = require('debug');
9
- var utils = require('./chunk-utils.cjs');
10
-
11
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
12
-
13
- function _interopNamespace(e) {
14
- if (e && e.__esModule) return e;
15
- var n = Object.create(null);
16
- if (e) {
17
- Object.keys(e).forEach(function (k) {
18
- if (k !== 'default') {
19
- var d = Object.getOwnPropertyDescriptor(e, k);
20
- Object.defineProperty(n, k, d.get ? d : {
21
- enumerable: true,
22
- get: function () { return e[k]; }
23
- });
24
- }
25
- });
26
- }
27
- n["default"] = e;
28
- return Object.freeze(n);
29
- }
30
-
31
- var vm__default = /*#__PURE__*/_interopDefaultLegacy(vm);
32
- var createDebug__default = /*#__PURE__*/_interopDefaultLegacy(createDebug);
33
-
34
- const debugExecute = createDebug__default["default"]("vite-node:client:execute");
35
- const debugNative = createDebug__default["default"]("vite-node:client:native");
36
- const DEFAULT_REQUEST_STUBS = {
37
- "/@vite/client": {
38
- injectQuery: (id) => id,
39
- createHotContext() {
40
- return {
41
- accept: () => {
42
- },
43
- prune: () => {
44
- },
45
- dispose: () => {
46
- },
47
- decline: () => {
48
- },
49
- invalidate: () => {
50
- },
51
- on: () => {
52
- }
53
- };
54
- },
55
- updateStyle() {
56
- }
57
- }
58
- };
59
- class ModuleCacheMap extends Map {
60
- normalizePath(fsPath) {
61
- return utils.normalizeModuleId(fsPath);
62
- }
63
- set(fsPath, mod) {
64
- fsPath = this.normalizePath(fsPath);
65
- if (!super.has(fsPath))
66
- super.set(fsPath, mod);
67
- else
68
- Object.assign(super.get(fsPath), mod);
69
- return this;
70
- }
71
- get(fsPath) {
72
- fsPath = this.normalizePath(fsPath);
73
- return super.get(fsPath);
74
- }
75
- delete(fsPath) {
76
- fsPath = this.normalizePath(fsPath);
77
- return super.delete(fsPath);
78
- }
79
- }
80
- class ViteNodeRunner {
81
- constructor(options) {
82
- this.options = options;
83
- this.root = options.root ?? process.cwd();
84
- this.moduleCache = options.moduleCache ?? new ModuleCacheMap();
85
- this.debug = options.debug ?? (typeof process !== "undefined" ? !!process.env.VITE_NODE_DEBUG : false);
86
- }
87
- async executeFile(file) {
88
- return await this.cachedRequest(`/@fs/${utils.slash(pathe.resolve(file))}`, []);
89
- }
90
- async executeId(id) {
91
- return await this.cachedRequest(id, []);
92
- }
93
- async cachedRequest(rawId, callstack) {
94
- var _a, _b;
95
- const id = utils.normalizeRequestId(rawId, this.options.base);
96
- const fsPath = utils.toFilePath(id, this.root);
97
- if ((_a = this.moduleCache.get(fsPath)) == null ? void 0 : _a.promise)
98
- return (_b = this.moduleCache.get(fsPath)) == null ? void 0 : _b.promise;
99
- const promise = this.directRequest(id, fsPath, callstack);
100
- this.moduleCache.set(fsPath, { promise });
101
- return await promise;
102
- }
103
- async directRequest(id, fsPath, _callstack) {
104
- const callstack = [..._callstack, utils.normalizeModuleId(id)];
105
- const request = async (dep) => {
106
- var _a;
107
- const getStack = () => {
108
- return `stack:
109
- ${[...callstack, dep].reverse().map((p) => `- ${p}`).join("\n")}`;
110
- };
111
- let debugTimer;
112
- if (this.debug)
113
- debugTimer = setTimeout(() => this.debugLog(() => `module ${dep} takes over 2s to load.
114
- ${getStack()}`), 2e3);
115
- try {
116
- if (callstack.includes(utils.normalizeModuleId(dep))) {
117
- this.debugLog(() => `circular dependency, ${getStack()}`);
118
- const depExports = (_a = this.moduleCache.get(dep)) == null ? void 0 : _a.exports;
119
- if (depExports)
120
- return depExports;
121
- throw new Error(`[vite-node] Failed to resolve circular dependency, ${getStack()}`);
122
- }
123
- const mod = await this.cachedRequest(dep, callstack);
124
- return mod;
125
- } finally {
126
- if (debugTimer)
127
- clearTimeout(debugTimer);
128
- }
129
- };
130
- Object.defineProperty(request, "callstack", { get: () => callstack });
131
- const resolveId = async (dep, callstackPosition = 1) => {
132
- if (this.options.resolveId && this.shouldResolveId(dep)) {
133
- let importer = callstack[callstack.length - callstackPosition];
134
- if (importer && importer.startsWith("mock:"))
135
- importer = importer.slice(5);
136
- const { id: id2 } = await this.options.resolveId(dep, importer) || {};
137
- dep = id2 && pathe.isAbsolute(id2) ? utils.mergeSlashes(`/@fs/${id2}`) : id2 || dep;
138
- }
139
- return dep;
140
- };
141
- id = await resolveId(id, 2);
142
- const requestStubs = this.options.requestStubs || DEFAULT_REQUEST_STUBS;
143
- if (id in requestStubs)
144
- return requestStubs[id];
145
- let { code: transformed, externalize } = await this.options.fetchModule(id);
146
- if (externalize) {
147
- debugNative(externalize);
148
- const mod = await this.interopedImport(externalize);
149
- this.moduleCache.set(fsPath, { exports: mod });
150
- return mod;
151
- }
152
- if (transformed == null)
153
- throw new Error(`[vite-node] Failed to load ${id}`);
154
- const url$1 = url.pathToFileURL(fsPath).href;
155
- const meta = { url: url$1 };
156
- const exports = /* @__PURE__ */ Object.create(null);
157
- exports[Symbol.toStringTag] = "Module";
158
- this.moduleCache.set(id, { code: transformed, exports });
159
- const __filename = url.fileURLToPath(url$1);
160
- const moduleProxy = {
161
- set exports(value) {
162
- exportAll(exports, value);
163
- exports.default = value;
164
- },
165
- get exports() {
166
- return exports;
167
- }
168
- };
169
- let hotContext;
170
- if (this.options.createHotContext) {
171
- Object.defineProperty(meta, "hot", {
172
- enumerable: true,
173
- get: () => {
174
- var _a, _b;
175
- hotContext || (hotContext = (_b = (_a = this.options).createHotContext) == null ? void 0 : _b.call(_a, this, `/@fs/${fsPath}`));
176
- return hotContext;
177
- }
178
- });
179
- }
180
- const context = this.prepareContext({
181
- __vite_ssr_import__: request,
182
- __vite_ssr_dynamic_import__: request,
183
- __vite_ssr_exports__: exports,
184
- __vite_ssr_exportAll__: (obj) => exportAll(exports, obj),
185
- __vite_ssr_import_meta__: meta,
186
- __vitest_resolve_id__: resolveId,
187
- require: module$1.createRequire(url$1),
188
- exports,
189
- module: moduleProxy,
190
- __filename,
191
- __dirname: pathe.dirname(__filename)
192
- });
193
- debugExecute(__filename);
194
- if (transformed[0] === "#")
195
- transformed = transformed.replace(/^\#\!.*/, (s) => " ".repeat(s.length));
196
- const fn = vm__default["default"].runInThisContext(`'use strict';async (${Object.keys(context).join(",")})=>{{${transformed}
197
- }}`, {
198
- filename: fsPath,
199
- lineOffset: 0
200
- });
201
- await fn(...Object.values(context));
202
- return exports;
203
- }
204
- prepareContext(context) {
205
- return context;
206
- }
207
- shouldResolveId(dep) {
208
- if (mlly.isNodeBuiltin(dep) || dep in (this.options.requestStubs || DEFAULT_REQUEST_STUBS) || dep.startsWith("/@vite"))
209
- return false;
210
- return !pathe.isAbsolute(dep) || !pathe.extname(dep);
211
- }
212
- shouldInterop(path, mod) {
213
- if (this.options.interopDefault === false)
214
- return false;
215
- return !path.endsWith(".mjs") && "default" in mod;
216
- }
217
- async interopedImport(path) {
218
- const mod = await (function (t) { return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(t)); }); })(path);
219
- if (this.shouldInterop(path, mod)) {
220
- const tryDefault = this.hasNestedDefault(mod);
221
- return new Proxy(mod, {
222
- get: proxyMethod("get", tryDefault),
223
- set: proxyMethod("set", tryDefault),
224
- has: proxyMethod("has", tryDefault),
225
- deleteProperty: proxyMethod("deleteProperty", tryDefault)
226
- });
227
- }
228
- return mod;
229
- }
230
- hasNestedDefault(target) {
231
- return "__esModule" in target && target.__esModule && "default" in target.default;
232
- }
233
- debugLog(msg) {
234
- if (this.debug)
235
- console.log(`[vite-node] ${msg()}`);
236
- }
237
- }
238
- function proxyMethod(name, tryDefault) {
239
- return function(target, key, ...args) {
240
- const result = Reflect[name](target, key, ...args);
241
- if (utils.isPrimitive(target.default))
242
- return result;
243
- if (tryDefault && key === "default" || typeof result === "undefined")
244
- return Reflect[name](target.default, key, ...args);
245
- return result;
246
- };
247
- }
248
- function exportAll(exports, sourceModule) {
249
- if (exports === sourceModule)
250
- return;
251
- for (const key in sourceModule) {
252
- if (key !== "default") {
253
- try {
254
- Object.defineProperty(exports, key, {
255
- enumerable: true,
256
- configurable: true,
257
- get() {
258
- return sourceModule[key];
259
- }
260
- });
261
- } catch (_err) {
262
- }
263
- }
264
- }
265
- }
266
-
267
- exports.DEFAULT_REQUEST_STUBS = DEFAULT_REQUEST_STUBS;
268
- exports.ModuleCacheMap = ModuleCacheMap;
269
- exports.ViteNodeRunner = ViteNodeRunner;
@@ -1,242 +0,0 @@
1
- import { createRequire } from 'module';
2
- import { pathToFileURL, fileURLToPath } from 'url';
3
- import vm from 'vm';
4
- import { resolve, dirname, isAbsolute, extname } from 'pathe';
5
- import { isNodeBuiltin } from 'mlly';
6
- import createDebug from 'debug';
7
- import { n as normalizeModuleId, s as slash, b as normalizeRequestId, a as toFilePath, i as isPrimitive, m as mergeSlashes } from './chunk-utils.mjs';
8
-
9
- const debugExecute = createDebug("vite-node:client:execute");
10
- const debugNative = createDebug("vite-node:client:native");
11
- const DEFAULT_REQUEST_STUBS = {
12
- "/@vite/client": {
13
- injectQuery: (id) => id,
14
- createHotContext() {
15
- return {
16
- accept: () => {
17
- },
18
- prune: () => {
19
- },
20
- dispose: () => {
21
- },
22
- decline: () => {
23
- },
24
- invalidate: () => {
25
- },
26
- on: () => {
27
- }
28
- };
29
- },
30
- updateStyle() {
31
- }
32
- }
33
- };
34
- class ModuleCacheMap extends Map {
35
- normalizePath(fsPath) {
36
- return normalizeModuleId(fsPath);
37
- }
38
- set(fsPath, mod) {
39
- fsPath = this.normalizePath(fsPath);
40
- if (!super.has(fsPath))
41
- super.set(fsPath, mod);
42
- else
43
- Object.assign(super.get(fsPath), mod);
44
- return this;
45
- }
46
- get(fsPath) {
47
- fsPath = this.normalizePath(fsPath);
48
- return super.get(fsPath);
49
- }
50
- delete(fsPath) {
51
- fsPath = this.normalizePath(fsPath);
52
- return super.delete(fsPath);
53
- }
54
- }
55
- class ViteNodeRunner {
56
- constructor(options) {
57
- this.options = options;
58
- this.root = options.root ?? process.cwd();
59
- this.moduleCache = options.moduleCache ?? new ModuleCacheMap();
60
- this.debug = options.debug ?? (typeof process !== "undefined" ? !!process.env.VITE_NODE_DEBUG : false);
61
- }
62
- async executeFile(file) {
63
- return await this.cachedRequest(`/@fs/${slash(resolve(file))}`, []);
64
- }
65
- async executeId(id) {
66
- return await this.cachedRequest(id, []);
67
- }
68
- async cachedRequest(rawId, callstack) {
69
- var _a, _b;
70
- const id = normalizeRequestId(rawId, this.options.base);
71
- const fsPath = toFilePath(id, this.root);
72
- if ((_a = this.moduleCache.get(fsPath)) == null ? void 0 : _a.promise)
73
- return (_b = this.moduleCache.get(fsPath)) == null ? void 0 : _b.promise;
74
- const promise = this.directRequest(id, fsPath, callstack);
75
- this.moduleCache.set(fsPath, { promise });
76
- return await promise;
77
- }
78
- async directRequest(id, fsPath, _callstack) {
79
- const callstack = [..._callstack, normalizeModuleId(id)];
80
- const request = async (dep) => {
81
- var _a;
82
- const getStack = () => {
83
- return `stack:
84
- ${[...callstack, dep].reverse().map((p) => `- ${p}`).join("\n")}`;
85
- };
86
- let debugTimer;
87
- if (this.debug)
88
- debugTimer = setTimeout(() => this.debugLog(() => `module ${dep} takes over 2s to load.
89
- ${getStack()}`), 2e3);
90
- try {
91
- if (callstack.includes(normalizeModuleId(dep))) {
92
- this.debugLog(() => `circular dependency, ${getStack()}`);
93
- const depExports = (_a = this.moduleCache.get(dep)) == null ? void 0 : _a.exports;
94
- if (depExports)
95
- return depExports;
96
- throw new Error(`[vite-node] Failed to resolve circular dependency, ${getStack()}`);
97
- }
98
- const mod = await this.cachedRequest(dep, callstack);
99
- return mod;
100
- } finally {
101
- if (debugTimer)
102
- clearTimeout(debugTimer);
103
- }
104
- };
105
- Object.defineProperty(request, "callstack", { get: () => callstack });
106
- const resolveId = async (dep, callstackPosition = 1) => {
107
- if (this.options.resolveId && this.shouldResolveId(dep)) {
108
- let importer = callstack[callstack.length - callstackPosition];
109
- if (importer && importer.startsWith("mock:"))
110
- importer = importer.slice(5);
111
- const { id: id2 } = await this.options.resolveId(dep, importer) || {};
112
- dep = id2 && isAbsolute(id2) ? mergeSlashes(`/@fs/${id2}`) : id2 || dep;
113
- }
114
- return dep;
115
- };
116
- id = await resolveId(id, 2);
117
- const requestStubs = this.options.requestStubs || DEFAULT_REQUEST_STUBS;
118
- if (id in requestStubs)
119
- return requestStubs[id];
120
- let { code: transformed, externalize } = await this.options.fetchModule(id);
121
- if (externalize) {
122
- debugNative(externalize);
123
- const mod = await this.interopedImport(externalize);
124
- this.moduleCache.set(fsPath, { exports: mod });
125
- return mod;
126
- }
127
- if (transformed == null)
128
- throw new Error(`[vite-node] Failed to load ${id}`);
129
- const url = pathToFileURL(fsPath).href;
130
- const meta = { url };
131
- const exports = /* @__PURE__ */ Object.create(null);
132
- exports[Symbol.toStringTag] = "Module";
133
- this.moduleCache.set(id, { code: transformed, exports });
134
- const __filename = fileURLToPath(url);
135
- const moduleProxy = {
136
- set exports(value) {
137
- exportAll(exports, value);
138
- exports.default = value;
139
- },
140
- get exports() {
141
- return exports;
142
- }
143
- };
144
- let hotContext;
145
- if (this.options.createHotContext) {
146
- Object.defineProperty(meta, "hot", {
147
- enumerable: true,
148
- get: () => {
149
- var _a, _b;
150
- hotContext || (hotContext = (_b = (_a = this.options).createHotContext) == null ? void 0 : _b.call(_a, this, `/@fs/${fsPath}`));
151
- return hotContext;
152
- }
153
- });
154
- }
155
- const context = this.prepareContext({
156
- __vite_ssr_import__: request,
157
- __vite_ssr_dynamic_import__: request,
158
- __vite_ssr_exports__: exports,
159
- __vite_ssr_exportAll__: (obj) => exportAll(exports, obj),
160
- __vite_ssr_import_meta__: meta,
161
- __vitest_resolve_id__: resolveId,
162
- require: createRequire(url),
163
- exports,
164
- module: moduleProxy,
165
- __filename,
166
- __dirname: dirname(__filename)
167
- });
168
- debugExecute(__filename);
169
- if (transformed[0] === "#")
170
- transformed = transformed.replace(/^\#\!.*/, (s) => " ".repeat(s.length));
171
- const fn = vm.runInThisContext(`'use strict';async (${Object.keys(context).join(",")})=>{{${transformed}
172
- }}`, {
173
- filename: fsPath,
174
- lineOffset: 0
175
- });
176
- await fn(...Object.values(context));
177
- return exports;
178
- }
179
- prepareContext(context) {
180
- return context;
181
- }
182
- shouldResolveId(dep) {
183
- if (isNodeBuiltin(dep) || dep in (this.options.requestStubs || DEFAULT_REQUEST_STUBS) || dep.startsWith("/@vite"))
184
- return false;
185
- return !isAbsolute(dep) || !extname(dep);
186
- }
187
- shouldInterop(path, mod) {
188
- if (this.options.interopDefault === false)
189
- return false;
190
- return !path.endsWith(".mjs") && "default" in mod;
191
- }
192
- async interopedImport(path) {
193
- const mod = await import(path);
194
- if (this.shouldInterop(path, mod)) {
195
- const tryDefault = this.hasNestedDefault(mod);
196
- return new Proxy(mod, {
197
- get: proxyMethod("get", tryDefault),
198
- set: proxyMethod("set", tryDefault),
199
- has: proxyMethod("has", tryDefault),
200
- deleteProperty: proxyMethod("deleteProperty", tryDefault)
201
- });
202
- }
203
- return mod;
204
- }
205
- hasNestedDefault(target) {
206
- return "__esModule" in target && target.__esModule && "default" in target.default;
207
- }
208
- debugLog(msg) {
209
- if (this.debug)
210
- console.log(`[vite-node] ${msg()}`);
211
- }
212
- }
213
- function proxyMethod(name, tryDefault) {
214
- return function(target, key, ...args) {
215
- const result = Reflect[name](target, key, ...args);
216
- if (isPrimitive(target.default))
217
- return result;
218
- if (tryDefault && key === "default" || typeof result === "undefined")
219
- return Reflect[name](target.default, key, ...args);
220
- return result;
221
- };
222
- }
223
- function exportAll(exports, sourceModule) {
224
- if (exports === sourceModule)
225
- return;
226
- for (const key in sourceModule) {
227
- if (key !== "default") {
228
- try {
229
- Object.defineProperty(exports, key, {
230
- enumerable: true,
231
- configurable: true,
232
- get() {
233
- return sourceModule[key];
234
- }
235
- });
236
- } catch (_err) {
237
- }
238
- }
239
- }
240
- }
241
-
242
- export { DEFAULT_REQUEST_STUBS as D, ModuleCacheMap as M, ViteNodeRunner as V };
@@ -1,197 +0,0 @@
1
- 'use strict';
2
-
3
- var pathe = require('pathe');
4
- var createDebug = require('debug');
5
- var fs = require('fs');
6
- var mlly = require('mlly');
7
- var utils = require('./chunk-utils.cjs');
8
-
9
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
10
-
11
- var createDebug__default = /*#__PURE__*/_interopDefaultLegacy(createDebug);
12
-
13
- const ESM_EXT_RE = /\.(es|esm|esm-browser|esm-bundler|es6|module)\.js$/;
14
- const ESM_FOLDER_RE = /\/esm\/(.*\.js)$/;
15
- const defaultInline = [
16
- /virtual:/,
17
- /\.ts$/
18
- ];
19
- const depsExternal = [
20
- /\.cjs\.js$/,
21
- /\.mjs$/
22
- ];
23
- function guessCJSversion(id) {
24
- if (id.match(ESM_EXT_RE)) {
25
- for (const i of [
26
- id.replace(ESM_EXT_RE, ".mjs"),
27
- id.replace(ESM_EXT_RE, ".umd.js"),
28
- id.replace(ESM_EXT_RE, ".cjs.js"),
29
- id.replace(ESM_EXT_RE, ".js")
30
- ]) {
31
- if (fs.existsSync(i))
32
- return i;
33
- }
34
- }
35
- if (id.match(ESM_FOLDER_RE)) {
36
- for (const i of [
37
- id.replace(ESM_FOLDER_RE, "/umd/$1"),
38
- id.replace(ESM_FOLDER_RE, "/cjs/$1"),
39
- id.replace(ESM_FOLDER_RE, "/$1")
40
- ]) {
41
- if (fs.existsSync(i))
42
- return i;
43
- }
44
- }
45
- }
46
- async function shouldExternalize(id, options, cache = /* @__PURE__ */ new Map()) {
47
- if (!cache.has(id))
48
- cache.set(id, _shouldExternalize(id, options));
49
- return cache.get(id);
50
- }
51
- async function _shouldExternalize(id, options) {
52
- if (mlly.isNodeBuiltin(id))
53
- return id;
54
- if (id.startsWith("data:"))
55
- return id;
56
- id = patchWindowsImportPath(id);
57
- if (matchExternalizePattern(id, options == null ? void 0 : options.inline))
58
- return false;
59
- if (matchExternalizePattern(id, options == null ? void 0 : options.external))
60
- return id;
61
- const isNodeModule = id.includes("/node_modules/");
62
- const guessCJS = isNodeModule && (options == null ? void 0 : options.fallbackCJS);
63
- id = guessCJS ? guessCJSversion(id) || id : id;
64
- if (matchExternalizePattern(id, defaultInline))
65
- return false;
66
- if (matchExternalizePattern(id, depsExternal))
67
- return id;
68
- const isDist = id.includes("/dist/");
69
- if ((isNodeModule || isDist) && await mlly.isValidNodeImport(id))
70
- return id;
71
- return false;
72
- }
73
- function matchExternalizePattern(id, patterns) {
74
- if (patterns == null)
75
- return false;
76
- if (patterns === true)
77
- return true;
78
- for (const ex of patterns) {
79
- if (typeof ex === "string") {
80
- if (id.includes(`/node_modules/${ex}/`))
81
- return true;
82
- } else {
83
- if (ex.test(id))
84
- return true;
85
- }
86
- }
87
- return false;
88
- }
89
- function patchWindowsImportPath(path) {
90
- if (path.match(/^\w:\\/))
91
- return `file:///${utils.slash(path)}`;
92
- else if (path.match(/^\w:\//))
93
- return `file:///${path}`;
94
- else
95
- return path;
96
- }
97
-
98
- const debugRequest = createDebug__default["default"]("vite-node:server:request");
99
- const RealDate = Date;
100
- class ViteNodeServer {
101
- constructor(server, options = {}) {
102
- this.server = server;
103
- this.options = options;
104
- this.fetchPromiseMap = /* @__PURE__ */ new Map();
105
- this.transformPromiseMap = /* @__PURE__ */ new Map();
106
- this.fetchCache = /* @__PURE__ */ new Map();
107
- var _a, _b;
108
- const ssrOptions = server.config.ssr;
109
- if (ssrOptions) {
110
- options.deps ?? (options.deps = {});
111
- if (ssrOptions.noExternal === true) {
112
- (_a = options.deps).inline ?? (_a.inline = true);
113
- } else if (options.deps.inline !== true) {
114
- (_b = options.deps).inline ?? (_b.inline = []);
115
- options.deps.inline.push(...utils.toArray(ssrOptions.noExternal));
116
- }
117
- }
118
- }
119
- shouldExternalize(id) {
120
- return shouldExternalize(id, this.options.deps);
121
- }
122
- async resolveId(id, importer) {
123
- if (importer && !importer.startsWith(this.server.config.root))
124
- importer = pathe.join(this.server.config.root, importer);
125
- const mode = importer && this.getTransformMode(importer) || "ssr";
126
- return this.server.pluginContainer.resolveId(id, importer, { ssr: mode === "ssr" });
127
- }
128
- async fetchModule(id) {
129
- if (!this.fetchPromiseMap.has(id)) {
130
- this.fetchPromiseMap.set(id, this._fetchModule(id).then((r) => {
131
- return this.options.sourcemap !== true ? { ...r, map: void 0 } : r;
132
- }).finally(() => {
133
- this.fetchPromiseMap.delete(id);
134
- }));
135
- }
136
- return this.fetchPromiseMap.get(id);
137
- }
138
- async transformRequest(id) {
139
- if (!this.transformPromiseMap.has(id)) {
140
- this.transformPromiseMap.set(id, this._transformRequest(id).finally(() => {
141
- this.transformPromiseMap.delete(id);
142
- }));
143
- }
144
- return this.transformPromiseMap.get(id);
145
- }
146
- getTransformMode(id) {
147
- var _a, _b, _c, _d;
148
- const withoutQuery = id.split("?")[0];
149
- if ((_b = (_a = this.options.transformMode) == null ? void 0 : _a.web) == null ? void 0 : _b.some((r) => withoutQuery.match(r)))
150
- return "web";
151
- if ((_d = (_c = this.options.transformMode) == null ? void 0 : _c.ssr) == null ? void 0 : _d.some((r) => withoutQuery.match(r)))
152
- return "ssr";
153
- if (withoutQuery.match(/\.([cm]?[jt]sx?|json)$/))
154
- return "ssr";
155
- return "web";
156
- }
157
- async _fetchModule(id) {
158
- let result;
159
- const filePath = utils.toFilePath(id, this.server.config.root);
160
- const module = this.server.moduleGraph.getModuleById(id);
161
- const timestamp = (module == null ? void 0 : module.lastHMRTimestamp) || RealDate.now();
162
- const cache = this.fetchCache.get(filePath);
163
- if (timestamp && cache && cache.timestamp >= timestamp)
164
- return cache.result;
165
- const externalize = await this.shouldExternalize(filePath);
166
- if (externalize) {
167
- result = { externalize };
168
- } else {
169
- const r = await this._transformRequest(id);
170
- result = { code: r == null ? void 0 : r.code, map: r == null ? void 0 : r.map };
171
- }
172
- this.fetchCache.set(filePath, {
173
- timestamp,
174
- result
175
- });
176
- return result;
177
- }
178
- async _transformRequest(id) {
179
- debugRequest(id);
180
- let result = null;
181
- if (this.getTransformMode(id) === "web") {
182
- result = await this.server.transformRequest(id);
183
- if (result)
184
- result = await this.server.ssrTransform(result.code, result.map, id);
185
- } else {
186
- result = await this.server.transformRequest(id, { ssr: true });
187
- }
188
- const sourcemap = this.options.sourcemap ?? "inline";
189
- if (sourcemap === "inline" && result && !id.includes("node_modules"))
190
- utils.withInlineSourcemap(result);
191
- return result;
192
- }
193
- }
194
-
195
- exports.ViteNodeServer = ViteNodeServer;
196
- exports.guessCJSversion = guessCJSversion;
197
- exports.shouldExternalize = shouldExternalize;