vite.config.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import path from "path";
  2. import vue from "@vitejs/plugin-vue";
  3. import config from "config";
  4. import fs from "fs";
  5. const fetchVersionAndGitInfo = () => {
  6. const debug = {
  7. git: {
  8. remote: "",
  9. remoteUrl: "",
  10. branch: "",
  11. latestCommit: "",
  12. latestCommitShort: ""
  13. },
  14. version: ""
  15. };
  16. try {
  17. const packageJson = JSON.parse(
  18. fs.readFileSync("./package.json").toString()
  19. );
  20. console.log(`Musare version: ${packageJson.version}.`);
  21. if (config.get("debug.version")) debug.version = packageJson.version;
  22. } catch (e) {
  23. console.log(`Could not get package info: ${e.message}.`);
  24. }
  25. try {
  26. let gitFolder = null;
  27. if (fs.existsSync(".parent_git/HEAD")) gitFolder = ".parent_git";
  28. else if (fs.existsSync("../.git/HEAD")) gitFolder = "../.git";
  29. if (gitFolder) {
  30. const headContents = fs
  31. .readFileSync(`${gitFolder}/HEAD`)
  32. .toString()
  33. .replace(/\n/g, "");
  34. const branch = /ref: refs\/heads\/([.A-Za-z0-9_-]+)/.exec(
  35. headContents
  36. )[1];
  37. const configContents = fs
  38. .readFileSync(`${gitFolder}/config`)
  39. .toString()
  40. .replace(/\t/g, "")
  41. .split("\n");
  42. let remote;
  43. let remoteUrl;
  44. let latestCommit;
  45. let latestCommitShort;
  46. if (configContents.indexOf(`[branch "${branch}"]`) >= 0) {
  47. remote = /remote = (.+)/.exec(
  48. configContents[
  49. configContents.indexOf(`[branch "${branch}"]`) + 1
  50. ]
  51. )[1];
  52. remoteUrl = /url = (.+)/.exec(
  53. configContents[
  54. configContents.indexOf(`[remote "${remote}"]`) + 1
  55. ]
  56. )[1];
  57. latestCommit = fs
  58. .readFileSync(`${gitFolder}/refs/heads/${branch}`)
  59. .toString()
  60. .replace(/\n/g, "");
  61. latestCommitShort = latestCommit.substr(0, 7);
  62. }
  63. console.log(
  64. `Git branch: ${remote}/${branch}. Remote url: ${remoteUrl}. Latest commit: ${latestCommit} (${latestCommitShort}).`
  65. );
  66. if (config.get("debug.git.remote")) debug.git.remote = remote;
  67. if (config.get("debug.git.remoteUrl"))
  68. debug.git.remoteUrl = remoteUrl;
  69. if (config.get("debug.git.branch")) debug.git.branch = branch;
  70. if (config.get("debug.git.latestCommit"))
  71. debug.git.latestCommit = latestCommit;
  72. if (config.get("debug.git.latestCommitShort"))
  73. debug.git.latestCommitShort = latestCommitShort;
  74. }
  75. } catch (e) {
  76. console.log(`Could not get Git info: ${e.message}.`, e);
  77. }
  78. return debug;
  79. };
  80. const debug = fetchVersionAndGitInfo();
  81. const siteName = config.has("siteSettings.sitename")
  82. ? config.get("siteSettings.sitename")
  83. : "Musare";
  84. const htmlPlugin = () => ({
  85. name: "html-transform",
  86. transformIndexHtml(originalHtml) {
  87. let html = originalHtml;
  88. html = html.replace(/{{ title }}/g, siteName);
  89. html = html.replace(/{{ version }}/g, debug.version);
  90. html = html.replace(/{{ gitRemote }}/g, debug.git.remote);
  91. html = html.replace(/{{ gitRemoteUrl }}/g, debug.git.remoteUrl);
  92. html = html.replace(/{{ gitBranch }}/g, debug.git.branch);
  93. html = html.replace(/{{ gitLatestCommit }}/g, debug.git.latestCommit);
  94. html = html.replace(
  95. /{{ gitLatestCommitShort }}/g,
  96. debug.git.latestCommitShort
  97. );
  98. return html;
  99. }
  100. });
  101. const mode = process.env.FRONTEND_MODE || "development";
  102. let server = null;
  103. if (mode === "development")
  104. server = {
  105. host: "0.0.0.0",
  106. port: config.get("devServer.port"),
  107. strictPort: true,
  108. hmr: {
  109. clientPort: config.get("devServer.hmrClientPort")
  110. }
  111. };
  112. export default {
  113. mode,
  114. root: "src",
  115. publicDir: "../dist",
  116. base: "/",
  117. resolve: {
  118. alias: [
  119. {
  120. find: "@",
  121. replacement: path.resolve(__dirname, "src")
  122. }
  123. ]
  124. },
  125. define: {
  126. __VUE_OPTIONS_API__: true,
  127. __VUE_PROD_DEVTOOLS__: false,
  128. MUSARE_VERSION: JSON.stringify(debug.version),
  129. MUSARE_GIT_REMOTE: JSON.stringify(debug.git.remote),
  130. MUSARE_GIT_REMOTE_URL: JSON.stringify(debug.git.remoteUrl),
  131. MUSARE_GIT_BRANCH: JSON.stringify(debug.git.branch),
  132. MUSARE_GIT_LATEST_COMMIT: JSON.stringify(debug.git.latestCommit),
  133. MUSARE_GIT_LATEST_COMMIT_SHORT: JSON.stringify(
  134. debug.git.latestCommitShort
  135. )
  136. },
  137. plugins: [vue(), htmlPlugin()],
  138. css: {
  139. preprocessorOptions: {
  140. less: {
  141. additionalData: `@import "@/styles/variables.less";`
  142. }
  143. }
  144. },
  145. server,
  146. build: {
  147. outDir: "../build"
  148. }
  149. };