From a09232d3e31db5ba55347ca8604512f0c634dca6 Mon Sep 17 00:00:00 2001 From: dshimkoski Date: Fri, 27 Jun 2014 00:07:09 -0400 Subject: [PATCH 1/2] remove custom react property from component.json, rely on .jsx extension instead --- README.md | 12 ++-- example/lib/boot/{button.js => button.jsx} | 0 example/lib/boot/component.json | 7 +-- example/lib/boot/{main.js => index.jsx} | 0 index.js | 70 ++++++++++------------ package.json | 1 + 6 files changed, 39 insertions(+), 51 deletions(-) rename example/lib/boot/{button.js => button.jsx} (100%) rename example/lib/boot/{main.js => index.jsx} (100%) diff --git a/README.md b/README.md index 131978e..8ebd162 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # component-react -A plugin to transpile [React](https://github.com/facebook/react) javascript files ('.js' or '.jsx') for the component builder. +A plugin to transpile [React](https://github.com/facebook/react) javascript files ('.jsx') for the component builder. ## Install @@ -10,16 +10,11 @@ $ npm install component-react ## Usage -Add your `.jsx` files to the `react` array in your `component.json`: +Add your `.jsx` files to the `scripts` array in your `component.json`: ```javascript { - "scripts": [ - "index.js" - ], - "react": [ - "button.jsx" - ] + "scripts": ["index.jsx", "button.jsx"] } ``` @@ -57,6 +52,7 @@ builder.build(function (err, res) { ## Authors * Neil Jagdish Patel +* Denny Shimkoski ## License diff --git a/example/lib/boot/button.js b/example/lib/boot/button.jsx similarity index 100% rename from example/lib/boot/button.js rename to example/lib/boot/button.jsx diff --git a/example/lib/boot/component.json b/example/lib/boot/component.json index 4b44929..d4156cc 100644 --- a/example/lib/boot/component.json +++ b/example/lib/boot/component.json @@ -3,10 +3,5 @@ "dependencies": { "njpatel/react": "*" }, - "main": "main.js", - "scripts": [ "main.js"], - "react": [ - "main.js", - "button.js" - ] + "scripts": [ "index.jsx", "button.js" ] } diff --git a/example/lib/boot/main.js b/example/lib/boot/index.jsx similarity index 100% rename from example/lib/boot/main.js rename to example/lib/boot/index.jsx diff --git a/index.js b/index.js index a5c2216..b4e808e 100644 --- a/index.js +++ b/index.js @@ -1,38 +1,34 @@ -// component-less was used as the base, so will be very similar to that - -var async = require('async') - , fs = require('fs') - , path = require('path') - , transform = require('react-tools').transform - ; - -module.exports = function(builder, options) { - options = options || {}; - - builder.hook('before scripts', function(builder, callback) { - var files = builder.config.react; - - if (!files) return callback(); - - if (!builder.config.scripts) - builder.config.scripts = []; - - async.each(files, function(file, done) { - var data = null - , orig = builder.path(file) - ; - - try { - data = fs.readFileSync(orig, 'utf8'); - } catch(error) { - return done(new Error('Error while reading ' + orig + ':' + error)); - } - - var newData = transform(data); - var newFile = path.basename(file, path.extname(file)) + '.js'; - builder.addFile('scripts', newFile, newData); - - done(); - }, callback); + +var fs = require('fs'), + path = require('path'), + debug = require('debug')('component:jsx'), + transform = require('react-tools').transform; + +module.exports = function(builder) { + + builder.hook('before scripts', function(pkg, next) { + + var scripts = pkg.config.scripts; + if (!scripts) return next(); + + var jsxFiles = scripts.filter(function(file) { return path.extname(file) == '.jsx'; }); + + jsxFiles.forEach(function(jsxFile) { + + var jsxPath = pkg.path(jsxFile); + var name = jsxFile.split('.')[0] + '.js'; + + debug('compiling: %s', jsxFile); + + var jsx = fs.readFileSync(jsxPath, 'utf8'); + + pkg.addFile('scripts', name, transform(jsx)); + pkg.removeFile('scripts', jsxFile); + + }); + + next(); + }); -} + +}; diff --git a/package.json b/package.json index 12df351..4e15b50 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ }, "dependencies": { "async": "~0.2.6", + "debug": "^1.0.2", "react-tools": "*" }, "devDependencies": { From e9d4ea672568b66927b4d65b7cdffd5670848c91 Mon Sep 17 00:00:00 2001 From: dshimkoski Date: Fri, 27 Jun 2014 00:08:52 -0400 Subject: [PATCH 2/2] fix example --- example/lib/boot/component.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/lib/boot/component.json b/example/lib/boot/component.json index d4156cc..aecf9a9 100644 --- a/example/lib/boot/component.json +++ b/example/lib/boot/component.json @@ -3,5 +3,5 @@ "dependencies": { "njpatel/react": "*" }, - "scripts": [ "index.jsx", "button.js" ] + "scripts": [ "index.jsx", "button.jsx" ] }