Skip to content

Commit 4e3d9a2

Browse files
committed
Optimize the project for Firebase hosting
Closes #96
1 parent b4f50c5 commit 4e3d9a2

21 files changed

+225
-176
lines changed

.gitignore

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
# Include your project-specific ignores in this file
22
# Read about how to use .gitignore: https://help.github.com/articles/ignoring-files
33

4-
build
4+
# Compiled output
5+
public/dist
6+
public/index.html
7+
8+
# Node.js and NPM
59
node_modules
610
npm-debug.log
11+
12+
# Firebase
13+
firebase-debug.log

README.md

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@
99
[![In progress](https://img.shields.io/waffle/label/koistya/react-static-boilerplate/in%20progress.svg?style=flat-square)](https://waffle.io/koistya/react-static-boilerplate)
1010

1111
> Single-page application boilerplate and tooling powered by [React](http://facebook.github.io/react/)
12-
> and [Redux](http://redux.js.org/). It's optimized for generating static websites from React components
13-
> ready to be deployed to a [CDN](https://en.wikipedia.org/wiki/Content_delivery_network) hosting
14-
> such as [GitHub Pages](https://pages.github.com/), [Amazon S3](http://docs.aws.amazon.com/AmazonS3/latest/dev/WebsiteHosting.html)
15-
> or [Firebase](https://firebase.google.com/docs/hosting/).
12+
> and [Redux](http://redux.js.org/). It's optimized for generating a static website from React components
13+
> ready to be deployed to [Firebase](https://firebase.google.com/) CDN hosting.
1614
1715
**The work is being sponsored by:**
1816

@@ -42,32 +40,36 @@
4240
&nbsp; &nbsp;[Code-splitting](https://github.com/webpack/docs/wiki/code-splitting) and async chunk loading via [Webpack](https://webpack.github.io/) and [ES6 System.import()](http://www.2ality.com/2014/09/es6-modules-final.html)<br>
4341
&nbsp; &nbsp; ✓ Hot Module Replacement ([HMR](https://webpack.github.io/docs/hot-module-replacement.html)) /w [React Hot Loader](http://gaearon.github.io/react-hot-loader/)<br>
4442
&nbsp; &nbsp; ✓ Cross-device testing with [Browsersync](https://browsersync.io/) (see [`run.js#start`](run.js))<br>
45-
&nbsp; &nbsp; ✓ Easy deployment to [GitHub Pages](https://pages.github.com/), [Amazon S3](http://davidwalsh.name/hosting-website-amazon-s3) or [Firebase](https://www.firebase.com/) (see [`run.js#publish`](run.js))<br>
4643
&nbsp; &nbsp; ✓ 24/7 community support on [Gitter](https://gitter.im/koistya/react-static-boilerplate); customization requests on [Codementor](https://www.codementor.io/koistya)<br>
4744

4845

4946
### Directory Layout
5047

5148
```shell
5249
.
53-
├── /build/ # The folder for compiled output
54-
├── /node_modules/ # 3rd-party libraries and utilities
55-
├── /components/ # Shared/generic UI components
56-
│ ├── /layout/ # Layout component
57-
│ ├── /button/ # Button component
50+
├── /components/ # Shared or generic UI components
51+
│ ├── /Button/ # Button component
52+
│ ├── /Layout/ # Website layout component
53+
│ ├── /Link / # Link component to be used insted of <a>
5854
│ └── /... # etc.
5955
├── /core/ # Core framework
6056
│ ├── /history.js # Handles client-side navigation
6157
│ ├── /router.js # Handles routing and data fetching
6258
│ └── /store.js # Application state manager (Redux)
59+
├── /node_modules/ # 3rd-party libraries and utilities
6360
├── /pages/ # React components for web pages
6461
│ ├── /about/ # About page
6562
│ ├── /error/ # Error page
6663
│ ├── /home/ # Home page
6764
│ └── /... # etc.
68-
├── /static/ # Static files such as favicon.ico etc.
65+
├── /public/ # Static files such as favicon.ico etc.
66+
│ ├── /dist/ # The folder for compiled output
67+
│ ├── favicon.ico # Application icon to be displayed in bookmarks
68+
│ ├── robots.txt # Instructions for search engine crawlers
69+
│ └── /... # etc.
6970
├── /test/ # Unit and integration tests
7071
├── /utils/ # Utility and helper classes
72+
│── index.html # HTML page that references application bundle
7173
│── main.js # React application entry point
7274
│── package.json # The list of project dependencies and NPM scripts
7375
│── routes.json # This list of application routes
@@ -84,8 +86,9 @@ Just clone the repo, install Node.js modules and run `npm start`:
8486
$ git clone -o react-static-boilerplate -b master --single-branch \
8587
https://github.com/koistya/react-static-boilerplate.git MyApp
8688
$ cd MyApp
89+
$ npm install -g firebase-tools
8790
$ npm install # Install project dependencies listed in package.json
88-
$ npm start # Build and launch the app, same as "node tools/start.js"
91+
$ npm start # Build and launch the app, same as `node run start`
8992
```
9093

9194
**NODE**: Make sure that you have [Node.js](https://nodejs.org/) v6 installed on your local machine.
@@ -96,7 +99,8 @@ $ npm start # Build and launch the app, same as "node tools/start.js
9699
The unit tests are powered by [chai](http://chaijs.com/) and [mocha](http://mochajs.org/).
97100

98101
```shell
99-
$ npm test
102+
$ npm run lint
103+
$ npm run test # Or, `npm run test:watch`
100104
```
101105

102106

@@ -105,14 +109,14 @@ $ npm test
105109
Update deployment URL inside `run.js` file, the run one of the following commands:
106110

107111
```shell
108-
$ npm run publish:gh # Build and publish the website to GitHub Pages
109-
$ npm run publish:s3 # Build and publish the website to Amazon S3
112+
$ firebase login # Login into Firebase and obtain an authentication token
113+
$ npm run publish # Build and publish the website to Firebase, same as `node run publish`
110114
```
111115

112116
If you need just to build the project without publishing it, run:
113117

114118
```shell
115-
$ npm run build # Build websites into a distributable format
119+
$ npm run build # Or, `node run build`, `node run build --debug`
116120
```
117121

118122

database.rules.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"rules": {
3+
".read": "auth != null",
4+
".write": "auth != null"
5+
}
6+
}

docs/recipes/deploy-to-amazon-s3.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## How to Publish Website to Amazon S3
2+
3+
`1`. Configure S3 bucket for hosting a static site:
4+
5+
http://docs.aws.amazon.com/AmazonS3/latest/dev/WebsiteHosting.html
6+
7+
`2`. Install [`s3`](https://github.com/andrewrk/node-s3-client) npm module:
8+
9+
```sh
10+
$ npm install s3 --save-dev
11+
```
12+
13+
`3`. Add deployment script to `run.js`:
14+
15+
```js
16+
tasks.set('publish', () => {
17+
const s3 = require('s3');
18+
return run('build').then(() => new Promise((resolve, reject) => {
19+
const client = s3.createClient({
20+
s3Options: {
21+
region: 'us-east-1',
22+
sslEnabled: true,
23+
},
24+
});
25+
const uploader = client.uploadDir({
26+
localDir: 'public',
27+
deleteRemoved: true,
28+
s3Params: { Bucket: 'www.example.com' }, // TODO: Update deployment URL
29+
});
30+
uploader.on('error', reject);
31+
uploader.on('end', resolve);
32+
}));
33+
});
34+
```
35+
36+
`4`. Whenever you need to publish your site to Amazon S3 simply run:
37+
38+
```sh
39+
$ node run publish --release
40+
```
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
## How to Publish a Static Site to [GitHub Pages](https://pages.github.com/)
2+
3+
`1`. Add deployment script to `run.js`:
4+
5+
```js
6+
tasks.set('publish', () => {
7+
const remote = {
8+
url: 'https://github.com/<owner>/<repo>.git', // TODO: Update deployment URL
9+
branch: 'gh-pages',
10+
};
11+
const spawn = require('child_process').spawn;
12+
const opts = { cwd: path.resolve(__dirname, './build'), stdio: ['ignore', 'inherit', 'inherit'] };
13+
const git = (...args) => new Promise((resolve, reject) => {
14+
spawn('git', args, opts).on('close', code => {
15+
if (code === 0) {
16+
resolve();
17+
} else {
18+
reject(new Error(`git ${args.join(' ')} => ${code} (error)`));
19+
}
20+
});
21+
});
22+
23+
return Promise.resolve()
24+
.then(() => run('clean'))
25+
.then(() => git('init', '--quiet'))
26+
.then(() => git('config', '--get', 'remote.origin.url')
27+
.then(() => git('remote', 'set-url', 'origin', remote.url))
28+
.catch(() => git('remote', 'add', 'origin', remote.url))
29+
)
30+
.then(() => git('ls-remote', '--exit-code', remote.url, 'master')
31+
.then(() => Promise.resolve()
32+
.then(() => git('fetch', 'origin'))
33+
.then(() => git('reset', `origin/${remote.branch}`, '--hard'))
34+
.then(() => git('clean', '--force'))
35+
)
36+
.catch(() => Promise.resolve())
37+
)
38+
.then(() => run('build'))
39+
.then(() => git('add', '.', '--all'))
40+
.then(() => git('commit', '--message', new Date().toUTCString())
41+
.catch(() => Promise.resolve()))
42+
.then(() => git('push', 'origin', `HEAD:${remote.branch}`, '--force', '--set-upstream'));
43+
});
44+
```
45+
46+
`2`. Whenever you need to publish your site to GitHub Pages simply run:
47+
48+
```sh
49+
$ node run publish --release
50+
```

firebase.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"database": {
3+
"rules": "database.rules.json"
4+
},
5+
"hosting": {
6+
"public": "public",
7+
"rewrites": [
8+
{
9+
"source": "**",
10+
"destination": "/index.html"
11+
}
12+
]
13+
}
14+
}

static/index.html renamed to index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
1010
<link rel="stylesheet" href="https://cdn.rawgit.com/tleunen/react-mdl/master/extra/material.min.css">
1111
<link rel="stylesheet" href="https://cdn.rawgit.com/isagalaev/highlight.js/master/src/styles/default.css">
12-
<link rel="apple-touch-icon" href="apple-touch-icon.png">
12+
<link rel="apple-touch-icon" href="public/apple-touch-icon.png">
1313
</head>
1414
<body>
1515
<div id="container"></div>
1616
<script src="https://cdn.rawgit.com/tleunen/react-mdl/master/extra/material.min.js"></script>
17-
<script src="/main.js"></script>
17+
<script src="/dist/main.js"></script>
1818
<!-- Google Analytics: change UA-XXXXX-Y to be your site's ID. -->
1919
<script>
2020
window.ga=function(){ga.q.push(arguments)};ga.q=[];ga.l=+new Date;

package.json

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
"babel-runtime": "^6.9.2",
3131
"browser-sync": "^2.13.0",
3232
"chai": "^3.5.0",
33-
"cpy": "^4.0.1",
3433
"css-loader": "^0.23.1",
3534
"del": "^2.2.1",
3635
"eslint": "^2.13.1",
@@ -44,7 +43,6 @@
4443
"highlight.js": "^9.5.0",
4544
"json-loader": "^0.5.4",
4645
"markdown-it": "^7.0.0",
47-
"mkdirp": "^0.5.1",
4846
"mocha": "^2.5.3",
4947
"path-to-regexp": "^1.5.3",
5048
"pixrem": "^3.0.1",
@@ -93,16 +91,15 @@
9391
},
9492
"scripts": {
9593
"eslint": "eslint components core pages test utils main.js run.js webpack.config.js",
96-
"stylelint": "stylelint \"components/**/*.css\" \"routes/**/*.css\"",
94+
"stylelint": "stylelint \"components/**/*.css\" \"pages/**/*.css\"",
9795
"lint": "npm run eslint && npm run stylelint",
9896
"test": "mocha --compilers js:babel-register",
9997
"test:watch": "mocha --compilers js:babel-register --reporter min --watch",
10098
"clean": "node run clean",
101-
"build": "node run build --release",
102-
"build:debug": "node run build",
103-
"publish": "node run publish --release",
104-
"publish:gh": "node run publish:gh --release",
105-
"publish:s3": "node run publish:s3 --release",
99+
"build": "node run build",
100+
"build:debug": "node run build --debug",
101+
"publish": "node run publish",
102+
"publish:debug": "node run publish --debug",
106103
"start": "node run"
107104
}
108105
}
File renamed without changes.

public/browserconfig.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- Please read: https://msdn.microsoft.com/en-us/library/ie/dn455106.aspx -->
3+
<browserconfig>
4+
<msapplication>
5+
<tile>
6+
<square70x70logo src="tile.png"/>
7+
<square150x150logo src="tile.png"/>
8+
<wide310x150logo src="tile-wide.png"/>
9+
<square310x310logo src="tile.png"/>
10+
</tile>
11+
</msapplication>
12+
</browserconfig>

0 commit comments

Comments
 (0)