File size: 2,394 Bytes
ae4627d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
var fs = require('fs')
var Asset = require('./asset')
var File = require('./file')
var Type = require('lighter-type')
var path = require('path')

/**
 * A Pack runs webpack and outputs the resulting file content.
 */
module.exports = File.extend(function Pack (path, stat, load) {
  var self = this
  var webpack = require('webpack')
  Asset.init.apply(this, arguments)
  this.wait()
  dive(path, function (paths) {
    var fs = new BypassFs()

    var fn = function (ignore, stats) {
      var name = stats.toJson().assets[0].name
      var type = name.replace(/^.*\./, '')
      if (self.type !== type) {
        self.type = type
        self.path += '.' + type
        self.location += '.' + type
      }
      self.handleContent(fs.content)
      self.unwait()
    }

    var compiler = webpack({entry: paths}, fn)
    compiler.outputFileSystem = fs

    // Check for changes once every 200ms.
    // TODO: Handle entry point changes in packed directories.
    compiler.watch(200, function (err, stats) {
      // Wait for compilation to return assets, and call the handler.
      self.wait()
      fn(err, stats)

      // Replay anything that depends on changes in this asset's load.
      load.then(function () {
        load.replay(self.location)
      })
    })
  })
})

/**
 * Bypass the file system with a fake one of our own.
 */
var BypassFs = Type.extend({

  // Remember the content, and call back without an error.
  writeFile: function (path, content, fn) {
    this.content = content
    fn()
  },

  // Call back without an error.
  mkdirp: function (path, fn) {
    fn()
  },

  // Join a directory and filename, and return a path.
  join: function (dir, file) {
    return path.join(dir, file)
  }

})

/**
 * Dive down a directory.
 */
function dive (path, fn) {
  var list = []
  var wait = 0
  got(path)
  function got (path) {
    wait++
    fs.lstat(path, function (error, stat) {
      if (!error) {
        if (stat.isFile()) {
          list.push(path)
        }
        if (stat.isDirectory()) {
          wait++
          fs.readdir(path, function (error, files) {
            if (!error) {
              files.forEach(function (file) {
                got(path + '/' + file)
              })
            }
            if (!--wait) done()
          })
        }
      }
      if (!--wait) done()
    })
  }
  function done () {
    list.sort()
    fn(list)
  }
}