forked from probmods/webppl
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbundle.js
68 lines (54 loc) · 1.8 KB
/
bundle.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
// A browserify plugin to include packages and version information in
// the browser bundle.
'use strict';
var path = require('path');
var assert = require('assert');
var through = require('through2');
var esprima = require('esprima');
var estraverse = require('estraverse');
var escodegen = require('escodegen');
var _ = require('lodash');
var pkg = require('./pkg');
var pkginfo = require('./pkginfo');
var util = require('./util');
var parseExpr = function(s) {
return esprima.parse('(' + s + ')').body[0].expression;
};
var transform = function(code, version, opts) {
var replace = _.partial(estraverse.replace, _, {
enter: function(node, parent) {
if (node.type === 'ArrayExpression' &&
parent.type === 'VariableDeclarator' &&
parent.id.type === 'Identifier' &&
parent.id.name === 'packages') {
assert(node.elements.length === 0);
var exprs = util.asArray(opts.require).map(function(name_or_path) {
return _.flowRight(parseExpr, pkg.stringify, pkg.read)(name_or_path);
});
return { type: node.type, elements: exprs };
}
if (node.type === 'Literal' &&
node.value === '' &&
parent.type === 'VariableDeclarator' &&
parent.id.type === 'Identifier' &&
parent.id.name === 'version') {
return { type: node.type, value: version };
}
}
});
var pipeline = _.flowRight(escodegen.generate, replace, esprima.parse);
return pipeline(code);
};
module.exports = function(file, opts) {
if (path.basename(file) !== 'browser.js') { return through(); }
var code = '';
return through(
function(buf, enc, next) {
code += buf;
next();
},
function(next) {
this.push(transform(code, pkginfo.version(), opts));
next();
});
};