generator-reactor

Yeoman generator for ReactJS and Webpack


Keywords
yeoman-generator, reactjs, reactor, webpack, scaffold, framework, component, front-end, flux, reflux, app
License
MIT
Install
npm install generator-reactor@1.3.3

Documentation

generator-reactor

Build Status

Yeoman generator for ReactJS - lets you quickly set up a project including karma test runner and Webpack module system. Using NPM Scripts, Karma and WebPack by default.

Example

http://willmendesneto.github.io/generator-reactor/

Usage

Install generator-reactor:

npm install -g generator-reactor

Make a new directory, and cd into it:

mkdir my-new-project && cd $_

Run yo reactor, optionally passing an app name:

yo reactor [app-name]

Run npm run build for building and npm run start-dist for preview in the browser at localhost.

Generators

Available generators:

and for Flux or Reflux :

App

Sets up a new ReactJS app, generating all the boilerplate you need to get started. The app generator also facilitates the following:

  1. Configures Webpack to modularise the app enabling loading of various file formats e.g. JSON, CSS, PNG, etc.
  2. Configures Karma to run all tests.
  3. Watches for changes and recompiles JS and refreshes the browser.

Example:

yo reactor

Component

Generates a JSX component in src/scripts/components, its corresponding test in src/spec/components and its style in src/style.

Example:

yo reactor:component foo  //or just: yo reactor:c foo

Produces src/components/Foo.js (javascript - JSX):

'use strict';

import React from 'react';

import './Checkup.scss';

export default class Checkup extends React.Component {

  constructor(props, context) {
    super(props, context);
  }

  render() {
    return (
      <div>
        <p>Content for Checkup</p>
      </div>
    );
  }
}

And test/spec/components/Foo.js (javascript - jasmine):

'use strict';

import Foo from 'components/Foo';
import React from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-addons-test-utils';

describe('Foo',  () => {
  let renderedComponent;

  beforeEach(() => {
    let node = document.createElement('div');
    renderedComponent = ReactDOM.render(<Foo />, node);
  });

  afterEach(() => {
    ReactDOM.unmountComponentAtNode(document);
  });

  it('should create a new instance of Foo', function () {
    expect(renderedComponent).toBeDefined();
  });

});

Create stylesheet of component

You can add a stylesheet file for your new component using - style flag:

yo reactor:c foo --style

And src/styles/Foo.css (or .sass, .less etc...) will be created:

.Foo{
  border: 1px dashed #f00;
}

rich flag

For all you lazy programmers out there, we've added another shortcut - rich flag:

yo reactor:c foo --rich

This will give you all of react component's most common stuff :

 'use strict';

 import React from 'react';

 import './Foo.scss';

 export default class Foo extends React.Component {

   constructor(props, context) {
     super(props, context);
   }

   getInitialState() {
      return({});
   }

   getDefaultProps() {}
   componentWillMount() {}
   componentDidMount() {}
   shouldComponentUpdate() {}
   componentDidUpdate() {}
   componentWillUnmount() {}

   render() {
     return (
       <div>
         <p>Content for Foo</p>
       </div>
     );
   }
 }

Just remove those you don't need, then fill and space out the rest.

Class

Generates a JSX component in src/scripts/components, its corresponding test in src/spec/components and its style in src/style.

Example:

yo reactor:component foo  //or just: yo reactor:c foo

Produces src/components/Foo.js (javascript - JSX):

'use strict';

import React from 'react';

import './Foo.scss';

export default class Foo extends React.createClass({

  render() {
    return (
      <div>
        <p>Content for Checkup-class</p>
      </div>
    );
  }
});


And test/spec/components/Foo.js (javascript - jasmine):

'use strict';

import Foo from 'components/Foo';
import React from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-addons-test-utils';

describe('Foo',  () => {
  let renderedComponent;

  beforeEach(() => {
    let node = document.createElement('div');
    renderedComponent = ReactDOM.render(<Foo />, node);
  });

  afterEach(() => {
    ReactDOM.unmountComponentAtNode(document);
  });

  it('should create a new instance of Foo', function () {
    expect(renderedComponent).toBeDefined();
  });

});

