vite.config.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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)
  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(".parent_git/HEAD")) gitFolder = ".parent_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. )[1];
  54. remoteUrl = /url = (.+)/.exec(
  55. configContents[
  56. configContents.indexOf(`[remote "${remote}"]`) + 1
  57. ]
  58. )[1];
  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) debug.git.remote = remote;
  69. if (process.env.MUSARE_DEBUG_GIT_REMOTE_URL)
  70. debug.git.remoteUrl = remoteUrl;
  71. if (process.env.MUSARE_DEBUG_GIT_BRANCH) debug.git.branch = branch;
  72. if (process.env.MUSARE_DEBUG_GIT_LATEST_COMMIT)
  73. debug.git.latestCommit = latestCommit;
  74. if (process.env.MUSARE_DEBUG_GIT_LATEST_COMMIT_SHORT)
  75. debug.git.latestCommitShort = latestCommitShort;
  76. }
  77. } catch (e) {
  78. console.log(`Could not get Git info: ${e.message}.`, e);
  79. }
  80. return debug;
  81. };
  82. const debug = fetchVersionAndGitInfo();
  83. const htmlPlugin = () => ({
  84. name: "html-transform",
  85. transformIndexHtml(originalHtml) {
  86. let html = originalHtml;
  87. html = html.replace(
  88. /{{ title }}/g,
  89. process.env.MUSARE_SITENAME ?? "Musare"
  90. );
  91. html = html.replace(/{{ version }}/g, debug.version);
  92. html = html.replace(/{{ gitRemote }}/g, debug.git.remote);
  93. html = html.replace(/{{ gitRemoteUrl }}/g, debug.git.remoteUrl);
  94. html = html.replace(/{{ gitBranch }}/g, debug.git.branch);
  95. html = html.replace(/{{ gitLatestCommit }}/g, debug.git.latestCommit);
  96. html = html.replace(
  97. /{{ gitLatestCommitShort }}/g,
  98. debug.git.latestCommitShort
  99. );
  100. return html;
  101. }
  102. });
  103. let server = null;
  104. if (process.env.FRONTEND_MODE === "development")
  105. server = {
  106. host: "0.0.0.0",
  107. port: process.env.FRONTEND_DEV_PORT ?? 81,
  108. strictPort: true,
  109. hmr: {
  110. clientPort: process.env.FRONTEND_CLIENT_PORT ?? 80
  111. }
  112. };
  113. export default {
  114. mode: process.env.FRONTEND_MODE,
  115. root: "src",
  116. publicDir: "../dist",
  117. base: "/",
  118. resolve: {
  119. alias: [
  120. {
  121. find: "@musare_types",
  122. replacement: path.resolve(__dirname, "../types")
  123. },
  124. {
  125. find: "@",
  126. replacement: path.resolve(__dirname, "src")
  127. }
  128. ],
  129. extensions: [
  130. ".mjs",
  131. ".js",
  132. ".mts",
  133. ".ts",
  134. ".jsx",
  135. ".tsx",
  136. ".json",
  137. ".vue"
  138. ]
  139. },
  140. define: {
  141. __VUE_PROD_DEVTOOLS__: !!process.env.FRONTEND_PROD_DEVTOOLS,
  142. MUSARE_VERSION: JSON.stringify(debug.version),
  143. MUSARE_GIT_REMOTE: JSON.stringify(debug.git.remote),
  144. MUSARE_GIT_REMOTE_URL: JSON.stringify(debug.git.remoteUrl),
  145. MUSARE_GIT_BRANCH: JSON.stringify(debug.git.branch),
  146. MUSARE_GIT_LATEST_COMMIT: JSON.stringify(debug.git.latestCommit),
  147. MUSARE_GIT_LATEST_COMMIT_SHORT: JSON.stringify(
  148. debug.git.latestCommitShort
  149. ),
  150. __VUE_I18N_LEGACY_API__: false
  151. },
  152. plugins: [
  153. vue(),
  154. htmlPlugin(),
  155. dynamicImport(),
  156. VueI18nPlugin({ include: path.resolve(__dirname, "src/locales/**") })
  157. ],
  158. css: {
  159. preprocessorOptions: {
  160. less: {
  161. additionalData: `@import "@/styles/variables.less";`
  162. }
  163. }
  164. },
  165. server,
  166. build: {
  167. outDir: "../build"
  168. },
  169. test: {
  170. globals: true,
  171. environment: "jsdom",
  172. coverage: {
  173. all: true,
  174. extension: [".ts", ".vue"]
  175. },
  176. setupFiles: "tests/utils/setup.ts"
  177. }
  178. };