Skip to content

Commit 0c5b752

Browse files
committedJul 8, 2018
feat: version data layer
Create a global variable "__VUEPRESS_VERSION__" to get current vuepress's version of the building documents
1 parent f0ff959 commit 0c5b752

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed
 

‎lib/app/clientEntry.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
/* global BASE_URL, GA_ID, ga, SW_ENABLED */
1+
/* global BASE_URL, GA_ID, ga, SW_ENABLED, VUEPRESS_VERSION, LAST_COMMIT_HASH*/
22

33
import { createApp } from './app'
44
import { register } from 'register-service-worker'
55

66
const { app, router } = createApp()
77

8+
window.__VUEPRESS_VERSION__ = {
9+
version: VUEPRESS_VERSION,
10+
hash: LAST_COMMIT_HASH
11+
}
12+
813
// Google analytics integration
914
if (process.env.NODE_ENV === 'production' && GA_ID) {
1015
(function (i, s, o, g, r, a, m) {

‎lib/webpack/createBaseConfig.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,19 @@ module.exports = function createBaseConfig ({
291291
.use(require('webpack/lib/DefinePlugin'), [{
292292
BASE_URL: JSON.stringify(siteConfig.base || '/'),
293293
GA_ID: siteConfig.ga ? JSON.stringify(siteConfig.ga) : false,
294-
SW_ENABLED: !!siteConfig.serviceWorker
294+
SW_ENABLED: !!siteConfig.serviceWorker,
295+
VUEPRESS_VERSION: JSON.stringify(require('../../package.json').version),
296+
LAST_COMMIT_HASH: JSON.stringify(getLastCommitHash())
295297
}])
296298

297299
return config
298300
}
301+
302+
function getLastCommitHash () {
303+
const spawn = require('cross-spawn')
304+
let hash
305+
try {
306+
hash = spawn.sync('git', ['log', '-1', '--format=%h']).stdout.toString('utf-8').trim()
307+
} catch (error) {}
308+
return hash
309+
}

0 commit comments

Comments
 (0)
Please sign in to comment.