vite.config.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. },
  134. define: {
  135. __VUE_PROD_DEVTOOLS__: config.get("mode") === "development",
  136. MUSARE_VERSION: JSON.stringify(debug.version),
  137. MUSARE_GIT_REMOTE: JSON.stringify(debug.git.remote),
  138. MUSARE_GIT_REMOTE_URL: JSON.stringify(debug.git.remoteUrl),
  139. MUSARE_GIT_BRANCH: JSON.stringify(debug.git.branch),
  140. MUSARE_GIT_LATEST_COMMIT: JSON.stringify(debug.git.latestCommit),
  141. MUSARE_GIT_LATEST_COMMIT_SHORT: JSON.stringify(
  142. debug.git.latestCommitShort
  143. ),
  144. __VUE_I18N_LEGACY_API__: false
  145. },
  146. plugins: [
  147. vue(),
  148. htmlPlugin(),
  149. dynamicImport(),
  150. vueI18n({ include: path.resolve(__dirname, "src/locales/**") })
  151. ],
  152. css: {
  153. preprocessorOptions: {
  154. less: {
  155. additionalData: `@import "@/styles/variables.less";`
  156. }
  157. }
  158. },
  159. server,
  160. build: {
  161. outDir: "../build"
  162. },
  163. test: {
  164. globals: true,
  165. environment: "jsdom",
  166. coverage: {
  167. all: true,
  168. extension: [".ts", ".vue"]
  169. },
  170. setupFiles: "tests/utils/setup.ts"
  171. }
  172. };