Skip to content

Instantly share code, notes, and snippets.

@n3dst4
Created September 22, 2014 07:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n3dst4/2e31b6e6b5586823c601 to your computer and use it in GitHub Desktop.
Save n3dst4/2e31b6e6b5586823c601 to your computer and use it in GitHub Desktop.
Troubleshooting how Browserify transforms get applied (i.e. why isn't my code getting processed by Reactify?)

If you're using Browserify and Reactify to write React components and build them for the browser, you may, depending on your setup, see errors along the lines of "unexpected token <", which means that your code is not getting Reactified properly. You will either see this in Gulp, because some subsequent step in the pipeline can't parse your code, or in the browser.

Here's how browserify decides how to apply transforms:

  1. Anything specified when you actually call browserify (i.e. in a gulpfile or on the command line) will be run first. Calls here can specify {global: true}, which will make them be applied to dependencies as well as code in the current module.
  2. If there is a browserify.transform key in package.json, it can list transforms which should be applied, in the order that they should be applied.

You need to specify reactify before anything else which is going to want to parse the code, because obvs. it isn't valid es5 until it's been reactified (so e.g. having ["browserify-shim", "reactify"] is an error because browserify-shim will try to parse your ESX code before reactify has a chance to fix it.) This is true of all high-level source transforms, like coffescript, purescript etc.

Transforms listed in package.json only affect the code in that actual package. Code brought in from node_modules will only be affected by transforms set up its own package.json.

So if you are writing a reusable React component, you must remember to put browserify: { transform: ["reactify"] } in the package.json for it.

To get the benefit of reactify's ES6 features, you need to give it the es6 option. You can do this in package.json as follows:

  browserify: {
    transform: [
      ["reactify", {"es6": true}]
    ] 
  }

I have taken to specifying reactify in packagae.json always, and not mentioning it in the actual gulpfile. Conversely, where I choose to shim out jQuery I want that to happen to all code brought in from everywhere, so I've put that in the gulpfile and added {global: true} to its options.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment