-
Notifications
You must be signed in to change notification settings - Fork 45
/
index.js
139 lines (119 loc) · 4.3 KB
/
index.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const path = require('path');
const fs = require('fs');
const http2 = require('http2');
const { fileExists } = require('@bolt/build-tools/utils/general');
const { getConfig } = require('@bolt/build-tools/utils/config-store.js');
const prettier = require('prettier');
const { JsDomRenderBackend } = require('./libs/render/backend/jsdom');
const {
WebComponentRenderer,
} = require('./libs/render/renderer/WebComponentRenderer');
async function run() {
let config = await getConfig();
config.components.individual = [];
config.prod = true;
config.enableCache = true;
config.mode = 'server';
config.env = 'pwa';
config.sourceMaps = false;
config.copy = [];
config.webpackDevServer = false;
config.buildDir = path.join(config.wwwDir, 'build-ssr');
config.dataDir = path.join(config.wwwDir, 'build-ssr', 'data');
const webpackStatsGenerated = await setupWebpack(config);
console.log('starting renderer...');
const rendererBackend = new JsDomRenderBackend(config, webpackStatsGenerated);
const renderer = new WebComponentRenderer(rendererBackend, [
'bolt-button',
'bolt-text',
'bolt-icon',
]);
await renderer.start();
console.log('renderer ready...');
const options = {};
const server = http2.createServer(options);
server.on('stream', (stream, headers) => {
if (headers[':path'] !== '/') {
stream.respond({ ':status': 404 });
stream.end();
} else if (headers[':method'].toLowerCase() !== 'post') {
stream.respond({ ':status': 405 });
stream.end();
} else {
const chunks = [];
stream.on('data', chunk => {
chunks.push(chunk);
});
stream.on('end', async () => {
try {
const rendered = await renderer.render(chunks.join());
const prettierHtml = prettier.format(rendered, {
singleQuote: true,
trailingComma: 'es5',
bracketSpacing: true,
jsxBracketSameLine: true,
parser: 'html',
});
stream.respond({ ':status': 200 });
stream.end(prettierHtml);
} catch (e) {
console.log(e);
stream.respond({ ':status': 500 });
stream.end();
}
});
}
});
server.listen(80);
console.log('listening on port 80...');
console.log('');
console.log('Test the server by running:');
console.log('');
console.log(
'curl http://localhost -v --http2-prior-knowledge -X POST -d "<bolt-button>hello world</bolt-button><bolt-icon name=\'close\'></bolt-icon>"',
);
}
async function setupWebpack(config) {
console.log('compiling webpack...');
let webpackConfig, manifestPath, webpackStatsGenerated;
return new Promise(async (resolve, reject) => {
manifestPath =
manifestPath ||
path.join(config.buildDir, 'bolt-webpack-manifest.server.json');
// return early if we already have what we need to do a build
if (await fileExists(manifestPath)) {
resolve(
(webpackStatsGenerated = JSON.parse(fs.readFileSync(manifestPath))),
);
} else {
const createWebpackConfig = require('@bolt/build-tools/create-webpack-config');
const webpack = require('webpack');
webpackConfig = await createWebpackConfig(config);
// strip out Sass files + other duplicate / not as necessary package files for server-side rendering (speeds up compile times)
webpackConfig[0].entry['bolt-global'] = webpackConfig[0].entry[
'bolt-global'
].filter(
item =>
!item.includes('.scss') &&
!item.includes('bolt-smooth-scroll') &&
!item.includes('bolt-sticky') &&
!item.includes('bolt-placeholder') &&
!item.includes('bolt-li') &&
!item.includes('packages/core') &&
!item.includes('global/styles/index.js') &&
!item.includes('bolt-copy-to-clipboard') &&
!item.includes('bolt-icons') &&
!item.includes('bolt-video'),
);
await webpack(webpackConfig, async (err, webpackStats) => {
// @todo: handle webpack errors
// if (err || webpackStats.hasErrors()) {}
// after the build completes, pass along the webpack manifest data like we do for builds already having been cached
resolve(
(webpackStatsGenerated = JSON.parse(fs.readFileSync(manifestPath))),
);
});
}
});
}
run();