From 0c5b752563dae1e9eac1f116c9ee32d86397c33d Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Sun, 8 Jul 2018 13:21:43 +0800 Subject: [PATCH] feat: version data layer Create a global variable "__VUEPRESS_VERSION__" to get current vuepress's version of the building documents --- lib/app/clientEntry.js | 7 ++++++- lib/webpack/createBaseConfig.js | 13 ++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/app/clientEntry.js b/lib/app/clientEntry.js index 4b11dbb48b..5691528ebf 100644 --- a/lib/app/clientEntry.js +++ b/lib/app/clientEntry.js @@ -1,10 +1,15 @@ -/* global BASE_URL, GA_ID, ga, SW_ENABLED */ +/* global BASE_URL, GA_ID, ga, SW_ENABLED, VUEPRESS_VERSION, LAST_COMMIT_HASH*/ import { createApp } from './app' import { register } from 'register-service-worker' const { app, router } = createApp() +window.__VUEPRESS_VERSION__ = { + version: VUEPRESS_VERSION, + hash: LAST_COMMIT_HASH +} + // Google analytics integration if (process.env.NODE_ENV === 'production' && GA_ID) { (function (i, s, o, g, r, a, m) { diff --git a/lib/webpack/createBaseConfig.js b/lib/webpack/createBaseConfig.js index 428344cb78..cee40f856a 100644 --- a/lib/webpack/createBaseConfig.js +++ b/lib/webpack/createBaseConfig.js @@ -291,8 +291,19 @@ module.exports = function createBaseConfig ({ .use(require('webpack/lib/DefinePlugin'), [{ BASE_URL: JSON.stringify(siteConfig.base || '/'), GA_ID: siteConfig.ga ? JSON.stringify(siteConfig.ga) : false, - SW_ENABLED: !!siteConfig.serviceWorker + SW_ENABLED: !!siteConfig.serviceWorker, + VUEPRESS_VERSION: JSON.stringify(require('../../package.json').version), + LAST_COMMIT_HASH: JSON.stringify(getLastCommitHash()) }]) return config } + +function getLastCommitHash () { + const spawn = require('cross-spawn') + let hash + try { + hash = spawn.sync('git', ['log', '-1', '--format=%h']).stdout.toString('utf-8').trim() + } catch (error) {} + return hash +}