vite.config.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. export default {
  103. mode,
  104. root: "src",
  105. publicDir: "../dist",
  106. base: "/",
  107. resolve: {
  108. alias: [
  109. {
  110. find: "@",
  111. replacement: path.resolve(__dirname, "src")
  112. }
  113. ]
  114. },
  115. define: {
  116. __VUE_OPTIONS_API__: true,
  117. __VUE_PROD_DEVTOOLS__: false,
  118. MUSARE_VERSION: JSON.stringify(debug.version),
  119. MUSARE_GIT_REMOTE: JSON.stringify(debug.git.remote),
  120. MUSARE_GIT_REMOTE_URL: JSON.stringify(debug.git.remoteUrl),
  121. MUSARE_GIT_BRANCH: JSON.stringify(debug.git.branch),
  122. MUSARE_GIT_LATEST_COMMIT: JSON.stringify(debug.git.latestCommit),
  123. MUSARE_GIT_LATEST_COMMIT_SHORT: JSON.stringify(
  124. debug.git.latestCommitShort
  125. )
  126. },
  127. plugins: [vue(), htmlPlugin()],
  128. css: {
  129. preprocessorOptions: {
  130. less: {
  131. additionalData: `@import "@/styles/variables.less";`
  132. }
  133. }
  134. },
  135. server: {
  136. host: "0.0.0.0",
  137. port: config.get("devServer.port"),
  138. strictPort: true,
  139. hmr: {
  140. clientPort: config.get("devServer.clientPort")
  141. }
  142. },
  143. build: {
  144. outDir: "../build"
  145. }
  146. };