index.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. export { parseAst, parseAstAsync } from 'rollup/parseAst';
  2. import { i as isInNodeModules, a as arraify } from './chunks/dep-DkOS1hkm.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-DkOS1hkm.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 'events';
  18. import 'assert';
  19. import 'node:child_process';
  20. import 'node:http';
  21. import 'node:https';
  22. import 'util';
  23. import 'net';
  24. import 'url';
  25. import 'http';
  26. import 'stream';
  27. import 'os';
  28. import 'child_process';
  29. import 'node:os';
  30. import 'node:crypto';
  31. import 'node:dns';
  32. import 'crypto';
  33. import 'module';
  34. import 'node:assert';
  35. import 'node:v8';
  36. import 'node:worker_threads';
  37. import 'node:buffer';
  38. import 'node:events';
  39. import 'querystring';
  40. import 'node:readline';
  41. import 'zlib';
  42. import 'buffer';
  43. import 'https';
  44. import 'tls';
  45. import 'node:zlib';
  46. // This file will be built for both ESM and CJS. Avoid relying on other modules as possible.
  47. // copy from constants.ts
  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. const isCSSRequest = (request) => CSS_LANGS_RE.test(request);
  52. // Use splitVendorChunkPlugin() to get the same manualChunks strategy as Vite 2.7
  53. // We don't recommend using this strategy as a general solution moving forward
  54. // splitVendorChunk is a simple index/vendor strategy that was used in Vite
  55. // until v2.8. It is exposed to let people continue to use it in case it was
  56. // working well for their setups.
  57. // The cache needs to be reset on buildStart for watch mode to work correctly
  58. // Don't use this manualChunks strategy for ssr, lib mode, and 'umd' or 'iife'
  59. /**
  60. * @deprecated use build.rollupOutput.manualChunks or framework specific configuration
  61. */
  62. class SplitVendorChunkCache {
  63. cache;
  64. constructor() {
  65. this.cache = new Map();
  66. }
  67. reset() {
  68. this.cache = new Map();
  69. }
  70. }
  71. /**
  72. * @deprecated use build.rollupOutput.manualChunks or framework specific configuration
  73. */
  74. function splitVendorChunk(options = {}) {
  75. const cache = options.cache ?? new SplitVendorChunkCache();
  76. return (id, { getModuleInfo }) => {
  77. if (isInNodeModules(id) &&
  78. !isCSSRequest(id) &&
  79. staticImportedByEntry(id, getModuleInfo, cache.cache)) {
  80. return 'vendor';
  81. }
  82. };
  83. }
  84. function staticImportedByEntry(id, getModuleInfo, cache, importStack = []) {
  85. if (cache.has(id)) {
  86. return cache.get(id);
  87. }
  88. if (importStack.includes(id)) {
  89. // circular deps!
  90. cache.set(id, false);
  91. return false;
  92. }
  93. const mod = getModuleInfo(id);
  94. if (!mod) {
  95. cache.set(id, false);
  96. return false;
  97. }
  98. if (mod.isEntry) {
  99. cache.set(id, true);
  100. return true;
  101. }
  102. const someImporterIs = mod.importers.some((importer) => staticImportedByEntry(importer, getModuleInfo, cache, importStack.concat(id)));
  103. cache.set(id, someImporterIs);
  104. return someImporterIs;
  105. }
  106. /**
  107. * @deprecated use build.rollupOutput.manualChunks or framework specific configuration
  108. */
  109. function splitVendorChunkPlugin() {
  110. const caches = [];
  111. function createSplitVendorChunk(output, config) {
  112. const cache = new SplitVendorChunkCache();
  113. caches.push(cache);
  114. const build = config.build ?? {};
  115. const format = output?.format;
  116. if (!build.ssr && !build.lib && format !== 'umd' && format !== 'iife') {
  117. return splitVendorChunk({ cache });
  118. }
  119. }
  120. return {
  121. name: 'vite:split-vendor-chunk',
  122. config(config) {
  123. let outputs = config?.build?.rollupOptions?.output;
  124. if (outputs) {
  125. outputs = arraify(outputs);
  126. for (const output of outputs) {
  127. const viteManualChunks = createSplitVendorChunk(output, config);
  128. if (viteManualChunks) {
  129. if (output.manualChunks) {
  130. if (typeof output.manualChunks === 'function') {
  131. const userManualChunks = output.manualChunks;
  132. output.manualChunks = (id, api) => {
  133. return userManualChunks(id, api) ?? viteManualChunks(id, api);
  134. };
  135. }
  136. else {
  137. // else, leave the object form of manualChunks untouched, as
  138. // we can't safely replicate rollup handling.
  139. // eslint-disable-next-line no-console
  140. console.warn("(!) the `splitVendorChunk` plugin doesn't have any effect when using the object form of `build.rollupOptions.output.manualChunks`. Consider using the function form instead.");
  141. }
  142. }
  143. else {
  144. output.manualChunks = viteManualChunks;
  145. }
  146. }
  147. }
  148. }
  149. else {
  150. return {
  151. build: {
  152. rollupOptions: {
  153. output: {
  154. manualChunks: createSplitVendorChunk({}, config),
  155. },
  156. },
  157. },
  158. };
  159. }
  160. },
  161. buildStart() {
  162. caches.forEach((cache) => cache.reset());
  163. },
  164. };
  165. }
  166. class ServerHMRBroadcasterClient {
  167. hmrChannel;
  168. constructor(hmrChannel) {
  169. this.hmrChannel = hmrChannel;
  170. }
  171. send(...args) {
  172. let payload;
  173. if (typeof args[0] === 'string') {
  174. payload = {
  175. type: 'custom',
  176. event: args[0],
  177. data: args[1],
  178. };
  179. }
  180. else {
  181. payload = args[0];
  182. }
  183. if (payload.type !== 'custom') {
  184. throw new Error('Cannot send non-custom events from the client to the server.');
  185. }
  186. this.hmrChannel.send(payload);
  187. }
  188. }
  189. /**
  190. * The connector class to establish HMR communication between the server and the Vite runtime.
  191. * @experimental
  192. */
  193. class ServerHMRConnector {
  194. handlers = [];
  195. hmrChannel;
  196. hmrClient;
  197. connected = false;
  198. constructor(server) {
  199. const hmrChannel = server.hot?.channels.find((c) => c.name === 'ssr');
  200. if (!hmrChannel) {
  201. throw new Error("Your version of Vite doesn't support HMR during SSR. Please, use Vite 5.1 or higher.");
  202. }
  203. this.hmrClient = new ServerHMRBroadcasterClient(hmrChannel);
  204. hmrChannel.api.outsideEmitter.on('send', (payload) => {
  205. this.handlers.forEach((listener) => listener(payload));
  206. });
  207. this.hmrChannel = hmrChannel;
  208. }
  209. isReady() {
  210. return this.connected;
  211. }
  212. send(message) {
  213. const payload = JSON.parse(message);
  214. this.hmrChannel.api.innerEmitter.emit(payload.event, payload.data, this.hmrClient);
  215. }
  216. onUpdate(handler) {
  217. this.handlers.push(handler);
  218. handler({ type: 'connected' });
  219. this.connected = true;
  220. }
  221. }
  222. function createHMROptions(server, options) {
  223. if (server.config.server.hmr === false || options.hmr === false) {
  224. return false;
  225. }
  226. const connection = new ServerHMRConnector(server);
  227. return {
  228. connection,
  229. logger: options.hmr?.logger,
  230. };
  231. }
  232. const prepareStackTrace = {
  233. retrieveFile(id) {
  234. if (existsSync(id)) {
  235. return readFileSync(id, 'utf-8');
  236. }
  237. },
  238. };
  239. function resolveSourceMapOptions(options) {
  240. if (options.sourcemapInterceptor != null) {
  241. if (options.sourcemapInterceptor === 'prepareStackTrace') {
  242. return prepareStackTrace;
  243. }
  244. if (typeof options.sourcemapInterceptor === 'object') {
  245. return { ...prepareStackTrace, ...options.sourcemapInterceptor };
  246. }
  247. return options.sourcemapInterceptor;
  248. }
  249. if (typeof process !== 'undefined' && 'setSourceMapsEnabled' in process) {
  250. return 'node';
  251. }
  252. return prepareStackTrace;
  253. }
  254. /**
  255. * Create an instance of the Vite SSR runtime that support HMR.
  256. * @experimental
  257. */
  258. async function createViteRuntime(server, options = {}) {
  259. const hmr = createHMROptions(server, options);
  260. return new ViteRuntime({
  261. ...options,
  262. root: server.config.root,
  263. fetchModule: server.ssrFetchModule,
  264. hmr,
  265. sourcemapInterceptor: resolveSourceMapOptions(options),
  266. }, options.runner || new ESModulesRunner());
  267. }
  268. export { ServerHMRConnector, createViteRuntime, isCSSRequest, splitVendorChunk, splitVendorChunkPlugin };