Code Monkey home page Code Monkey logo

minimal-hapi-react-webpack's Introduction

Minimal Hapi + React + Webpack + HMR Sandbox

This starter kit connects a hapi.js server with webpack dev server with react hot module reloading all set up.

Very basic production config is also in place.

You can use this starter kit to efficiently develop react components and applications that might talk to a Hapi.js backend.

The primary purpose of this repo however is to demonstrate how to wire together these technologies. The config files are organized to have an obvious single source of truth. I've added plenty of comments to explain which config does what.

Features

  • React (client) hot module reloading is configured with react-transform-hmr
  • ESLint error & warnings are logged to browser console
  • Application server (Hapi.js) is separate from webpack dev server (Express.js)
  • Hapi.js proxies requests for webpack assets to webpack dev server
  • Hapi.js is configured to serve static files, and a couple example routes
  • A separate sandbox entry point is set up for white room component development
  • Sample client-side React view with a counter to easily test hot module reloading (any time the page does a full reload, the counter resets).
  • Server-side React views instead of index.html (see /server-views). Isomorphism is easy to configure if needed using the same dependency.
  • Automatic application server reloading using nodemon
  • Babel transpilation of ES6 and JSX into browser-compatible ES5, both for server and client code. Use all the fancy stuff today!
  • Webpack assets are generated with content hashes in filenames for easy cache busting
  • Webpack assets are optimized with Uglify.js on production, removing dead code (TODO: add server-only code)
  • Webpack asset URLs are recorded in a file, and a helper method is provided to extract them for usage in HTML / JSX.
  • Basic testing setup with Mocha, Chai and jsdom

Configuration

  • Raw configuration parameters are stored in src/config/variables.js – this is the source of truth compiled for easy understanding of what goes where.
  • Webpack configuration is compiled using the parameters above in src/config/webpack-config.js
  • A couple tooling config files (e.g. .eslintignore) are generated from the config parameters as well, again, to make it clear what exactly is affected by the configs.

Usage

Dependencies

  • You need node.js v4+ installed globally on your machine. If using OS X, best to install node using Homebrew. Node v0.12 might work too, but I don't test it anymore.
  • npm dependencies are required to build and run the app in production.
  • npm devDependencies are additionally required to build and run the app in development.
  • Core dependencies: Hapi, Webpack, Babel 6, React, react-transform-hmr.

Development

  • git clone this repo to your local machine
  • cd into project folder
  • npm install to install dependencies
  • npm run dev to start Hapi application server
  • npm run webpack in another terminal session to start webpack dev server with hot module reloading
  • Visit http://localhost:3000 and open your browser's web development console
  • Try changing and saving src/components/counter.css – you'll see the changes applied to the page without the page itself reloading. How cool is that, eh?
  • ESLint errors are printed into webpack dev server output and also into the browser's console. To run ESLint separately from that, do npm run lint.

Production

  • git clone and npm install
  • npm run build to generate config files and webpack assets. This means we don't need to run webpack dev server in production.
  • npm start to start up node
  • Visit http://localhost:2000 (note the port is different from dev)
  • Build something people want, acquire paying customers, retire to New Zealand.

Caveats

  • Hot module reloading only works for react components and CSS files.
  • If you change files that cause nodemon to reload, HMR will temporarily break and you'll need to reload the page manually (or make another change to another file that is covered by HMR but does not trigger a server reload).
  • If you edit webpack-config.js or change the contents of node_modules (e.g. by installing a new dependency), you'll need to restart webpack dev server.
  • (TODO) Reloading while webpack is compiling loads a broken page. Ideally it should wait for webpack to finish compiling, and only then load the page.
  • (TODO) I didn't test webpack dev server https support. Might need a couple tweaks.

Resources

SurviveJS is a good introduction to React and Webpack.

Follow Dan Abramov and check out his projects, he knows a thing or two about react and hot module reloading.

This project uses ES6 language features. If you're not into that yet, check out Understanding ES6.

If something's not working in my setup, please file an issue on Github.

License

MIT. Go nuts.

Author

Nikita Gazarov + Contributors

[email protected]

@raquo

minimal-hapi-react-webpack's People

Contributors

kaplona avatar malissa avatar raquo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

minimal-hapi-react-webpack's Issues

Update dependencies

This project has been "done" for a long time. I don't expect to add any new features, as that would break the "minimal" promise.

However, the dependencies are perpetually getting out of date as new versions of babel / hapi / webpack / etc. are released. Usually such updates don't need much work, but it's still work that adds up, and that I don't have time for especially since I've largely moved on from JS to Scala and Scala.js.

So what I'm saying is – don't expect more updates from me, but PRs are welcome.

Docker

First super job here :-)

I tried to implement it with Docker, but I can't get it up and running


FROM node:4.2.2

RUN useradd --user-group --create-home --shell /bin/false app &&\
  npm install --global [email protected]

ENV HOME=/home/app

COPY package.json npm-shrinkwrap.json $HOME/chat/
RUN chown -R app:app $HOME/*

USER app
WORKDIR $HOME/chat
RUN npm install

USER root
COPY . $HOME/chat
RUN chown -R app:app $HOME/*
USER app

CMD ["node", "run", "dev"]


chat:
  build: .
  command: node_modules/.bin/nodemon src/server.js dev
  environment:
    NODE_ENV: development
  ports:
    - '3000:3000'
  volumes:
    - .:/home/app/chat
    - /home/app/chat/node_modules

npm info it worked if it ends with ok
npm info using [email protected]
npm info using [email protected]
npm info lifecycle [email protected]~prebuild: [email protected]
npm info lifecycle [email protected]~build: [email protected]

> [email protected] build /home/app/chat
> NODE_ENV=production     node --harmony src/scripts/make.js  &&  NODE_ENV=production   webpack -p --config src/config/webpack-config.js

module.js:339
    throw err;
    ^

Error: Cannot find module '/home/app/chat/src/scripts/make.js'
    at Function.Module._resolveFilename (module.js:337:15)
    at Function.Module._load (module.js:287:25)
    at Function.Module.runMain (module.js:467:10)
    at startup (node.js:136:18)
    at node.js:963:3

The only error I got during the docker-compose build is


> [email protected] install /home/app/chat/node_modules/ws
> (node-gyp rebuild 2> builderror.log) || (exit 0)

make: Entering directory '/home/app/chat/node_modules/ws/build'
  CXX(target) Release/obj.target/bufferutil/src/bufferutil.o
bufferutil.target.mk:92: recipe for target 'Release/obj.target/bufferutil/src/bufferutil.o' failed
make: Leaving directory '/home/app/chat/node_modules/ws/build'

Regarding websockets/bufferutil#15 I tried to set dependencie in package as

"bufferutil": "^1.2.0",

But didn't solve anything... any idea is welcome :-)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.