|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- 'use strict';
-
- Object.defineProperty(exports, '__esModule', { value: true });
-
- var perf_hooks = require('perf_hooks');
- var fs = require('fs');
- var pathe = require('pathe');
- var createDebug = require('debug');
- var mlly = require('mlly');
- var utils = require('./utils.cjs');
- var c = require('picocolors');
- var sourceMap = require('./source-map.cjs');
- require('node:url');
- require('path');
-
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
-
- var createDebug__default = /*#__PURE__*/_interopDefaultLegacy(createDebug);
- var c__default = /*#__PURE__*/_interopDefaultLegacy(c);
-
- const KNOWN_ASSET_TYPES = [
- "png",
- "jpe?g",
- "jfif",
- "pjpeg",
- "pjp",
- "gif",
- "svg",
- "ico",
- "webp",
- "avif",
- "mp4",
- "webm",
- "ogg",
- "mp3",
- "wav",
- "flac",
- "aac",
- "woff2?",
- "eot",
- "ttf",
- "otf",
- "webmanifest",
- "pdf",
- "txt"
- ];
- const ESM_EXT_RE = /\.(es|esm|esm-browser|esm-bundler|es6|module)\.js$/;
- const ESM_FOLDER_RE = /\/(es|esm)\/(.*\.js)$/;
- const defaultInline = [
- /virtual:/,
- /\.[mc]?ts$/,
- /[?&](init|raw|url|inline)\b/,
- new RegExp(`\\.(${KNOWN_ASSET_TYPES.join("|")})$`)
- ];
- const depsExternal = [
- /\.cjs\.js$/,
- /\.mjs$/
- ];
- function guessCJSversion(id) {
- if (id.match(ESM_EXT_RE)) {
- for (const i of [
- id.replace(ESM_EXT_RE, ".mjs"),
- id.replace(ESM_EXT_RE, ".umd.js"),
- id.replace(ESM_EXT_RE, ".cjs.js"),
- id.replace(ESM_EXT_RE, ".js")
- ]) {
- if (fs.existsSync(i))
- return i;
- }
- }
- if (id.match(ESM_FOLDER_RE)) {
- for (const i of [
- id.replace(ESM_FOLDER_RE, "/umd/$1"),
- id.replace(ESM_FOLDER_RE, "/cjs/$1"),
- id.replace(ESM_FOLDER_RE, "/lib/$1"),
- id.replace(ESM_FOLDER_RE, "/$1")
- ]) {
- if (fs.existsSync(i))
- return i;
- }
- }
- }
- const _defaultExternalizeCache = /* @__PURE__ */ new Map();
- async function shouldExternalize(id, options, cache = _defaultExternalizeCache) {
- if (!cache.has(id))
- cache.set(id, _shouldExternalize(id, options));
- return cache.get(id);
- }
- async function _shouldExternalize(id, options) {
- if (mlly.isNodeBuiltin(id))
- return id;
- if (id.startsWith("data:"))
- return id;
- id = patchWindowsImportPath(id);
- if ((options == null ? void 0 : options.cacheDir) && id.includes(options.cacheDir))
- return id;
- if (matchExternalizePattern(id, options == null ? void 0 : options.inline))
- return false;
- if (matchExternalizePattern(id, options == null ? void 0 : options.external))
- return id;
- const isNodeModule = id.includes("/node_modules/");
- const guessCJS = isNodeModule && (options == null ? void 0 : options.fallbackCJS);
- id = guessCJS ? guessCJSversion(id) || id : id;
- if (matchExternalizePattern(id, defaultInline))
- return false;
- if (matchExternalizePattern(id, depsExternal))
- return id;
- const isDist = id.includes("/dist/");
- if ((isNodeModule || isDist) && await mlly.isValidNodeImport(id))
- return id;
- return false;
- }
- function matchExternalizePattern(id, patterns) {
- if (patterns == null)
- return false;
- if (patterns === true)
- return true;
- for (const ex of patterns) {
- if (typeof ex === "string") {
- if (id.includes(`/node_modules/${ex}/`))
- return true;
- } else {
- if (ex.test(id))
- return true;
- }
- }
- return false;
- }
- function patchWindowsImportPath(path) {
- if (path.match(/^\w:\\/))
- return `file:///${utils.slash(path)}`;
- else if (path.match(/^\w:\//))
- return `file:///${path}`;
- else
- return path;
- }
-
- function hashCode(s) {
- return s.split("").reduce((a, b) => {
- a = (a << 5) - a + b.charCodeAt(0);
- return a & a;
- }, 0);
- }
- class Debugger {
- constructor(root, options) {
- this.options = options;
- this.externalizeMap = /* @__PURE__ */ new Map();
- if (options.dumpModules)
- this.dumpDir = pathe.resolve(root, options.dumpModules === true ? ".vite-node/dump" : options.dumpModules);
- if (this.dumpDir) {
- if (options.loadDumppedModules)
- console.info(c__default["default"].gray(`[vite-node] [debug] load modules from ${this.dumpDir}`));
- else
- console.info(c__default["default"].gray(`[vite-node] [debug] dump modules to ${this.dumpDir}`));
- }
- this.initPromise = this.clearDump();
- }
- async clearDump() {
- if (!this.dumpDir)
- return;
- if (!this.options.loadDumppedModules && fs.existsSync(this.dumpDir))
- await fs.promises.rm(this.dumpDir, { recursive: true, force: true });
- await fs.promises.mkdir(this.dumpDir, { recursive: true });
- }
- encodeId(id) {
- return `${id.replace(/[^\w@_-]/g, "_").replace(/_+/g, "_")}-${hashCode(id)}.js`;
- }
- async recordExternalize(id, path) {
- if (!this.dumpDir)
- return;
- this.externalizeMap.set(id, path);
- await this.writeInfo();
- }
- async dumpFile(id, result) {
- if (!result || !this.dumpDir)
- return;
- await this.initPromise;
- const name = this.encodeId(id);
- return await fs.promises.writeFile(pathe.join(this.dumpDir, name), `// ${id.replace(/\0/g, "\\0")}
- ${result.code}`, "utf-8");
- }
- async loadDump(id) {
- if (!this.dumpDir)
- return null;
- await this.initPromise;
- const name = this.encodeId(id);
- const path = pathe.join(this.dumpDir, name);
- if (!fs.existsSync(path))
- return null;
- const code = await fs.promises.readFile(path, "utf-8");
- return {
- code: code.replace(/^\/\/.*?\n/, ""),
- map: void 0
- };
- }
- async writeInfo() {
- if (!this.dumpDir)
- return;
- const info = JSON.stringify({
- time: new Date().toLocaleString(),
- externalize: Object.fromEntries(this.externalizeMap.entries())
- }, null, 2);
- return fs.promises.writeFile(pathe.join(this.dumpDir, "info.json"), info, "utf-8");
- }
- }
-
- const debugRequest = createDebug__default["default"]("vite-node:server:request");
- class ViteNodeServer {
- constructor(server, options = {}) {
- this.server = server;
- this.options = options;
- this.fetchPromiseMap = /* @__PURE__ */ new Map();
- this.transformPromiseMap = /* @__PURE__ */ new Map();
- this.existingOptimizedDeps = /* @__PURE__ */ new Set();
- this.fetchCache = /* @__PURE__ */ new Map();
- this.externalizeCache = /* @__PURE__ */ new Map();
- var _a, _b;
- const ssrOptions = server.config.ssr;
- options.deps ?? (options.deps = {});
- options.deps.cacheDir = pathe.relative(server.config.root, server.config.cacheDir);
- if (ssrOptions) {
- if (ssrOptions.noExternal === true) {
- (_a = options.deps).inline ?? (_a.inline = true);
- } else if (options.deps.inline !== true) {
- (_b = options.deps).inline ?? (_b.inline = []);
- options.deps.inline.push(...utils.toArray(ssrOptions.noExternal));
- }
- }
- if (process.env.VITE_NODE_DEBUG_DUMP) {
- options.debug = Object.assign({
- dumpModules: !!process.env.VITE_NODE_DEBUG_DUMP,
- loadDumppedModules: process.env.VITE_NODE_DEBUG_DUMP === "load"
- }, options.debug ?? {});
- }
- if (options.debug)
- this.debugger = new Debugger(server.config.root, options.debug);
- }
- shouldExternalize(id) {
- return shouldExternalize(id, this.options.deps, this.externalizeCache);
- }
- async ensureExists(id) {
- if (this.existingOptimizedDeps.has(id))
- return true;
- if (fs.existsSync(id)) {
- this.existingOptimizedDeps.add(id);
- return true;
- }
- return new Promise((resolve2) => {
- setTimeout(() => {
- this.ensureExists(id).then(() => {
- resolve2(true);
- });
- });
- });
- }
- async resolveId(id, importer, transformMode) {
- if (importer && !importer.startsWith(this.server.config.root))
- importer = pathe.resolve(this.server.config.root, importer);
- const mode = transformMode ?? (importer && this.getTransformMode(importer) || "ssr");
- return this.server.pluginContainer.resolveId(id, importer, { ssr: mode === "ssr" });
- }
- getSourceMap(source) {
- var _a, _b;
- const fetchResult = (_a = this.fetchCache.get(source)) == null ? void 0 : _a.result;
- if (fetchResult == null ? void 0 : fetchResult.map)
- return fetchResult.map;
- const ssrTransformResult = (_b = this.server.moduleGraph.getModuleById(source)) == null ? void 0 : _b.ssrTransformResult;
- return (ssrTransformResult == null ? void 0 : ssrTransformResult.map) || null;
- }
- async fetchModule(id, transformMode) {
- id = utils.normalizeModuleId(id);
- if (!this.fetchPromiseMap.has(id)) {
- this.fetchPromiseMap.set(
- id,
- this._fetchModule(id, transformMode).then((r) => {
- return this.options.sourcemap !== true ? { ...r, map: void 0 } : r;
- }).finally(() => {
- this.fetchPromiseMap.delete(id);
- })
- );
- }
- return this.fetchPromiseMap.get(id);
- }
- async transformRequest(id) {
- if (!this.transformPromiseMap.has(id)) {
- this.transformPromiseMap.set(
- id,
- this._transformRequest(id).finally(() => {
- this.transformPromiseMap.delete(id);
- })
- );
- }
- return this.transformPromiseMap.get(id);
- }
- getTransformMode(id) {
- var _a, _b, _c, _d;
- const withoutQuery = id.split("?")[0];
- if ((_b = (_a = this.options.transformMode) == null ? void 0 : _a.web) == null ? void 0 : _b.some((r) => withoutQuery.match(r)))
- return "web";
- if ((_d = (_c = this.options.transformMode) == null ? void 0 : _c.ssr) == null ? void 0 : _d.some((r) => withoutQuery.match(r)))
- return "ssr";
- if (withoutQuery.match(/\.([cm]?[jt]sx?|json)$/))
- return "ssr";
- return "web";
- }
- async _fetchModule(id, transformMode) {
- var _a, _b;
- let result;
- const cacheDir = (_a = this.options.deps) == null ? void 0 : _a.cacheDir;
- if (cacheDir && id.includes(cacheDir)) {
- if (!id.startsWith(this.server.config.root))
- id = pathe.join(this.server.config.root, id);
- const timeout = setTimeout(() => {
- throw new Error(`ViteNodeServer: ${id} not found. This is a bug, please report it.`);
- }, 5e3);
- await this.ensureExists(id);
- clearTimeout(timeout);
- }
- const { path: filePath } = utils.toFilePath(id, this.server.config.root);
- const module = this.server.moduleGraph.getModuleById(id);
- const timestamp = module ? module.lastHMRTimestamp : null;
- const cache = this.fetchCache.get(filePath);
- if (timestamp && cache && cache.timestamp >= timestamp)
- return cache.result;
- const time = Date.now();
- const externalize = await this.shouldExternalize(filePath);
- let duration;
- if (externalize) {
- result = { externalize };
- (_b = this.debugger) == null ? void 0 : _b.recordExternalize(id, externalize);
- } else {
- const start = perf_hooks.performance.now();
- const r = await this._transformRequest(id, transformMode);
- duration = perf_hooks.performance.now() - start;
- result = { code: r == null ? void 0 : r.code, map: r == null ? void 0 : r.map };
- }
- this.fetchCache.set(filePath, {
- duration,
- timestamp: time,
- result
- });
- return result;
- }
- async processTransformResult(result) {
- return sourceMap.withInlineSourcemap(result);
- }
- async _transformRequest(id, customTransformMode) {
- var _a, _b, _c, _d;
- debugRequest(id);
- let result = null;
- if ((_a = this.options.debug) == null ? void 0 : _a.loadDumppedModules) {
- result = await ((_b = this.debugger) == null ? void 0 : _b.loadDump(id)) ?? null;
- if (result)
- return result;
- }
- const transformMode = customTransformMode ?? this.getTransformMode(id);
- if (transformMode === "web") {
- result = await this.server.transformRequest(id);
- if (result)
- result = await this.server.ssrTransform(result.code, result.map, id);
- } else {
- result = await this.server.transformRequest(id, { ssr: true });
- }
- const sourcemap = this.options.sourcemap ?? "inline";
- if (sourcemap === "inline" && result && !id.includes("node_modules"))
- result = await this.processTransformResult(result);
- if ((_c = this.options.debug) == null ? void 0 : _c.dumpModules)
- await ((_d = this.debugger) == null ? void 0 : _d.dumpFile(id, result));
- return result;
- }
- }
-
- exports.ViteNodeServer = ViteNodeServer;
- exports.guessCJSversion = guessCJSversion;
- exports.shouldExternalize = shouldExternalize;
|