-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
162 lines (135 loc) · 4.57 KB
/
index.js
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
'use strict';
const assignIn = require('lodash.assignin');
const fs = require('fs-extra');
const path = require('path');
const RSVP = require('rsvp');
const mkdirp = require('mkdirp');
const browserify = require('browserify');
const Plugin = require('broccoli-plugin');
const md5Hex = require('md5hex');
const TreeSync = require('tree-sync');
const nodeModulesPath = require('node-modules-path');
const symlinkOrCopySync = require('symlink-or-copy').sync;
const statsForPaths = require('./lib/stats-for-paths');
const updateCacheFromStats = require('./lib/update-cache-from-stats');
const through = require('through2');
const xtend = require('xtend');
module.exports = function(inputTree, options) {
return new Watchify(inputTree, options);
}
class Watchify extends Plugin {
constructor(inputTree, options) {
super([inputTree], options);
this._persistentOutput = true;
this.options = assignIn(this.getDefaultOptions(), options);
this.clearCache();
this._fileToChecksumMap = Object.create(null); // TODO: extract SP
this._tree = undefined;
this._last = false;
}
getDefaultOptions() {
return {
outputFile: '/browserify.js',
browserify: {
entries: ['index.js'],
},
cache: true,
init: function (browserify) {},
nodeModulesPath: nodeModulesPath(process.cwd())
};
}
writeFileIfContentChanged(fullPath, content) {
const previous = this._fileToChecksumMap[fullPath];
const next = md5Hex(content);
if (previous === next) {
// hit
} else {
fs.mkdirpSync(path.dirname(fullPath));
fs.writeFileSync(fullPath, content);
this._fileToChecksumMap[fullPath] = next; // update map
}
}
syncInputAndCache() {
// node doesn't like symlinks, specifically node x, where x is a symlink will
// actually execute with __dirname of the realpath of that symlink. So to
// make this work, we deference our inputPath[0] into our cachePath, where we
// run browserify/watchify without browserify being aware it is actually
// operating on symlinked copies of stuff.
//
// Note: we likely though, don't want to materialize something like
// node_modules. We should investigate this in the future.
if (this._tree === undefined) {
symlinkOrCopySync(this.options.nodeModulesPath, this.cachePath + '/node_modules');
this._tree = new TreeSync(this.inputPaths[0], this.cachePath + '/build');
}
this._tree.sync();
}
build() {
this.syncInputAndCache();
const plugin = this;
const srcDir = this.inputPaths[0];
const destDir = this.outputPath;
const outputDir = path.dirname(this.options.outputFile);
const outputFile = destDir + '/' + this.options.outputFile;
mkdirp.sync(this.outputPath + '/' + path.dirname(outputDir));
this.options.browserify.paths = [ this.cachePath + '/node_modules' ];
this.options.browserify.basedir = this.cachePath + '/build';
const browserifyOptions = assignIn(this.options.browserify, this.watchifyData);
if (this.options.cache && this._last) {
const skipBuild = this.updateCaches();
if (skipBuild) { return; }
}
const b = browserify(browserifyOptions);
if (this.options.cache) {
b.on('reset', collect.bind(null, b));
collect(b);
}
this.options.init(b);
return new RSVP.Promise((resolve, reject) => {
b.bundle((err, data) => {
try {
if (err) {
plugin.clearCache();
reject(err);
} else {
plugin.statDependencies();
plugin.writeFileIfContentChanged(outputFile, data);
resolve(destDir);
}
} catch (e) {
reject(e);
}
});
});
}
statDependencies() {
this._last = statsForPaths(Object.keys(this.watchifyData.cache));
this._lastPackages = statsForPaths(Object.keys(this.watchifyData.packageCache));
}
updateCaches() {
const last = updateCacheFromStats(this._last, this.watchifyData.cache);
const lastPackages = updateCacheFromStats(this._lastPackages, this.watchifyData.packageCache);
return last && lastPackages;
}
clearCache() {
this._last = false;
this._lastPackages = false;
this.watchifyData = {
cache: {},
packageCache: {}
};
}
}
module.exports.Class = Watchify;
// extracted from watchify
function collect (b) {
b.pipeline.get('deps').push(through.obj(function(row, enc, next) {
let file = row.expose ? b._expose[row.id] : row.file;
b._options.cache[file] = {
source: row.source,
deps: xtend(row.deps)
};
this.push(row);
next();
}));
}