-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.js
434 lines (396 loc) · 15.7 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
var through = require('through2');
var Readable = require('readable-stream').Readable;
var concat = require('concat-stream');
var duplexer = require('duplexer2');
var acorn = require('acorn-node');
var walkAst = require('acorn-node/walk').full;
var scan = require('scope-analyzer');
var unparse = require('escodegen').generate;
var inspect = require('object-inspect');
var evaluate = require('static-eval');
var copy = require('shallow-copy');
var has = require('has');
var MagicString = require('magic-string');
var convertSourceMap = require('convert-source-map');
var mergeSourceMap = require('merge-source-map');
module.exports = function parse (modules, opts) {
if (!opts) opts = {};
var vars = opts.vars || {};
var varModules = opts.varModules || {};
var parserOpts = copy(opts.parserOpts || {});
var updates = [];
var moduleBindings = [];
var sourcemapper;
var inputMap;
var output = through();
var body, ast;
return duplexer(concat({ encoding: 'buffer' }, function (buf) {
try {
body = buf.toString('utf8').replace(/^#!/, '//#!');
var matches = false;
for (var key in modules) {
if (body.indexOf(key) !== -1) {
matches = true;
break;
}
}
if (!matches) {
// just pass it through
output.end(buf);
return;
}
if (opts.sourceMap) {
inputMap = convertSourceMap.fromSource(body);
if (inputMap) inputMap = inputMap.toObject();
body = convertSourceMap.removeComments(body);
sourcemapper = new MagicString(body);
}
ast = acorn.parse(body, parserOpts);
// scan.crawl does .parent tracking, so we can use acorn's builtin walker.
scan.crawl(ast);
walkAst(ast, walk);
}
catch (err) { return error(err) }
finish(body);
}), output);
function finish (src) {
var pos = 0;
src = String(src);
moduleBindings.forEach(function (binding) {
if (binding.isReferenced()) {
return;
}
var node = binding.initializer;
if (node.type === 'VariableDeclarator') {
var i = node.parent.declarations.indexOf(node);
if (node.parent.declarations.length === 1) {
// remove the entire declaration
updates.push({
start: node.parent.start,
offset: node.parent.end - node.parent.start,
stream: st()
});
} else if (i === node.parent.declarations.length - 1) {
updates.push({
// remove ", a = 1"
start: node.parent.declarations[i - 1].end,
offset: node.end - node.parent.declarations[i - 1].end,
stream: st()
});
} else {
updates.push({
// remove "a = 1, "
start: node.start,
offset: node.parent.declarations[i + 1].start - node.start,
stream: st()
});
}
} else if (node.parent.type === 'SequenceExpression' && node.parent.expressions.length > 1) {
var i = node.parent.expressions.indexOf(node);
if (i === node.parent.expressions.length - 1) {
updates.push({
// remove ", a = 1"
start: node.parent.expressions[i - 1].end,
offset: node.end - node.parent.expressions[i - 1].end,
stream: st()
});
} else {
updates.push({
// remove "a = 1, "
start: node.start,
offset: node.parent.expressions[i + 1].start - node.start,
stream: st()
});
}
} else {
if (node.parent.type === 'ExpressionStatement') node = node.parent;
updates.push({
start: node.start,
offset: node.end - node.start,
stream: st()
});
}
});
updates.sort(function(a, b) { return a.start - b.start; });
(function next () {
if (updates.length === 0) return done();
var s = updates.shift();
output.write(src.slice(pos, s.start));
pos = s.start + s.offset;
s.stream.pipe(output, { end: false });
if (opts.sourceMap) {
s.stream.pipe(concat({ encoding: 'string' }, function (chunk) {
// We have to give magic-string the replacement string,
// so it can calculate the amount of lines and columns.
if (s.offset === 0) {
sourcemapper.appendRight(s.start, chunk);
} else {
sourcemapper.overwrite(s.start, s.start + s.offset, chunk);
}
})).on('finish', next);
} else {
s.stream.on('end', next);
}
})();
function done () {
output.write(src.slice(pos));
if (opts.sourceMap) {
var map = sourcemapper.generateMap({
source: opts.inputFilename || 'input.js',
includeContent: true
});
if (inputMap) {
var merged = mergeSourceMap(inputMap, map);
output.write('\n' + convertSourceMap.fromObject(merged).toComment() + '\n');
} else {
output.write('\n//# sourceMappingURL=' + map.toUrl() + '\n');
}
}
output.end();
}
}
function error (msg) {
var err = typeof msg === 'string' ? new Error(msg) : msg;
output.emit('error', err);
}
function walk (node) {
if (opts.sourceMap) {
sourcemapper.addSourcemapLocation(node.start);
sourcemapper.addSourcemapLocation(node.end);
}
var isreq = isRequire(node);
var isreqm = false, isreqv = false, reqid;
if (isreq) {
reqid = node.arguments[0].value;
isreqm = has(modules, reqid);
isreqv = has(varModules, reqid);
}
if (isreqv && node.parent.type === 'VariableDeclarator'
&& node.parent.id.type === 'Identifier') {
var binding = scan.getBinding(node.parent.id);
if (binding) binding.value = varModules[reqid];
}
else if (isreqv && node.parent.type === 'AssignmentExpression'
&& node.parent.left.type === 'Identifier') {
var binding = scan.getBinding(node.parent.left);
if (binding) binding.value = varModules[reqid];
}
else if (isreqv && node.parent.type === 'MemberExpression'
&& isStaticProperty(node.parent.property)
&& node.parent.parent.type === 'VariableDeclarator'
&& node.parent.parent.id.type === 'Identifier') {
var binding = scan.getBinding(node.parent.parent.id);
var v = varModules[reqid][resolveProperty(node.parent.property)];
if (binding) binding.value = v;
}
else if (isreqv && node.parent.type === 'MemberExpression'
&& node.parent.property.type === 'Identifier') {
//vars[node.parent.parent.id.name] = varModules[reqid];
}
else if (isreqv && node.parent.type === 'CallExpression') {
//
}
if (isreqm && node.parent.type === 'VariableDeclarator'
&& node.parent.id.type === 'Identifier') {
var binding = scan.getBinding(node.parent.id);
if (binding) {
binding.module = modules[reqid];
binding.initializer = node.parent;
binding.remove(node.parent.id);
moduleBindings.push(binding);
}
}
else if (isreqm && node.parent.type === 'AssignmentExpression'
&& node.parent.left.type === 'Identifier') {
var binding = scan.getBinding(node.parent.left);
if (binding) {
binding.module = modules[reqid];
binding.initializer = node.parent;
binding.remove(node.parent.left);
moduleBindings.push(binding);
}
}
else if (isreqm && node.parent.type === 'MemberExpression'
&& isStaticProperty(node.parent.property)
&& node.parent.parent.type === 'VariableDeclarator'
&& node.parent.parent.id.type === 'Identifier') {
var binding = scan.getBinding(node.parent.parent.id);
if (binding) {
binding.module = modules[reqid][resolveProperty(node.parent.property)];
binding.initializer = node.parent.parent;
binding.remove(node.parent.parent.id);
moduleBindings.push(binding);
}
}
else if (isreqm && node.parent.type === 'MemberExpression'
&& isStaticProperty(node.parent.property)) {
var name = resolveProperty(node.parent.property);
var cur = copy(node.parent.parent);
cur.callee = copy(node.parent.property);
cur.callee.parent = cur;
traverse(cur.callee, modules[reqid][name]);
}
else if (isreqm && node.parent.type === 'CallExpression') {
var cur = copy(node.parent);
var iname = Math.pow(16,8) * Math.random();
cur.callee = {
type: 'Identifier',
name: '_' + Math.floor(iname).toString(16),
parent: cur
};
traverse(cur.callee, modules[reqid]);
}
if (node.type === 'Identifier') {
var binding = scan.getBinding(node)
if (binding && binding.module) traverse(node, binding.module, binding);
}
}
function traverse (node, val, binding) {
for (var p = node; p; p = p.parent) {
if (p.start === undefined || p.end === undefined) continue;
}
if (node.parent.type === 'CallExpression') {
if (typeof val !== 'function') {
return error(
'tried to statically call ' + inspect(val)
+ ' as a function'
);
}
var xvars = getVars(node.parent, vars);
xvars[node.name] = val;
var res = evaluate(node.parent, xvars);
if (res !== undefined) {
if (binding) binding.remove(node)
updates.push({
start: node.parent.start,
offset: node.parent.end - node.parent.start,
stream: isStream(res) ? wrapStream(res) : st(String(res))
});
}
}
else if (node.parent.type === 'MemberExpression') {
if (!isStaticProperty(node.parent.property)) {
return error(
'dynamic property in member expression: '
+ body.slice(node.parent.start, node.parent.end)
);
}
var cur = node.parent.parent;
if (cur.type === 'MemberExpression') {
cur = cur.parent;
if (cur.type !== 'CallExpression'
&& cur.parent.type === 'CallExpression') {
cur = cur.parent;
}
}
if (node.parent.type === 'MemberExpression'
&& (cur.type !== 'CallExpression'
&& cur.type !== 'MemberExpression')) {
cur = node.parent;
}
var xvars = getVars(cur, vars);
xvars[node.name] = val;
var res = evaluate(cur, xvars);
if (res === undefined && cur.type === 'CallExpression') {
// static-eval can't safely evaluate code with callbacks, so do it manually in a safe way
var callee = evaluate(cur.callee, xvars);
var args = cur.arguments.map(function (arg) {
// Return a function stub for callbacks so that `static-module` users
// can do `callback.toString()` and get the original source
if (arg.type === 'FunctionExpression' || arg.type === 'ArrowFunctionExpression') {
var fn = function () {
throw new Error('static-module: cannot call callbacks defined inside source code');
};
fn.toString = function () {
return body.slice(arg.start, arg.end);
};
return fn;
}
return evaluate(arg, xvars);
});
if (callee !== undefined) {
try {
res = callee.apply(null, args);
} catch (err) {
// Evaluate to undefined
}
}
}
if (res !== undefined) {
if (binding) binding.remove(node)
updates.push({
start: cur.start,
offset: cur.end - cur.start,
stream: isStream(res) ? wrapStream(res) : st(String(res))
});
}
}
else if (node.parent.type === 'UnaryExpression') {
var xvars = getVars(node.parent, vars);
xvars[node.name] = val;
var res = evaluate(node.parent, xvars);
if (res !== undefined) {
if (binding) binding.remove(node)
updates.push({
start: node.parent.start,
offset: node.parent.end - node.parent.start,
stream: isStream(res) ? wrapStream(res) : st(String(res))
});
} else {
output.emit('error', new Error(
'unsupported unary operator: ' + node.parent.operator
));
}
}
else {
output.emit('error', new Error(
'unsupported type for static module: ' + node.parent.type
+ '\nat expression:\n\n ' + unparse(node.parent) + '\n'
));
}
}
}
function isRequire (node) {
var c = node.callee;
return c
&& node.type === 'CallExpression'
&& c.type === 'Identifier'
&& c.name === 'require'
;
}
function isStream (s) {
return s && typeof s === 'object' && typeof s.pipe === 'function';
}
function wrapStream (s) {
if (typeof s.read === 'function') return s
else return (new Readable).wrap(s)
}
function isStaticProperty(node) {
return node.type === 'Identifier' || node.type === 'Literal';
}
function resolveProperty(node) {
return node.type === 'Identifier' ? node.name : node.value;
}
function st (msg) {
var r = new Readable;
r._read = function () {};
if (msg != null) r.push(msg);
r.push(null);
return r;
}
function nearestScope(node) {
do {
var scope = scan.scope(node);
if (scope) return scope;
} while ((node = node.parent));
}
function getVars(node, vars) {
var xvars = copy(vars || {});
var scope = nearestScope(node);
if (scope) {
scope.forEachAvailable(function (binding, name) {
if (binding.hasOwnProperty('value')) xvars[name] = binding.value;
});
}
return xvars;
}