vite.config.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import path from "path";
  2. import vue from "@vitejs/plugin-vue";
  3. import dynamicImport from "vite-plugin-dynamic-import";
  4. import vueI18n from "@intlify/vite-plugin-vue-i18n";
  5. import config from "config";
  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 (config.has("debug.version") && config.get("debug.version"))
  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(".parent_git/HEAD")) gitFolder = ".parent_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. )[1];
  55. remoteUrl = /url = (.+)/.exec(
  56. configContents[
  57. configContents.indexOf(`[remote "${remote}"]`) + 1
  58. ]
  59. )[1];
  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 (config.get("debug.git.remote")) debug.git.remote = remote;
  70. if (config.get("debug.git.remoteUrl"))
  71. debug.git.remoteUrl = remoteUrl;
  72. if (config.get("debug.git.branch")) debug.git.branch = branch;
  73. if (config.get("debug.git.latestCommit"))
  74. debug.git.latestCommit = latestCommit;
  75. if (config.get("debug.git.latestCommitShort"))
  76. debug.git.latestCommitShort = latestCommitShort;
  77. }
  78. } catch (e) {
  79. console.log(`Could not get Git info: ${e.message}.`, e);
  80. }
  81. return debug;
  82. };
  83. const debug = fetchVersionAndGitInfo();
  84. const siteName = config.has("siteSettings.sitename")
  85. ? config.get("siteSettings.sitename")
  86. : "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. const mode = process.env.FRONTEND_MODE || "dev";
  105. let server = null;
  106. if (mode === "dev")
  107. server = {
  108. host: "0.0.0.0",
  109. port: config.has("devServer.port") ? config.get("devServer.port") : 81,
  110. strictPort: true,
  111. hmr: {
  112. clientPort: config.has("devServer.hmrClientPort")
  113. ? config.get("devServer.hmrClientPort")
  114. : 80
  115. }
  116. };
  117. export default {
  118. mode: mode === "dev" ? "development" : "production",
  119. root: "src",
  120. publicDir: "../dist",
  121. base: "/",
  122. resolve: {
  123. alias: [
  124. {
  125. find: "@musare_types",
  126. replacement: path.resolve(__dirname, "../types")
  127. },
  128. {
  129. find: "@",
  130. replacement: path.resolve(__dirname, "src")
  131. }
  132. ],
  133. extensions: [
  134. ".mjs",
  135. ".js",
  136. ".mts",
  137. ".ts",
  138. ".jsx",
  139. ".tsx",
  140. ".json",
  141. ".vue"
  142. ]
  143. },
  144. define: {
  145. __VUE_PROD_DEVTOOLS__: config.get("mode") === "development",
  146. MUSARE_VERSION: JSON.stringify(debug.version),
  147. MUSARE_GIT_REMOTE: JSON.stringify(debug.git.remote),
  148. MUSARE_GIT_REMOTE_URL: JSON.stringify(debug.git.remoteUrl),
  149. MUSARE_GIT_BRANCH: JSON.stringify(debug.git.branch),
  150. MUSARE_GIT_LATEST_COMMIT: JSON.stringify(debug.git.latestCommit),
  151. MUSARE_GIT_LATEST_COMMIT_SHORT: JSON.stringify(
  152. debug.git.latestCommitShort
  153. ),
  154. __VUE_I18N_LEGACY_API__: false
  155. },
  156. plugins: [
  157. vue(),
  158. htmlPlugin(),
  159. dynamicImport(),
  160. vueI18n({ include: path.resolve(__dirname, "src/locales/**") })
  161. ],
  162. css: {
  163. preprocessorOptions: {
  164. less: {
  165. additionalData: `@import "@/styles/variables.less";`
  166. }
  167. }
  168. },
  169. server,
  170. build: {
  171. outDir: "../build"
  172. },
  173. test: {
  174. globals: true,
  175. environment: "jsdom",
  176. coverage: {
  177. all: true,
  178. extension: [".ts", ".vue"]
  179. },
  180. setupFiles: "tests/utils/setup.ts"
  181. }
  182. };