UNPKG

1.43 kBJavaScriptView Raw
1var trackDir = require('./track-dir')
2var utils = require('./utils')
3var path = require('path')
4
5var dirs = [
6 'assets',
7 'content',
8 'public'
9]
10
11// This module handles all static assets. It only ever links to files, and
12// always reads them from disk. Else files might make Node run out of memory
13// (e.g. video files). The following steps are taken
14//
15// 1. Read out all files in the directory lists.
16// 2. Write all file names to a list, and emit `list`.
17// 3. Estimate total size of all files combined, and emit `size`.
18//
19// TODO: optimize assets (on the fly); e.g. convert images to webp, etc.
20// TODO: also emit `progress`.
21
22module.exports = node
23
24function node (state, createEdge) {
25 var basedir = utils.dirname(state.metadata.entry)
26 var self = this
27
28 if (state.tracker) return
29 var tracker = state.tracker = trackDir()
30
31 tracker.start(basedir, dirs, { watch: state.metadata.watch })
32 state.metadata.assets = tracker.files
33
34 tracker.on('error', function (err) {
35 self.emit('error', 'assets', 'tracker', err)
36 })
37
38 tracker.on('change', function () {
39 var list = tracker.list().map(function (file) {
40 return path.posix.format(path.parse(path.relative(basedir, file)))
41 })
42 createEdge('list', Buffer.from(list.join(',')))
43 })
44
45 tracker.on('progress', function (progress) {
46 // self.emit('progress', 'assets', progress)
47 })
48
49 this.on('close', function () {
50 tracker.unwatch()
51 })
52}