Skip to content
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"plugins": ["lodash"],
"presets": ["es2015", "stage-2", "react"]
}
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ module.exports = {
"jest/no-focused-tests": "error",
"jest/no-identical-title": "error",
"jest/valid-expect": "error",
"no-confusing-arrow": ["error", {"allowParens": true}]
"no-confusing-arrow": ["error", {"allowParens": true}],
"no-plusplus": ["error", {"allowForLoopAfterthoughts": true}]
},
"plugins": [
"jest",
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,4 @@ public/dist/

# webstorm files
.idea
webpack-stats.json
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: ./node_modules/.bin/forever build/app.js
20 changes: 14 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
"db:seed": "babel-node --presets env,es2015,stage-1 server/db/seed.js",
"db:refresh": "npm run db:teardown && npm run db:setup && npm run db:seed",
"lint": "eslint --ext js --ext jsx .",
"build": "webpack -d --watch && cp public/index.html dist/index.html && webpack-dev-server --content-base src/ --inline",
"build:prod": "webpack -p && cp public/index.html dist/index.html",
"build": "webpack -d --watch",
"build:prod": "webpack -p",
"build:profile": "webpack --profile --json > webpack-stats.json",
"start": "nodemon server/app.js --exec babel-node --presets env,es2015,stage-1",
"test": "npm run lint && npm run test:server && npm run test:client",
"test:client": "jest",
"test:client:refresh": "jest --updateSnapshot",
"test:server": "PG_CONNECTION_STRING=$PG_CONNECTION_STRING_TEST DEBUG=true mocha './test/server' --recursive --compilers js:babel-core/register"
"test:server": "PG_CONNECTION_STRING=$PG_CONNECTION_STRING_TEST DEBUG=true mocha './test/server' --recursive --compilers js:babel-core/register",
"heroku-prebuild": "export NPM_CONFIG_PRODUCTION=false; npm install --only=dev --dev",
"heroku-postbuild": "export NPM_CONFIG_PRODUCTION=true; npm run babel; npm run build:prod;"
},
"jest": {
"verbose": true,
Expand All @@ -28,7 +31,7 @@
"dependencies": {
"axios": "^0.16.2",
"axios-mock-adapter": "^1.9.0",
"bcrypt": "^1.0.2",
"bcrypt": "^1.0.3",
"body-parser": "~1.17.1",
"bookshelf": "^0.10.4",
"bookshelf-bcrypt": "^2.1.0",
Expand All @@ -44,6 +47,7 @@
"express": "~4.15.2",
"express-fileupload": "^0.1.4",
"faker": "^4.1.0",
"forever": "^0.15.3",
"immutable": "^3.8.1",
"jsonwebtoken": "^7.4.1",
"knex": "^0.13.0",
Expand Down Expand Up @@ -74,16 +78,18 @@
"twilio": "^3.6.5"
},
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-cli": "^6.26.0",
"babel-core": "^6.25.0",
"babel-jest": "^20.0.3",
"babel-loader": "^7.1.1",
"babel-plugin-lodash": "^3.2.11",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.6.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-1": "^6.24.1",
"bootstrap": "^3.3.7",
"compression-webpack-plugin": "^1.0.0",
"css-loader": "^0.28.4",
"enzyme": "^2.9.1",
"eslint": "^3.19.0",
Expand All @@ -94,6 +100,7 @@
"eslint-plugin-react": "^7.1.0",
"extract-text-webpack-plugin": "^3.0.0",
"file-loader": "^0.11.2",
"html-webpack-plugin": "^2.30.1",
"jest": "^20.0.4",
"less": "^2.7.2",
"less-loader": "^4.0.5",
Expand All @@ -104,7 +111,8 @@
"request": "^2.81.0",
"style-loader": "^0.18.2",
"url-loader": "^0.5.9",
"webpack": "^3.3.0",
"webpack": "^3.6.0",
"webpack-bundle-analyzer": "^2.9.0",
"webpack-dev-server": "^2.5.1"
}
}
6 changes: 5 additions & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
</head>
<body>
<div id="root"></div>
<script src="/bundle.js"></script>
<% if (typeof htmlWebpackPlugin != "undefined") { for (var chunk in htmlWebpackPlugin.files.chunks) { %>
<script src="<%= htmlWebpackPlugin.files.chunks[chunk].entry %>"></script>
<% } } else { %>
<script src="/main.js"></script>
<% } %>
</body>
</html>
6 changes: 6 additions & 0 deletions server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ server.on('listening', errorHandle.onListening);
middleware(app, express);
// routes(app, express);

