Skip to content

Commit 14e7bc2

Browse files
committed
Fix issue caused by assuming webpack context would contain the git repository
That won’t necessarily be true, and most likely, won’t. So fallback to use the working directory of the Node process by default and allow customising its location if needed via a `gitWorkTree` option.
1 parent 8753243 commit 14e7bc2

4 files changed

Lines changed: 22 additions & 20 deletions

File tree

fixtures/project/webpack.config.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
var path = require('path')
2-
var GitRevisionPlugin = require(path.join(__dirname, '../../'))
3-
41
module.exports = {
52
entry: './index.js',
63

@@ -16,9 +13,5 @@ module.exports = {
1613
loader: 'file?name=[name][git-revision-version].[ext]'
1714
}
1815
]
19-
},
20-
21-
plugins: [
22-
new GitRevisionPlugin()
23-
]
16+
}
2417
}

lib/build-file.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@ var path = require('path')
22
var exec = require('child_process').exec
33
var removeEmptyLines = require('./helpers/remove-empty-lines.js')
44

5-
module.exports = function buildFile (compiler, command, replacePattern, asset) {
5+
module.exports = function buildFile (compiler, gitWorkTree, command, replacePattern, asset) {
66
var data = ''
77

88
compiler.plugin('compilation', function (compilation) {
9-
var workTree = compilation.options.context
10-
var gitDir = path.join(workTree, '.git')
11-
var gitCommand = [
12-
'git',
13-
'--git-dir=' + gitDir,
14-
'--work-tree=' + workTree,
15-
command
16-
].join(' ')
9+
var gitCommand = gitWorkTree
10+
? [
11+
'git',
12+
'--git-dir=' + path.join(gitWorkTree, '.git'),
13+
'--work-tree=' + gitWorkTree,
14+
command
15+
].join(' ')
16+
: [
17+
'git',
18+
command
19+
].join(' ')
1720

1821
compilation.plugin('optimize-tree', function (chunks, modules, callback) {
1922
exec(gitCommand, function (err, stdout) {

lib/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
var buildFile = require('./build-file')
22

3-
function GitRevisionPlugin () {}
3+
function GitRevisionPlugin (options) {
4+
this.gitWorkTree = options && options.gitWorkTree
5+
}
46

57
GitRevisionPlugin.prototype.apply = function (compiler) {
6-
buildFile(compiler, 'rev-parse HEAD', /\[git-revision-hash\]/gi, 'COMMITHASH')
7-
buildFile(compiler, 'describe --always', /\[git-revision-version\]/gi, 'VERSION')
8+
buildFile(compiler, this.gitWorkTree, 'rev-parse HEAD', /\[git-revision-hash\]/gi, 'COMMITHASH')
9+
buildFile(compiler, this.gitWorkTree, 'describe --always', /\[git-revision-version\]/gi, 'VERSION')
810
}
911

1012
module.exports = GitRevisionPlugin

lib/index.spec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var expect = require('chai').expect
44
var webpack = require('webpack')
55
var fs = require('fs-extra')
66
var path = require('path')
7+
var GitRevisionPlugin = require('.')
78

89
var sourceProject = path.join(__dirname, '../fixtures/project')
910
var sourceGitRepository = path.join(__dirname, '../fixtures/git-repository')
@@ -28,6 +29,9 @@ describe('git-revision-webpack-plugin', function () {
2829

2930
config.context = targetProject
3031
config.output.path = targetBuild
32+
config.plugins = [
33+
new GitRevisionPlugin({ gitWorkTree: targetProject })
34+
]
3135

3236
webpack(config, function () {
3337
done()

0 commit comments

Comments
 (0)