vite.config.js 4.6 KB

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