constants.js 4.0 KB

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