vite.config.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import path from "path";
  2. import vue from "@vitejs/plugin-vue";
  3. import dynamicImport from "vite-plugin-dynamic-import";
  4. import VueI18nPlugin from "@intlify/unplugin-vue-i18n/vite";
  5. import { VitePWA } from "vite-plugin-pwa";
  6. import fs from "fs";
  7. const fetchVersionAndGitInfo = () => {
  8. const debug = {
  9. git: {
  10. remote: "",
  11. remoteUrl: "",
  12. branch: "",
  13. latestCommit: "",
  14. latestCommitShort: ""
  15. },
  16. version: ""
  17. };
  18. try {
  19. const packageJson = JSON.parse(
  20. fs.readFileSync("./package.json").toString()
  21. );
  22. console.log(`Musare version: ${packageJson.version}.`);
  23. if (process.env.MUSARE_DEBUG_VERSION === "true")
  24. debug.version = packageJson.version;
  25. } catch (e) {
  26. console.log(`Could not get package info: ${e.message}.`);
  27. }
  28. try {
  29. let gitFolder = null;
  30. if (fs.existsSync("../.git/HEAD")) gitFolder = "../.git";
  31. else if (fs.existsSync(".git/HEAD")) gitFolder = ".git";
  32. if (gitFolder) {
  33. const headContents = fs
  34. .readFileSync(`${gitFolder}/HEAD`)
  35. .toString()
  36. .replace(/\n/g, "");
  37. const branch = /ref: refs\/heads\/([.A-Za-z0-9_-]+)/.exec(
  38. headContents
  39. )[1];
  40. const configContents = fs
  41. .readFileSync(`${gitFolder}/config`)
  42. .toString()
  43. .replace(/\t/g, "")
  44. .split("\n");
  45. let remote;
  46. let remoteUrl;
  47. let latestCommit;
  48. let latestCommitShort;
  49. if (configContents.indexOf(`[branch "${branch}"]`) >= 0) {
  50. [, remote] = /remote = (.+)/.exec(
  51. configContents[
  52. configContents.indexOf(`[branch "${branch}"]`) + 1
  53. ]
  54. );
  55. [, remoteUrl] = /url = (.+)/.exec(
  56. configContents[
  57. configContents.indexOf(`[remote "${remote}"]`) + 1
  58. ]
  59. );
  60. latestCommit = fs
  61. .readFileSync(`${gitFolder}/refs/heads/${branch}`)
  62. .toString()
  63. .replace(/\n/g, "");
  64. latestCommitShort = latestCommit.substr(0, 7);
  65. }
  66. console.log(
  67. `Git branch: ${remote}/${branch}. Remote url: ${remoteUrl}. Latest commit: ${latestCommit} (${latestCommitShort}).`
  68. );
  69. if (process.env.MUSARE_DEBUG_GIT_REMOTE === "true")
  70. debug.git.remote = remote;
  71. if (process.env.MUSARE_DEBUG_GIT_REMOTE_URL === "true")
  72. debug.git.remoteUrl = remoteUrl;
  73. if (process.env.MUSARE_DEBUG_GIT_BRANCH === "true")
  74. debug.git.branch = branch;
  75. if (process.env.MUSARE_DEBUG_GIT_LATEST_COMMIT === "true")
  76. debug.git.latestCommit = latestCommit;
  77. if (process.env.MUSARE_DEBUG_GIT_LATEST_COMMIT_SHORT === "true")
  78. debug.git.latestCommitShort = latestCommitShort;
  79. } else console.log("Could not find .git folder.");
  80. } catch (e) {
  81. console.log(`Could not get Git info: ${e.message}.`, e);
  82. }
  83. return debug;
  84. };
  85. const debug = fetchVersionAndGitInfo();
  86. const sitename = process.env.MUSARE_SITENAME ?? "Musare";
  87. const htmlPlugin = () => ({
  88. name: "html-transform",
  89. transformIndexHtml(originalHtml) {
  90. let html = originalHtml;
  91. html = html.replace(/{{ title }}/g, sitename);
  92. html = html.replace(/{{ version }}/g, debug.version);
  93. html = html.replace(/{{ gitRemote }}/g, debug.git.remote);
  94. html = html.replace(/{{ gitRemoteUrl }}/g, debug.git.remoteUrl);
  95. html = html.replace(/{{ gitBranch }}/g, debug.git.branch);
  96. html = html.replace(/{{ gitLatestCommit }}/g, debug.git.latestCommit);
  97. html = html.replace(
  98. /{{ gitLatestCommitShort }}/g,
  99. debug.git.latestCommitShort
  100. );
  101. return html;
  102. }
  103. });
  104. let server = null;
  105. if (process.env.FRONTEND_MODE === "development")
  106. server = {
  107. host: "0.0.0.0",
  108. port: process.env.FRONTEND_DEV_PORT ?? 81,
  109. strictPort: true,
  110. hmr: {
  111. clientPort: process.env.FRONTEND_CLIENT_PORT ?? 80
  112. }
  113. };
  114. export default {
  115. mode: process.env.FRONTEND_MODE,
  116. root: "src",
  117. publicDir: "../dist",
  118. base: "/",
  119. resolve: {
  120. alias: [
  121. {
  122. find: "@musare_types",
  123. replacement: path.resolve(__dirname, "../types")
  124. },
  125. {
  126. find: "@",
  127. replacement: path.resolve(__dirname, "src")
  128. }
  129. ],
  130. extensions: [
  131. ".mjs",
  132. ".js",
  133. ".mts",
  134. ".ts",
  135. ".jsx",
  136. ".tsx",
  137. ".json",
  138. ".vue"
  139. ]
  140. },
  141. define: {
  142. __VUE_PROD_DEVTOOLS__: process.env.FRONTEND_PROD_DEVTOOLS === "true",
  143. MUSARE_SITENAME: JSON.stringify(sitename),
  144. MUSARE_VERSION: JSON.stringify(debug.version),
  145. MUSARE_GIT_REMOTE: JSON.stringify(debug.git.remote),
  146. MUSARE_GIT_REMOTE_URL: JSON.stringify(debug.git.remoteUrl),
  147. MUSARE_GIT_BRANCH: JSON.stringify(debug.git.branch),
  148. MUSARE_GIT_LATEST_COMMIT: JSON.stringify(debug.git.latestCommit),
  149. MUSARE_GIT_LATEST_COMMIT_SHORT: JSON.stringify(
  150. debug.git.latestCommitShort
  151. ),
  152. __VUE_I18N_LEGACY_API__: false
  153. },
  154. plugins: [
  155. vue(),
  156. htmlPlugin(),
  157. dynamicImport(),
  158. VueI18nPlugin({ include: path.resolve(__dirname, "src/locales/**") }),
  159. VitePWA({
  160. useCredentials: true,
  161. devOptions: {
  162. enabled: true,
  163. type: "module",
  164. navigateFallback: "index.html",
  165. resolveTempFolder: () => path.resolve(__dirname, "dev-dist")
  166. },
  167. manifest: {
  168. name: sitename,
  169. short_name: sitename,
  170. theme_color: "#03a9f4",
  171. display: "standalone",
  172. start_url: "/",
  173. id: "/",
  174. icons: [
  175. {
  176. src: "/assets/favicon/android-chrome-36x36.png?v=06042016",
  177. sizes: "36x36",
  178. type: "image/png",
  179. density: 0.75
  180. },
  181. {
  182. src: "/assets/favicon/android-chrome-48x48.png?v=06042016",
  183. sizes: "48x48",
  184. type: "image/png",
  185. density: 1
  186. },
  187. {
  188. src: "/assets/favicon/android-chrome-72x72.png?v=06042016",
  189. sizes: "72x72",
  190. type: "image/png",
  191. density: 1.5
  192. },
  193. {
  194. src: "/assets/favicon/android-chrome-96x96.png?v=06042016",
  195. sizes: "96x96",
  196. type: "image/png",
  197. density: 2
  198. },
  199. {
  200. src: "/assets/favicon/android-chrome-144x144.png?v=06042016",
  201. sizes: "144x144",
  202. type: "image/png",
  203. density: 3
  204. },
  205. {
  206. src: "/assets/favicon/android-chrome-192x192.png?v=06042016",
  207. sizes: "192x192",
  208. type: "image/png",
  209. density: 4
  210. }
  211. ]
  212. }
  213. })
  214. ],
  215. css: {
  216. preprocessorOptions: {
  217. less: {
  218. additionalData: `@import "@/styles/variables.less";`
  219. }
  220. }
  221. },
  222. server,
  223. build: {
  224. outDir: "../build"
  225. },
  226. test: {
  227. globals: true,
  228. environment: "jsdom",
  229. coverage: {
  230. all: true,
  231. extension: [".ts", ".vue"]
  232. },
  233. setupFiles: "tests/utils/setup.ts"
  234. }
  235. };