webpack.common.js 3.8 KB

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