Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switched from browserify to pure-cjs bundler. #1234

Merged
merged 4 commits into from
Mar 18, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var exec = require('child_process').exec;
var jsxTask = require('./grunt/tasks/jsx');
var browserifyTask = require('./grunt/tasks/browserify');
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');
Expand All @@ -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'),
bundle: require('./grunt/config/bundle'),
populist: require('./grunt/config/populist'),
connect: require('./grunt/config/server')(grunt),
"webdriver-jasmine": require('./grunt/config/webdriver-jasmine'),
Expand Down Expand Up @@ -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 bundle-based tasks to build a single JS file build
grunt.registerMultiTask('bundle', bundleTask);

grunt.registerMultiTask('populist', populistTask);

Expand All @@ -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', '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',
'browserify:withCodeCoverageLogging'
'bundle:withCodeCoverageLogging'
]);
grunt.registerTask('build:perf', [
'jsx:normal',
'version-check',
'browserify:transformer',
'browserify:basic',
'browserify:min',
'bundle:transformer',
'bundle:basic',
'bundle:min',
'download-previous-version'
]);
grunt.registerTask('build:test', [
Expand Down Expand Up @@ -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',
'bundle:basic',
'bundle:transformer',
'bundle:addons',
'bundle:min',
'bundle:addonsMin',
'npm-react:release',
'npm-react:pack',
'npm-react-tools:pack',
Expand Down
106 changes: 58 additions & 48 deletions grunt/config/browserify.js → grunt/config/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

'use strict';

var deamdify = require('deamdify');
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 =
Expand Down Expand Up @@ -55,66 +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'}), uglifyify],
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',
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'}), uglifyify],
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,
Expand Down
68 changes: 0 additions & 68 deletions grunt/tasks/browserify.js

This file was deleted.

38 changes: 38 additions & 0 deletions grunt/tasks/bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

var cjs = require('pure-cjs');
var grunt = require('grunt');

module.exports = function() {
var config = this.options({
transforms: [],
after: []
});

// This task is async...
var done = this.async();

// Extract options
var options = {
input: this.files[0].src[0],
output: this.files[0].dest,
map: config.debug, // sourcemaps
exports: config.standalone, // global
transform: config.transforms,
dryRun: true // we will write to disk ourselves
};

// Actually bundle it up
var _this = this;

cjs.transform(options).then(function(result) {
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(false);
});
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -61,6 +61,7 @@
"phantomjs": "~1.9",
"platform": "~1.0.0",
"populist": "~0.1.6",
"pure-cjs": "~1.9.0",
"recast": "~0.5.6",
"sauce-tunnel": "~1.1.0",
"semver": "~2.2.1",
Expand Down
3 changes: 1 addition & 2 deletions vendor/browser-transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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))))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why unescape and not decodeURIComponent?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Common practice for handling UTF-8. decodeURIComponent + encodeURIComponent composed would give identity function:

> btoa(decodeURIComponent(encodeURIComponent('ы')))
[Exception... "String contains an invalid character"  code: "5" nsresult: "0x80530005 (InvalidCharacterError)"  location: "<unknown>"]
> btoa(unescape(encodeURIComponent('ы')))
"0Ys="

More details are here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Base64_encoding_and_decoding#The_.22Unicode_Problem.22

);
} else {
return code;
Expand Down