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

Move bare and node options into API. #1804

Merged
merged 1 commit into from
Feb 9, 2018
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
16 changes: 3 additions & 13 deletions bin/args.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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