vite.config.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import path from "path";
  2. import vue from "@vitejs/plugin-vue";
  3. import dynamicImport from "vite-plugin-dynamic-import";
  4. import config from "config";
  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 (config.has("debug.version") && config.get("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 (config.get("debug.git.remote")) debug.git.remote = remote;
  69. if (config.get("debug.git.remoteUrl"))
  70. debug.git.remoteUrl = remoteUrl;
  71. if (config.get("debug.git.branch")) debug.git.branch = branch;
  72. if (config.get("debug.git.latestCommit"))
  73. debug.git.latestCommit = latestCommit;
  74. if (config.get("debug.git.latestCommitShort"))
  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 siteName = config.has("siteSettings.sitename")
  84. ? config.get("siteSettings.sitename")
  85. : "Musare";
  86. const htmlPlugin = () => ({
  87. name: "html-transform",
  88. transformIndexHtml(originalHtml) {
  89. let html = originalHtml;
  90. html = html.replace(/{{ title }}/g, siteName);
  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. const mode = process.env.FRONTEND_MODE || "dev";
  104. let server = null;
  105. if (mode === "dev")
  106. server = {
  107. host: "0.0.0.0",
  108. port: config.has("devServer.port") ? config.get("devServer.port") : 81,
  109. strictPort: true,
  110. hmr: {
  111. clientPort: config.has("devServer.hmrClientPort")
  112. ? config.get("devServer.hmrClientPort")
  113. : 80
  114. }
  115. };
  116. export default {
  117. mode: mode === "dev" ? "development" : "production",
  118. root: "src",
  119. publicDir: "../dist",
  120. base: "/",
  121. resolve: {
  122. alias: [
  123. {
  124. find: "@",
  125. replacement: path.resolve(__dirname, "src")
  126. }
  127. ]
  128. },
  129. define: {
  130. __VUE_PROD_DEVTOOLS__: false,
  131. MUSARE_VERSION: JSON.stringify(debug.version),
  132. MUSARE_GIT_REMOTE: JSON.stringify(debug.git.remote),
  133. MUSARE_GIT_REMOTE_URL: JSON.stringify(debug.git.remoteUrl),
  134. MUSARE_GIT_BRANCH: JSON.stringify(debug.git.branch),
  135. MUSARE_GIT_LATEST_COMMIT: JSON.stringify(debug.git.latestCommit),
  136. MUSARE_GIT_LATEST_COMMIT_SHORT: JSON.stringify(
  137. debug.git.latestCommitShort
  138. )
  139. },
  140. plugins: [vue(), htmlPlugin(), dynamicImport()],
  141. css: {
  142. preprocessorOptions: {
  143. less: {
  144. additionalData: `@import "@/styles/variables.less";`
  145. }
  146. }
  147. },
  148. server,
  149. build: {
  150. outDir: "../build"
  151. },
  152. test: {
  153. globals: true,
  154. environment: "jsdom",
  155. coverage: {
  156. all: true,
  157. extension: [".ts", ".vue"]
  158. }
  159. }
  160. };