From 3171436d979a61f55e997ac74915b94726fcbe7b Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Sat, 8 Mar 2014 16:33:58 +0200 Subject: [PATCH 1/4] Switched from browserify to pure-cjs bundler. Optimizations and fix for JSXTransformer build. Dropped dependency on emulation of Node.js native modules. Added deamdify step for JSXTransformer build. --- grunt/config/browserify.js | 2 ++ grunt/tasks/browserify.js | 55 ++++++++++-------------------------- package.json | 3 +- vendor/browser-transforms.js | 3 +- 4 files changed, 20 insertions(+), 43 deletions(-) diff --git a/grunt/config/browserify.js b/grunt/config/browserify.js index 61481e4353414..45b1074c9a5b3 100644 --- a/grunt/config/browserify.js +++ b/grunt/config/browserify.js @@ -3,6 +3,7 @@ 'use strict'; +var deamdify = require('deamdify'); var envify = require('envify/custom'); var es3ify = require('es3ify'); var grunt = require('grunt'); @@ -81,6 +82,7 @@ var transformer = { outfile: './build/JSXTransformer.js', debug: false, standalone: 'JSXTransformer', + transforms: [deamdify], after: [es3ify.transform, simpleBannerify] }; diff --git a/grunt/tasks/browserify.js b/grunt/tasks/browserify.js index e36c545e17bf0..9a0eea744bcb1 100644 --- a/grunt/tasks/browserify.js +++ b/grunt/tasks/browserify.js @@ -1,6 +1,6 @@ 'use strict'; -var browserify = require('browserify'); +var cjs = require('pure-cjs'); var grunt = require('grunt'); module.exports = function() { @@ -18,51 +18,26 @@ module.exports = function() { config.after = [config.after]; } - // create the bundle we'll work with - var entries = grunt.file.expand(config.entries); - var bundle = browserify(entries); - - // Make sure the things that need to be exposed are. - var requires = config.requires || {}; - if (requires instanceof Array) { - grunt.file.expand({ - nonull: true, // Keep IDs that don't expand to anything. - cwd: "src" - }, requires).forEach(function(name) { - bundle.require("./build/modules/" + name, { - expose: name.replace(/\.js$/i, "") - }); - }); - } else if (typeof requires === "object") { - Object.keys(requires).forEach(function(name) { - bundle.require(requires[name], { expose: name }); - }); - } - - // Extract other options + // Extract options var options = { - debug: config.debug, // sourcemaps - standalone: config.standalone // global + input: config.entries[0], + output: config.outfile, + map: config.debug, // sourcemaps + exports: config.standalone, // global + transform: config.transforms, + dryRun: true // we will write to disk ourselves }; - // TODO: make sure this works, test with this too - config.transforms.forEach(function(transform) { - bundle.transform({}, transform); - }); - // Actually bundle it up var _this = this; - bundle.bundle(options, function(err, src) { - if (err) { - grunt.log.error(err); - done(); - } - - config.after.forEach(function(fn) { - src = fn.call(_this, src); - }); + cjs.transform(options).then(function(result) { + grunt.file.write(config.outfile, config.after.reduce(function(src, fn) { + return fn.call(_this, src); + }, result.code)); - grunt.file.write(config.outfile, src); + done(); + }, function(err) { + grunt.log.error(err); done(); }); }; diff --git a/package.json b/package.json index 0860f2f5877d8..6b4da68ca0fd6 100644 --- a/package.json +++ b/package.json @@ -39,8 +39,8 @@ }, "devDependencies": { "benchmark": "~1.0.0", - "browserify": "~3.20.0", "coverify": "~1.0.4", + "deamdify": "~0.1.1", "envify": "~1.2.0", "es3ify": "~0.1.2", "es5-shim": "~2.3.0", @@ -61,6 +61,7 @@ "phantomjs": "~1.9", "platform": "~1.0.0", "populist": "~0.1.6", + "pure-cjs": "~1.8.3", "recast": "~0.5.6", "sauce-tunnel": "~1.1.0", "semver": "~2.2.1", diff --git a/vendor/browser-transforms.js b/vendor/browser-transforms.js index 26c48a7f9b718..a35db1e9cd590 100644 --- a/vendor/browser-transforms.js +++ b/vendor/browser-transforms.js @@ -20,7 +20,6 @@ var runScripts; var headEl; -var buffer = require('buffer'); var transform = require('jstransform').transform; var visitors = require('./fbtransform/visitors').transformVisitors; var docblock = require('jstransform/src/docblock'); @@ -123,7 +122,7 @@ var transformCode = function(code, source) { return ( transformed.code + '//# sourceMappingURL=data:application/json;base64,' + - buffer.Buffer(JSON.stringify(map)).toString('base64') + btoa(unescape(encodeURIComponent(JSON.stringify(map)))) ); } else { return code; From 9e224e615f9001d0a3c56c302dd504c7601e8af3 Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Tue, 11 Mar 2014 13:26:36 +0200 Subject: [PATCH 2/4] Renamed `browserify` tasks/configs to `cjs`, updated pure-cjs to 1.9.0 for better build speed. --- Gruntfile.js | 36 ++++++++++----------- grunt/config/{browserify.js => commonjs.js} | 5 ++- grunt/tasks/{browserify.js => commonjs.js} | 0 package.json | 2 +- 4 files changed, 21 insertions(+), 22 deletions(-) rename grunt/config/{browserify.js => commonjs.js} (95%) rename grunt/tasks/{browserify.js => commonjs.js} (100%) diff --git a/Gruntfile.js b/Gruntfile.js index a0ca9148dc979..285fbf9916e95 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -2,7 +2,7 @@ var exec = require('child_process').exec; var jsxTask = require('./grunt/tasks/jsx'); -var browserifyTask = require('./grunt/tasks/browserify'); +var commonjsTask = require('./grunt/tasks/commonjs'); var populistTask = require('./grunt/tasks/populist'); var webdriverPhantomJSTask = require('./grunt/tasks/webdriver-phantomjs'); var webdriverJasmineTasks = require('./grunt/tasks/webdriver-jasmine'); @@ -19,7 +19,7 @@ module.exports = function(grunt) { pkg: grunt.file.readJSON('package.json'), copy: require('./grunt/config/copy'), jsx: require('./grunt/config/jsx'), - browserify: require('./grunt/config/browserify'), + commonjs: require('./grunt/config/commonjs'), populist: require('./grunt/config/populist'), connect: require('./grunt/config/server')(grunt), "webdriver-jasmine": require('./grunt/config/webdriver-jasmine'), @@ -52,8 +52,8 @@ module.exports = function(grunt) { // Register jsx:normal and :release tasks. grunt.registerMultiTask('jsx', jsxTask); - // Our own browserify-based tasks to build a single JS file build - grunt.registerMultiTask('browserify', browserifyTask); + // Our own commonjs-based tasks to build a single JS file build + grunt.registerMultiTask('commonjs', commonjsTask); grunt.registerMultiTask('populist', populistTask); @@ -71,22 +71,22 @@ module.exports = function(grunt) { grunt.registerTask('version-check', versionCheckTask); - grunt.registerTask('build:basic', ['jsx:normal', 'version-check', 'browserify:basic']); - grunt.registerTask('build:addons', ['jsx:normal', 'browserify:addons']); - grunt.registerTask('build:transformer', ['jsx:normal', 'browserify:transformer']); - grunt.registerTask('build:min', ['jsx:normal', 'version-check', 'browserify:min']); - grunt.registerTask('build:addons-min', ['jsx:normal', 'browserify:addonsMin']); + grunt.registerTask('build:basic', ['jsx:normal', 'version-check', 'commonjs:basic']); + grunt.registerTask('build:addons', ['jsx:normal', 'commonjs:addons']); + grunt.registerTask('build:transformer', ['jsx:normal', 'commonjs:transformer']); + grunt.registerTask('build:min', ['jsx:normal', 'version-check', 'commonjs:min']); + grunt.registerTask('build:addons-min', ['jsx:normal', 'commonjs:addonsMin']); grunt.registerTask('build:withCodeCoverageLogging', [ 'jsx:normal', 'version-check', - 'browserify:withCodeCoverageLogging' + 'commonjs:withCodeCoverageLogging' ]); grunt.registerTask('build:perf', [ 'jsx:normal', 'version-check', - 'browserify:transformer', - 'browserify:basic', - 'browserify:min', + 'commonjs:transformer', + 'commonjs:basic', + 'commonjs:min', 'download-previous-version' ]); grunt.registerTask('build:test', [ @@ -195,11 +195,11 @@ module.exports = function(grunt) { 'delete-build-modules', 'jsx:normal', 'version-check', - 'browserify:basic', - 'browserify:transformer', - 'browserify:addons', - 'browserify:min', - 'browserify:addonsMin', + 'commonjs:basic', + 'commonjs:transformer', + 'commonjs:addons', + 'commonjs:min', + 'commonjs:addonsMin', 'npm-react:release', 'npm-react:pack', 'npm-react-tools:pack', diff --git a/grunt/config/browserify.js b/grunt/config/commonjs.js similarity index 95% rename from grunt/config/browserify.js rename to grunt/config/commonjs.js index 45b1074c9a5b3..1c283ba5a5ff4 100644 --- a/grunt/config/browserify.js +++ b/grunt/config/commonjs.js @@ -8,7 +8,6 @@ var envify = require('envify/custom'); var es3ify = require('es3ify'); var grunt = require('grunt'); var UglifyJS = require('uglify-js'); -var uglifyify = require('uglifyify'); var _ = require('lodash'); var SIMPLE_TEMPLATE = @@ -71,7 +70,7 @@ var basic = { var min = _.merge({}, basic, { outfile: './build/react.min.js', debug: false, - transforms: [envify({NODE_ENV: 'production'}), uglifyify], + transforms: [envify({NODE_ENV: 'production'})], after: [minify, bannerify] }); @@ -101,7 +100,7 @@ var addons = { var addonsMin = _.merge({}, addons, { outfile: './build/react-with-addons.min.js', debug: false, - transforms: [envify({NODE_ENV: 'production'}), uglifyify], + transforms: [envify({NODE_ENV: 'production'})], after: [minify, bannerify] }); diff --git a/grunt/tasks/browserify.js b/grunt/tasks/commonjs.js similarity index 100% rename from grunt/tasks/browserify.js rename to grunt/tasks/commonjs.js diff --git a/package.json b/package.json index 6b4da68ca0fd6..8d54d45edb074 100644 --- a/package.json +++ b/package.json @@ -61,7 +61,7 @@ "phantomjs": "~1.9", "platform": "~1.0.0", "populist": "~0.1.6", - "pure-cjs": "~1.8.3", + "pure-cjs": "~1.9.0", "recast": "~0.5.6", "sauce-tunnel": "~1.1.0", "semver": "~2.2.1", From 25773ed1b39a20e0beaedefb0bd618e1bea5164e Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Tue, 11 Mar 2014 13:40:38 +0000 Subject: [PATCH 3/4] 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 1c283ba5a5ff4..fd056515e3421 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 9a0eea744bcb1..ff15d6ef16c9c 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)); From 3f3187c14f81987ab397f46b9f5c2c06b8e0189c Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Mon, 17 Mar 2014 16:53:02 +0200 Subject: [PATCH 4/4] Renamed commojs task to bundle, added failing build if bundler failed. --- Gruntfile.js | 36 ++++++++++++------------- grunt/config/{commonjs.js => bundle.js} | 0 grunt/tasks/{commonjs.js => bundle.js} | 4 +-- 3 files changed, 20 insertions(+), 20 deletions(-) rename grunt/config/{commonjs.js => bundle.js} (100%) rename grunt/tasks/{commonjs.js => bundle.js} (87%) diff --git a/Gruntfile.js b/Gruntfile.js index 285fbf9916e95..f6f8ebf877878 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -2,7 +2,7 @@ var exec = require('child_process').exec; var jsxTask = require('./grunt/tasks/jsx'); -var commonjsTask = require('./grunt/tasks/commonjs'); +var bundleTask = require('./grunt/tasks/bundle'); var populistTask = require('./grunt/tasks/populist'); var webdriverPhantomJSTask = require('./grunt/tasks/webdriver-phantomjs'); var webdriverJasmineTasks = require('./grunt/tasks/webdriver-jasmine'); @@ -19,7 +19,7 @@ module.exports = function(grunt) { pkg: grunt.file.readJSON('package.json'), copy: require('./grunt/config/copy'), jsx: require('./grunt/config/jsx'), - commonjs: require('./grunt/config/commonjs'), + bundle: require('./grunt/config/bundle'), populist: require('./grunt/config/populist'), connect: require('./grunt/config/server')(grunt), "webdriver-jasmine": require('./grunt/config/webdriver-jasmine'), @@ -52,8 +52,8 @@ module.exports = function(grunt) { // Register jsx:normal and :release tasks. grunt.registerMultiTask('jsx', jsxTask); - // Our own commonjs-based tasks to build a single JS file build - grunt.registerMultiTask('commonjs', commonjsTask); + // Our own bundle-based tasks to build a single JS file build + grunt.registerMultiTask('bundle', bundleTask); grunt.registerMultiTask('populist', populistTask); @@ -71,22 +71,22 @@ module.exports = function(grunt) { grunt.registerTask('version-check', versionCheckTask); - grunt.registerTask('build:basic', ['jsx:normal', 'version-check', 'commonjs:basic']); - grunt.registerTask('build:addons', ['jsx:normal', 'commonjs:addons']); - grunt.registerTask('build:transformer', ['jsx:normal', 'commonjs:transformer']); - grunt.registerTask('build:min', ['jsx:normal', 'version-check', 'commonjs:min']); - grunt.registerTask('build:addons-min', ['jsx:normal', 'commonjs:addonsMin']); + grunt.registerTask('build:basic', ['jsx:normal', 'version-check', 'bundle:basic']); + grunt.registerTask('build:addons', ['jsx:normal', 'bundle:addons']); + grunt.registerTask('build:transformer', ['jsx:normal', 'bundle:transformer']); + grunt.registerTask('build:min', ['jsx:normal', 'version-check', 'bundle:min']); + grunt.registerTask('build:addons-min', ['jsx:normal', 'bundle:addonsMin']); grunt.registerTask('build:withCodeCoverageLogging', [ 'jsx:normal', 'version-check', - 'commonjs:withCodeCoverageLogging' + 'bundle:withCodeCoverageLogging' ]); grunt.registerTask('build:perf', [ 'jsx:normal', 'version-check', - 'commonjs:transformer', - 'commonjs:basic', - 'commonjs:min', + 'bundle:transformer', + 'bundle:basic', + 'bundle:min', 'download-previous-version' ]); grunt.registerTask('build:test', [ @@ -195,11 +195,11 @@ module.exports = function(grunt) { 'delete-build-modules', 'jsx:normal', 'version-check', - 'commonjs:basic', - 'commonjs:transformer', - 'commonjs:addons', - 'commonjs:min', - 'commonjs:addonsMin', + 'bundle:basic', + 'bundle:transformer', + 'bundle:addons', + 'bundle:min', + 'bundle:addonsMin', 'npm-react:release', 'npm-react:pack', 'npm-react-tools:pack', diff --git a/grunt/config/commonjs.js b/grunt/config/bundle.js similarity index 100% rename from grunt/config/commonjs.js rename to grunt/config/bundle.js diff --git a/grunt/tasks/commonjs.js b/grunt/tasks/bundle.js similarity index 87% rename from grunt/tasks/commonjs.js rename to grunt/tasks/bundle.js index ff15d6ef16c9c..692c55642c308 100644 --- a/grunt/tasks/commonjs.js +++ b/grunt/tasks/bundle.js @@ -26,13 +26,13 @@ module.exports = function() { var _this = this; cjs.transform(options).then(function(result) { - grunt.file.write(_this.files[0].dest, config.after.reduce(function(src, fn) { + grunt.file.write(result.options.output, config.after.reduce(function(src, fn) { return fn.call(_this, src); }, result.code)); done(); }, function(err) { grunt.log.error(err); - done(); + done(false); }); };