Skip to content

Commit

Permalink
Recursively check for undefined variables and skip inlining if they e…
Browse files Browse the repository at this point in the history
…xist
  • Loading branch information
Tim Beyer committed Aug 5, 2013
1 parent b2da1f1 commit bf45b3a
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 14 deletions.
47 changes: 33 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var through = require('through');
var falafel = require('falafel');
var unparse = require('escodegen').generate;


module.exports = function (file) {
if (/\.json$/.test(file)) return through();
var data = '';
Expand All @@ -15,7 +16,22 @@ module.exports = function (file) {

var tr = through(write, end);
return tr;


function containsUndefinedVariable (node) {
if (node.type === 'Identifier') {
if (vars.indexOf(node.name) === -1) {
return true;
}
}
else if (node.type === 'BinaryExpression') {
return containsUndefinedVariable(node.left) || containsUndefinedVariable(node.right);
}
else {
return false;
}
};


function write (buf) { data += buf }
function end () {
try { var output = parse() }
Expand Down Expand Up @@ -51,20 +67,23 @@ module.exports = function (file) {
&& fsNames[node.callee.object.name]
&& node.callee.property.type === 'Identifier'
&& node.callee.property.name === 'readFileSync') {

var args = node.arguments;
var t = 'return ' + unparse(args[0]);
var fpath = Function(vars, t)(file, dirname);
var enc = args[1]
? Function('return ' + unparse(args[1]))()
: 'utf8'
;
++ pending;
fs.readFile(fpath, enc, function (err, src) {
if (err) return tr.emit('error', err);
node.update(JSON.stringify(src));
if (--pending === 0) finish(output);
});
var canBeInlined = !containsUndefinedVariable(args[0]);

if (canBeInlined) {
var t = 'return ' + unparse(args[0]);
var fpath = Function(vars, t)(file, dirname);
var enc = args[1]
? Function('return ' + unparse(args[1]))()
: 'utf8'
;
++ pending;
fs.readFile(fpath, enc, function (err, src) {
if (err) return tr.emit('error', err);
node.update(JSON.stringify(src));
if (--pending === 0) finish(output);
});
}
}
});
return output;
Expand Down
17 changes: 17 additions & 0 deletions test/dynamic_read_concat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var test = require('tap').test;
var browserify = require('browserify');
var path = require('path');

test('dynamically loaded file gets skipped', function (t) {
t.plan(1);

var b = browserify();
b.add(__dirname + '/files/dynamic_read_concat');
b.transform(path.dirname(__dirname));

b.bundle(function (err, src) {
if (err) t.fail(err);
else t.ok(true, 'build success');
});

});
17 changes: 17 additions & 0 deletions test/dynamic_read_no_concat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var test = require('tap').test;
var browserify = require('browserify');
var path = require('path');

test('dynamically loaded file gets skipped', function (t) {
t.plan(1);

var b = browserify();
b.add(__dirname + '/files/dynamic_read_no_concat.js');
b.transform(path.dirname(__dirname));

b.bundle(function (err, src) {
if (err) t.fail(err);
else t.ok(true, 'build success');
});

});
8 changes: 8 additions & 0 deletions test/files/dynamic_read_concat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var fs,
path,
dynamicallyCreatedFilename;
fs = require('fs');
path = require('path');

dynamicallyCreatedFilename = path.join('/files/', 'somefile');
var stuff = fs.readFileSync(__dirname + dynamicallyCreatedFilename + __dirname);
8 changes: 8 additions & 0 deletions test/files/dynamic_read_no_concat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var fs,
path,
dynamicallyCreatedFilename;
fs = require('fs');
path = require('path');

dynamicallyCreatedFilename = path.join('/files/', 'somefile');
var stuff = fs.readFileSync(dynamicallyCreatedFilename);

0 comments on commit bf45b3a

Please sign in to comment.