vite.config.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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.APP_ENV === "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.APP_ENV,
  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. find: "@common",
  132. replacement: path.resolve(__dirname, "../common")
  133. }
  134. ],
  135. extensions: [
  136. ".mjs",
  137. ".js",
  138. ".mts",
  139. ".ts",
  140. ".jsx",
  141. ".tsx",
  142. ".json",
  143. ".vue"
  144. ]
  145. },
  146. define: {
  147. __VUE_PROD_DEVTOOLS__: process.env.FRONTEND_PROD_DEVTOOLS === "true",
  148. MUSARE_SITENAME: JSON.stringify(
  149. process.env.MUSARE_SITENAME ?? "Musare"
  150. ),
  151. MUSARE_PRIMARY_COLOR: JSON.stringify(
  152. process.env.MUSARE_PRIMARY_COLOR ?? "#03a9f4"
  153. ),
  154. MUSARE_VERSION: JSON.stringify(debug.version),
  155. MUSARE_GIT: debug.git,
  156. __VUE_I18N_LEGACY_API__: false
  157. },
  158. plugins: [
  159. vue(),
  160. htmlPlugin(),
  161. dynamicImport(),
  162. VueI18nPlugin({ include: path.resolve(__dirname, "src/locales/**") })
  163. ],
  164. css: {
  165. preprocessorOptions: {
  166. less: {
  167. additionalData: `@import "@/styles/variables.less";`
  168. }
  169. }
  170. },
  171. server,
  172. build: {
  173. outDir: "../build"
  174. },
  175. test: {
  176. globals: true,
  177. environment: "jsdom",
  178. coverage: {
  179. all: true,
  180. extension: [".ts", ".vue"]
  181. },
  182. setupFiles: "tests/utils/setup.ts"
  183. }
  184. };