webpack.common.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. process.env.NODE_CONFIG_DIR = `${__dirname}/dist/config/`;
  2. const path = require("path");
  3. const fs = require("fs");
  4. const config = require("config");
  5. const { VueLoaderPlugin } = require("vue-loader");
  6. const HtmlWebpackPlugin = require("html-webpack-plugin");
  7. const ESLintPlugin = require("eslint-webpack-plugin");
  8. const fetchVersionAndGitInfo = cb => {
  9. const debug = {
  10. git: {
  11. remote: "",
  12. remoteUrl: "",
  13. branch: "",
  14. latestCommit: "",
  15. latestCommitShort: ""
  16. },
  17. version: ""
  18. };
  19. try {
  20. const packageJson = JSON.parse(
  21. fs.readFileSync("./package.json").toString()
  22. );
  23. const headContents = fs
  24. .readFileSync(".parent_git/HEAD")
  25. .toString()
  26. .replace(/\n/g, "");
  27. const branch = new RegExp("ref: refs/heads/([.A-Za-z0-9_-]+)").exec(
  28. headContents
  29. )[1];
  30. const configContents = fs
  31. .readFileSync(".parent_git/config")
  32. .toString()
  33. .replace(/\t/g, "")
  34. .split("\n");
  35. const remote = new RegExp("remote = (.+)").exec(
  36. configContents[configContents.indexOf(`[branch "${branch}"]`) + 1]
  37. )[1];
  38. const remoteUrl = new RegExp("url = (.+)").exec(
  39. configContents[configContents.indexOf(`[remote "${remote}"]`) + 1]
  40. )[1];
  41. const latestCommit = fs
  42. .readFileSync(`.parent_git/refs/heads/${branch}`)
  43. .toString()
  44. .replace(/\n/g, "");
  45. const latestCommitShort = latestCommit.substr(0, 7);
  46. console.log(`Musare version: ${packageJson.version}.`);
  47. console.log(
  48. `Git branch: ${remote}/${branch}. Remote url: ${remoteUrl}. Latest commit: ${latestCommit} (${latestCommitShort}).`
  49. );
  50. if (config.get("debug.version")) debug.version = packageJson.version;
  51. if (config.get("debug.git.remote")) debug.git.remote = remote;
  52. if (config.get("debug.git.remoteUrl")) debug.git.remoteUrl = remoteUrl;
  53. if (config.get("debug.git.branch")) debug.git.branch = branch;
  54. if (config.get("debug.git.latestCommit"))
  55. debug.git.latestCommit = latestCommit;
  56. if (config.get("debug.git.latestCommitShort"))
  57. debug.git.latestCommitShort = latestCommitShort;
  58. } catch (e) {
  59. console.log(`Could not get Git info: ${e.message}.`);
  60. }
  61. cb(debug);
  62. };
  63. fetchVersionAndGitInfo(() => {});
  64. class InsertDebugInfoPlugin {
  65. apply(compiler) {
  66. compiler.hooks.compilation.tap("InsertDebugInfoPlugin", compilation => {
  67. HtmlWebpackPlugin.getHooks(
  68. compilation
  69. ).beforeAssetTagGeneration.tapAsync(
  70. "InsertDebugInfoPlugin",
  71. (data, cb) => {
  72. fetchVersionAndGitInfo(debug => {
  73. data.plugin.userOptions.debug.version = debug.version;
  74. data.plugin.userOptions.debug.git.remote =
  75. debug.git.remote;
  76. data.plugin.userOptions.debug.git.remoteUrl =
  77. debug.git.remoteUrl;
  78. data.plugin.userOptions.debug.git.branch =
  79. debug.git.branch;
  80. data.plugin.userOptions.debug.git.latestCommit =
  81. debug.git.latestCommit;
  82. data.plugin.userOptions.debug.git.latestCommitShort =
  83. debug.git.latestCommitShort;
  84. cb(null, data);
  85. });
  86. }
  87. );
  88. });
  89. }
  90. }
  91. module.exports = {
  92. entry: "./src/main.js",
  93. output: {
  94. path: `${__dirname}/dist/build/`,
  95. filename: "[name].[contenthash].js"
  96. },
  97. resolve: {
  98. alias: {
  99. "@": path.resolve(__dirname, "./src/")
  100. },
  101. extensions: [".js", ".vue"]
  102. },
  103. plugins: [
  104. new VueLoaderPlugin(),
  105. new HtmlWebpackPlugin({
  106. title: config.get("siteSettings.sitename"),
  107. hash: true,
  108. template: "dist/index.tpl.html",
  109. inject: "body",
  110. filename: "index.html",
  111. debug: {
  112. git: {
  113. remote: "",
  114. remoteUrl: "",
  115. branch: "",
  116. latestCommit: "",
  117. latestCommitShort: ""
  118. },
  119. version: ""
  120. }
  121. }),
  122. new ESLintPlugin(),
  123. new InsertDebugInfoPlugin()
  124. ],
  125. module: {
  126. rules: [
  127. {
  128. test: /\.vue$/,
  129. loader: "vue-loader",
  130. exclude: /node_modules/
  131. },
  132. {
  133. test: /\.js$/,
  134. loader: "babel-loader",
  135. exclude: /node_modules/
  136. },
  137. {
  138. test: /\.less$/i,
  139. exclude: /node_modules/,
  140. use: [
  141. "vue-style-loader",
  142. {
  143. loader: "css-loader",
  144. options: {
  145. url: false
  146. }
  147. },
  148. "less-loader",
  149. {
  150. loader: "style-resources-loader",
  151. options: {
  152. patterns: [
  153. path.resolve(
  154. __dirname,
  155. "./src/styles/variables.less"
  156. )
  157. ]
  158. }
  159. }
  160. ]
  161. }
  162. ]
  163. }
  164. };