var runGitCommand = require('./helpers/run-git-command')

module.exports = function buildFile (compiler, gitWorkTree, command, replacePattern, asset) {
  var data = ''

  compiler.plugin('compilation', function (compilation) {
    compilation.plugin('optimize-tree', function (chunks, modules, callback) {
      runGitCommand(gitWorkTree, command, function (err, res) {
        if (err) { return callback(err) }
        data = res

        callback()
      })
    })

    compilation.mainTemplate.plugin('asset-path', function (path) {
      return path.replace(replacePattern, data)
    })
  })

  compiler.plugin('emit', function (compilation, callback) {
    compilation.assets[asset] = {
      source: function () {
        return data
      },
      size: function () {
        return data.length
      }
    }

    callback()
  })
}

@@ -173,22 +173,37 @@
 ompiler.
-plugin
+hooks.compilation.tap
 ('compil
@@ -209,25 +209,16 @@
 lation',
- function
  (compil
@@ -223,16 +223,19 @@
 ilation)
+ =%3E
  %7B%0A    c
@@ -245,22 +245,43 @@
 ilation.
-plugin
+hooks.optimizeTree.tapAsync
 ('optimi
@@ -289,25 +289,16 @@
 e-tree',
- function
  (chunks
@@ -313,24 +313,27 @@
 s, callback)
+ =%3E
  %7B%0A      run
@@ -514,22 +514,35 @@
 emplate.
-plugin
+hooks.assetPath.tap
 ('asset-
@@ -551,24 +551,18 @@
 th',
- function
  (path)
+ =%3E
  %7B%0A 
@@ -636,31 +636,35 @@
 ler.
-plugin('emit', function
+hooks.emit.tapAsync('emit',
  (co
@@ -683,16 +683,19 @@
 allback)
+ =%3E
  %7B%0A    c

Update build-file to Webpack 4 to use .hooks instead of .plugin