Skip to content

Commit

Permalink
Firebase hosting working
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgilbertson committed Dec 9, 2016
1 parent 3d4fdaf commit 134cf04
Show file tree
Hide file tree
Showing 15 changed files with 8,906 additions and 8,860 deletions.
5 changes: 5 additions & 0 deletions .firebaserc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"projects": {
"default": "knowitall-9a92e"
}
}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.idea
static
public
webpack/jsPackageName.json
private

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Know it all

https://knowitall-9a92e.firebaseapp.com/

The goal of know-it-all is to help you discover things
that you don't know about web development.

Expand Down
5 changes: 3 additions & 2 deletions app/client/client.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react';
import ReactDOM from 'react-dom';
import Perf from 'react-addons-perf';
import App from '../components/App/App.jsx';

window.Perf = Perf;
if (process.env.NODE_ENV !== `production`) {
window.Perf = require(`react-addons-perf`); // eslint-disable-line global-require
}

// We don't want to inline this data cos it's HUGE
// We fetch here, but we have already pre-fetched in the <head>
Expand Down
5 changes: 3 additions & 2 deletions app/components/TableRow/TableRow.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,10 @@ class TableRow extends Component {
{ 'table-row--expanded': props.item.get(`isExpanded`) }
);

const contentStyle = {
const contentStyle = props.item.get(`score`)
? {
boxShadow: `inset 4px 0 ${props.item.get(`score`).color}`,
};
} : null;

