Skip to content

Commit bb2a99d

Browse files
committedSep 24, 2015
Merge pull request #1361 from substack/fix-bare
global var insertion fixes
2 parents e781d9d + cf32d77 commit bb2a99d

File tree

5 files changed

+52
-13
lines changed

5 files changed

+52
-13
lines changed
 

‎bin/args.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,21 @@ module.exports = function (args, opts) {
7171
if (argv.bare) {
7272
argv.builtins = false;
7373
argv.commondir = false;
74-
argv.detectGlobals = false;
7574
if (argv.igv === undefined) {
7675
argv.igv = '__filename,__dirname';
7776
}
7877
}
79-
78+
79+
if (argv.igv) {
80+
var insertGlobalVars = {};
81+
var wantedGlobalVars = argv.igv.split(',');
82+
Object.keys(insertGlobals.vars).forEach(function (x) {
83+
if (wantedGlobalVars.indexOf(x) === -1) {
84+
insertGlobalVars[x] = undefined;
85+
}
86+
});
87+
}
88+
8089
var ignoreTransform = argv['ignore-transform'] || argv.it;
8190
var b = browserify(xtend({
8291
noParse: Array.isArray(argv.noParse) ? argv.noParse : [argv.noParse],
@@ -240,14 +249,6 @@ module.exports = function (args, opts) {
240249
return b;
241250
}
242251

243-
var insertGlobalVars;
244-
if (argv.igv) {
245-
insertGlobalVars = argv.igv.split(',').reduce(function (vars, x) {
246-
vars[x] = insertGlobals.vars[x];
247-
return vars;
248-
}, {});
249-
}
250-
251252
return b;
252253
};
253254

‎index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -552,8 +552,8 @@ Browserify.prototype._createDeps = function (opts) {
552552
}, opts.insertGlobalVars);
553553

554554
if (opts.bundleExternal === false) {
555-
delete vars.process;
556-
delete vars.buffer;
555+
vars.process = undefined;
556+
vars.buffer = undefined;
557557
}
558558

559559
return insertGlobals(file, xtend(opts, {

‎test/bare.js

+31
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,34 @@ test('bare', function (t) {
3737
t.equal(code, 0);
3838
});
3939
});
40+
41+
test('bare inserts __filename,__dirname but not process,global,Buffer', function (t) {
42+
t.plan(2);
43+
44+
var ps = spawn(process.execPath, [
45+
path.resolve(__dirname, '../bin/cmd.js'),
46+
path.resolve(__dirname, 'bare/main.js'),
47+
'--bare'
48+
]);
49+
50+
ps.stdout.pipe(concat(function (body) {
51+
vm.runInNewContext(body, {
52+
console: {
53+
log: function (msg) {
54+
t.same(msg, [
55+
path.join(__dirname, 'bare'),
56+
path.join(__dirname, 'bare/main.js'),
57+
'undefined',
58+
'undefined',
59+
'undefined'
60+
]);
61+
}
62+
}
63+
});
64+
}));
65+
ps.stdin.end();
66+
67+
ps.on('exit', function (code) {
68+
t.equal(code, 0);
69+
});
70+
});

‎test/bare/main.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
console.log([
2+
__dirname,
3+
__filename,
4+
typeof process,
5+
typeof global,
6+
typeof Buffer
7+
]);

‎test/bundle_external_global.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ test('bundle external global', function (t) {
1818
process: process
1919
});
2020
function log (msg) {
21-
t.equal(typeof msg.nextTick, 'function');
21+
t.equal(msg, process);
2222
}
2323
});
2424
});

0 commit comments

Comments
 (0)
Please sign in to comment.