index.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. export { parseAst, parseAstAsync } from 'rollup/parseAst';
  2. import { i as isInNodeModules, a as arraify } from './chunks/dep-BWSbWtLw.js';
  3. export { b as build, g as buildErrorMessage, k as createFilter, v as createLogger, c as createServer, d as defineConfig, h as fetchModule, f as formatPostcssSourceMap, x as isFileServingAllowed, l as loadConfigFromFile, y as loadEnv, j as mergeAlias, m as mergeConfig, n as normalizePath, o as optimizeDeps, e as preprocessCSS, p as preview, r as resolveConfig, z as resolveEnvPrefix, q as rollupVersion, w as searchForWorkspaceRoot, u as send, s as sortUserPlugins, t as transformWithEsbuild } from './chunks/dep-BWSbWtLw.js';
  4. export { VERSION as version } from './constants.js';
  5. export { version as esbuildVersion } from 'esbuild';
  6. import { existsSync, readFileSync } from 'node:fs';
  7. import { ViteRuntime, ESModulesRunner } from 'vite/runtime';
  8. import 'node:fs/promises';
  9. import 'node:path';
  10. import 'node:url';
  11. import 'node:util';
  12. import 'node:perf_hooks';
  13. import 'node:module';
  14. import 'tty';
  15. import 'path';
  16. import 'fs';
  17. import 'node:events';
  18. import 'node:stream';
  19. import 'node:string_decoder';
  20. import 'node:child_process';
  21. import 'node:http';
  22. import 'node:https';
  23. import 'util';
  24. import 'net';
  25. import 'events';
  26. import 'url';
  27. import 'http';
  28. import 'stream';
  29. import 'os';
  30. import 'child_process';
  31. import 'node:os';
  32. import 'node:crypto';
  33. import 'node:dns';
  34. import 'crypto';
  35. import 'module';
  36. import 'node:assert';
  37. import 'node:v8';
  38. import 'node:worker_threads';
  39. import 'node:buffer';
  40. import 'querystring';
  41. import 'node:readline';
  42. import 'zlib';
  43. import 'buffer';
  44. import 'https';
  45. import 'tls';
  46. import 'assert';
  47. import 'node:zlib';
  48. const CSS_LANGS_RE = (
  49. // eslint-disable-next-line regexp/no-unused-capturing-group
  50. /\.(css|less|sass|scss|styl|stylus|pcss|postcss|sss)(?:$|\?)/
  51. );
  52. const isCSSRequest = (request) => CSS_LANGS_RE.test(request);
  53. class SplitVendorChunkCache {
  54. cache;
  55. constructor() {
  56. this.cache = /* @__PURE__ */ new Map();
  57. }
  58. reset() {
  59. this.cache = /* @__PURE__ */ new Map();
  60. }
  61. }
  62. function splitVendorChunk(options = {}) {
  63. const cache = options.cache ?? new SplitVendorChunkCache();
  64. return (id, { getModuleInfo }) => {
  65. if (isInNodeModules(id) && !isCSSRequest(id) && staticImportedByEntry(id, getModuleInfo, cache.cache)) {
  66. return "vendor";
  67. }
  68. };
  69. }
  70. function staticImportedByEntry(id, getModuleInfo, cache, importStack = []) {
  71. if (cache.has(id)) {
  72. return cache.get(id);
  73. }
  74. if (importStack.includes(id)) {
  75. cache.set(id, false);
  76. return false;
  77. }
  78. const mod = getModuleInfo(id);
  79. if (!mod) {
  80. cache.set(id, false);
  81. return false;
  82. }
  83. if (mod.isEntry) {
  84. cache.set(id, true);
  85. return true;
  86. }
  87. const someImporterIs = mod.importers.some(
  88. (importer) => staticImportedByEntry(
  89. importer,
  90. getModuleInfo,
  91. cache,
  92. importStack.concat(id)
  93. )
  94. );
  95. cache.set(id, someImporterIs);
  96. return someImporterIs;
  97. }
  98. function splitVendorChunkPlugin() {
  99. const caches = [];
  100. function createSplitVendorChunk(output, config) {
  101. const cache = new SplitVendorChunkCache();
  102. caches.push(cache);
  103. const build = config.build ?? {};
  104. const format = output?.format;
  105. if (!build.ssr && !build.lib && format !== "umd" && format !== "iife") {
  106. return splitVendorChunk({ cache });
  107. }
  108. }
  109. return {
  110. name: "vite:split-vendor-chunk",
  111. config(config) {
  112. let outputs = config?.build?.rollupOptions?.output;
  113. if (outputs) {
  114. outputs = arraify(outputs);
  115. for (const output of outputs) {
  116. const viteManualChunks = createSplitVendorChunk(output, config);
  117. if (viteManualChunks) {
  118. if (output.manualChunks) {
  119. if (typeof output.manualChunks === "function") {
  120. const userManualChunks = output.manualChunks;
  121. output.manualChunks = (id, api) => {
  122. return userManualChunks(id, api) ?? viteManualChunks(id, api);
  123. };
  124. } else {
  125. console.warn(
  126. "(!) the `splitVendorChunk` plugin doesn't have any effect when using the object form of `build.rollupOptions.output.manualChunks`. Consider using the function form instead."
  127. );
  128. }
  129. } else {
  130. output.manualChunks = viteManualChunks;
  131. }
  132. }
  133. }
  134. } else {
  135. return {
  136. build: {
  137. rollupOptions: {
  138. output: {
  139. manualChunks: createSplitVendorChunk({}, config)
  140. }
  141. }
  142. }
  143. };
  144. }
  145. },
  146. buildStart() {
  147. caches.forEach((cache) => cache.reset());
  148. }
  149. };
  150. }
  151. class ServerHMRBroadcasterClient {
  152. constructor(hmrChannel) {
  153. this.hmrChannel = hmrChannel;
  154. }
  155. send(...args) {
  156. let payload;
  157. if (typeof args[0] === "string") {
  158. payload = {
  159. type: "custom",
  160. event: args[0],
  161. data: args[1]
  162. };
  163. } else {
  164. payload = args[0];
  165. }
  166. if (payload.type !== "custom") {
  167. throw new Error(
  168. "Cannot send non-custom events from the client to the server."
  169. );
  170. }
  171. this.hmrChannel.send(payload);
  172. }
  173. }
  174. class ServerHMRConnector {
  175. handlers = [];
  176. hmrChannel;
  177. hmrClient;
  178. connected = false;
  179. constructor(server) {
  180. const hmrChannel = server.hot?.channels.find(
  181. (c) => c.name === "ssr"
  182. );
  183. if (!hmrChannel) {
  184. throw new Error(
  185. "Your version of Vite doesn't support HMR during SSR. Please, use Vite 5.1 or higher."
  186. );
  187. }
  188. this.hmrClient = new ServerHMRBroadcasterClient(hmrChannel);
  189. hmrChannel.api.outsideEmitter.on("send", (payload) => {
  190. this.handlers.forEach((listener) => listener(payload));
  191. });
  192. this.hmrChannel = hmrChannel;
  193. }
  194. isReady() {
  195. return this.connected;
  196. }
  197. send(message) {
  198. const payload = JSON.parse(message);
  199. this.hmrChannel.api.innerEmitter.emit(
  200. payload.event,
  201. payload.data,
  202. this.hmrClient
  203. );
  204. }
  205. onUpdate(handler) {
  206. this.handlers.push(handler);
  207. handler({ type: "connected" });
  208. this.connected = true;
  209. }
  210. }
  211. function createHMROptions(server, options) {
  212. if (server.config.server.hmr === false || options.hmr === false) {
  213. return false;
  214. }
  215. const connection = new ServerHMRConnector(server);
  216. return {
  217. connection,
  218. logger: options.hmr?.logger
  219. };
  220. }
  221. const prepareStackTrace = {
  222. retrieveFile(id) {
  223. if (existsSync(id)) {
  224. return readFileSync(id, "utf-8");
  225. }
  226. }
  227. };
  228. function resolveSourceMapOptions(options) {
  229. if (options.sourcemapInterceptor != null) {
  230. if (options.sourcemapInterceptor === "prepareStackTrace") {
  231. return prepareStackTrace;
  232. }
  233. if (typeof options.sourcemapInterceptor === "object") {
  234. return { ...prepareStackTrace, ...options.sourcemapInterceptor };
  235. }
  236. return options.sourcemapInterceptor;
  237. }
  238. if (typeof process !== "undefined" && "setSourceMapsEnabled" in process) {
  239. return "node";
  240. }
  241. return prepareStackTrace;
  242. }
  243. async function createViteRuntime(server, options = {}) {
  244. const hmr = createHMROptions(server, options);
  245. return new ViteRuntime(
  246. {
  247. ...options,
  248. root: server.config.root,
  249. fetchModule: server.ssrFetchModule,
  250. hmr,
  251. sourcemapInterceptor: resolveSourceMapOptions(options)
  252. },
  253. options.runner || new ESModulesRunner()
  254. );
  255. }
  256. export { ServerHMRConnector, createViteRuntime, isCSSRequest, splitVendorChunk, splitVendorChunkPlugin };