When exploring a new framework or technology, it is nice to have a seed project or sandbox so you can just get in and start playing. Let's build our React playground using npm, browserify, 6to5, and more within WebStorm!
Once that's done we'll have a bunch of node modules in our project. I'm going to go ahead and create an html file, let's call it index, and create a new file in a source directory called app.jsx and hit OK. With this app.jsx file we can start watching for the file changes and bundling it up and transforming it into the output file that we want.
I'll go ahead and say, "Watchify transform using 6to5-ify," meaning pass all that JSX and ES6 stuff through this transformation using our source/app.jsx file and output to a dist/bundle.js file. I like to say -v for verbose which will log out every time the file changes. I like to see that that's working.
I'll go ahead and hit enter. You'll see that we get our first error, just meaning that the dist folder isn't created. I'll go ahead and do that directory dist. Once I run this again, hit enter, you'll see that it has written a new file which has bundled up some of that basic Browserify code right now.
To see if this is working we'll go ahead and require React. We'll say var React = require React and the React create class, render. I'll use the ES6 syntax for this. You'll see in WebStorm right now since I began typing that there's some red code here and it's throwing some errors.
Once I hit save you'll see that it writes out all the changes to my bundle file. I'll go ahead and create an app from this that we can use to React render app. We'll place that in a document get element by ID. We'll create something called example.
If we switch over to our index file and load our script here, we're going to load the bundle file and then create a div called example. Hit save. You can see that if we open this file in Chrome it will render out hello world for us. We can change this to make sure it's working. It will say, "Hello working." Hit save. The file updated here. Refresh, and it says, "Hello working."
Now because I might want to share this with someone else I'm going to copy this command that we ran. I'm going to say npm emit. This is going to great a package.json file. I'll just accept all the defaults. Once our package.json file is created you can see it has all those dependencies we already installed. I'm going to set up a script called watch.
Now that we have this same command as a npm script this time we can simply say, "npm run watch," hit enter, and it's sitting there and watching for any sort of change. Hit save. You'll see it's writing out the file. Hit refresh, and everything is working.
Now if you want to share your project with someone else, we'll go ahead and create a new readme and say, "npm install and npm run watch". That's all they need to do to get it installed and running. Since we want to keep this dist folder but we don't want to keep this in version control we'll create a file gitignore.
We want to ignore all of the files in this folder, but we don't want to ignore the gitignore file itself. This way it will keep this folder but ignore any files in it. Then we'll create a gitignore file on the root of our project gitignore and ignore hidden folder called idea and node modules.
That should be good enough for our needs because you never want to check in your node modules and you usually don't want to check in this configuration for WebStorm or whatever tool you're using. WebStorm has a built in thing called Share Project on GitHub.
I already configured my account, so I just have to enter my password. We'll just keep the repo name as React-Playground. I'm going to ignore the description, hit share. It will ask me which files I want to commit or basically which I want to do a git-add on.
You can see it's already ignoring the files that I specified, the node modules and everything. This looks right, so I'll just hit OK. Then once I hop over to GitHub and under my account under repositories you can see I have a React-Playground where I could just send someone this link. They could clone it, run npm install, run npm, run watch, and they'd have this Playground up and running for themselves.