Skip to content
This repository was archived by the owner on Dec 29, 2020. It is now read-only.

Commit fc94e29

Browse files
author
destructobeam
committed
some production stuff
1 parent ae3dc7e commit fc94e29

File tree

9 files changed

+2116
-60
lines changed

9 files changed

+2116
-60
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,17 @@ Includes Webpack hot reloading for server and client apps.
1717
Uses the beautiful nature of Koa's async function middleware to encapsulate the
1818
series of steps required to render your application on the server.
1919

20+
## Notes
21+
22+
### Prefetching
23+
24+
If you want route based prefetching, After.js seems like a good option.
25+
26+
GraphQL should be fetched on server by something like ApolloClient.
27+
2028
## TODO
2129

2230
- Figure out and implement client only code splitting for when you need super fast TTI, and don't care about overly complex auxiliary interfaces being rendered on the server and included in the initially loaded bundle.
2331
- Possibly use streaming response on server
32+
- Testing setup
2433
- Production settings

jest.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
snapshotSerializers: ['jest-emotion'],
3+
};

package.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
"main": "index.js",
55
"license": "MIT",
66
"scripts": {
7-
"dev": "rm -rdf ./dist && node scripts/webpack",
7+
"dev": "rm -rdf ./dist && node scripts/webpack.js",
88
"clean": "rm -rdf ./dist",
9-
"build": "NODE_ENV=production node scripts/webpack",
10-
"start": "NODE_ENV=production node dist/server.js"
9+
"build": "node scripts/webpack.js",
10+
"start": "node dist/server.js",
11+
"test": "NODE_ENV=test jest"
1112
},
1213
"dependencies": {
1314
"@loadable/component": "^5.2.2",
@@ -32,6 +33,11 @@
3233
"@loadable/webpack-plugin": "^5.2.2",
3334
"babel-loader": "^8.0.4",
3435
"babel-plugin-emotion": "^10.0.5",
36+
"compression-webpack-plugin": "^2.0.0",
37+
"jest": "^23.6.0",
38+
"jest-emotion": "^10.0.5",
39+
"react-test-renderer": "^16.7.0",
40+
"size-plugin": "^1.1.1",
3541
"start-server-webpack-plugin": "^2.2.5",
3642
"webpack": "^4.27.1",
3743
"webpack-dev-server": "^3.1.10",

scripts/serve.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ const https = require('https');
33

44
const app = require('../src/server').default;
55
const dev = process.env.NODE_ENV !== 'production';
6+
const ssl = process.env.PROTOCOL || 'http';
67
const port = process.env.PORT || 3000;
78

89
let handler = app.callback();
910
let server;
1011

11-
if (dev) {
12+
if (ssl) {
1213
server = http.createServer(handler);
1314
} else {
1415
server = https.createServer(handler);
@@ -19,7 +20,7 @@ server.listen(port, error => {
1920
console.log(error);
2021
}
2122

22-
console.log('Server started');
23+
console.log('Server started on port ', port);
2324
});
2425

2526
if (module.hot) {

src/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const server = new Koa();
1616

1717
if (!dev) {
1818
// Use webpack-dev-server in dev mode, otherwise serve static assets normally
19-
server.use(KoaStatic(path.resolve('..', 'public')));
19+
server.use(KoaStatic(path.resolve('public')));
2020
}
2121

2222
// Load asset manifest in to state and watch changes to manifest in development

src/server/render.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ const render = async (context, next) => {
2020
${title.toString()}
2121
${meta.toString()}
2222
${link.toString()}
23+
24+
${chunkExtractor.getScriptTags()}
2325
</head>
2426
<body ${bodyAttributes.toString()}>
2527
<div id="main" role="main">${reactString}</div>
26-
${chunkExtractor.getScriptTags()}
2728
</body>
2829
</html>
2930
`;

webpack/client.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,15 @@ const merge = require('webpack-merge');
33

44
const dev = process.env.NODE_ENV !== 'production';
55

6+
const CompressionPlugin = require('compression-webpack-plugin');
67
const { HotModuleReplacementPlugin } = require('webpack');
78
const LoadablePlugin = require('@loadable/webpack-plugin');
9+
const SizePlugin = require('size-plugin');
810
const WebpackBar = require('webpackbar');
911

1012
let config = {
1113
name: 'client',
1214

13-
output: {
14-
path: path.resolve(__dirname, '..', 'public', 'assets'),
15-
publicPath: 'http://localhost:3001/assets/',
16-
filename: '[name].[hash].js',
17-
},
18-
1915
module: {
2016
rules: [
2117
{
@@ -34,6 +30,7 @@ let config = {
3430
],
3531
'@babel/preset-react',
3632
],
33+
3734
plugins: [
3835
'@babel/plugin-syntax-dynamic-import',
3936
'@loadable/babel-plugin',
@@ -49,8 +46,8 @@ let config = {
4946
new LoadablePlugin({
5047
writeToDisk: true,
5148
}),
49+
new SizePlugin(),
5250
],
53-
5451
};
5552

5653
if (dev) {
@@ -61,13 +58,20 @@ if (dev) {
6158
main: ['webpack/hot/signal', './src/client.js'],
6259
},
6360

61+
output: {
62+
path: path.resolve(__dirname, '..', 'public', 'assets'),
63+
publicPath: 'http://localhost:3001/assets/',
64+
filename: '[name].[hash].js',
65+
},
66+
6467
devtool: 'source-map',
6568
devServer: {
6669
hot: true,
6770
headers: { 'Access-Control-Allow-Origin': '*' },
6871
contentBase: path.resolve('..', 'public', 'assets'),
6972
publicPath: 'http://localhost:3001/assets/',
7073
port: 3001,
74+
quiet: true,
7175
},
7276

7377
plugins: [
@@ -78,9 +82,18 @@ if (dev) {
7882
} else {
7983
config = merge(config, {
8084
mode: 'production',
85+
8186
entry: {
8287
main: './src/client.js',
8388
},
89+
90+
output: {
91+
path: path.resolve(__dirname, '..', 'public', 'assets'),
92+
publicPath: '/assets/',
93+
filename: '[name].[hash].js',
94+
},
95+
96+
plugins: [new CompressionPlugin()],
8497
});
8598
}
8699

webpack/server.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ let config = {
1818
filename: 'server.js',
1919
},
2020

21+
devtool: 'source-map',
22+
2123
module: {
2224
rules: [
2325
{
@@ -38,6 +40,7 @@ let config = {
3840
],
3941
'@babel/preset-react',
4042
],
43+
4144
plugins: [
4245
'@babel/plugin-syntax-dynamic-import',
4346
'@loadable/babel-plugin',
@@ -58,12 +61,14 @@ let config = {
5861

5962
if (dev) {
6063
config = merge(config, {
64+
mode: 'development',
65+
6166
entry: {
6267
server: ['webpack/hot/signal', './scripts/serve.js'],
6368
},
64-
mode: 'development',
65-
devtool: 'source-map',
69+
6670
watch: true,
71+
6772
plugins: [
6873
new HotModuleReplacementPlugin(),
6974
new WebpackBar({ name: 'Server', color: 'blue' }),
@@ -78,9 +83,14 @@ if (dev) {
7883
} else {
7984
config = merge(config, {
8085
mode: 'production',
86+
8187
entry: {
8288
server: './scripts/serve.js',
8389
},
90+
91+
optimization: {
92+
minimize: false,
93+
},
8494
});
8595
}
8696

0 commit comments

Comments
 (0)