webpack.common.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. hash: true,
  107. template: "dist/index.tpl.html",
  108. inject: "body",
  109. filename: "index.html",
  110. debug: {
  111. git: {
  112. remote: "",
  113. remoteUrl: "",
  114. branch: "",
  115. latestCommit: "",
  116. latestCommitShort: ""
  117. },
  118. version: ""
  119. }
  120. }),
  121. new ESLintPlugin(),
  122. new InsertDebugInfoPlugin()
  123. ],
  124. module: {
  125. rules: [
  126. {
  127. test: /\.vue$/,
  128. loader: "vue-loader",
  129. exclude: /node_modules/
  130. },
  131. {
  132. test: /\.js$/,
  133. loader: "babel-loader",
  134. exclude: /node_modules/
  135. },
  136. {
  137. test: /\.less$/i,
  138. exclude: /node_modules/,
  139. use: [
  140. "vue-style-loader",
  141. {
  142. loader: "css-loader",
  143. options: {
  144. url: false
  145. }
  146. },
  147. "less-loader",
  148. {
  149. loader: "style-resources-loader",
  150. options: {
  151. patterns: [
  152. path.resolve(
  153. __dirname,
  154. "./src/styles/variables.less"
  155. )
  156. ]
  157. }
  158. }
  159. ]
  160. }
  161. ]
  162. }
  163. };