Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

global var insertion fixes #1361

Merged
merged 2 commits into from
Sep 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 11 additions & 10 deletions bin/args.js
Original file line number Diff line number Diff line change
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 @@ -234,14 +243,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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);
}
});
});