constants.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import path, { resolve } from 'node:path';
  2. import { fileURLToPath } from 'node:url';
  3. import { readFileSync } from 'node:fs';
  4. const { version } = JSON.parse(
  5. readFileSync(new URL("../../package.json", import.meta.url)).toString()
  6. );
  7. const VERSION = version;
  8. const DEFAULT_MAIN_FIELDS = [
  9. "browser",
  10. "module",
  11. "jsnext:main",
  12. // moment still uses this...
  13. "jsnext"
  14. ];
  15. const ESBUILD_MODULES_TARGET = [
  16. "es2020",
  17. // support import.meta.url
  18. "edge88",
  19. "firefox78",
  20. "chrome87",
  21. "safari14"
  22. ];
  23. const DEFAULT_EXTENSIONS = [
  24. ".mjs",
  25. ".js",
  26. ".mts",
  27. ".ts",
  28. ".jsx",
  29. ".tsx",
  30. ".json"
  31. ];
  32. const DEFAULT_CONFIG_FILES = [
  33. "vite.config.js",
  34. "vite.config.mjs",
  35. "vite.config.ts",
  36. "vite.config.cjs",
  37. "vite.config.mts",
  38. "vite.config.cts"
  39. ];
  40. const JS_TYPES_RE = /\.(?:j|t)sx?$|\.mjs$/;
  41. const CSS_LANGS_RE = /\.(css|less|sass|scss|styl|stylus|pcss|postcss|sss)(?:$|\?)/;
  42. const OPTIMIZABLE_ENTRY_RE = /\.[cm]?[jt]s$/;
  43. const SPECIAL_QUERY_RE = /[?&](?:worker|sharedworker|raw|url)\b/;
  44. const FS_PREFIX = `/@fs/`;
  45. const CLIENT_PUBLIC_PATH = `/@vite/client`;
  46. const ENV_PUBLIC_PATH = `/@vite/env`;
  47. const VITE_PACKAGE_DIR = resolve(
  48. // import.meta.url is `dist/node/constants.js` after bundle
  49. fileURLToPath(import.meta.url),
  50. "../../.."
  51. );
  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. const KNOWN_ASSET_TYPES = [
  56. // images
  57. "apng",
  58. "bmp",
  59. "png",
  60. "jpe?g",
  61. "jfif",
  62. "pjpeg",
  63. "pjp",
  64. "gif",
  65. "svg",
  66. "ico",
  67. "webp",
  68. "avif",
  69. // media
  70. "mp4",
  71. "webm",
  72. "ogg",
  73. "mp3",
  74. "wav",
  75. "flac",
  76. "aac",
  77. "opus",
  78. "mov",
  79. "m4a",
  80. "vtt",
  81. // fonts
  82. "woff2?",
  83. "eot",
  84. "ttf",
  85. "otf",
  86. // other
  87. "webmanifest",
  88. "pdf",
  89. "txt"
  90. ];
  91. const DEFAULT_ASSETS_RE = new RegExp(
  92. `\\.(` + KNOWN_ASSET_TYPES.join("|") + `)(\\?.*)?$`
  93. );
  94. const DEP_VERSION_RE = /[?&](v=[\w.-]+)\b/;
  95. const loopbackHosts = /* @__PURE__ */ new Set([
  96. "localhost",
  97. "127.0.0.1",
  98. "::1",
  99. "0000:0000:0000:0000:0000:0000:0000:0001"
  100. ]);
  101. const wildcardHosts = /* @__PURE__ */ new Set([
  102. "0.0.0.0",
  103. "::",
  104. "0000:0000:0000:0000:0000:0000:0000:0000"
  105. ]);
  106. const DEFAULT_DEV_PORT = 5173;
  107. const DEFAULT_PREVIEW_PORT = 4173;
  108. const DEFAULT_ASSETS_INLINE_LIMIT = 4096;
  109. const METADATA_FILENAME = "_metadata.json";
  110. 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 };