Skip to content

Commit

Permalink
Move bare and node options into API.
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop committed Feb 8, 2018
1 parent 38141f1 commit b233840
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 13 deletions.
16 changes: 3 additions & 13 deletions bin/args.js
Expand Up @@ -64,19 +64,7 @@ module.exports = function (args, opts) {
}
return entry;
});

if (argv.node) {
argv.bare = true;
argv.browserField = false;
}
if (argv.bare) {
argv.builtins = false;
argv.commondir = false;
if (argv.igv === undefined) {
argv.igv = '__filename,__dirname';
}
}


if (argv.igv) {
var insertGlobalVars = {};
var wantedGlobalVars = argv.igv.split(',');
Expand All @@ -89,6 +77,8 @@ module.exports = function (args, opts) {

var ignoreTransform = argv['ignore-transform'] || argv.it;
var b = browserify(xtend({
node: argv.node,
bare: argv.bare,
noParse: Array.isArray(argv.noParse) ? argv.noParse : [argv.noParse],
extensions: [].concat(argv.extension).filter(Boolean).map(function (extension) {
if (extension.charAt(0) != '.') {
Expand Down
17 changes: 17 additions & 0 deletions index.js
Expand Up @@ -44,6 +44,23 @@ function Browserify (files, opts) {
opts = xtend(opts, { entries: [].concat(opts.entries || [], files) });
}
else opts = xtend(files, opts);

if (opts.node) {
opts.bare = true;
opts.browserField = false;
}
if (opts.bare) {
opts.builtins = false;
opts.commondir = false;
if (opts.insertGlobalVars === undefined) {
opts.insertGlobalVars = {}
Object.keys(insertGlobals.vars).forEach(function (name) {
if (name !== '__dirname' && name !== '__filename') {
opts.insertGlobalVars[name] = undefined;
}
})
}
}

self._options = opts;
if (opts.noparse) opts.noParse = opts.noparse;
Expand Down
6 changes: 6 additions & 0 deletions readme.markdown
Expand Up @@ -487,6 +487,12 @@ as the `opts.vars` parameter.
`opts.externalRequireName` defaults to `'require'` in `expose` mode but you can
use another name.

`opts.bare` creates a bundle that does not include Node builtins, and does not
replace global Node variables except for `__dirname` and `__filename`.

`opts.node` creates a bundle that runs in Node and does not use the browser
versions of dependencies. Same as passing `{ bare: true, browserField: false }`.

Note that if files do not contain javascript source code then you also need to
specify a corresponding transform for them.

Expand Down
27 changes: 27 additions & 0 deletions test/bare.js
@@ -1,9 +1,11 @@
var test = require('tap').test;
var spawn = require('child_process').spawn;
var browserify = require('../');
var path = require('path');
var concat = require('concat-stream');
var vm = require('vm');
var fs = require('fs');
var through = require('through2');
var temp = require('temp');
temp.track();
var tmpdir = temp.mkdirSync({prefix: 'browserify-test'});
Expand Down Expand Up @@ -42,6 +44,31 @@ test('bare', function (t) {
});
});

test('bare api', function (t) {
t.plan(3);

var input = through();
var b = browserify(input, { bare: true });
b.bundle().pipe(concat(function (body) {
vm.runInNewContext(body, {
Buffer: function (s) { return s.toLowerCase() },
console: {
log: function (msg) { t.equal(msg, 'abc') }
}
});
vm.runInNewContext(body, {
Buffer: Buffer,
console: {
log: function (msg) {
t.ok(Buffer.isBuffer(msg));
t.equal(msg.toString('utf8'), 'ABC')
}
}
});
}));
input.end('console.log(Buffer("ABC"))');
});

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

Expand Down

0 comments on commit b233840

Please sign in to comment.