Skip to content

Commit

Permalink
Enable (and apply) global transforms with browserify
Browse files Browse the repository at this point in the history
This ensures that we can consume code from npm that has our process.env pattern. Unfortunately we'll run the same transform on minified builds but it's pretty quick.
  • Loading branch information
zpao committed Jul 24, 2015
1 parent 83c8857 commit 6fc53e0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
16 changes: 12 additions & 4 deletions grunt/config/browserify.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ var uglifyify = require('uglifyify');
var derequire = require('derequire');
var collapser = require('bundle-collapser/plugin');

var envifyDev = envify({NODE_ENV: process.env.NODE_ENV || 'development'});
var envifyProd = envify({NODE_ENV: process.env.NODE_ENV || 'production'});

var SIMPLE_TEMPLATE =
'/**\n\
* @PACKAGE@ v@VERSION@\n\
Expand Down Expand Up @@ -56,7 +59,8 @@ var basic = {
outfile: './build/react.js',
debug: false,
standalone: 'React',
transforms: [envify({NODE_ENV: process.env.NODE_ENV || 'development'})],
// Apply as global transform so that we also envify fbjs and any other deps
globalTransforms: [envifyDev],
plugins: [collapser],
after: [derequire, simpleBannerify],
};
Expand All @@ -68,7 +72,10 @@ var min = {
outfile: './build/react.min.js',
debug: false,
standalone: 'React',
transforms: [envify({NODE_ENV: process.env.NODE_ENV || 'production'}), uglifyify],
// Envify twice. The first ensures that when we uglifyify, we have the right
// conditions to exclude requires. The global transform runs on deps.
transforms: [envifyProd, uglifyify],
globalTransforms: [envifyProd],
plugins: [collapser],
// No need to derequire because the minifier will mangle
// the "require" calls.
Expand Down Expand Up @@ -100,7 +107,7 @@ var addons = {
debug: false,
standalone: 'React',
packageName: 'React (with addons)',
transforms: [envify({NODE_ENV: process.env.NODE_ENV || 'development'})],
globalTransforms: [envifyDev],
plugins: [collapser],
after: [derequire, simpleBannerify],
};
Expand All @@ -113,7 +120,8 @@ var addonsMin = {
debug: false,
standalone: 'React',
packageName: 'React (with addons)',
transforms: [envify({NODE_ENV: process.env.NODE_ENV || 'production'}), uglifyify],
transforms: [envifyProd, uglifyify],
globalTransforms: [envifyProd],
plugins: [collapser],
// No need to derequire because the minifier will mangle
// the "require" calls.
Expand Down
5 changes: 5 additions & 0 deletions grunt/tasks/browserify.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = function() {
// grunt.config.requires('outfile');
// grunt.config.requires('entries');
config.transforms = config.transforms || [];
config.globalTransforms = config.globalTransforms || [];
config.plugins = config.plugins || [];
config.after = config.after || [];
config.paths = config.paths || [];
Expand All @@ -35,6 +36,10 @@ module.exports = function() {
bundle.transform({}, transform);
});

config.globalTransforms.forEach(function(transform) {
bundle.transform({global: true}, transform);
});

config.plugins.forEach(bundle.plugin, bundle);

// Actually bundle it up
Expand Down

0 comments on commit 6fc53e0

Please sign in to comment.