webpack.common.js 4.4 KB

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