| 1 |
var fs = require('fs'); |
| 2 |
var path = require('path'); |
| 3 |
|
| 4 |
var through = require('through2'); |
| 5 |
var Readable = require('readable-stream').Readable; |
| 6 |
|
| 7 |
var concat = require('concat-stream'); |
| 8 |
var duplexer = require('duplexer2'); |
| 9 |
var falafel = require('falafel'); |
| 10 |
var unparse = require('escodegen').generate; |
| 11 |
var inspect = require('object-inspect'); |
| 12 |
var evaluate = require('static-eval'); |
| 13 |
var copy = require('shallow-copy'); |
| 14 |
var has = require('has'); |
| 15 |
var MagicString = require('magic-string'); |
| 16 |
var convertSourceMap = require('convert-source-map'); |
| 17 |
var mergeSourceMap = require('merge-source-map'); |
| 18 |
|
| 19 |
module.exports = function parse (modules, opts) { |
| 20 |
if (!opts) opts = {}; |
| 21 |
var vars = opts.vars || {}; |
| 22 |
var varNames = opts.varNames || {}; |
| 23 |
var varModules = opts.varModules || {}; |
| 24 |
var skip = opts.skip || {}; |
| 25 |
var skipOffset = opts.skipOffset || 0; |
| 26 |
var parserOpts = opts.parserOpts || { ecmaVersion: 8 }; |
| 27 |
var updates = []; |
| 28 |
var sourcemapper; |
| 29 |
var inputMap; |
| 30 |
|
| 31 |
var output = through(); |
| 32 |
var body; |
| 33 |
return duplexer(concat({ encoding: 'buffer' }, function (buf) { |
| 34 |
try { |
| 35 |
body = buf.toString('utf8').replace(/^#!/, '//#!'); |
| 36 |
var matches = false; |
| 37 |
for (var key in modules) { |
| 38 |
if (body.indexOf(key) !== -1) { |
| 39 |
matches = true; |
| 40 |
break; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
if (!matches) { |
| 45 |
// just pass it through |
| 46 |
output.end(buf); |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
if (opts.sourceMap) { |
| 51 |
inputMap = convertSourceMap.fromSource(body); |
| 52 |
if (inputMap) inputMap = inputMap.toObject(); |
| 53 |
body = convertSourceMap.removeComments(body); |
| 54 |
sourcemapper = new MagicString(body); |
| 55 |
} |
| 56 |
|
| 57 |
falafel(body, parserOpts, walk); |
| 58 |
} |
| 59 |
catch (err) { return error(err) } |
| 60 |
finish(body); |
| 61 |
}), output); |
| 62 |
|
| 63 |
function finish (src) { |
| 64 |
var pos = 0; |
| 65 |
src = String(src); |
| 66 |
|
| 67 |
(function next () { |
| 68 |
if (updates.length === 0) return done(); |
| 69 |
var s = updates.shift(); |
| 70 |
|
| 71 |
output.write(src.slice(pos, s.start)); |
| 72 |
pos = s.start + s.offset; |
| 73 |
|
| 74 |
s.stream.pipe(output, { end: false }); |
| 75 |
if (opts.sourceMap) { |
| 76 |
s.stream.pipe(concat({ encoding: 'string' }, function (chunk) { |
| 77 |
// We have to give magic-string the replacement string, |
| 78 |
// so it can calculate the amount of lines and columns. |
| 79 |
if (s.offset === 0) { |
| 80 |
sourcemapper.appendRight(s.start, chunk); |
| 81 |
} else { |
| 82 |
sourcemapper.overwrite(s.start, s.start + s.offset, chunk); |
| 83 |
} |
| 84 |
})).on('finish', next); |
| 85 |
} else { |
| 86 |
s.stream.on('end', next); |
| 87 |
} |
| 88 |
})(); |
| 89 |
|
| 90 |
function done () { |
| 91 |
output.write(src.slice(pos)); |
| 92 |
if (opts.sourceMap) { |
| 93 |
var map = sourcemapper.generateMap({ |
| 94 |
source: opts.inputFilename || 'input.js', |
| 95 |
includeContent: true |
| 96 |
}); |
| 97 |
if (inputMap) { |
| 98 |
var merged = mergeSourceMap(inputMap, map); |
| 99 |
output.write('\n' + convertSourceMap.fromObject(merged).toComment() + '\n'); |
| 100 |
} else { |
| 101 |
output.write('\n//# sourceMappingURL=' + map.toUrl() + '\n'); |
| 102 |
} |
| 103 |
} |
| 104 |
output.end(); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
function error (msg) { |
| 109 |
var err = typeof msg === 'string' ? new Error(msg) : msg; |
| 110 |
output.emit('error', err); |
| 111 |
} |
| 112 |
|
| 113 |
function walk (node) { |
| 114 |
if (opts.sourceMap) { |
| 115 |
sourcemapper.addSourcemapLocation(node.start); |
| 116 |
sourcemapper.addSourcemapLocation(node.end); |
| 117 |
} |
| 118 |
|
| 119 |
var isreq = isRequire(node); |
| 120 |
var isreqm = false, isreqv = false, reqid; |
| 121 |
if (isreq) { |
| 122 |
reqid = node.arguments[0].value; |
| 123 |
isreqm = has(modules, reqid); |
| 124 |
isreqv = has(varModules, reqid); |
| 125 |
} |
| 126 |
|
| 127 |
if (isreqv && node.parent.type === 'VariableDeclarator' |
| 128 |
&& node.parent.id.type === 'Identifier') { |
| 129 |
vars[node.parent.id.name] = varModules[reqid]; |
| 130 |
} |
| 131 |
else if (isreqv && node.parent.type === 'AssignmentExpression' |
| 132 |
&& node.parent.left.type === 'Identifier') { |
| 133 |
vars[node.parent.left.name] = varModules[reqid]; |
| 134 |
} |
| 135 |
else if (isreqv && node.parent.type === 'MemberExpression' |
| 136 |
&& isStaticProperty(node.parent.property) |
| 137 |
&& node.parent.parent.type === 'VariableDeclarator' |
| 138 |
&& node.parent.parent.id.type === 'Identifier') { |
| 139 |
var v = varModules[reqid][resolveProperty(node.parent.property)]; |
| 140 |
vars[node.parent.parent.id.name] = v; |
| 141 |
} |
| 142 |
else if (isreqv && node.parent.type === 'MemberExpression' |
| 143 |
&& node.parent.property.type === 'Identifier') { |
| 144 |
//vars[node.parent.parent.id.name] = varModules[reqid]; |
| 145 |
} |
| 146 |
else if (isreqv && node.parent.type === 'CallExpression') { |
| 147 |
// |
| 148 |
} |
| 149 |
|
| 150 |
if (isreqm && node.parent.type === 'VariableDeclarator' |
| 151 |
&& node.parent.id.type === 'Identifier') { |
| 152 |
varNames[node.parent.id.name] = reqid; |
| 153 |
var decs = node.parent.parent.declarations; |
| 154 |
var ix = decs.indexOf(node.parent); |
| 155 |
var dec; |
| 156 |
if (ix >= 0) { |
| 157 |
dec = decs[ix]; |
| 158 |
decs.splice(ix, 1); |
| 159 |
} |
| 160 |
|
| 161 |
if (decs.length) { |
| 162 |
var src = unparse(node.parent.parent); |
| 163 |
updates.push({ |
| 164 |
start: node.parent.parent.start, |
| 165 |
offset: node.parent.parent.end - node.parent.parent.start, |
| 166 |
stream: st('var ') |
| 167 |
}); |
| 168 |
decs.forEach(function (d, i) { |
| 169 |
var key = (d.start + skipOffset) |
| 170 |
+ ',' + (d.end + skipOffset) |
| 171 |
; |
| 172 |
skip[key] = true; |
| 173 |
|
| 174 |
var s = parse(modules, { |
| 175 |
skip: skip, |
| 176 |
skipOffset: skipOffset + (d.init ? d.init.start : 0), |
| 177 |
vars: vars, |
| 178 |
varNames: varNames |
| 179 |
}); |
| 180 |
var up = { |
| 181 |
start: node.parent.parent.end, |
| 182 |
offset: 0, |
| 183 |
stream: s |
| 184 |
}; |
| 185 |
updates.push(up); |
| 186 |
if (i < decs.length - 1) { |
| 187 |
var comma; |
| 188 |
if (i === ix - 1) { |
| 189 |
comma = body.slice(d.end, dec.start); |
| 190 |
} |
| 191 |
else comma = body.slice(d.end, decs[i+1].start); |
| 192 |
updates.push({ |
| 193 |
start: node.parent.parent.end, |
| 194 |
offset: 0, |
| 195 |
stream: st(comma) |
| 196 |
}); |
| 197 |
} |
| 198 |
else { |
| 199 |
updates.push({ |
| 200 |
start: node.parent.parent.end, |
| 201 |
offset: 0, |
| 202 |
stream: st(';') |
| 203 |
}); |
| 204 |
} |
| 205 |
s.end(unparse(d)); |
| 206 |
}); |
| 207 |
} |
| 208 |
else { |
| 209 |
updates.push({ |
| 210 |
start: node.parent.parent.start, |
| 211 |
offset: node.parent.parent.end - node.parent.parent.start, |
| 212 |
stream: st() |
| 213 |
}); |
| 214 |
} |
| 215 |
} |
| 216 |
else if (isreqm && node.parent.type === 'AssignmentExpression' |
| 217 |
&& node.parent.left.type === 'Identifier') { |
| 218 |
varNames[node.parent.left.name] = reqid; |
| 219 |
var cur = node.parent.parent; |
| 220 |
if (cur.type === 'SequenceExpression') { |
| 221 |
var ex = cur.expressions; |
| 222 |
var ix = ex.indexOf(node.parent); |
| 223 |
if (ix >= 0) ex.splice(ix, 1); |
| 224 |
updates.push({ |
| 225 |
start: node.parent.parent.start, |
| 226 |
offset: node.parent.parent.end - node.parent.parent.start, |
| 227 |
stream: st(unparse(node.parent.parent)) |
| 228 |
}); |
| 229 |
} |
| 230 |
else { |
| 231 |
updates.push({ |
| 232 |
start: node.parent.parent.start, |
| 233 |
offset: node.parent.parent.end - node.parent.parent.start, |
| 234 |
stream: st() |
| 235 |
}); |
| 236 |
} |
| 237 |
} |
| 238 |
else if (isreqm && node.parent.type === 'MemberExpression' |
| 239 |
&& isStaticProperty(node.parent.property) |
| 240 |
&& node.parent.parent.type === 'VariableDeclarator' |
| 241 |
&& node.parent.parent.id.type === 'Identifier') { |
| 242 |
varNames[node.parent.parent.id.name] = [ |
| 243 |
reqid, resolveProperty(node.parent.property) |
| 244 |
]; |
| 245 |
var decNode = node.parent.parent.parent; |
| 246 |
var decs = decNode.declarations; |
| 247 |
var ix = decs.indexOf(node.parent.parent); |
| 248 |
if (ix >= 0) decs.splice(ix, 1); |
| 249 |
|
| 250 |
updates.push({ |
| 251 |
start: decNode.start, |
| 252 |
offset: decNode.end - decNode.start, |
| 253 |
stream: decs.length ? st(unparse(decNode)) : st() |
| 254 |
}); |
| 255 |
} |
| 256 |
else if (isreqm && node.parent.type === 'MemberExpression' |
| 257 |
&& isStaticProperty(node.parent.property)) { |
| 258 |
var name = resolveProperty(node.parent.property); |
| 259 |
var cur = copy(node.parent.parent); |
| 260 |
cur.callee = copy(node.parent.property); |
| 261 |
cur.callee.parent = cur; |
| 262 |
traverse(cur.callee, modules[reqid][name]); |
| 263 |
} |
| 264 |
else if (isreqm && node.parent.type === 'CallExpression') { |
| 265 |
var cur = copy(node.parent); |
| 266 |
var iname = Math.pow(16,8) * Math.random(); |
| 267 |
cur.callee = { |
| 268 |
type: 'Identifier', |
| 269 |
name: '_' + Math.floor(iname).toString(16), |
| 270 |
parent: cur |
| 271 |
}; |
| 272 |
traverse(cur.callee, modules[reqid]); |
| 273 |
} |
| 274 |
|
| 275 |
if (node.type === 'Identifier' && has(varNames, node.name)) { |
| 276 |
var vn = varNames[node.name]; |
| 277 |
if (Array.isArray(vn)) { |
| 278 |
traverse(node, modules[vn[0]][vn[1]]); |
| 279 |
} |
| 280 |
else traverse(node, modules[vn]); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
function traverse (node, val) { |
| 285 |
for (var p = node; p; p = p.parent) { |
| 286 |
if (p.start === undefined || p.end === undefined) continue; |
| 287 |
var key = (p.start + skipOffset) |
| 288 |
+ ',' + (p.end + skipOffset) |
| 289 |
; |
| 290 |
if (skip[key]) { |
| 291 |
skip[key] = false; |
| 292 |
return; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
if (skip[key]) { |
| 297 |
skip[key] = false; |
| 298 |
return; |
| 299 |
} |
| 300 |
|
| 301 |
if (node.parent.type === 'CallExpression') { |
| 302 |
if (typeof val !== 'function') { |
| 303 |
return error( |
| 304 |
'tried to statically call ' + inspect(val) |
| 305 |
+ ' as a function' |
| 306 |
); |
| 307 |
} |
| 308 |
|
| 309 |
var xvars = copy(vars); |
| 310 |
xvars[node.name] = val; |
| 311 |
|
| 312 |
var res = evaluate(node.parent, xvars); |
| 313 |
if (res !== undefined) { |
| 314 |
updates.push({ |
| 315 |
start: node.parent.start, |
| 316 |
offset: node.parent.end - node.parent.start, |
| 317 |
stream: isStream(res) ? wrapStream(res) : st(String(res)) |
| 318 |
}); |
| 319 |
} |
| 320 |
} |
| 321 |
else if (node.parent.type === 'MemberExpression') { |
| 322 |
if (!isStaticProperty(node.parent.property)) { |
| 323 |
return error( |
| 324 |
'dynamic property in member expression: ' |
| 325 |
+ node.parent.source() |
| 326 |
); |
| 327 |
} |
| 328 |
|
| 329 |
var cur = node.parent.parent; |
| 330 |
|
| 331 |
if (cur.type === 'MemberExpression') { |
| 332 |
cur = cur.parent; |
| 333 |
if (cur.type !== 'CallExpression' |
| 334 |
&& cur.parent.type === 'CallExpression') { |
| 335 |
cur = cur.parent; |
| 336 |
} |
| 337 |
} |
| 338 |
if (node.parent.type === 'MemberExpression' |
| 339 |
&& (cur.type !== 'CallExpression' |
| 340 |
&& cur.type !== 'MemberExpression')) { |
| 341 |
cur = node.parent; |
| 342 |
} |
| 343 |
|
| 344 |
var xvars = copy(vars); |
| 345 |
xvars[node.name] = val; |
| 346 |
|
| 347 |
var res = evaluate(cur, xvars); |
| 348 |
if (res === undefined && cur.type === 'CallExpression') { |
| 349 |
// static-eval can't safely evaluate code with callbacks, so do it manually in a safe way |
| 350 |
var callee = evaluate(cur.callee, xvars); |
| 351 |
var args = cur.arguments.map(function (arg) { |
| 352 |
// Return a function stub for callbacks so that `static-module` users |
| 353 |
// can do `callback.toString()` and get the original source |
| 354 |
if (arg.type === 'FunctionExpression' || arg.type === 'ArrowFunctionExpression') { |
| 355 |
var fn = function () { |
| 356 |
throw new Error('static-module: cannot call callbacks defined inside source code'); |
| 357 |
}; |
| 358 |
fn.toString = function () { |
| 359 |
return body.slice(arg.start, arg.end); |
| 360 |
}; |
| 361 |
return fn; |
| 362 |
} |
| 363 |
return evaluate(arg, xvars); |
| 364 |
}); |
| 365 |
|
| 366 |
if (callee !== undefined) { |
| 367 |
try { |
| 368 |
res = callee.apply(null, args); |
| 369 |
} catch (err) { |
| 370 |
// Evaluate to undefined |
| 371 |
} |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
if (res !== undefined) { |
| 376 |
updates.push({ |
| 377 |
start: cur.start, |
| 378 |
offset: cur.end - cur.start, |
| 379 |
stream: isStream(res) ? wrapStream(res) : st(String(res)) |
| 380 |
}); |
| 381 |
} |
| 382 |
} |
| 383 |
else if (node.parent.type === 'UnaryExpression') { |
| 384 |
var xvars = copy(vars); |
| 385 |
xvars[node.name] = val; |
| 386 |
|
| 387 |
var res = evaluate(node.parent, xvars); |
| 388 |
if (res !== undefined) { |
| 389 |
updates.push({ |
| 390 |
start: node.parent.start, |
| 391 |
offset: node.parent.end - node.parent.start, |
| 392 |
stream: isStream(res) ? wrapStream(res) : st(String(res)) |
| 393 |
}); |
| 394 |
} else { |
| 395 |
output.emit('error', new Error( |
| 396 |
'unsupported unary operator: ' + node.parent.operator |
| 397 |
)); |
| 398 |
} |
| 399 |
} |
| 400 |
else { |
| 401 |
output.emit('error', new Error( |
| 402 |
'unsupported type for static module: ' + node.parent.type |
| 403 |
+ '\nat expression:\n\n ' + unparse(node.parent) + '\n' |
| 404 |
)); |
| 405 |
} |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
function isRequire (node) { |
| 410 |
var c = node.callee; |
| 411 |
return c |
| 412 |
&& node.type === 'CallExpression' |
| 413 |
&& c.type === 'Identifier' |
| 414 |
&& c.name === 'require' |
| 415 |
; |
| 416 |
} |
| 417 |
|
| 418 |
function isStream (s) { |
| 419 |
return s && typeof s === 'object' && typeof s.pipe === 'function'; |
| 420 |
} |
| 421 |
|
| 422 |
function wrapStream (s) { |
| 423 |
if (typeof s.read === 'function') return s |
| 424 |
else return (new Readable).wrap(s) |
| 425 |
} |
| 426 |
|
| 427 |
function isStaticProperty(node) { |
| 428 |
return node.type === 'Identifier' || node.type === 'Literal'; |
| 429 |
} |
| 430 |
|
| 431 |
function resolveProperty(node) { |
| 432 |
return node.type === 'Identifier' ? node.name : node.value; |
| 433 |
} |
| 434 |
|
| 435 |
function st (msg) { |
| 436 |
var r = new Readable; |
| 437 |
r._read = function () {}; |
| 438 |
if (msg != null) r.push(msg); |
| 439 |
r.push(null); |
| 440 |
return r; |
| 441 |
} |