Skip to content

Commit

Permalink
reorganized
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Aug 1, 2014
1 parent a63a03d commit ca6b446
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
28 changes: 1 addition & 27 deletions index.js
@@ -1,6 +1,6 @@
var unpack = require('browser-unpack');
var pack = require('browser-pack');
var falafel = require('falafel');
var replace = require('./lib/replace.js');

module.exports = function (src) {
var rows = unpack(src);
Expand All @@ -13,29 +13,3 @@ module.exports = function (src) {
p.end();
return p;
};

function replace (src, deps) {
return falafel(src, function (node) {
if (isRequire(node)) {
var value = node.arguments[0].value;
if (has(deps, value)) {
node.update('require(' + deps[value] + ')');
}
}
}).toString();
}

function isRequire (node) {
var c = node.callee;
return c
&& node.type === 'CallExpression'
&& c.type === 'Identifier'
&& c.name === 'require'
&& node.arguments[0]
&& node.arguments[0].type === 'Literal'
;
}

function has (obj, key) {
return Object.prototype.hasOwnProperty.call(obj, key);
}
28 changes: 28 additions & 0 deletions lib/replace.js
@@ -0,0 +1,28 @@
var falafel = require('falafel');
module.exports = replace;

function replace (src, deps) {
return falafel(src, function (node) {
if (isRequire(node)) {
var value = node.arguments[0].value;
if (has(deps, value)) {
node.update('require(' + deps[value] + ')');
}
}
}).toString();
}

function isRequire (node) {
var c = node.callee;
return c
&& node.type === 'CallExpression'
&& c.type === 'Identifier'
&& c.name === 'require'
&& node.arguments[0]
&& node.arguments[0].type === 'Literal'
;
}

function has (obj, key) {
return Object.prototype.hasOwnProperty.call(obj, key);
}

0 comments on commit ca6b446

Please sign in to comment.