return (
<div
Expand Down
17,663 changes: 8,832 additions & 8,831 deletions app/data/data.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion app/server/appHtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ import {

const data = require(`../data/data.json`);

// TODO (davidg): load polyfilledScriptFileName if the browser doesn't have fetch
// is there anything else I need to polyfill?
export default ({ dataFileName = `data.json`, scriptFileName, mode }) => {
let scriptSrc;
let styleTag = ``;

if (mode === `production`) {
scriptSrc = scriptFileName;

const stylesPath = path.resolve(__dirname, `../../static/styles.css`);
const stylesPath = path.resolve(__dirname, `../../public/styles.css`);
const styles = fs.readFileSync(stylesPath, `utf8`);

styleTag = `<style>${styles}</style>`;
Expand Down
8 changes: 5 additions & 3 deletions app/server/server.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import express from 'express';
import compression from 'compression';
const app = express();
app.use(compression());

import appHtml from './appHtml.js';
import { PORT } from '../constants.js';

// In dev mode, generate the html dynamically
// otherwise, serve everything from static
// so for testing 'production' locally I'll get the index.html served from static
// otherwise, serve everything from public
// so for testing 'production' locally I'll get the index.html served from public
if (process.env.NODE_ENV !== `production`) {
app.get(`/`, (req, res) => {
res.send(appHtml({ mode: `dev` }));
});
}

app.use(express.static(`static`));
app.use(express.static(`public`));
app.use(express.static(`app/data`)); // default location for data.json

app.listen(PORT, () => {
Expand Down
2 changes: 1 addition & 1 deletion app/utils/decorateData.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function parseData(items, path = []) {
for (let i = 0; i < items.length; i++) {
const item = items[i];

item.isExpanded = depth < 0;
item.isExpanded = depth < 1;
item.row = rowCount++;
item.depth = depth;
item.path = path.slice();
Expand Down
Binary file added assets/favicon.ico
Binary file not shown.
16 changes: 16 additions & 0 deletions firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"hosting": {
"public": "public",
"headers": [
{
"source": "**/*.@(js|json|ico)",
"headers": [
{
"key": "Cache-Control",
"value": "max-age=31536000"
}
]
}
]
}
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"heroku-postbuild": "npm run build",
"start": "node index.js",
"build": "node webpack/build.js",
"deploy": "firebase deploy",
"dev": "nodemon index.dev.js",
"test": "eslint ./app ./webpack --color --ext .jsx,.js"
},
Expand All @@ -30,6 +31,7 @@
"babel-preset-stage-2": "^6.11.0",
"babel-register": "^6.11.6",
"classnames": "2.2.5",
"compression": "^1.6.2",
"css-loader": "0.26.1",
"express": "^4.14.0",
"extract-text-webpack-plugin": "1.0.1",
Expand Down
29 changes: 19 additions & 10 deletions webpack/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@ const appHtml = require(`../app/server/appHtml`).default;

const rimraf = require(`rimraf`);

rimraf.sync(path.resolve(__dirname, `../static`));
rimraf.sync(path.resolve(__dirname, `../public`));

config.entry.unshift(`babel-polyfill`);
fs.mkdirSync(`./public`);

fs.createReadStream(`./assets/favicon.ico`).pipe(fs.createWriteStream(`./public/favicon.ico`));

config.plugins = config.plugins.concat([
new webpack.optimize.UglifyJsPlugin({
sourceMap: false,
}),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify(`production`),
},
}),
new webpack.optimize.UglifyJsPlugin({ sourceMap: true }),
new webpack.optimize.DedupePlugin(),
]);

config.plugins.push(
Expand Down Expand Up @@ -57,10 +58,16 @@ compiler.run((compileError, stats) => {
chunks: false,
});

console.log(stats.toString({
chunks: false,
colors: true,
}));

// get the name of the JS file (including the hash webpack created)
const scriptFileName = jsonStats.assetsByChunkName.main.filter(asset => asset.endsWith(`.js`));
const scriptFileName = jsonStats.assetsByChunkName.app[0];
const polyfilledScriptFileName = jsonStats.assetsByChunkName[`app-with-polyfills`][0];

// We get the hash for the data file and copy it to static
// get the hash for the data file and copy it to public
fs.readFile(`./app/data/data.json`, `utf8`, (dataFileError, dataJson) => {
const dataHash = crypto.createHash(`md5`).update(dataJson).digest(`hex`);
const dataFileName = `data.${dataHash}.json`;
Expand All @@ -69,16 +76,18 @@ compiler.run((compileError, stats) => {
const htmlString = appHtml({
dataFileName,
scriptFileName,
polyfilledScriptFileName,
mode: `production`,
});

// write the data to a new file with the hash in the name
// this could be a copy/rename but we already had the file read
fs.writeFile(`./static/${dataFileName}`, dataJson, `utf8`, (dataJsonError) => {
// this could be a copy/rename but we already
// had the file loaded and perf doesn't matter here
fs.writeFile(`./public/${dataFileName}`, dataJson, `utf8`, (dataJsonError) => {
if (dataJsonError) console.error(`Error writing data.json to disk: ${dataJsonError}`);
});

fs.writeFile(`./static/index.html`, htmlString, `utf8`, (indexError) => {
fs.writeFile(`./public/index.html`, htmlString, `utf8`, (indexError) => {
if (indexError) console.error(`Error creating index.html: ${indexError}`);

console.timeEnd(`build`);
Expand Down
20 changes: 12 additions & 8 deletions webpack/config.shared.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
const webpack = require(`webpack`);
const path = require(`path`);

process.env.WEBPACKING = true;

module.exports = {
entry: [
`./app/client/client.js`,
],
entry: {
app: [
`./app/client/client.js`,
],
'app-with-polyfills': [
`babel-polyfill`,
`./app/client/client.js`,
],
},
output: {
path: path.resolve(__dirname, `../static`),
filename: `app.[hash].js`,
path: path.resolve(__dirname, `../public`),
filename: `[name].[hash].js`,
},
module: {
loaders: [
Expand All @@ -20,7 +24,7 @@ module.exports = {
},
],
// noParse: [
// /localforage/,
// /localforage/,
// ],
},
resolve: {
Expand Down
2 changes: 1 addition & 1 deletion webpack/devServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const CONSTANTS = require(`../app/constants.js`);

const contentBase = `http://localhost:${CONSTANTS.DEV_PORT}`;

config.entry.unshift(
config.entry.app.unshift(
`webpack-dev-server/client?${contentBase}`,
`webpack/hot/only-dev-server`
);
Expand Down

0 comments on commit 134cf04

Please sign in to comment.