├── .babelrc
├── .gitignore
├── README.md
├── package.json
├── public
├── favicon.ico
└── robots.txt
├── razzle.config.js
├── src
├── App.css
├── App.js
├── App.test.js
├── Home.css
├── Home.js
├── Intro.js
├── Logo.js
├── Welcome.js
├── client.js
├── index.js
├── react.svg
├── server.js
└── setupTests.js
└── yarn.lock
/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "presets": ["razzle/babel"],
3 | "plugins": ["react-loadable/babel"]
4 | }
5 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | logs
2 | *.log
3 | npm-debug.log*
4 | .DS_Store
5 |
6 | coverage
7 | node_modules
8 | build
9 | .env.local
10 | .env.development.local
11 | .env.test.local
12 | .env.production.local
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | 
2 |
3 | [](https://circleci.com/gh/jaredpalmer/razzle/tree/master)  [](https://badge.fury.io/js/razzle)
4 |
5 | Universal JavaScript applications are tough to setup. Either you buy into a framework like [Next.js](https://github.com/zeit/next.js) or [react-server](https://github.com/redfin/react-server), fork a boilerplate, or set things up yourself. Razzle aims to fill this void by abstracting all the required tooling for your universal JavaScript application into a single dependency, and then leaving the rest of the architectural decisions about frameworks, routing, and data fetching up to you.
6 |
7 | **Razzle comes with the "battery-pack included"**:
8 |
9 | - :fire: Universal Hot Module Replacement, so both the client and server update whenever you make edits. No annoying restarts necessary
10 | - Comes with your favorite ES6 JavaScript goodies (through `babel-preset-razzle`)
11 | - Comes with the same CSS setup as [create-react-app](https://github.com/facebookincubator/create-react-app)
12 | - Works with [React](https://github.com/facebook/react), [Preact](https://github.com/developit/preact), [Elm](http://elm-lang.org/), [Reason-React](https://github.com/jaredpalmer/razzle/tree/master/examples/with-reason-react), [Inferno](https://github.com/infernojs), and [Rax](https://github.com/alibaba/rax) as well as [Angular](https://github.com/angular/angular) and [Vue](https://github.com/vuejs/vue) if that's your thing
13 | - Escape hatches for customization via `.babelrc` and `razzle.config.js`
14 | - [Jest](https://github.com/facebook/jest) test runner setup with sensible defaults via `razzle test`
15 |
16 |
17 | ## Quick Start
18 |
19 | ```bash
20 | npm install -g create-razzle-app
21 |
22 | create-razzle-app my-app
23 | cd my-app
24 | npm start
25 | ```
26 |
27 | Then open http://localhost:3000/ to see your app. Your console should look like this:
28 |
29 |
30 |
31 | **That's it**. You don't need to worry about setting up multiple webpack configs or other build tools. Just start editing `src/App.js` and go!
32 |
33 | Below is a list of commands you will probably find useful.
34 |
35 | ### `npm start` or `yarn start`
36 |
37 | Runs the project in development mode.
38 | You can view your application at `http://localhost:3000`
39 |
40 | The page will reload if you make edits.
41 |
42 | ### `npm run build` or `yarn build`
43 | Builds the app for production to the build folder.
44 |
45 | The build is minified and the filenames include the hashes.
46 | Your app is ready to be deployed!
47 |
48 | ### `npm run start:prod` or `yarn start:prod`
49 | Runs the compiled app in production.
50 |
51 | You can again view your application at `http://localhost:3000`
52 |
53 | ### `npm test` or `yarn test`
54 |
55 | Runs the test watcher (Jest) in an interactive mode.
56 | By default, runs tests related to files changed since the last commit.
57 |
58 | ### `npm start -- --inspect` or `yarn start -- --inspect`
59 |
60 | To debug the node server, you can use `razzle start --inspect`. This will start the node server and enable the inspector agent. For more information, see [this](https://nodejs.org/en/docs/inspector/).
61 |
62 | ---
63 |
64 |
65 |
66 | **Table of Contents**
67 |
68 | - [Customization](#customization)
69 | - [Extending Babel Config](#extending-babel-config)
70 | - [Extending Webpack](#extending-webpack)
71 | - [Environment Variables](#environment-variables)
72 | - [Adding Temporary Environment Variables In Your Shell](#adding-temporary-environment-variables-in-your-shell)
73 | - [Windows (cmd.exe)](#windows-cmdexe)
74 | - [Linux, macOS (Bash)](#linux-macos-bash)
75 | - [Adding Environment Variables In `.env`](#adding-environment-variables-in-env)
76 | - [What other `.env` files are can be used?](#what-other-env-files-are-can-be-used)
77 | - [How Razzle works (the secret sauce)](#how-razzle-works-the-secret-sauce)
78 | - [Inspiration](#inspiration)
79 | - [Author](#author)
80 | - [Contributors](#contributors)
81 |
82 |
83 |
84 | ## Customization
85 |
86 | ### Extending Babel Config
87 |
88 | Razzle comes with most of ES6 stuff you need. However, if you want to add your own babel transformations, just add a `.babelrc` file to the root of your project.
89 |
90 | ```json
91 | {
92 | "presets": [
93 | "razzle/babel",
94 | "stage-0"
95 | ]
96 | }
97 | ```
98 |
99 | ### Extending Webpack
100 |
101 | You can also extend the underlying webpack config. Create a file called `razzle.config.js` in your project's root.
102 |
103 | ```js
104 | // razzle.config.js
105 |
106 | module.exports = {
107 | modify: (config, {target, dev}, webpack) => {
108 | // do something to config
109 |
110 | return config
111 | }
112 | }
113 | ```
114 |
115 | A word of advice: `razzle.config.js` is an escape hatch. However, since it's just JavaScript, you can and should publish your `modify` function to npm to make it reusable across your projects. For example, imagine you added some custom webpack loaders and published it as a package to npm as `my-razzle-modifictions`. You could then write your `razzle.config.js` like so:
116 |
117 | ```
118 | // razzle.config.js
119 | const modify = require('my-razzle-modifictions');
120 |
121 | module.exports = {
122 | modify
123 | }
124 | ```
125 |
126 | Last but not least, if you find yourself needing a more customized setup, Razzle is _very_ forkable. There is one webpack configuration factory that is 300 lines of code, and 4 scripts (`build`, `start`, `test`, and `init`). The paths setup is shamelessly taken from [create-react-app](https://github.com/facebookincubator/create-react-app), and the rest of the code related to logging.
127 |
128 | ### Environment Variables
129 |
130 | **The environment variables are embedded during the build time.** Since Razzle produces a static HTML/CSS/JS bundle and an equivalent static bundle for your server, it cannot possibly read them at runtime.
131 |
132 | - `process.env.RAZZLE_PUBLIC_DIR`: Path to the public directory.
133 | - `process.env.RAZZLE_ASSETS_MANIFEST`: Path to a file containing compiled asset outputs
134 | - `process.env.REACT_DEV_BUNDLE_PATH`: Relative path to where React will be bundled during development. Unless you are modifying the output path of your webpack config, you can safely ignore this. This path is used by `react-error-overlay` and webpack to power up the fancy runtime error iframe. For example, if you are using common chunks and an extra entry to create a vendor bundle with stuff like react, react-dom, react-router, etc. called `vendor.js`, and you've changed webpack's output to `[name].js` in development, you'd want to set this environment variable to `/static/js/vendor.js`. If you do not make this change, nothing bad will happen, you will simply not get the cool error overlay when there are runtime errors. You'll just see them in the console. Note: This does not impact production bundling.
135 | - `process.env.VERBOSE`: default is false, setting this to true will not clear the console when you make edits in development (useful for debugging).
136 | - `process.env.PORT`: default is `3000`, unless changed
137 | - `process.env.HOST`: default is `0.0.0.0`
138 | - `process.env.NODE_ENV`: `'development'` or `'production'`
139 | - `process.env.BUILD_TARGET`: either `'client'` or `'server'`
140 |
141 | You can create your own custom build-time environment variables. They must start
142 | with `RAZZLE_`. Any other variables except the ones listed above will be ignored to avoid accidentally exposing a private key on the machine that could have the same name. Changing any environment variables will require you to restart the development server if it is running.
143 |
144 | These environment variables will be defined for you on `process.env`. For example, having an environment variable named `RAZZLE_SECRET_CODE` will be exposed in your JS as `process.env.RAZZLE_SECRET_CODE`.
145 |
146 | ### Adding Temporary Environment Variables In Your Shell
147 |
148 | Defining environment variables can vary between OSes. It’s also important to know that this manner is temporary for the
149 | life of the shell session.
150 |
151 | #### Windows (cmd.exe)
152 |
153 | ```cmd
154 | set RAZZLE_SECRET_CODE=abcdef&&npm start
155 | ```
156 |
157 | (Note: the lack of whitespace is intentional.)
158 |
159 | #### Linux, macOS (Bash)
160 |
161 | ```bash
162 | RAZZLE_SECRET_CODE=abcdef npm start
163 | ```
164 |
165 | ### Adding Environment Variables In `.env`
166 |
167 | To define permanent environment variables, create a file called .env in the root of your project:
168 |
169 | ```
170 | RAZZLE_SECRET_CODE=abcdef
171 | ```
172 |
173 | #### What other `.env` files are can be used?
174 |
175 | * `.env`: Default.
176 | * `.env.local`: Local overrides. **This file is loaded for all environments except test.**
177 | * `.env.development`, `.env.test`, `.env.production`: Environment-specific settings.
178 | * `.env.development.local`, `.env.test.local`, `.env.production.local`: Local overrides of environment-specific settings.
179 |
180 | Files on the left have more priority than files on the right:
181 |
182 | * `npm start`: `.env.development.local`, `.env.development`, `.env.local`, `.env`
183 | * `npm run build`: `.env.production.local`, `.env.production`, `.env.local`, `.env`
184 | * `npm test`: `.env.test.local`, `.env.test`, `.env` (note `.env.local` is missing)
185 |
186 | These variables will act as the defaults if the machine does not explicitly set them.
187 | Please refer to the [dotenv documentation](https://github.com/motdotla/dotenv) for more details.
188 |
189 | >Note: If you are defining environment variables for development, your CI and/or hosting platform will most likely need
190 | these defined as well. Consult their documentation how to do this. For example, see the documentation for [Travis CI](https://docs.travis-ci.com/user/environment-variables/) or [Heroku](https://devcenter.heroku.com/articles/config-vars).
191 |
192 | ## How Razzle works (the secret sauce)
193 |
194 | **tl;dr**: 2 configs, 2 ports, 2 webpack instances, both watching and hot reloading the same filesystem, in parallel during development and a little `webpack.output.publicPath` magic.
195 |
196 | In development mode (`razzle start`), Razzle bundles both your client and server code using two different webpack instances running with Hot Module Replacement in parallel. While your server is bundled and run on whatever port your specify in `src/index.js` (`3000` is the default), the client bundle (i.e. entry point at `src/client.js`) is served via `webpack-dev-server` on a different port (`3001` by default) with its `publicPath` explicitly set to `localhost:3001` (and not `/` like many other setups do). Then the server's html template just points to the absolute url of the client JS: `localhost:3001/static/js/client.js`. Since both webpack instances watch the same files, whenever you make edits, they hot reload at _exactly_ the same time. Best of all, because they use the same code, the same webpack loaders, and the same babel transformations, you never run into a React checksum mismatch error.
197 |
198 | ## Inspiration
199 |
200 | - [palmerhq/backpack](https://github.com/palmerhq/backpack)
201 | - [nytimes/kyt](https://github.com/nytimes/kyt)
202 | - [facebookincubator/create-react-app](https://github.com/facebookincubator/create-react-app)
203 | - [humblespark/sambell](https://github.com/humblespark/sambell)
204 | - [zeit/next.js](https://github.com/zeit/next.js)
205 |
206 |
207 | #### Author
208 | - [Jared Palmer](https://twitter.com/jaredpalmer)
209 |
210 | ---
211 | MIT License
212 |
213 | ## Contributors
214 |
215 | Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)):
216 |
217 |
218 | | [
Jared Palmer](http://jaredpalmer.com)
[💬](#question-jaredpalmer "Answering Questions") [💻](https://github.com/jaredpalmer/razzle/commits?author=jaredpalmer "Code") [🎨](#design-jaredpalmer "Design") [📖](https://github.com/jaredpalmer/razzle/commits?author=jaredpalmer "Documentation") [💡](#example-jaredpalmer "Examples") [🤔](#ideas-jaredpalmer "Ideas, Planning, & Feedback") [👀](#review-jaredpalmer "Reviewed Pull Requests") [⚠️](https://github.com/jaredpalmer/razzle/commits?author=jaredpalmer "Tests") [🔧](#tool-jaredpalmer "Tools") | [
Jari Zwarts](https://jari.io)
[💬](#question-jariz "Answering Questions") [💻](https://github.com/jaredpalmer/razzle/commits?author=jariz "Code") [🤔](#ideas-jariz "Ideas, Planning, & Feedback") [🔌](#plugin-jariz "Plugin/utility libraries") [👀](#review-jariz "Reviewed Pull Requests") | [
Dan Abramov](http://twitter.com/dan_abramov)
[💻](https://github.com/jaredpalmer/razzle/commits?author=gaearon "Code") [🤔](#ideas-gaearon "Ideas, Planning, & Feedback") | [
Eric Clemmons](http://ericclemmons.github.com/)
[💻](https://github.com/jaredpalmer/razzle/commits?author=ericclemmons "Code") [🤔](#ideas-ericclemmons "Ideas, Planning, & Feedback") |
219 | | :---: | :---: | :---: | :---: |
220 |
221 |
222 | This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!
223 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "my-razzle-app",
3 | "version": "0.1.0",
4 | "license": "MIT",
5 | "scripts": {
6 | "start": "razzle start",
7 | "build": "razzle build",
8 | "test": "razzle test --env=jsdom",
9 | "start:prod": "NODE_ENV=production node build/server.js"
10 | },
11 | "dependencies": {
12 | "express": "^4.16.2",
13 | "raf": "^3.4.0",
14 | "razzle": "^0.8.4",
15 | "react": "^16.0.0",
16 | "react-dom": "^16.0.0",
17 | "react-loadable": "^5.3.1",
18 | "react-router-dom": "^4.2.2"
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/glennreyes/razzle-experiments/8a10c0e461c4fc049abf0ce72d498da33629113a/public/favicon.ico
--------------------------------------------------------------------------------
/public/robots.txt:
--------------------------------------------------------------------------------
1 | User-agent: *
2 |
3 |
--------------------------------------------------------------------------------
/razzle.config.js:
--------------------------------------------------------------------------------
1 | const { ReactLoadablePlugin } = require('react-loadable/webpack');
2 |
3 | module.exports = {
4 | modify: (config, { target }) =>
5 | target === 'web'
6 | ? {
7 | ...config,
8 | plugins: [
9 | ...config.plugins,
10 | new ReactLoadablePlugin({
11 | filename: './build/react-loadable.json',
12 | }),
13 | ],
14 | }
15 | : config,
16 | };
17 |
--------------------------------------------------------------------------------
/src/App.css:
--------------------------------------------------------------------------------
1 | body {
2 | margin: 0;
3 | padding: 0;
4 | font-family: sans-serif;
5 | }
--------------------------------------------------------------------------------
/src/App.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import Route from 'react-router-dom/Route';
3 | import Switch from 'react-router-dom/Switch';
4 | import Home from './Home';
5 | import './App.css';
6 |
7 | const App = () => (
8 |
5 | To get started, edit src/App.js
or src/Home.js
and
6 | save to reload.
7 |