From 25773ed1b39a20e0beaedefb0bd618e1bea5164e Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Tue, 11 Mar 2014 13:40:38 +0000 Subject: [PATCH] CommonJS builder config to Grunt style for easier handling and readability. --- grunt/config/commonjs.js | 105 +++++++++++++++++++++------------------ grunt/tasks/commonjs.js | 21 +++----- 2 files changed, 65 insertions(+), 61 deletions(-) diff --git a/grunt/config/commonjs.js b/grunt/config/commonjs.js index 1c283ba5a5..fd056515e3 100644 --- a/grunt/config/commonjs.js +++ b/grunt/config/commonjs.js @@ -55,67 +55,76 @@ function simpleBannerify(src) { '\n' + src; } -// Our basic config which we'll add to to make our other builds +function override(obj1, obj2) { + return _.merge({}, obj1, obj2, function (a, b) { + if (_.isArray(a)) { + return b; + } + }) +} + var basic = { - entries: [ - './build/modules/React.js' - ], - outfile: './build/react.js', - debug: false, - standalone: 'React', - transforms: [envify({NODE_ENV: 'development'})], - after: [es3ify.transform, simpleBannerify] + src: './build/modules/React.js', + dest: './build/react.js', + options: { + debug: false, + standalone: 'React', + transforms: [envify({NODE_ENV: 'development'})], + after: [es3ify.transform, simpleBannerify] + } }; -var min = _.merge({}, basic, { - outfile: './build/react.min.js', - debug: false, - transforms: [envify({NODE_ENV: 'production'})], - after: [minify, bannerify] +var min = override(basic, { + dest: './build/react.min.js', + options: { + debug: false, + transforms: [envify({NODE_ENV: 'production'})], + after: [minify, bannerify] + } }); var transformer = { - entries:[ - './vendor/browser-transforms.js' - ], - outfile: './build/JSXTransformer.js', - debug: false, - standalone: 'JSXTransformer', - transforms: [deamdify], - after: [es3ify.transform, simpleBannerify] + src: './vendor/browser-transforms.js', + dest: './build/JSXTransformer.js', + options: { + debug: false, + standalone: 'JSXTransformer', + transforms: [deamdify], + after: [es3ify.transform, simpleBannerify] + } }; var addons = { - entries: [ - './build/modules/ReactWithAddons.js' - ], - outfile: './build/react-with-addons.js', - debug: false, - standalone: 'React', - transforms: [envify({NODE_ENV: 'development'})], - packageName: 'React (with addons)', - after: [es3ify.transform, simpleBannerify] + src: './build/modules/ReactWithAddons.js', + dest: './build/react-with-addons.js', + options: { + debug: false, + standalone: 'React', + transforms: [envify({NODE_ENV: 'development'})], + packageName: 'React (with addons)', + after: [es3ify.transform, simpleBannerify] + } }; -var addonsMin = _.merge({}, addons, { - outfile: './build/react-with-addons.min.js', - debug: false, - transforms: [envify({NODE_ENV: 'production'})], - after: [minify, bannerify] +var addonsMin = override(addons, { + dest: './build/react-with-addons.min.js', + options: { + debug: false, + transforms: [envify({NODE_ENV: 'production'})], + after: [minify, bannerify] + } }); -var withCodeCoverageLogging = { - entries: [ - './build/modules/React.js' - ], - outfile: './build/react.js', - debug: true, - standalone: 'React', - transforms: [ - envify({NODE_ENV: 'development'}), - require('coverify') - ] -}; +var withCodeCoverageLogging = override(basic, { + options: { + debug: true, + transforms: [ + envify({NODE_ENV: 'development'}), + require('coverify') + ], + after: [] + } +}); module.exports = { basic: basic, diff --git a/grunt/tasks/commonjs.js b/grunt/tasks/commonjs.js index 9a0eea744b..ff15d6ef16 100644 --- a/grunt/tasks/commonjs.js +++ b/grunt/tasks/commonjs.js @@ -4,24 +4,18 @@ var cjs = require('pure-cjs'); var grunt = require('grunt'); module.exports = function() { - var config = this.data; + var config = this.options({ + transforms: [], + after: [] + }); // This task is async... var done = this.async(); - // More/better assertions - // grunt.config.requires('outfile'); - // grunt.config.requires('entries'); - config.transforms = config.transforms || []; - config.after = config.after || []; - if (typeof config.after === 'function') { - config.after = [config.after]; - } - // Extract options var options = { - input: config.entries[0], - output: config.outfile, + input: this.files[0].src[0], + output: this.files[0].dest, map: config.debug, // sourcemaps exports: config.standalone, // global transform: config.transforms, @@ -30,8 +24,9 @@ module.exports = function() { // Actually bundle it up var _this = this; + cjs.transform(options).then(function(result) { - grunt.file.write(config.outfile, config.after.reduce(function(src, fn) { + grunt.file.write(_this.files[0].dest, config.after.reduce(function(src, fn) { return fn.call(_this, src); }, result.code));