Bec d'état - Rebecca Scott

Blog | Links | Archive
About | Resume | Advisor profile | Projects | Contact

~/Build pipeline for React

27 Aug 2015

I’m going to eshew Bower and just use NPM as the package manager. I’ve already got Node and NPM installed. The example site I’m creating is survey-thing - a simple thing for creating surveys. I also have a stub package which just includes a script for setting up the environment and a simple landing page.

I’m going to use all the shinies - Browserify for CommonJS modules and JSX instead of the seperate .js/.html structure of a typical AngularJS application, using Babel for ES6 features and SASS for stylesheet preprocessing.

Primer: Nancy as a static server

The site will be hosted in a simple Nancy app so the built React application will be output to src\SurveyThing\app. Host this in Nancy with a static convention - this example is for an ASP.NET site using OWIN.

Install some NuGet packages (Microsoft.Owin, Microsoft.Owin.Host.SystemWeb, Nancy and Nancy.Owin).

Add these values to the web.config file, within the configuration element:

	<add key="owin:HandleAllRequests" value="true" />
	<modules runAllManagedModulesForAllRequests="true"/>

Add Startup and Bootstrapper classes:

public class Startup
    public void Configuration(IAppBuilder app)
            .UseNancy(new NancyOptions
                Bootstrapper = new Bootstrapper()

public class Bootstrapper : DefaultNancyBootstrapper
    protected override void ConfigureConventions(NancyConventions nancyConventions)
        Conventions.StaticContentsConventions.AddDirectory("/", "app");


If you create \app\test.html inside the Nancy site’s root it should be served from https://localhost:PORT/test.html. To serve \app\index.html when requesting https://localhost:PORT a static route needs to added:

public class StaticModule : NancyModule
    public StaticModule()
        Get["/"] = _ => Response.AsFile("app/index.html");

Set up Gulp

I want to use:

There’s a fair bit happening in the gulpfile.js script but here are some highlights.

The 'code' step uses browserify to set up the CommonJS module system and uses app/layout/index.jsx as the entry point to the application. Babel is used to take advantage of ES6 and the script is minified.

 gulp.task('code', function(){
		entries: 'app/layout/layout.jsx',
		extensions: ['.jsx'],
		debug: true

CSS preprocessing is done using SASS. The entry point is app/site.scss.

gulp.task('css', function(){
		.pipe(sass().on('error', sass.logError))

The template HTML file is copied verbatim to the destination path.

gulp.task('html', function(){

The default gulp task runs all the main tasks ('vendor' is not shown, it just bundles up vendor CSS and JS into vendor.css and vendor.js respectively).

gulp.task('default', ['code', 'css', 'html', 'vendor']);

The 'watch' task splits up the workload to keep live rebuilds snappy:

gulp.task('watch', ['default'], function() {
		['app/**/*.css', 'app/**/*.scss'],

React app structure

The structure is pretty trivial at this point:


index.html references the stylesheets and scripts, and includes a div with an ID that will be used by the React app:

<!DOCTYPE html>
		<title>Survey Thing</title>
		<link rel="stylesheet" type="text/css" href="site.css"/>
		<link rel="stylesheet" type="text/css" href="vendor.css"/>
		<div id="content"></div>

		<script src="site.js"></script>
		<script src="vendor.js"></script>

In layout.jsx, ‘Hello world’ is rendered into the div using inline markup:

import React from 'react';

	<h1>Hello, world!</h1>,

Running gulp will run the 'default' task, which should build the app and write the artifacts to \src\SurveyThing\app. Running gulp watch will run the 'watch' task and rebuild whenever the monitored files change.

If everything works, we should have a happy ‘hello world’ page. I made mine pink because pink is cool to test SASS:

Minifying the site.js file

Without minification the site.js file is huge (1.5M). To conditionally minify the script I check for a --release argument:

var yargs = require('yargs');

var buildRelease = yargs.argv.release || false;

gulp.task('code', function(){
	var pipeline = browserify({
		entries: 'app/layout/layout.jsx',
		extensions: ['.jsx'],
		debug: true

	if (buildRelease) {


Now running gulp --release results in a much more managable 185k file.


Check out the survey-thing repo for updates and lols.

Resources & further reading: