Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Organizing a React JS project - building single JS file

I just got started using React. I went through the CommentBox tutorial without any issues. But the framework does not give much/any guidance to organizing your JS files or compiling a single minified JS file for a SPA. I already know the framework is flexible and does not enforce a standard and I'm sure these questions are probably dead obvious for someone who develops in the Javascript ecosystem.

I would imagine the consensus is to use Browserify and in the docs there's a link to a git starter project:

https://github.com/petehunt/react-browserify-template

This is a good start, but still it only compiles a single JS file "index.js". I read through some of the browserify handbook and I thought I simply had to 'require' my other files (and those files need to export themselves).

So I modified index.js to look like this:

/** @jsx React.DOM */
var React = require('react');
var pkg = require('./package.json');

var commentBox = require('./comment-box.js');

comment-box.js is basically a hello world test:

/** @jsx React.DOM */
var React = require('react');
var CommentBox = React.createClass({
    render: function() {
        return (
            <div className="commentBox">
            Hello, world! I am a CommentBox.
            </div>
            );
    }
});

React.renderComponent(
    <CommentBox />,
    document.getElementById('content')
);

module.exports = CommentBox;

If I run the react-browserify-template's start target it seems to generate browser-bundle.js fine:

npm start

But if I try the build target

npm build

...nothing happens. I changed the output of npm to verbose and I get the following:

npm info it worked if it ends with ok
npm verb cli [ 'node', '/usr/local/bin/npm', 'build' ]
npm info using [email protected]
npm info using [email protected]
npm verb exit [ 0, true ]
npm info ok

According to package.json it's supposed to generate a file "browser-bundle.min.js" but instead I get no output. I'm hoping someone can clear this up.

like image 789
nogridbag Avatar asked Jul 29 '14 20:07

nogridbag


People also ask

Should React components be in separate files?

React is all about re-using code, and it is recommended to split your components into separate files. To do that, create a new file with a . js file extension and put the code inside it: Note that the filename must start with an uppercase character.

What is the best folder structure for React?

The simplest folder structure for this case seems to be the “group files by their types” option mentioned in the React docs. This makes our lives easy: Components go in the components folder, hooks in the hooks folder, and contexts in the contexts folder.


1 Answers

I figured out what the problem was. As I stated originally, it is probably obvious for someone who's been developing in the JS ecosystem :)

The package.json in the react-browserify-template has three scripts in the "scripts" section with the keys "start", "build", and "test".

As I said previously, start worked fine by running:

npm start

I wrongly assumbed that I could run the build script similarly:

npm build (this will never work and there will be no errors/output)

It turns out I needed to run the build script using:

npm run-script build

One extra line in the documentation might have saved me hours of trouble :D I'm now using this approach as it seems quite a bit simpler. Also, it sets NODE_ENV to production and uses envify which apparently is important: https://github.com/facebook/react/issues/1772

One other thing, some of the official examples such as todomvc-flux use the lowercase 'react' in the require function:

var React = require('react');

so I suppose that's the recommended syntax (?)

like image 129
nogridbag Avatar answered Sep 22 '22 14:09

nogridbag