diff --git a/docs/build.md b/docs/build.md index 78ebd66f..f440f410 100644 --- a/docs/build.md +++ b/docs/build.md @@ -2,11 +2,11 @@ Send has two build configurations, development and production. Both can be run v # Development -`npm start` launches a `webpack-dev-server` on port 8080 that compiles the assets and watches files for changes. It also serves the backend API and frontend unit tests via the `server/dev.js` entrypoint. The frontend tests can be run in the browser by navigating to http://localhost:8080/test and will rerun automatically as the watched files are saved with changes. +`npm start` launches a `webpack-dev-server` on port 8080 that compiles the assets and watches files for changes. It also serves the backend API and frontend unit tests via the `server/bin/dev.js` entrypoint. The frontend tests can be run in the browser by navigating to http://localhost:8080/test and will rerun automatically as the watched files are saved with changes. # Production -`npm run build` compiles the assets and writes the files to the `dist/` directory. `npm run prod` launches an Express server on port 1443 that serves the backend API and frontend static assets from `dist/` via the `server/prod.js` entrypoint. +`npm run build` compiles the assets and writes the files to the `dist/` directory. `npm run prod` launches an Express server on port 1443 that serves the backend API and frontend static assets from `dist/` via the `server/bin/prod.js` entrypoint. # Notable differences diff --git a/package.json b/package.json index 77e8e22f..b927d113 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "test-integration": "docker-compose up --abort-on-container-exit --exit-code-from integration-tests --build --remove-orphans --quiet-pull && docker-compose down", "test-integration-stage": "cross-env BASE_URL=https://send.stage.mozaws.net npm run test-integration", "start": "npm run clean && cross-env NODE_ENV=development webpack-dev-server", - "prod": "node server/prod.js" + "prod": "node server/bin/prod.js" }, "lint-staged": { "*.js": [ diff --git a/server/dev.js b/server/bin/dev.js similarity index 62% rename from server/dev.js rename to server/bin/dev.js index 463a01b3..d94eb8c4 100644 --- a/server/dev.js +++ b/server/bin/dev.js @@ -1,15 +1,15 @@ -const assets = require('../common/assets'); -const locales = require('../common/locales'); -const routes = require('./routes'); -const pages = require('./routes/pages'); -const tests = require('../test/frontend/routes'); +const assets = require('../../common/assets'); +const locales = require('../../common/locales'); +const routes = require('../routes'); +const pages = require('../routes/pages'); +const tests = require('../../test/frontend/routes'); const express = require('express'); const expressWs = require('express-ws'); -const config = require('./config'); +const config = require('../config'); const wsapp = express(); expressWs(wsapp, null, { perMessageDeflate: false }); -wsapp.ws('/api/ws', require('./routes/ws')); +wsapp.ws('/api/ws', require('../routes/ws')); wsapp.listen(8081, config.listen_address); module.exports = function(app, devServer) { diff --git a/server/prod.js b/server/bin/prod.js similarity index 71% rename from server/prod.js rename to server/bin/prod.js index a59f6ec8..d52393ed 100644 --- a/server/prod.js +++ b/server/bin/prod.js @@ -1,9 +1,9 @@ const express = require('express'); const path = require('path'); const Raven = require('raven'); -const config = require('./config'); -const routes = require('./routes'); -const pages = require('./routes/pages'); +const config = require('../config'); +const routes = require('../routes'); +const pages = require('../routes/pages'); const expressWs = require('express-ws'); if (config.sentry_dsn) { @@ -12,11 +12,11 @@ if (config.sentry_dsn) { const app = express(); expressWs(app, null, { perMessageDeflate: false }); -app.ws('/api/ws', require('./routes/ws')); +app.ws('/api/ws', require('../routes/ws')); routes(app); app.use( - express.static(path.resolve(__dirname, '../dist/'), { + express.static(path.resolve(__dirname, '../../dist/'), { setHeaders: function(res) { res.set('Cache-Control', 'public, max-age=31536000, immutable'); res.removeHeader('Pragma'); diff --git a/server/bin/test.js b/server/bin/test.js new file mode 100644 index 00000000..b5f349a5 --- /dev/null +++ b/server/bin/test.js @@ -0,0 +1,18 @@ +const assets = require('../../common/assets'); +const locales = require('../../common/locales'); +const routes = require('../routes'); +const pages = require('../routes/pages'); +const tests = require('../../test/frontend/routes'); +const expressWs = require('express-ws'); + +module.exports = function(app, devServer) { + assets.setMiddleware(devServer.middleware); + locales.setMiddleware(devServer.middleware); + expressWs(app, null, { perMessageDeflate: false }); + app.ws('/api/ws', require('../routes/ws')); + routes(app); + tests(app); + // webpack-dev-server routes haven't been added yet + // so wait for next tick to add 404 handler + process.nextTick(() => app.use(pages.notfound)); +}; diff --git a/test/frontend/runner.js b/test/frontend/runner.js index d474b2d8..3ae95f2f 100644 --- a/test/frontend/runner.js +++ b/test/frontend/runner.js @@ -7,7 +7,7 @@ const webpack = require('webpack'); const config = require('../../webpack.config'); const middleware = require('webpack-dev-middleware'); const express = require('express'); -const devRoutes = require('../../server/dev'); +const devRoutes = require('../../server/bin/test'); const app = express(); const wpm = middleware(webpack(config), { logLevel: 'silent' }); diff --git a/webpack.config.js b/webpack.config.js index fb085c73..6619f697 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -209,7 +209,7 @@ module.exports = { devServer: { compress: true, host: '0.0.0.0', - before: IS_DEV ? require('./server/dev') : undefined, + before: IS_DEV ? require('./server/bin/dev') : undefined, proxy: { '/api/ws': { target: 'ws://localhost:8081',