From 658f41cb30765185ef04611da43b9ece5b66568c Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Fri, 6 Sep 2013 16:09:30 -0400 Subject: [PATCH] Simplify bin/jsx to perform just the JSX transform. We will continue using `bin/jsx-internal`, well, internally. Note that this version no longer respects `@providesModule`, and it doesn't do anything special with constants like `__DEV__`, so we can no longer get to claim that `bin/jsx` can be used to build the core. I'm happy about this, personally, because it demonstrates the flexibility of Commoner. --- bin/jsx | 47 ++------------------------------------- bin/jsx-internal | 55 ++++++++++++++++++++++++++++++++++++++++++++++ grunt/tasks/jsx.js | 2 +- grunt/tasks/npm.js | 4 ++-- package.json | 4 ++-- 5 files changed, 62 insertions(+), 50 deletions(-) create mode 100755 bin/jsx-internal diff --git a/bin/jsx b/bin/jsx index 3ad9f7c40a..c848f496e9 100755 --- a/bin/jsx +++ b/bin/jsx @@ -3,53 +3,10 @@ var visitors = require('../vendor/fbtransform/visitors').transformVisitors; var transform = require('jstransform').transform; -var propagate = require("../vendor/constants").propagate; require("commoner").resolve(function(id) { - var context = this; - - // Note that the result of context.getProvidedP() is cached for the - // duration of the build, so it is both consistent and cheap to - // evaluate multiple times. - return context.getProvidedP().then(function(idToPath) { - // If a module declares its own identifier using @providesModule - // then that identifier will be a key in the idToPath object. - if (idToPath.hasOwnProperty(id)) { - return context.readFileP(idToPath[id]); - } - - // Otherwise assume the identifier maps directly to a path in the - // filesystem. - return context.readModuleP(id); - }); - + return this.readModuleP(id); }).process(function(id, source) { - var context = this; - var constants = context.config.constants || {}; - // This is where JSX, ES6, etc. desugaring happens. - source = transform(visitors.react, source).code; - - // Constant propagation means removing any obviously dead code after - // replacing constant expressions with literal (boolean) values. - source = propagate(constants, source); - - if (context.config.mocking) { - // Make sure there is exactly one newline at the end of the module. - source = source.replace(/\s+$/m, "\n"); - - return context.getProvidedP().then(function(idToPath) { - if (id !== "mock-modules" && - id !== "mocks" && - id !== "test/all" && - idToPath.hasOwnProperty("mock-modules")) { - return source + '\nrequire("mock-modules").register(' + - JSON.stringify(id) + ', module);\n'; - } - - return source; - }); - } - - return source; + return transform(visitors.react, source).code; }); diff --git a/bin/jsx-internal b/bin/jsx-internal new file mode 100755 index 0000000000..3ad9f7c40a --- /dev/null +++ b/bin/jsx-internal @@ -0,0 +1,55 @@ +#!/usr/bin/env node +"use strict"; + +var visitors = require('../vendor/fbtransform/visitors').transformVisitors; +var transform = require('jstransform').transform; +var propagate = require("../vendor/constants").propagate; + +require("commoner").resolve(function(id) { + var context = this; + + // Note that the result of context.getProvidedP() is cached for the + // duration of the build, so it is both consistent and cheap to + // evaluate multiple times. + return context.getProvidedP().then(function(idToPath) { + // If a module declares its own identifier using @providesModule + // then that identifier will be a key in the idToPath object. + if (idToPath.hasOwnProperty(id)) { + return context.readFileP(idToPath[id]); + } + + // Otherwise assume the identifier maps directly to a path in the + // filesystem. + return context.readModuleP(id); + }); + +}).process(function(id, source) { + var context = this; + var constants = context.config.constants || {}; + + // This is where JSX, ES6, etc. desugaring happens. + source = transform(visitors.react, source).code; + + // Constant propagation means removing any obviously dead code after + // replacing constant expressions with literal (boolean) values. + source = propagate(constants, source); + + if (context.config.mocking) { + // Make sure there is exactly one newline at the end of the module. + source = source.replace(/\s+$/m, "\n"); + + return context.getProvidedP().then(function(idToPath) { + if (id !== "mock-modules" && + id !== "mocks" && + id !== "test/all" && + idToPath.hasOwnProperty("mock-modules")) { + return source + '\nrequire("mock-modules").register(' + + JSON.stringify(id) + ', module);\n'; + } + + return source; + }); + } + + return source; +}); diff --git a/grunt/tasks/jsx.js b/grunt/tasks/jsx.js index 290816711f..b84be32cb4 100644 --- a/grunt/tasks/jsx.js +++ b/grunt/tasks/jsx.js @@ -27,7 +27,7 @@ module.exports = function() { args.push("--config", config.configFile); var child = spawn({ - cmd: "bin/jsx", + cmd: "bin/jsx-internal", args: args }, function(error, result, code) { if (error) { diff --git a/grunt/tasks/npm.js b/grunt/tasks/npm.js index 72d95e536b..9d97bb3d15 100644 --- a/grunt/tasks/npm.js +++ b/grunt/tasks/npm.js @@ -64,8 +64,8 @@ module.exports = function() { var pkgDir = path.join(nodePath, pkg.name); var doneCount = 2; - // Make sure that bin/jsx is runnable by echoing main.js. - run("bin/jsx", ["main.js"], { + // Make sure that bin/jsx-internal is runnable by echoing main.js. + run("bin/jsx-internal", ["main.js"], { cwd: pkgDir }, function(result) { assert.ok(result.stdout.indexOf("transform") >= 0, result.stdout); diff --git a/package.json b/package.json index 5dd706200d..a8d669b49b 100644 --- a/package.json +++ b/package.json @@ -38,8 +38,7 @@ "dependencies": { "commoner": "~0.8.4", "esprima-fb": "1001.1001.1000-dev-harmony-fb", - "jstransform": "~1.0.0", - "recast": "~0.4.8" + "jstransform": "~1.0.0" }, "devDependencies": { "browserify": "~2.29.0", @@ -51,6 +50,7 @@ "grunt-contrib-jshint": "~0.6.0", "optimist": "~0.6.0", "phantomjs": "~1.9.1-4", + "recast": "~0.4.8", "semver": "~2.1.0", "uglify-js": "~2.4.0", "grunt-contrib-clean": "~0.5.0",