1 | var falafel = require('falafel');
|
2 | module.exports = replace;
|
3 |
|
4 | function replace (src, deps) {
|
5 | var opts = {
|
6 | ecmaVersion: 8,
|
7 | allowReturnOutsideFunction: true
|
8 | };
|
9 | return falafel(src, opts, function (node) {
|
10 | if (isRequire(node)) {
|
11 | var value = node.arguments[0].value;
|
12 | if (has(deps, value) && deps[value]) {
|
13 | node.update('require(' + JSON.stringify(deps[value]) + ')');
|
14 | }
|
15 | }
|
16 | }).toString();
|
17 | }
|
18 |
|
19 | function isRequire (node) {
|
20 | var c = node.callee;
|
21 | return c
|
22 | && node.type === 'CallExpression'
|
23 | && c.type === 'Identifier'
|
24 | && c.name === 'require'
|
25 | && node.arguments[0]
|
26 | && node.arguments[0].type === 'Literal'
|
27 | ;
|
28 | }
|
29 |
|
30 | function has (obj, key) {
|
31 | return Object.prototype.hasOwnProperty.call(obj, key);
|
32 | }
|