Create stylesheet of class

You can add a stylesheet file for your new class using - style flag:

yo reactor:class foo --style

And src/styles/Foo.css (or .sass, .less etc...) will be created:

.Foo{
  border: 1px dashed #f00;
}

rich flag

For all you lazy programmers out there, we've added another shortcut - rich flag:

yo reactor:c foo --rich

This will give you all of react component's most common stuff :

'use strict';

import React from 'react';

import './Foo.scss';

export default class Foo extends React.createClass({

  getInitialState() {
    return({});
  },

  getDefaultProps() {},
  componentWillMount() {},
  componentDidMount() {},
  shouldComponentUpdate() {},
  componentDidUpdate() {},
  componentWillUnmount() {},

  render() {
    return (
      <div>
        <p>Content for Foo</p>
      </div>
    );
  }
});

Just remove those you don't need, then fill and space out the rest.

Action

When using Flux or Reflux architecture, it generates an actionCreator in src/actions and it's corresponding test in src/spec/actions.

Example:

yo reactor:action bar //or just: yo reactor:a bar

Will create a file - src/actions/BarActionCreators.js

if 'architecture' is Flux, it Produces :

'use strict';

var ActActionCreators = {

}

export default ActActionCreators;

And if it's Reflux:

'use strict';

import Reflux from 'reflux';

const BarActionCreators  =  Reflux.createActions([

]);


export default BarActionCreators;

and same test for both architectures:

'use strict';

import action from 'actions/BarActionCreators';

describe('BarActionCreators', () => {

  it('should be defined', () => {
    expect(action).toBeDefined();
  });

});

Store

When using Flux or Reflux architecture, it generates a store in src/stores and it's corresponding test in src/spec/stores.

Example:

yo reactor:store baz //or just: yo reactor:s baz

Will create a file - src/stores/BazStore.js

if 'architecture' is Flux, it Produces :

'use strict';

import {EventEmitter} from 'events';
import assign from 'object-assign';
import BazStoreDispatcher from '../dispatcher/BazStoreDispatcher';

let BazStore = assign({}, EventEmitter.prototype, {});

BazStore.dispatchToken = BazStoreDispatcher.register( action => {

  switch(action.type) {
    default:
  }

});

export default BazStore;


module.exports = BazStore;

And if it's Reflux:

'use strict';

import Reflux from 'reflux';
import Actions from 'actions/..';

const BazStore = Reflux.createStore({
  listenables: Actions,
});

export default BazStore;

and same test for both architectures:

'use strict';

import BazStore from 'stores/BazStoreDispatcher';

describe('BazStore', () => {
  it('should be defined', () => {
    expect(classedName).toBeDefined();
  });
});

Options

Options are available as additional installs to the initial application generation phase.

ReactRouter

A complete routing library for React. This option only adds the basic hooks to get started with react router.

styles language

css, sass, scss, less or stylus

Sets the style file's template and extension

architecture

flux or reflux

Testing

Running npm test will run the unit tests with karma. Tests are written using Jasmine by default.

Further Information

Modules

Each component is a module and can be required using the Webpack module system. Webpack uses Loaders which means you can also require CSS and a host of other file types. Read the Webpack documentation to find out more.

NPM Scripts

Out the box, the generator uses npm scripts configured with the following:

  1. webpack: uses the webpack to load all required modules and output to a single JS file src/scripts/components/main.js. This is included in the src/index.html file by default and will reload in the browser as and when it is recompiled.
  2. webpack-dev-server: uses the webpack-dev-server to watch for file changes and also serve the webpack app in development.
  3. karma: uses the karma to load the Karma configuration file karma.conf.js located in the project root. This will run all tests using PhantomJS by default but supports many other browsers.
  4. editorconfig-tools: check and validate all files application (src and test folders) based in .editorconfig params.

Stylesheet

Included in the project with options:

  • CSS;
  • SASS;
  • SCSS;
  • LESS;
  • STYLUS;

Running Tests

npm test