-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverUtils.js
57 lines (49 loc) · 1.3 KB
/
serverUtils.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
function watchAndHotInject() {
const chokidar = require('chokidar');
const watcher = chokidar.watch('./server');
watcher.on('ready', () => {
watcher.on('all', () => {
Object.keys(require.cache).forEach((id) => {
if (/[\/\\]server[\/\\]/.test(id)) {
delete require.cache[id];
}
});
console.log('Server was hot updated..');
});
});
}
function conditionallyInjectNextSSR(server, dev, useNextSSR = true) {
return new Promise((resolve, reject) => {
if (useNextSSR) {
const moduleAlias = require('module-alias');
const app = require('next')({ dev });
const handle = app.getRequestHandler();
moduleAlias.addAlias('react-native', 'react-native-web');
temporaryFixBodymovin();
app.prepare().then(() => {
server.get('*', (req, res) => {
return handle(req, res);
});
resolve(server);
}).catch(ex => reject(ex));
} else {
resolve(server);
}
});
}
function temporaryFixBodymovin() {
global.requestAnimationFrame = () => {};
global.document = {
createElement: () => ({ getContext: () => {} }),
createElementNS: () => ({ getContext: () => {} }),
getElementsByTagName: () => ({ getContext: () => {} }),
};
global.navigator = {
userAgent: 'user-agent',
isServer: true,
};
}
module.exports = {
watchAndHotInject,
conditionallyInjectNextSSR,
};