forked from baconjs/bacon.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assemble.js
executable file
·132 lines (113 loc) · 3.49 KB
/
assemble.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
#!/usr/bin/env babel-node
/**
* This file is responsible for building Bacon.js, Bacon.noAssert.js and Bacon.min.js
*/
"use strict";
var _ = require("lodash");
var fs = require("fs");
var path = require("path");
var Deps = require("./build-deps");
var babel = require("babel");
var assert = require("assert");
var uglifyjs = require("uglify-js");
var esprima = require("esprima");
var estraverse = require("estraverse");
var escodegen = require("escodegen");
var jsstana = require("jsstana");
var argPieceNames = process.argv.slice(2)
var manifests = argPieceNames.length ? argPieceNames : ["main"];
var defaultOutput = path.join(__dirname, "dist", "Bacon.js");
var defaultNoAssert = path.join(__dirname, "dist", "Bacon.noAssert.js");
var defaultMinified = path.join(__dirname, "dist", "Bacon.min.js");
// Boilerplate: *header* and *footer*
var header = fs.readFileSync(path.join(__dirname, "src", "boilerplate", "object.js"), "utf-8");
var footer = fs.readFileSync(path.join(__dirname, "src", "boilerplate", "exports.js"), "utf-8");
// 16 spaces
var padding = " ";
var main = function(options){
options = options || {};
var pieces = Deps.resolvePieces(manifests, "src");
if (options.verbose) {
console.info("Linearised dependency graph:")
_.each(pieces, function (p) {
var name = p.name + (p.type === "js" ? "*" : "");
if (p.deps.length === 0) {
console.info(" ", name);
} else {
console.info(" ", name + padding.substr(name.length), "←", p.deps.join(", "));
}
});
}
// let's be conservative with Babel options:
var babelOptions = {
comments: false,
whitelist: [
"es3.memberExpressionLiterals",
"es3.propertyLiterals",
"es6.arrowFunctions",
"es6.blockScoping",
"es6.properties.shorthand",
"es6.constants",
"es6.destructuring",
"es6.parameters",
"es6.spread"
],
loose: [
"es6.spread",
"es6.destructuring"
]
};
var esOutput = header + _.pluck(pieces, "contents").join("\n") + footer;
var esTranspiled = babel.transform(esOutput, babelOptions);
var output = "(function() {\n" + esTranspiled.code + "\n}).call(this);";
// Stripping asserts
function notAssertStatement(node) {
var m1 = jsstana.match("(expr (call (ident ?fn) ??))", node);
if (m1 && m1.fn.match(/^assert/)) {
return false;
}
var m2 = jsstana.match("(expr (assign = (ident ?fn) ?))", node);
if (m2 && m2.fn.match(/^assert/)) {
return false;
}
return true;
}
function stripAsserts(code) {
var ast = esprima.parse(code);
estraverse.replace(ast, {
enter: function (node, parent) {
if (node !== null && node.type === "BlockStatement") {
node.body = node.body.filter(notAssertStatement);
return node;
}
}
})
return escodegen.generate(ast);
}
var noAssertOutput = stripAsserts(output);
// Minifying
var minifiedOutput = uglifyjs.minify(noAssertOutput, {
fromString: true,
}).code;
if (options.output) {
try {fs.mkdirSync("dist")} catch (e) {}
fs.writeFileSync(options.output, output);
if (options.noAssert) {
fs.writeFileSync(options.noAssert, noAssertOutput);
}
if (options.minified) {
fs.writeFileSync(options.minified, minifiedOutput);
}
} else {
console.log(output);
}
}
if (require.main === module) {
main({
verbose: true,
output: defaultOutput,
noAssert: defaultNoAssert,
minified: defaultMinified,
});
}
exports.main = main;