Skip to content

Commit

Permalink
feat: version data layer
Browse files Browse the repository at this point in the history
Create a global variable "__VUEPRESS_VERSION__" to get current vuepress's version of the building documents
  • Loading branch information
ulivz committed Jul 8, 2018
1 parent f0ff959 commit 0c5b752
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/app/clientEntry.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
13 changes: 12 additions & 1 deletion lib/webpack/createBaseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 0c5b752

Please sign in to comment.