webpack.common.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. title: config.get("siteSettings.sitename"),
  95. hash: true,
  96. template: "dist/index.tpl.html",
  97. inject: "body",
  98. filename: "index.html",
  99. debug: {
  100. git: {
  101. remote: "",
  102. remoteUrl: "",
  103. branch: "",
  104. latestCommit: "",
  105. latestCommitShort: ""
  106. },
  107. version: ""
  108. }
  109. }),
  110. new ESLintPlugin(),
  111. new InsertDebugInfoPlugin()
  112. ],
  113. module: {
  114. rules: [
  115. {
  116. test: /\.vue$/,
  117. loader: "vue-loader",
  118. exclude: /node_modules/
  119. },
  120. {
  121. test: /\.js$/,
  122. loader: "babel-loader",
  123. exclude: /node_modules/
  124. },
  125. {
  126. test: /\.scss$/,
  127. exclude: /node_modules/,
  128. use: [
  129. "vue-style-loader",
  130. {
  131. loader: "css-loader",
  132. options: {
  133. url: false
  134. }
  135. },
  136. "sass-loader"
  137. ]
  138. }
  139. ]
  140. }
  141. };