/
server.js
67 lines (62 loc) · 1.92 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const path = require('path')
const express = require('express')
const webpack = require('webpack')
const middleware = require('webpack-dev-middleware')
const compiler = webpack(require('../webpack.config.js'))
// Turns input into an array if not one already
function normalizeArray (arr) {
return Array.isArray(arr) ? arr : [arr]
}
// Gets all the Javascript paths that Webpack has compiled, across chunks
function getAllJsPaths (webpackJson) {
const { assetsByChunkName } = webpackJson
return Object.values(assetsByChunkName).reduce((paths, assets) => {
for (let asset of normalizeArray(assets)) {
if (asset != null && asset.endsWith('.js')) {
paths.push(asset)
}
}
return paths
}, [])
}
// Optionally, just get the Javascript paths from specific chunks
function getJsPathsFromChunks (webpackJson, chunkNames) {
const { assetsByChunkName } = webpackJson
chunkNames = normalizeArray(chunkNames)
return chunkNames.reduce((paths, name) => {
if (assetsByChunkName[name] != null) {
for (let asset of normalizeArray(assetsByChunkName[name])) {
if (asset != null && asset.endsWith('.js')) {
paths.push(asset)
}
}
}
return paths
}, [])
}
let port = 4444
const index = Math.max(process.argv.indexOf('--port'), process.argv.indexOf('-p'))
if (index !== -1) {
port = +process.argv[index + 1] || port
}
const app = express()
.use(middleware(compiler, { serverSideRender: true }))
.use((req, res) => {
const webpackJson = res.locals.webpackStats.toJson()
const paths = getAllJsPaths(webpackJson)
res.send(
`<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<div id="root"></div>
${paths.map((path) => `<script src="${path}"></script>`).join('')}
</body>
</html>`
)
})
.listen(port, () => {
console.log(`Server started at http://localhost:${port}/`)
})