Skip to content
Permalink
Browse files

Merge pull request #1725 from jviotti/dynamic-dirname-filename

Dynamically calculate __dirname and __filename when --node is passed
  • Loading branch information
goto-bus-stop committed Feb 7, 2018
2 parents b38b709 + 9306a57 commit 2a574e45fb41f57cc5c931cdc1e6a70bc06f8c86
Showing with 103 additions and 6 deletions.
  1. +16 −2 index.js
  2. +77 −1 test/bare.js
  3. +4 −0 test/bare/dirname-filename.js
  4. +6 −3 test/no_builtins.js
@@ -1,3 +1,4 @@
var path = require('path');
var mdeps = require('module-deps');
var depsSort = require('deps-sort');
var bpack = require('browser-pack');
@@ -570,6 +571,19 @@ Browserify.prototype._createDeps = function (opts) {
return through();
}
}

if (opts.commondir === false && opts.builtins === false) {
opts.insertGlobalVars = xtend({
__dirname: function(file, basedir) {
var dir = path.dirname(path.relative(basedir, file));
return 'require("path").join(__dirname,' + dir.split(path.sep).map(JSON.stringify).join(',') + ')';
},
__filename: function(file, basedir) {
var filename = path.relative(basedir, file);
return 'require("path").join(__dirname,' + filename.split(path.sep).map(JSON.stringify).join(',') + ')';
}
}, opts.insertGlobalVars);
}

var vars = xtend({
process: function () { return "require('_process')" },
@@ -579,11 +593,11 @@ Browserify.prototype._createDeps = function (opts) {
vars.process = undefined;
vars.buffer = undefined;
}

return insertGlobals(file, xtend(opts, {
debug: opts.debug,
always: opts.insertGlobals,
basedir: opts.commondir === false
basedir: opts.commondir === false && isArray(opts.builtins)
? '/'
: opts.basedir || process.cwd()
,
@@ -3,6 +3,10 @@ var spawn = require('child_process').spawn;
var path = require('path');
var concat = require('concat-stream');
var vm = require('vm');
var fs = require('fs');
var temp = require('temp');
temp.track();
var tmpdir = temp.mkdirSync({prefix: 'browserify-test'});

test('bare', function (t) {
t.plan(4);
@@ -41,14 +45,17 @@ test('bare', function (t) {
test('bare inserts __filename,__dirname but not process,global,Buffer', function (t) {
t.plan(2);

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

ps.stdout.pipe(concat(function (body) {
vm.runInNewContext(body, {
require: require,
__dirname: process.cwd(),
console: {
log: function (msg) {
t.same(msg, [
@@ -68,3 +75,72 @@ test('bare inserts __filename,__dirname but not process,global,Buffer', function
t.equal(code, 0);
});
});

test('bare inserts dynamic __filename,__dirname', function (t) {
t.plan(2);

var file = path.join(tmpdir, 'dirname-filename.js');

fs.writeFileSync(
file,
fs.readFileSync(path.resolve(__dirname, 'bare/dirname-filename.js'))
);

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

ps.stdout.pipe(concat(function (body) {
vm.runInNewContext(body, {
require: require,
__dirname: path.dirname(file),
console: {
log: function (msg) {
t.same(msg, [
path.dirname(file),
file
]);
}
}
});
}));
ps.stdin.end();

ps.on('exit', function (code) {
t.equal(code, 0);
});
});

test('bare inserts dynamic __filename,__dirname with basedir', function (t) {
t.plan(2);

var file = 'dirname-filename.js';
var ps = spawn(process.execPath, [
path.resolve(__dirname, '../bin/cmd.js'),
file,
'--bare',
'--basedir=' + path.join(__dirname, 'bare')
]);

ps.stdout.pipe(concat(function (body) {
vm.runInNewContext(body, {
require: require,
__dirname: process.cwd(),
console: {
log: function (msg) {
t.same(msg, [
__dirname,
path.join(__dirname, file)
]);
}
}
});
}));
ps.stdin.end();

ps.on('exit', function (code) {
t.equal(code, 0);
});
});
@@ -0,0 +1,4 @@
console.log([
__dirname,
__filename
]);
@@ -1,12 +1,14 @@
var browserify = require('../');
var test = require('tap').test;
var path = require('path');
var vm = require('vm');

test('builtins false', function (t) {
t.plan(1);


var file = __dirname + '/no_builtins/main.js';
var b = browserify({
entries: [ __dirname + '/no_builtins/main.js' ],
entries: [ file ],
commondir: false,
builtins: false
});
@@ -15,7 +17,8 @@ test('builtins false', function (t) {
console: { log: function (msg) {
t.equal(msg, 'beep boop\n');
} },
require: require
require: require,
__dirname: process.cwd()
};
vm.runInNewContext(src, c);
});

0 comments on commit 2a574e4

Please sign in to comment.
You can’t perform that action at this time.