constants.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. 'browser',
  8. 'module',
  9. 'jsnext:main',
  10. 'jsnext',
  11. ];
  12. // Baseline support browserslist
  13. // "defaults and supports es6-module and supports es6-module-dynamic-import"
  14. // Higher browser versions may be needed for extra features.
  15. const ESBUILD_MODULES_TARGET = [
  16. 'es2020',
  17. 'edge88',
  18. 'firefox78',
  19. 'chrome87',
  20. 'safari14',
  21. ];
  22. const DEFAULT_EXTENSIONS = [
  23. '.mjs',
  24. '.js',
  25. '.mts',
  26. '.ts',
  27. '.jsx',
  28. '.tsx',
  29. '.json',
  30. ];
  31. const DEFAULT_CONFIG_FILES = [
  32. 'vite.config.js',
  33. 'vite.config.mjs',
  34. 'vite.config.ts',
  35. 'vite.config.cjs',
  36. 'vite.config.mts',
  37. 'vite.config.cts',
  38. ];
  39. const JS_TYPES_RE = /\.(?:j|t)sx?$|\.mjs$/;
  40. const CSS_LANGS_RE = /\.(css|less|sass|scss|styl|stylus|pcss|postcss|sss)(?:$|\?)/;
  41. const OPTIMIZABLE_ENTRY_RE = /\.[cm]?[jt]s$/;
  42. const SPECIAL_QUERY_RE = /[?&](?:worker|sharedworker|raw|url)\b/;
  43. /**
  44. * Prefix for resolved fs paths, since windows paths may not be valid as URLs.
  45. */
  46. const FS_PREFIX = `/@fs/`;
  47. const CLIENT_PUBLIC_PATH = `/@vite/client`;
  48. const ENV_PUBLIC_PATH = `/@vite/env`;
  49. const VITE_PACKAGE_DIR = resolve(
  50. // import.meta.url is `dist/node/constants.js` after bundle
  51. fileURLToPath(import.meta.url), '../../..');
  52. const CLIENT_ENTRY = resolve(VITE_PACKAGE_DIR, 'dist/client/client.mjs');
  53. const ENV_ENTRY = resolve(VITE_PACKAGE_DIR, 'dist/client/env.mjs');
  54. const CLIENT_DIR = path.dirname(CLIENT_ENTRY);
  55. // ** READ THIS ** before editing `KNOWN_ASSET_TYPES`.
  56. // If you add an asset to `KNOWN_ASSET_TYPES`, make sure to also add it
  57. // to the TypeScript declaration file `packages/vite/client.d.ts` and
  58. // add a mime type to the `registerCustomMime` in
  59. // `packages/vite/src/node/plugin/assets.ts` if mime type cannot be
  60. // looked up by mrmime.
  61. const KNOWN_ASSET_TYPES = [
  62. // images
  63. 'apng',
  64. 'png',
  65. 'jpe?g',
  66. 'jfif',
  67. 'pjpeg',
  68. 'pjp',
  69. 'gif',
  70. 'svg',
  71. 'ico',
  72. 'webp',
  73. 'avif',
  74. // media
  75. 'mp4',
  76. 'webm',
  77. 'ogg',
  78. 'mp3',
  79. 'wav',
  80. 'flac',
  81. 'aac',
  82. 'opus',
  83. 'mov',
  84. 'm4a',
  85. 'vtt',
  86. // fonts
  87. 'woff2?',
  88. 'eot',
  89. 'ttf',
  90. 'otf',
  91. // other
  92. 'webmanifest',
  93. 'pdf',
  94. 'txt',
  95. ];
  96. const DEFAULT_ASSETS_RE = new RegExp(`\\.(` + KNOWN_ASSET_TYPES.join('|') + `)(\\?.*)?$`);
  97. const DEP_VERSION_RE = /[?&](v=[\w.-]+)\b/;
  98. const loopbackHosts = new Set([
  99. 'localhost',
  100. '127.0.0.1',
  101. '::1',
  102. '0000:0000:0000:0000:0000:0000:0000:0001',
  103. ]);
  104. const wildcardHosts = new Set([
  105. '0.0.0.0',
  106. '::',
  107. '0000:0000:0000:0000:0000:0000:0000:0000',
  108. ]);
  109. const DEFAULT_DEV_PORT = 5173;
  110. const DEFAULT_PREVIEW_PORT = 4173;
  111. const DEFAULT_ASSETS_INLINE_LIMIT = 4096;
  112. const METADATA_FILENAME = '_metadata.json';
  113. export { CLIENT_DIR, CLIENT_ENTRY, CLIENT_PUBLIC_PATH, CSS_LANGS_RE, DEFAULT_ASSETS_INLINE_LIMIT, 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, METADATA_FILENAME, OPTIMIZABLE_ENTRY_RE, SPECIAL_QUERY_RE, VERSION, VITE_PACKAGE_DIR, loopbackHosts, wildcardHosts };