vite.config.js 4.1 KB

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