Skip to content

Commit 7ea9da9

Browse files
authored
Improving SSR Support (#65)
* Adding react-loadable * Updating the libs for react-loadable * Updating webpack config * Updating webpack * Adding ssr addon * Updating noderesolve * Updating deps * Updating loadable * Updating ace * webpack config updates * Updating addon * Adding loadable proxy * Updating deps for loadable plugin * Updating plugin location * Updating deps * Updates
1 parent d58809d commit 7ea9da9

File tree

6 files changed

+58
-21
lines changed

6 files changed

+58
-21
lines changed

lib/start.js

+22-10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ const {
77
retrieveClientConfig,
88
retrieveServerConfig,
99
} = require('./webpack.config')
10+
const path = require('path')
11+
12+
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
1013

1114
module.exports = ({ args, getPackage }) => {
1215
const pkg = getPackage()
@@ -47,21 +50,30 @@ module.exports = ({ args, getPackage }) => {
4750
const webpackDevServer = new WebpackDevServer(clientCompiler, {
4851
before: app => {
4952
if (args.middleware) {
50-
let clientBundle = null
51-
52-
clientCompiler.hooks.emit.tap('CompiledFilePlugin', compilation => {
53-
clientBundle = Object.keys(compilation.assets).filter(name =>
54-
name.match(/^bundle.[a-z0-9]+.\.js$/)
55-
)[0]
56-
})
57-
5853
const server = retrieveServerConfig({
5954
...configuration,
6055
main: args.middleware,
6156
})
6257

63-
app.use((req, res, next) => {
64-
req.clientBundle = clientBundle
58+
app.use(async (req, res, next) => {
59+
const fs = clientCompiler.outputFileSystem
60+
const jsonFile = path.join(
61+
process.cwd(),
62+
'./target/webapp/react-loadable.json'
63+
)
64+
65+
while (!fs.existsSync(jsonFile)) {
66+
await sleep(100)
67+
}
68+
69+
try {
70+
const clientBundles = JSON.parse(fs.readFileSync(jsonFile, 'utf-8'))
71+
req.clientBundles = clientBundles
72+
} catch (e) {
73+
console.error(e)
74+
req.clientBundles = []
75+
}
76+
6577
next()
6678
})
6779

lib/webpack.config.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const WebpackBundleAnalyzerPlugin = require('webpack-bundle-analyzer')
1515
const getPackageWhitelist = require('./package-utils').getPackageWhitelist
1616
const getResolves = require('./package-utils').getResolves
1717
const getTemplatePath = require('./package-utils').getTemplatePath
18-
const nodeExternals = require('webpack-node-externals')
18+
const ReactLoadableSSRAddon = require('react-loadable-ssr-addon')
1919

2020
const resolve = place => path.resolve(place)
2121
const nodeResolve = place => require.resolve(place)
@@ -46,7 +46,11 @@ const babelLoader = (plugins = []) => ({
4646
nodeResolve('babel-preset-stage-0'),
4747
],
4848
cacheDirectory: true,
49-
plugins: [nodeResolve('react-hot-loader/babel'), ...plugins],
49+
plugins: [
50+
nodeResolve('react-hot-loader/babel'),
51+
nodeResolve('react-loadable/babel'),
52+
...plugins,
53+
],
5054
},
5155
})
5256

@@ -399,6 +403,12 @@ const dev = (base, { main, packageJson }) => {
399403
new SimpleProgressWebpackPlugin({
400404
format: 'compact',
401405
}),
406+
new ReactLoadableSSRAddon({
407+
filename: path.join(
408+
process.cwd(),
409+
'./target/webapp/react-loadable.json'
410+
),
411+
}),
402412
],
403413
})
404414
}
@@ -478,6 +488,12 @@ const prod = (base, { main, packageJson }) => {
478488
filename: 'index.html',
479489
template,
480490
}),
491+
new ReactLoadableSSRAddon({
492+
filename: path.join(
493+
process.cwd(),
494+
'./target/webapp/react-loadable.json'
495+
),
496+
}),
481497
],
482498
module: {
483499
rules: [

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070
"react-dev-utils": "5.0.2",
7171
"react-docgen-typescript-loader": "^3.1.0",
7272
"react-hot-loader": "4.12.6",
73+
"react-loadable": "^5.5.0",
74+
"react-loadable-ssr-addon": "https://github.com/connexta/react-loadable-ssr-addon#8c8d2e52884250c0e82ebcb64d6a64544b393fae",
7375
"require-from-string": "^2.0.2",
7476
"rimraf": "2.6.2",
7577
"script-loader": "0.7.2",
@@ -90,15 +92,14 @@
9092
"webpack-bundle-analyzer": "3.3.2",
9193
"webpack-dev-server": "3.2.1",
9294
"webpack-merge": "4.1.2",
93-
"webpack-node-externals": "^1.7.2",
9495
"whatwg-fetch": "2.0.4",
9596
"worker-loader": "1.1.1"
9697
},
9798
"devDependencies": {
9899
"@types/storybook__addon-info": "^4.1.2",
99100
"@types/storybook__react": "^4.0.2",
100101
"@types/webpack-env": "1.13.6",
101-
"react-dom": "^15.1.0 || ^16.0.0",
102-
"react": "^15.1.0 || ^16.0.0"
102+
"react": "^15.1.0 || ^16.0.0",
103+
"react-dom": "^15.1.0 || ^16.0.0"
103104
}
104105
}

react-loadable/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('react-loadable')

react-loadable/webpack.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.getBundles = require('react-loadable-ssr-addon').getBundles

yarn.lock

+12-6
Original file line numberDiff line numberDiff line change
@@ -10343,7 +10343,7 @@ promise@^7.0.3, promise@^7.1.1:
1034310343
dependencies:
1034410344
asap "~2.0.3"
1034510345

10346-
[email protected], prop-types@^15.5.10, prop-types@^15.5.8, prop-types@^15.6.0, prop-types@^15.6.1, prop-types@^15.6.2, prop-types@^15.7.2:
10346+
[email protected], prop-types@^15.5.0, prop-types@^15.5.10, prop-types@^15.5.8, prop-types@^15.6.0, prop-types@^15.6.1, prop-types@^15.6.2, prop-types@^15.7.2:
1034710347
version "15.7.2"
1034810348
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.7.2.tgz#52c41e75b8c87e72b9d9360e0206b99dcbffa6c5"
1034910349
integrity sha512-8QQikdH7//R2vurIJSutZ1smHYTcLpRWEOlHnzcWHmBYrOGUysKwSsrC89BCiFj3CbrfJ/nXFdJepOVrY1GCHQ==
@@ -10777,6 +10777,17 @@ react-lifecycles-compat@^3.0.4:
1077710777
resolved "https://registry.yarnpkg.com/react-lifecycles-compat/-/react-lifecycles-compat-3.0.4.tgz#4f1a273afdfc8f3488a8c516bfda78f872352362"
1077810778
integrity sha512-fBASbA6LnOU9dOU2eW7aQ8xmYBSXUIWr+UmF9b1efZBazGNO+rcXT/icdKnYm2pTwcRylVUYwW7H1PHfLekVzA==
1077910779

10780+
"react-loadable-ssr-addon@https://github.com/connexta/react-loadable-ssr-addon#8c8d2e52884250c0e82ebcb64d6a64544b393fae":
10781+
version "0.2.0"
10782+
resolved "https://github.com/connexta/react-loadable-ssr-addon#8c8d2e52884250c0e82ebcb64d6a64544b393fae"
10783+
10784+
react-loadable@^5.5.0:
10785+
version "5.5.0"
10786+
resolved "https://registry.yarnpkg.com/react-loadable/-/react-loadable-5.5.0.tgz#582251679d3da86c32aae2c8e689c59f1196d8c4"
10787+
integrity sha512-C8Aui0ZpMd4KokxRdVAm2bQtI03k2RMRNzOB+IipV3yxFTSVICv7WoUr5L9ALB5BmKO1iHgZtWM8EvYG83otdg==
10788+
dependencies:
10789+
prop-types "^15.5.0"
10790+
1078010791
react-popper-tooltip@^2.8.3:
1078110792
version "2.8.3"
1078210793
resolved "https://registry.yarnpkg.com/react-popper-tooltip/-/react-popper-tooltip-2.8.3.tgz#1c63e7473a96362bd93be6c94fa404470a265197"
@@ -13229,11 +13240,6 @@ [email protected]:
1322913240
dependencies:
1323013241
lodash "^4.17.5"
1323113242

13232-
webpack-node-externals@^1.7.2:
13233-
version "1.7.2"
13234-
resolved "https://registry.yarnpkg.com/webpack-node-externals/-/webpack-node-externals-1.7.2.tgz#6e1ee79ac67c070402ba700ef033a9b8d52ac4e3"
13235-
integrity sha512-ajerHZ+BJKeCLviLUUmnyd5B4RavLF76uv3cs6KNuO8W+HuQaEs0y0L7o40NQxdPy5w0pcv8Ew7yPUAQG0UdCg==
13236-
1323713243
webpack-sources@^1.0.1, webpack-sources@^1.1.0, webpack-sources@^1.4.0, webpack-sources@^1.4.1:
1323813244
version "1.4.3"
1323913245
resolved "https://registry.yarnpkg.com/webpack-sources/-/webpack-sources-1.4.3.tgz#eedd8ec0b928fbf1cbfe994e22d2d890f330a933"

0 commit comments

Comments
 (0)