Commit 7f87d41691d33d93609b3c90ddb62da7e9182940

Authored by Min
1 parent 21ee7bfd

更新以下内容: 配置注入构建版本号到全局和环境变量

通过webpack.DefinePlugin定义process.env.APP_VERSION,同时将版本号挂载到window对象,方便在运行时获取版本信息
src/index.js
... ... @@ -11,6 +11,10 @@ import './g2';
11 11 import './variable.less'; // 用于覆盖上面定义的变量
12 12  
13 13  
  14 +const buildDate = new Date();
  15 +const version = process.env.APP_VERSION || `JMES-${buildDate.getFullYear()}${String(buildDate.getMonth() + 1).padStart(2, '0')}${String(buildDate.getDate()).padStart(2, '0')} ${String(buildDate.getHours()).padStart(2, '0')}${String(buildDate.getMinutes()).padStart(2, '0')}v1`;
  16 +window.myversion = version;
  17 +
14 18 const ERROR_MSG_DURATION = 5;
15 19  
16 20 const app = dva({
... ...
webpack.config.js
... ... @@ -3,6 +3,9 @@ import SpriteLoaderPlugin from "svg-sprite-loader/plugin";
3 3 import { resolve } from "path";
4 4  
5 5 export default webpackConfig => {
  6 + const webpack = require("webpack");
  7 + const now = new Date();
  8 + const version = `JMES-${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')} ${String(now.getHours()).padStart(2, '0')}${String(now.getMinutes()).padStart(2, '0')}v1`;
6 9 const data = webpackConfig.resolve.alias;
7 10 webpackConfig.resolve.alias = {
8 11 "@": `${__dirname}/src`,
... ... @@ -25,7 +28,10 @@ export default webpackConfig => {
25 28 }
26 29 ].concat(webpackConfig.module.rules);
27 30 webpackConfig.plugins = webpackConfig.plugins.concat([
28   - new SpriteLoaderPlugin()
  31 + new SpriteLoaderPlugin(),
  32 + new webpack.DefinePlugin({
  33 + "process.env.APP_VERSION": JSON.stringify(version)
  34 + })
29 35 ]);
30 36 if (process.env.NODE_ENV === "production") {
31 37 webpackConfig.plugins.splice(
... ...