webpack.common.js 4.3 KB

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