Skip to content

Commit

Permalink
Work around but in component/builder.js. Fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
njpatel committed Jan 9, 2014
1 parent 12676ce commit 8a5e0d3
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 33 deletions.
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
# component-react

A plugin to transpile [React](https://github.com/facebook/react) .jsx files for the component builder.
A plugin to transpile [React](https://github.com/facebook/react) JS files ('.js or .jsx') for the component builder.

## Install

`$ npm install component-react`

## Usage

Add your `.jsx` files to the `scripts` array in your `component.json`:
Add your `.jsx` files to the `react` array in your `component.json`:

```javascript
{
"scripts": [
"index.js",
"scripts": [
"index.js"
],
"react": [
"button.jsx"
]
}
```

Use the plugin during your build process:
Then either use the ```component``` command:

```bash
$ component build --use component-react
```

Or use the plugin during your custom build process:

```javascript
var Builder = require('component-builder')
Expand Down
2 changes: 1 addition & 1 deletion example/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ require.relative = function(parent) {
if (!i) i = 0;
path = segs.slice(0, i + 1).join('/') + '/deps/' + path;
return path;
;
};

/**
* Check if module is defined at `path`.
Expand Down
2 changes: 1 addition & 1 deletion example/component.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "example",
"dependencies": {},
"scripts": ["index.jsx"]
"react": ["index.jsx"]
}
34 changes: 10 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,33 @@ var async = require('async')
, transform = require('react-tools').transform
;

function isJSX(filename) {
var ext = path.extname(filename);

if (ext === '.jsx')
return true;

return false;
}

module.exports = function(builder, options) {
options = options || {};

builder.hook('before scripts', function(builder, callback) {
if (!builder.config.scripts) {
return callback();
}
var files = builder.config.react;

// Make a copy of the list of files to parse
var files = builder.config.scripts.slice(0);
if (!files) return callback();

async.each(files, function(file, cb) {
if (!builder.config.scripts)
builder.config.scripts = [];

async.each(files, function(file, done) {
var data = null
, orig = builder.path(file)
;

if (!isJSX(file))
return cb();


try {
data = fs.readFileSync(orig, 'utf8');
} catch(error) {
return cb(new Error('Error while reading ' + orig + ':' + 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);
builder.removeFile('scripts', file);

cb();

done();
}, callback);
});
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "component-react",
"version": "0.1.1",
"version": "1.0.0",
"description": "A plugin to transpile React jsx files for the component builder",
"keywords": [
"component",
Expand Down Expand Up @@ -28,7 +28,7 @@
"bugs": {
"url": "https://github.com/njpatel/component-react/issues"
},
"_id": "component-react@0.1.0",
"_id": "component-react@1.0.0",
"dist": {
"shasum": ""
},
Expand Down

0 comments on commit 8a5e0d3

Please sign in to comment.