Skip to content
This repository was archived by the owner on Jun 3, 2019. It is now read-only.

Commit f6fab53

Browse files
oyeanujSteven Truesdell
authored and
Steven Truesdell
committed
Colors to chalk (#512)
* Minor readme updates (#501) * Update Webpack version * Update Webpack version * Fix acronym * Fix markdown formatting. * Update that redux branch does not have Flow * Add 'Who is using it' section in the README As per @ctrlplusb’s comment here: #437 (comment) 98854669 * Server logging enhancements (#508) * Improving logging experience + consolidating to single log function * Adding pretty-error for more readable node error stack * Fix eslint error * Logging requests received * Re-commit logging requests received * Replacing colors with chalk
1 parent 1d4164f commit f6fab53

File tree

13 files changed

+6922
-32
lines changed

13 files changed

+6922
-32
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ This starter kit contains all the build tooling and configuration you need to ki
2424
- 🌍 Server Side Rendering.
2525
- 😎 Progressive Web Application ready, with offline support, via a Service Worker.
2626
- 🐘 Long term browser caching of assets with automated cache invalidation.
27-
- 📦 All source is bundled using Webpack v2.
27+
- 📦 All source is bundled using Webpack v3.
2828
- 🚀 Full ES2017+ support - use the exact same JS syntax across the entire project. No more folder context switching! We also only use syntax that is stage-3 or later in the TC39 process.
2929
- 🔧 Centralised application configuration with helpers to avoid boilerplate in your code. Also has support for environment specific configuration files.
3030
- 🔥 Extreme live development - hot reloading of ALL changes to client/server source, with auto development server restarts when your application configuration changes. All this with a high level of error tolerance and verbose logging to the console.
@@ -61,6 +61,10 @@ Now go make some changes to the `Home` component to see the tooling in action.
6161
- [Deploy your very own Server Side Rendering React App in 5 easy steps](/internal/docs/DEPLOY_TO_NOW.md)
6262
- [Changelog](/CHANGELOG.md)
6363

64+
## Who's using it and where?
65+
66+
You can see who is using it and how in [the comments here](https://github.com/ctrlplusb/react-universally/issues/437). Feel free to add to that telling us how you are using it, we'd love to hear from you.
67+
6468
## Contributors
6569

6670
Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)):

config/utils/envVars.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
*/
88

99
import appRootDir from 'app-root-dir';
10-
import colors from 'colors/safe';
1110
import dotenv from 'dotenv';
1211
import fs from 'fs';
1312
import path from 'path';
1413

1514
import ifElse from '../../shared/utils/logic/ifElse';
1615
import removeNil from '../../shared/utils/arrays/removeNil';
1716

17+
import { log } from '../../internal/utils';
18+
1819
// PRIVATES
1920

2021
function registerEnvFile() {
@@ -40,7 +41,11 @@ function registerEnvFile() {
4041
// If we found an env file match the register it.
4142
if (envFilePath) {
4243
// eslint-disable-next-line no-console
43-
console.log(colors.bgBlue.white(`==> Registering environment variables from: ${envFilePath}`));
44+
log({
45+
title: 'server',
46+
level: 'special',
47+
message: `Registering environment variables from: ${envFilePath}`,
48+
});
4449
dotenv.config({ path: envFilePath });
4550
}
4651
}

internal/development/createVendorDLL.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ function createVendorDLL(bundleName, bundleConfig) {
5858
log({
5959
title: 'vendorDLL',
6060
level: 'info',
61-
message: `Vendor DLL build complete. The following dependencies have been included:\n\t-${devDLLDependencies.join('\n\t-')}\n`,
61+
message: `Vendor DLL build complete. The following dependencies have been included:\n\t-${devDLLDependencies.join(
62+
'\n\t-',
63+
)}\n`,
6264
});
6365

6466
const webpackConfig = webpackConfigFactory();
@@ -83,9 +85,14 @@ function createVendorDLL(bundleName, bundleConfig) {
8385
title: 'vendorDLL',
8486
level: 'warn',
8587
message: `Generating a new "${bundleName}" Vendor DLL for boosted development performance.
86-
The Vendor DLL helps to speed up your development workflow by reducing Webpack build times. It does this by seperating Vendor DLLs from your primary bundles, thereby allowing Webpack to ignore them when having to rebuild your code for changes. We recommend that you add all your client bundle specific dependencies to the Vendor DLL configuration (within /config).`,
88+
89+
The Vendor DLL helps to speed up your development workflow by reducing Webpack build times. It does this by seperating Vendor DLLs from your primary bundles, thereby allowing Webpack to ignore them when having to rebuild your code for changes.
90+
91+
We recommend that you add all your client bundle specific dependencies to the Vendor DLL configuration (within /config).`,
8792
});
88-
buildVendorDLL().then(resolve).catch(reject);
93+
buildVendorDLL()
94+
.then(resolve)
95+
.catch(reject);
8996
} else {
9097
// first check if the md5 hashes match
9198
const dependenciesHash = fs.readFileSync(vendorDLLHashFilePath, 'utf8');
@@ -97,7 +104,9 @@ The Vendor DLL helps to speed up your development workflow by reducing Webpack b
97104
level: 'warn',
98105
message: `New "${bundleName}" vendor dependencies detected. Regenerating the vendor dll...`,
99106
});
100-
buildVendorDLL().then(resolve).catch(reject);
107+
buildVendorDLL()
108+
.then(resolve)
109+
.catch(reject);
101110
} else {
102111
log({
103112
title: 'vendorDLL',

internal/docs/FEATURE_BRANCHES.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Below are a list of extensions to this repository, in the form of branches. Eac
1212
- [`apollo`](https://github.com/ctrlplusb/react-universally/tree/feature/apollo) - Adds the Apollo Stack (i.e. Graphql).
1313
- [`mobx`](https://github.com/andreyluiz/react-universally/tree/feature/mobx) - Adds MobX as a state management library.
1414
- [`postcss-sass`](https://github.com/ctrlplusb/react-universally/tree/feature/postcss-sass) - Adds PostCSS and SASS.
15-
- [`redux-opinionated`](https://github.com/ctrlplusb/react-universally/tree/feature/redux-opinionated) - Adds an opinionated Redux implementation, using `redux-thunk` and `react-jobs` to support data loading across the client/server. It also merges in the `flow` feature branch.
15+
- [`redux-opinionated`](https://github.com/ctrlplusb/react-universally/tree/feature/redux-opinionated) - Adds an opinionated Redux implementation, using `redux-thunk` and `react-jobs` to support data loading across the client/server.
1616

1717
If you would like to add a new feature branch log an issue describing your chosen technology and we can come up with a plan together. :)
1818

internal/docs/PKG_SCRIPTS.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99

1010
## `npm run analyze:client`
1111

12-
Creates an 'webpack-bundle-analyze' session against the production build of the client bundle.
12+
Creates an `webpack-bundle-analyze` session against the production build of the client bundle.
1313

1414
## `npm run analyze:server`
1515

16-
Creates an 'webpack-bundle-analyze' session against the production build of the server bundle.
16+
Creates an `webpack-bundle-analyze` session against the production build of the server bundle.
1717

1818
## `npm run build`
1919

@@ -43,10 +43,10 @@ Executes `eslint` against the project. Alternatively you could look to install t
4343

4444
Executes the server. It expects you to have already built the bundles using the `npm run build` command.
4545

46-
## `npm run test`
46+
## `npm run test`
4747

4848
Runs the `jest` tests.
4949

50-
## `npm run test:coverage`
50+
## `npm run test:coverage`
5151

5252
Runs the `jest` tests and generates a coverage report. I recommend you look at [codecov.io](https://codecov.io) to host your coverage reports.

internal/docs/PROJECT_OVERVIEW.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
Below is a general overview of the project.
1111

12-
## TOC
12+
## ToC
1313

1414
- [Bundled by Webpack](#bundled-by-webpack)
1515
- [Transpiled by Babel](#transpiled-by-babel)
@@ -18,7 +18,7 @@ Below is a general overview of the project.
1818

1919
## Bundled by Webpack
2020

21-
This starter uses Webpack 2 to produce bundles for both the client and the server. The `internal/webpack/configFactory.js` is used to generate the respective Webpack configuration for all our bundles. The factory is heavily commented to help you understand what is going on within the Webpack configuration.
21+
This starter uses Webpack 3 to produce bundles for both the client and the server. The `internal/webpack/configFactory.js` is used to generate the respective Webpack configuration for all our bundles. The factory is heavily commented to help you understand what is going on within the Webpack configuration.
2222

2323
> Note: Given that we are bundling our server code I have included the `source-map-support` module to ensure that we still get nice stack traces when executing our code.
2424

internal/utils.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os from 'os';
22
import HappyPack from 'happypack';
33
import notifier from 'node-notifier';
4-
import colors from 'colors/safe';
4+
import chalk from 'chalk';
55
import { execSync } from 'child_process';
66
import appRootDir from 'app-root-dir';
77

@@ -31,18 +31,21 @@ export function log(options) {
3131
}
3232

3333
const level = options.level || 'info';
34-
const msg = `==> ${title} -> ${options.message}`;
34+
const msg = `${title}: ${options.message}`;
3535

3636
switch (level) {
3737
case 'warn':
38-
console.log(colors.yellow(msg));
38+
console.log(chalk.yellowBright(msg));
3939
break;
4040
case 'error':
41-
console.log(colors.bgRed.white(msg));
41+
console.log(chalk.bgRed.white.bold(msg));
42+
break;
43+
case 'special':
44+
console.log(chalk.italic.cyanBright(msg));
4245
break;
4346
case 'info':
4447
default:
45-
console.log(colors.green(msg));
48+
console.log(chalk.gray(msg));
4649
}
4750
}
4851

internal/webpack/configFactory.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import path from 'path';
66
import webpack from 'webpack';
77
import WebpackMd5Hash from 'webpack-md5-hash';
88

9-
import { happyPackPlugin } from '../utils';
9+
import { happyPackPlugin, log } from '../utils';
1010
import { ifElse } from '../../shared/utils/logic';
1111
import { mergeDeep } from '../../shared/utils/objects';
1212
import { removeNil } from '../../shared/utils/arrays';
@@ -44,11 +44,13 @@ export default function webpackConfigFactory(buildOptions) {
4444
const ifDevClient = ifElse(isDev && isClient);
4545
const ifProdClient = ifElse(isProd && isClient);
4646

47-
console.log(
48-
`==> Creating ${isProd
47+
log({
48+
level: 'info',
49+
title: 'Webpack',
50+
message: `Creating ${isProd
4951
? 'an optimised'
5052
: 'a development'} bundle configuration for the "${target}"`,
51-
);
53+
});
5254

5355
const bundleConfig =
5456
isServer || isClient

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
"homepage": "https://github.com/ctrlplusb/react-universally#readme",
6060
"dependencies": {
6161
"app-root-dir": "1.0.2",
62-
"colors": "1.1.2",
6362
"compression": "1.7.1",
6463
"cross-env": "5.0.5",
6564
"dotenv": "4.0.0",
@@ -69,6 +68,7 @@
6968
"modernizr": "3.5.0",
7069
"normalize.css": "7.0.0",
7170
"offline-plugin": "4.8.4",
71+
"pretty-error": "2.1.1",
7272
"prop-types": "15.6.0",
7373
"raf": "3.4.0",
7474
"react": "16.0.0",
@@ -95,6 +95,7 @@
9595
"babel-preset-react": "6.24.1",
9696
"babel-preset-stage-3": "6.24.1",
9797
"babel-template": "6.26.0",
98+
"chalk": "2.1.0",
9899
"chokidar": "1.7.0",
99100
"css-loader": "0.28.7",
100101
"enzyme": "3.1.0",

server/index.js

+33-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import serviceWorker from './middleware/serviceWorker';
1111
import offlinePage from './middleware/offlinePage';
1212
import errorHandlers from './middleware/errorHandlers';
1313
import config from '../config';
14+
import { log } from '../internal/utils';
1415

1516
// Create our express based server.
1617
const app = express();
@@ -43,14 +44,44 @@ app.use(config('bundles.client.webPath'), clientBundle);
4344
app.use(express.static(pathResolve(appRootDir.get(), config('publicAssetsPath'))));
4445

4546
// The React application middleware.
46-
app.get('*', reactApplication);
47+
app.get('*', (request, response) => {
48+
log({
49+
title: 'Request',
50+
level: 'special',
51+
message: `Received for "${request.url}"`,
52+
});
53+
54+
return reactApplication(request, response);
55+
});
4756

4857
// Error Handler middlewares.
4958
app.use(...errorHandlers);
5059

5160
// Create an http listener for our express app.
5261
const listener = app.listen(config('port'), () =>
53-
console.log(`Server listening on port ${config('port')}`));
62+
log({
63+
title: 'server',
64+
level: 'special',
65+
message: `✓
66+
67+
${config('welcomeMessage')}
68+
69+
${config('htmlPage.defaultTitle')} is ready!
70+
71+
with
72+
73+
Service Workers: ${config('serviceWorker.enabled')}
74+
Polyfills: ${config('polyfillIO.enabled')} (${config('polyfillIO.features').join(', ')})
75+
76+
Server is now listening on Port ${config('port')}
77+
You can access it in the browser at http://${config('host')}/${config('port')}
78+
Press Ctrl-C to stop.
79+
80+
81+
82+
`,
83+
}),
84+
);
5485

5586
// We export the listener as it will be handy for our development hot reloader,
5687
// or for exposing a general extension layer for application customisations.

server/middleware/errorHandlers.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
/* eslint-disable no-console */
22
/* eslint-disable no-unused-vars */
33

4+
const prettyError = require('pretty-error').start();
5+
6+
// Configure prettyError to simplify the stack trace:
7+
8+
// skip events.js and http.js and similar core node files
9+
prettyError.skipNodeFiles();
10+
11+
// skip all the trace lines about express` core and sub-modules
12+
prettyError.skipPackage('express');
13+
414
const errorHandlersMiddleware = [
515
/**
616
* 404 errors middleware.
@@ -21,8 +31,7 @@ const errorHandlersMiddleware = [
2131
*/
2232
function unexpectedErrorMiddleware(err, req, res, next) {
2333
if (err) {
24-
console.log(err);
25-
console.log(err.stack);
34+
console.log(prettyError.render(err));
2635
}
2736
res.status(500).send('Sorry, an unexpected error occurred.');
2837
},

server/middleware/reactApplication/index.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import config from '../../../config';
99

1010
import ServerHTML from './ServerHTML';
1111
import DemoApp from '../../../shared/components/DemoApp';
12+
import { log } from '../../../internal/utils';
1213

1314
/**
1415
* React application middleware, supports server side rendering.
@@ -26,7 +27,11 @@ export default function reactApplicationMiddleware(request, response) {
2627
if (config('disableSSR')) {
2728
if (process.env.BUILD_FLAG_IS_DEV === 'true') {
2829
// eslint-disable-next-line no-console
29-
console.log('==> Handling react route without SSR');
30+
log({
31+
title: 'Server',
32+
level: 'info',
33+
message: `Handling react route without SSR: ${request.url}`,
34+
});
3035
}
3136
// SSR is disabled so we will return an "empty" html page and
3237
// rely on the client to initialize and render the react application.
@@ -78,10 +83,10 @@ export default function reactApplicationMiddleware(request, response) {
7883
.status(
7984
reactRouterContext.missed
8085
? // If the renderResult contains a "missed" match then we set a 404 code.
81-
// Our App component will handle the rendering of an Error404 view.
82-
404
86+
// Our App component will handle the rendering of an Error404 view.
87+
404
8388
: // Otherwise everything is all good and we send a 200 OK status.
84-
200,
89+
200,
8590
)
8691
.send(`<!DOCTYPE html>${html}`);
8792
});

0 commit comments

Comments
 (0)