app.get('*.js', (req, res, next) => {
req.url = `${req.url}.gz`;
res.set('Content-Encoding', 'gzip');
next();
});

export default server;
21 changes: 20 additions & 1 deletion server/config/middleware.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import logger from 'morgan';
import bodyParser from 'body-parser';
import path from 'path';
import fs from 'fs';
import ejs from 'ejs';
import indexRouter from './routes/index';
import scriptsRouter from './routes/scripts';
import questionsRouter from './routes/questions';
Expand All @@ -18,7 +20,22 @@ export default function middleware(app, express) {
app.use(bodyParser.json());

// define where express should look for static assests
app.use(express.static(path.join(__dirname, '../../public/dist/src')));
const staticFilesDir = path.join(__dirname, '../../public/dist/src');
app.use(express.static(staticFilesDir));
app.get('/main.js', (req, res) => {
// redirect to latest build with content hash
try {
const files = fs.readdirSync(staticFilesDir);
for (let i = 0; i < files.length; i++) {
if (files[i].lastIndexOf('main') === 0) {
return res.redirect(files[i]);
}
}
return res.send('alert("No main.js found! You may need to run npm build.");');
} catch (error) {
return res.send('alert("No public/dist/src dir! You may need to run npm build.");');
}
});

// use passport for authentication
app.use(passport.initialize());
Expand All @@ -31,6 +48,8 @@ export default function middleware(app, express) {
app.use('/users', usersRouter);
app.use('/auth', authRouter);
app.use('/campaigns', campaignsRouter);

app.engine('html', ejs.renderFile);
app.use('*', indexRouter);

// pass the logger
Expand Down
6 changes: 5 additions & 1 deletion server/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import path from 'path';

export default function serveReactApp(req, res) {
res.sendFile(path.resolve(__dirname, '../../public/index.html'));
if (process.env.NODE_ENV === 'production') {
res.sendFile(path.resolve(__dirname, '../../public/dist/src/index.html'));
} else {
res.render(path.resolve(__dirname, '../../public/index.html'));
}
}
2 changes: 1 addition & 1 deletion server/db/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import knexModule from 'knex';

const config = {
client: 'pg',
connection: process.env.PG_CONNECTION_STRING,
connection: process.env.PG_CONNECTION_STRING || process.env.DATABASE_URL,
debug: process.env.DEBUG
};
const knex = knexModule(config);
Expand Down
22 changes: 20 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const path = require('path');
const webpack = require('webpack');

const SRC_DIR = path.resolve(__dirname, 'public');
const DIST_DIR = path.resolve(__dirname, 'public/dist');

const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CompressionPlugin = require('compression-webpack-plugin');

const extractSass = new ExtractTextPlugin({
filename: '[name].[contenthash].css',
Expand All @@ -14,7 +17,7 @@ module.exports = {
entry: `${SRC_DIR}/src/index.jsx`,
output: {
path: `${DIST_DIR}/src/`,
filename: 'bundle.js',
filename: '[name].[chunkhash:8].js',
publicPath: '/'
},
resolve: {
Expand All @@ -28,6 +31,7 @@ module.exports = {
exclude: /node_modules/,
loader: 'babel-loader',
query: {
plugins: ['lodash'],
presets: ['react', 'es2015', 'stage-1', 'env']
}
},
Expand Down Expand Up @@ -57,7 +61,21 @@ module.exports = {
]
},
plugins: [
new ExtractTextPlugin('style.css')
new HtmlWebpackPlugin({
inject: false,
template: 'public/index.html',
}),
new ExtractTextPlugin('style.css'),
new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /en/), // add more locales here if needed
new webpack.optimize.UglifyJsPlugin(),
new webpack.optimize.AggressiveMergingPlugin(),
new CompressionPlugin({
asset: "[path].gz[query]",
algorithm: "gzip",
test: /\.js$|\.css$|\.html$/,
threshold: 10240,
minRatio: 0.8
})
]

};