From b233840cfd6928cadfa101c13507cd9de0c91d26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9e=20Kooi?= Date: Thu, 8 Feb 2018 09:47:57 +0100 Subject: [PATCH] Move `bare` and `node` options into API. --- bin/args.js | 16 +++------------- index.js | 17 +++++++++++++++++ readme.markdown | 6 ++++++ test/bare.js | 27 +++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 13 deletions(-) diff --git a/bin/args.js b/bin/args.js index 6aa07567b..130ace75b 100644 --- a/bin/args.js +++ b/bin/args.js @@ -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(','); @@ -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) != '.') { diff --git a/index.js b/index.js index 60fea5adc..b8bf81d25 100644 --- a/index.js +++ b/index.js @@ -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; diff --git a/readme.markdown b/readme.markdown index a9c79f9d7..7c97a847b 100644 --- a/readme.markdown +++ b/readme.markdown @@ -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. diff --git a/test/bare.js b/test/bare.js index 340e4f949..2526e97c7 100644 --- a/test/bare.js +++ b/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'}); @@ -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);