Skip to content

Commit

Permalink
add cli support for --transform-key to support mode's like production…
Browse files Browse the repository at this point in the history
…/staging/etc..
  • Loading branch information
serapath committed Apr 4, 2017
1 parent a5aa660 commit d7ff52d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 27 deletions.
28 changes: 18 additions & 10 deletions bin/advanced.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ Advanced Options:

Skip detection and always insert definitions for process, global,
__filename, and __dirname.

benefit: faster builds
cost: extra bytes

--insert-global-vars, --igv

Comma-separated list of global variables to detect and define.
Default: __filename,__dirname,process,Buffer,global

--detect-globals, --dg [default: true]

Detect the presence of process, global, __filename, and __dirname and define
Expand All @@ -36,16 +36,16 @@ Advanced Options:
provides the core builtins.

--no-commondir

Turn off setting a commondir. This is useful if you want to preserve the
original paths that a bundle was generated with.
original paths that a bundle was generated with.

--no-bundle-external

Turn off bundling of all external modules. This is useful if you only want
to bundle your local files.

--bare
--bare

Alias for both --no-builtins, --no-commondir, and sets --insert-global-vars
to just "__filename,__dirname". This is handy if you want to run bundles in
Expand All @@ -56,6 +56,15 @@ Advanced Options:
Turn off package.json browser field resolution. This is also handy if you
need to run a bundle in node.

--transform-key

Instead of the default package.json#browserify#transform field to list
all transforms to apply when running browserify, a custom field, like, e.g.
package.json#browserify#production or package.json#browserify#staging
can be used, by for example running:
* `browserify index.js --transform-key=production > bundle.js`
* `browserify index.js --transform-key=staging > bundle.js`

--node

Alias for --bare and --no-browser-field.
Expand All @@ -66,7 +75,7 @@ Advanced Options:
preserving the original paths that a bundle was generated with.

--deps

Instead of standard bundle output, print the dependency array generated by
module-deps.

Expand All @@ -75,7 +84,7 @@ Advanced Options:
Turn off deduping.

--list

Print each file in the dependency graph. Useful for makefiles.

--extension=EXTENSION
Expand All @@ -101,4 +110,3 @@ Passing arguments to transforms and plugins:
will call the `foo` transform for each applicable file by calling:

foo(file, { x: 3, beep: true })

35 changes: 18 additions & 17 deletions bin/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module.exports = function (args, opts) {
node: false
}
});

var entries = argv._.concat(argv.entry)
.filter(Boolean).map(function (entry) {
if (entry === '-') {
Expand All @@ -64,7 +64,7 @@ module.exports = function (args, opts) {
}
return entry;
});

if (argv.node) {
argv.bare = true;
argv.browserField = false;
Expand All @@ -76,7 +76,7 @@ module.exports = function (args, opts) {
argv.igv = '__filename,__dirname';
}
}

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

var ignoreTransform = argv['ignore-transform'] || argv.it;
var b = browserify(xtend({
noParse: Array.isArray(argv.noParse) ? argv.noParse : [argv.noParse],
extensions: [].concat(argv.extension).filter(Boolean).map(function (extension) {
if (extension.charAt(0) != '.') {
if (extension.charAt(0) != '.') {
return '.' + extension;
} else {
return extension
Expand All @@ -105,6 +105,7 @@ module.exports = function (args, opts) {
bundleExternal: argv['bundle-external'],
basedir: argv.basedir,
browserField: argv.browserField,
transformKey: argv['transform-key'] ? ['browserify', argv['transform-key']] : undefined,
dedupe: argv['dedupe'],

detectGlobals: argv.detectGlobals,
Expand All @@ -128,7 +129,7 @@ module.exports = function (args, opts) {
b.plugin(pf, pOpts);
})
;

[].concat(argv.ignore).filter(Boolean)
.forEach(function (i) {
b._pending ++;
Expand All @@ -144,11 +145,11 @@ module.exports = function (args, opts) {
});
})
;

[].concat(argv.exclude).filter(Boolean)
.forEach(function (u) {
b.exclude(u);

b._pending ++;
glob(u, function (err, files) {
if (err) return b.emit('error', err);
Expand All @@ -164,7 +165,7 @@ module.exports = function (args, opts) {
b.require(xs[0], { expose: xs.length === 1 ? xs[0] : xs[1] })
})
;

// resolve any external files and add them to the bundle as externals
[].concat(argv.external).filter(Boolean)
.forEach(function (x) {
Expand All @@ -181,26 +182,26 @@ module.exports = function (args, opts) {
});
}
else add(x, {});

function add (x, opts) {
if (/^[\/.]/.test(x)) b.external(path.resolve(x), opts)
else b.external(x, opts)
}
})
;

[].concat(argv.transform)
.filter(Boolean)
.forEach(function (t) { addTransform(t) })
;

[].concat(argv.g).concat(argv['global-transform'])
.filter(Boolean)
.forEach(function (t) {
addTransform(t, { global: true });
})
;

function addTransform (t, opts) {
if (typeof t === 'string' || typeof t === 'function') {
b.transform(opts, t);
Expand All @@ -218,7 +219,7 @@ module.exports = function (args, opts) {
}
else error('unexpected transform of type ' + typeof t);
}

[].concat(argv.command).filter(Boolean)
.forEach(function (c) {
var cmd = parseShell(c);
Expand All @@ -231,7 +232,7 @@ module.exports = function (args, opts) {
var ps = spawn(cmd[0], cmd.slice(1), { env: env });
var error = '';
ps.stderr.on('data', function (buf) { error += buf });

ps.on('exit', function (code) {
if (code === 0) return;
console.error([
Expand All @@ -245,12 +246,12 @@ module.exports = function (args, opts) {
});
})
;

if (argv.standalone === '') {
error('--standalone requires an export name argument');
return b;
}

return b;
};

Expand Down
9 changes: 9 additions & 0 deletions readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,15 @@ Advanced Options:
Turn off package.json browser field resolution. This is also handy if you
need to run a bundle in node.
--transform-key
Instead of the default package.json#browserify#transform field to list
all transforms to apply when running browserify, a custom field, like, e.g.
package.json#browserify#production or package.json#browserify#staging
can be used, by for example running:
* `browserify index.js --transform-key=production > bundle.js`
* `browserify index.js --transform-key=staging > bundle.js`
--node
Alias for --bare and --no-browser-field.
Expand Down

0 comments on commit d7ff52d

Please sign in to comment.