vite.config.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 fs from "fs";
  6. const fetchVersionAndGitInfo = () => {
  7. const debug = {
  8. git: {
  9. remote: "",
  10. remoteUrl: "",
  11. branch: "",
  12. latestCommit: "",
  13. latestCommitShort: ""
  14. },
  15. version: ""
  16. };
  17. try {
  18. const packageJson = JSON.parse(
  19. fs.readFileSync("./package.json").toString()
  20. );
  21. console.log(`Musare version: ${packageJson.version}.`);
  22. if (process.env.MUSARE_DEBUG_VERSION === "true")
  23. debug.version = packageJson.version;
  24. } catch (e) {
  25. console.log(`Could not get package info: ${e.message}.`);
  26. }
  27. try {
  28. let gitFolder = null;
  29. if (fs.existsSync("../.git/HEAD")) gitFolder = "../.git";
  30. else if (fs.existsSync(".git/HEAD")) gitFolder = ".git";
  31. if (gitFolder) {
  32. const headContents = fs
  33. .readFileSync(`${gitFolder}/HEAD`)
  34. .toString()
  35. .replace(/\n/g, "");
  36. const branch = /ref: refs\/heads\/([.A-Za-z0-9_-]+)/.exec(
  37. headContents
  38. )[1];
  39. const configContents = fs
  40. .readFileSync(`${gitFolder}/config`)
  41. .toString()
  42. .replace(/\t/g, "")
  43. .split("\n");
  44. let remote;
  45. let remoteUrl;
  46. let latestCommit;
  47. let latestCommitShort;
  48. if (configContents.indexOf(`[branch "${branch}"]`) >= 0) {
  49. [, remote] = /remote = (.+)/.exec(
  50. configContents[
  51. configContents.indexOf(`[branch "${branch}"]`) + 1
  52. ]
  53. );
  54. [, remoteUrl] = /url = (.+)/.exec(
  55. configContents[
  56. configContents.indexOf(`[remote "${remote}"]`) + 1
  57. ]
  58. );
  59. latestCommit = fs
  60. .readFileSync(`${gitFolder}/refs/heads/${branch}`)
  61. .toString()
  62. .replace(/\n/g, "");
  63. latestCommitShort = latestCommit.substr(0, 7);
  64. }
  65. console.log(
  66. `Git branch: ${remote}/${branch}. Remote url: ${remoteUrl}. Latest commit: ${latestCommit} (${latestCommitShort}).`
  67. );
  68. if (process.env.MUSARE_DEBUG_GIT_REMOTE === "true")
  69. debug.git.remote = remote;
  70. if (process.env.MUSARE_DEBUG_GIT_REMOTE_URL === "true")
  71. debug.git.remoteUrl = remoteUrl;
  72. if (process.env.MUSARE_DEBUG_GIT_BRANCH === "true")
  73. debug.git.branch = branch;
  74. if (process.env.MUSARE_DEBUG_GIT_LATEST_COMMIT === "true")
  75. debug.git.latestCommit = latestCommit;
  76. if (process.env.MUSARE_DEBUG_GIT_LATEST_COMMIT_SHORT === "true")
  77. debug.git.latestCommitShort = latestCommitShort;
  78. } else console.log("Could not find .git folder.");
  79. } catch (e) {
  80. console.log(`Could not get Git info: ${e.message}.`, e);
  81. }
  82. return debug;
  83. };
  84. const debug = fetchVersionAndGitInfo();
  85. const htmlPlugin = () => ({
  86. name: "html-transform",
  87. transformIndexHtml(originalHtml) {
  88. let html = originalHtml;
  89. html = html.replace(
  90. /{{ title }}/g,
  91. process.env.MUSARE_SITENAME ?? "Musare"
  92. );
  93. html = html.replace(/{{ version }}/g, debug.version);
  94. html = html.replace(/{{ gitRemote }}/g, debug.git.remote);
  95. html = html.replace(/{{ gitRemoteUrl }}/g, debug.git.remoteUrl);
  96. html = html.replace(/{{ gitBranch }}/g, debug.git.branch);
  97. html = html.replace(/{{ gitLatestCommit }}/g, debug.git.latestCommit);
  98. html = html.replace(
  99. /{{ gitLatestCommitShort }}/g,
  100. debug.git.latestCommitShort
  101. );
  102. return html;
  103. }
  104. });
  105. let server = null;
  106. if (process.env.FRONTEND_MODE === "development")
  107. server = {
  108. host: "0.0.0.0",
  109. port: process.env.FRONTEND_DEV_PORT ?? 81,
  110. strictPort: true,
  111. hmr: {
  112. clientPort: process.env.FRONTEND_CLIENT_PORT ?? 80
  113. }
  114. };
  115. export default {
  116. mode: process.env.FRONTEND_MODE,
  117. root: "src",
  118. publicDir: "../dist",
  119. base: "/",
  120. resolve: {
  121. alias: [
  122. {
  123. find: "@musare_types",
  124. replacement: path.resolve(__dirname, "../types")
  125. },
  126. {
  127. find: "@",
  128. replacement: path.resolve(__dirname, "src")
  129. }
  130. ],
  131. extensions: [
  132. ".mjs",
  133. ".js",
  134. ".mts",
  135. ".ts",
  136. ".jsx",
  137. ".tsx",
  138. ".json",
  139. ".vue"
  140. ]
  141. },
  142. define: {
  143. __VUE_PROD_DEVTOOLS__: process.env.FRONTEND_PROD_DEVTOOLS === "true",
  144. MUSARE_SITENAME: JSON.stringify(
  145. process.env.MUSARE_SITENAME ?? "Musare"
  146. ),
  147. MUSARE_VERSION: JSON.stringify(debug.version),
  148. MUSARE_GIT_REMOTE: JSON.stringify(debug.git.remote),
  149. MUSARE_GIT_REMOTE_URL: JSON.stringify(debug.git.remoteUrl),
  150. MUSARE_GIT_BRANCH: JSON.stringify(debug.git.branch),
  151. MUSARE_GIT_LATEST_COMMIT: JSON.stringify(debug.git.latestCommit),
  152. MUSARE_GIT_LATEST_COMMIT_SHORT: JSON.stringify(
  153. debug.git.latestCommitShort
  154. ),
  155. __VUE_I18N_LEGACY_API__: false
  156. },
  157. plugins: [
  158. vue(),
  159. htmlPlugin(),
  160. dynamicImport(),
  161. VueI18nPlugin({ include: path.resolve(__dirname, "src/locales/**") })
  162. ],
  163. css: {
  164. preprocessorOptions: {
  165. less: {
  166. additionalData: `@import "@/styles/variables.less";`
  167. }
  168. }
  169. },
  170. server,
  171. build: {
  172. outDir: "../build"
  173. },
  174. test: {
  175. globals: true,
  176. environment: "jsdom",
  177. coverage: {
  178. all: true,
  179. extension: [".ts", ".vue"]
  180. },
  181. setupFiles: "tests/utils/setup.ts"
  182. }
  183. };