Skip to content

Commit

Permalink
Merge pull request #1361 from substack/fix-bare
Browse files Browse the repository at this point in the history
global var insertion fixes
  • Loading branch information
zertosh committed Sep 24, 2015
2 parents e781d9d + cf32d77 commit bb2a99d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 13 deletions.
21 changes: 11 additions & 10 deletions bin/args.js
Expand Up @@ -71,12 +71,21 @@ module.exports = function (args, opts) {
if (argv.bare) {
argv.builtins = false;
argv.commondir = false;
argv.detectGlobals = false;
if (argv.igv === undefined) {
argv.igv = '__filename,__dirname';
}
}


if (argv.igv) {
var insertGlobalVars = {};
var wantedGlobalVars = argv.igv.split(',');
Object.keys(insertGlobals.vars).forEach(function (x) {
if (wantedGlobalVars.indexOf(x) === -1) {
insertGlobalVars[x] = undefined;
}
});
}

var ignoreTransform = argv['ignore-transform'] || argv.it;
var b = browserify(xtend({
noParse: Array.isArray(argv.noParse) ? argv.noParse : [argv.noParse],
Expand Down Expand Up @@ -240,14 +249,6 @@ module.exports = function (args, opts) {
return b;
}

var insertGlobalVars;
if (argv.igv) {
insertGlobalVars = argv.igv.split(',').reduce(function (vars, x) {
vars[x] = insertGlobals.vars[x];
return vars;
}, {});
}

return b;
};

Expand Down
4 changes: 2 additions & 2 deletions index.js
Expand Up @@ -552,8 +552,8 @@ Browserify.prototype._createDeps = function (opts) {
}, opts.insertGlobalVars);

if (opts.bundleExternal === false) {
delete vars.process;
delete vars.buffer;
vars.process = undefined;
vars.buffer = undefined;
}

return insertGlobals(file, xtend(opts, {
Expand Down
31 changes: 31 additions & 0 deletions test/bare.js
Expand Up @@ -37,3 +37,34 @@ test('bare', function (t) {
t.equal(code, 0);
});
});

test('bare inserts __filename,__dirname but not process,global,Buffer', function (t) {
t.plan(2);

var ps = spawn(process.execPath, [
path.resolve(__dirname, '../bin/cmd.js'),
path.resolve(__dirname, 'bare/main.js'),
'--bare'
]);

ps.stdout.pipe(concat(function (body) {
vm.runInNewContext(body, {
console: {
log: function (msg) {
t.same(msg, [
path.join(__dirname, 'bare'),
path.join(__dirname, 'bare/main.js'),
'undefined',
'undefined',
'undefined'
]);
}
}
});
}));
ps.stdin.end();

ps.on('exit', function (code) {
t.equal(code, 0);
});
});
7 changes: 7 additions & 0 deletions test/bare/main.js
@@ -0,0 +1,7 @@
console.log([
__dirname,
__filename,
typeof process,
typeof global,
typeof Buffer
]);
2 changes: 1 addition & 1 deletion test/bundle_external_global.js
Expand Up @@ -18,7 +18,7 @@ test('bundle external global', function (t) {
process: process
});
function log (msg) {
t.equal(typeof msg.nextTick, 'function');
t.equal(msg, process);
}
});
});

0 comments on commit bb2a99d

Please sign in to comment.