constants.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import path, { resolve } from 'node:path';
  2. import { fileURLToPath } from 'node:url';
  3. var version = "3.2.8";
  4. const VERSION = version;
  5. const DEFAULT_MAIN_FIELDS = [
  6. 'module',
  7. 'jsnext:main',
  8. 'jsnext'
  9. ];
  10. // Baseline support browserslist
  11. // "defaults and supports es6-module and supports es6-module-dynamic-import"
  12. // Higher browser versions may be needed for extra features.
  13. const ESBUILD_MODULES_TARGET = [
  14. 'es2020',
  15. 'edge88',
  16. 'firefox78',
  17. 'chrome87',
  18. 'safari13' // transpile nullish coalescing
  19. ];
  20. const DEFAULT_EXTENSIONS = [
  21. '.mjs',
  22. '.js',
  23. '.mts',
  24. '.ts',
  25. '.jsx',
  26. '.tsx',
  27. '.json'
  28. ];
  29. const DEFAULT_CONFIG_FILES = [
  30. 'vite.config.js',
  31. 'vite.config.mjs',
  32. 'vite.config.ts',
  33. 'vite.config.cjs',
  34. 'vite.config.mts',
  35. 'vite.config.cts'
  36. ];
  37. const JS_TYPES_RE = /\.(?:j|t)sx?$|\.mjs$/;
  38. const OPTIMIZABLE_ENTRY_RE = /\.(?:[cm]?[jt]s)$/;
  39. const SPECIAL_QUERY_RE = /[\?&](?:worker|sharedworker|raw|url)\b/;
  40. /**
  41. * Prefix for resolved fs paths, since windows paths may not be valid as URLs.
  42. */
  43. const FS_PREFIX = `/@fs/`;
  44. /**
  45. * Prefix for resolved Ids that are not valid browser import specifiers
  46. */
  47. const VALID_ID_PREFIX = `/@id/`;
  48. /**
  49. * Plugins that use 'virtual modules' (e.g. for helper functions), prefix the
  50. * module ID with `\0`, a convention from the rollup ecosystem.
  51. * This prevents other plugins from trying to process the id (like node resolution),
  52. * and core features like sourcemaps can use this info to differentiate between
  53. * virtual modules and regular files.
  54. * `\0` is not a permitted char in import URLs so we have to replace them during
  55. * import analysis. The id will be decoded back before entering the plugins pipeline.
  56. * These encoded virtual ids are also prefixed by the VALID_ID_PREFIX, so virtual
  57. * modules in the browser end up encoded as `/@id/__x00__{id}`
  58. */
  59. const NULL_BYTE_PLACEHOLDER = `__x00__`;
  60. const CLIENT_PUBLIC_PATH = `/@vite/client`;
  61. const ENV_PUBLIC_PATH = `/@vite/env`;
  62. const VITE_PACKAGE_DIR = resolve(
  63. // import.meta.url is `dist/node/constants.js` after bundle
  64. fileURLToPath(import.meta.url), '../../..');
  65. const CLIENT_ENTRY = resolve(VITE_PACKAGE_DIR, 'dist/client/client.mjs');
  66. const ENV_ENTRY = resolve(VITE_PACKAGE_DIR, 'dist/client/env.mjs');
  67. const CLIENT_DIR = path.dirname(CLIENT_ENTRY);
  68. // ** READ THIS ** before editing `KNOWN_ASSET_TYPES`.
  69. // If you add an asset to `KNOWN_ASSET_TYPES`, make sure to also add it
  70. // to the TypeScript declaration file `packages/vite/client.d.ts` and
  71. // add a mime type to the `registerCustomMime` in
  72. // `packages/vite/src/node/plugin/assets.ts` if mime type cannot be
  73. // looked up by mrmime.
  74. const KNOWN_ASSET_TYPES = [
  75. // images
  76. 'png',
  77. 'jpe?g',
  78. 'jfif',
  79. 'pjpeg',
  80. 'pjp',
  81. 'gif',
  82. 'svg',
  83. 'ico',
  84. 'webp',
  85. 'avif',
  86. // media
  87. 'mp4',
  88. 'webm',
  89. 'ogg',
  90. 'mp3',
  91. 'wav',
  92. 'flac',
  93. 'aac',
  94. // fonts
  95. 'woff2?',
  96. 'eot',
  97. 'ttf',
  98. 'otf',
  99. // other
  100. 'webmanifest',
  101. 'pdf',
  102. 'txt'
  103. ];
  104. const DEFAULT_ASSETS_RE = new RegExp(`\\.(` + KNOWN_ASSET_TYPES.join('|') + `)(\\?.*)?$`);
  105. const DEP_VERSION_RE = /[\?&](v=[\w\.-]+)\b/;
  106. const loopbackHosts = new Set([
  107. 'localhost',
  108. '127.0.0.1',
  109. '::1',
  110. '0000:0000:0000:0000:0000:0000:0000:0001'
  111. ]);
  112. const wildcardHosts = new Set([
  113. '0.0.0.0',
  114. '::',
  115. '0000:0000:0000:0000:0000:0000:0000:0000'
  116. ]);
  117. export { CLIENT_DIR, CLIENT_ENTRY, CLIENT_PUBLIC_PATH, DEFAULT_ASSETS_RE, DEFAULT_CONFIG_FILES, DEFAULT_EXTENSIONS, DEFAULT_MAIN_FIELDS, DEP_VERSION_RE, ENV_ENTRY, ENV_PUBLIC_PATH, ESBUILD_MODULES_TARGET, FS_PREFIX, JS_TYPES_RE, KNOWN_ASSET_TYPES, NULL_BYTE_PLACEHOLDER, OPTIMIZABLE_ENTRY_RE, SPECIAL_QUERY_RE, VALID_ID_PREFIX, VERSION, VITE_PACKAGE_DIR, loopbackHosts, wildcardHosts };