-
Notifications
You must be signed in to change notification settings - Fork 0
/
craco.config.js
68 lines (56 loc) · 1.74 KB
/
craco.config.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
const chalk = require('chalk');
const fs = require('fs');
const path = require('path');
const { whenDev } = require('@craco/craco');
function useHotReload (app, root) {
let router = require(root);
app.use((req, res, next) => router(req, res, next));
fs.watch(root, { recursive: true }, (type, file) => {
try {
let diskRoot = path.resolve(root);
for (let filename in require.cache) {
if (require.cache[filename].filename.startsWith(diskRoot)) {
delete require.cache[filename]
}
}
router = require(root);
console.log('')
console.log('the API server has reloaded', chalk.bold(chalk.green('successfully')))
console.log('')
return true;
} catch (e) {
console.error('')
console.error(chalk.bgRed('An error occurred when trying to reload the API server:'))
// only show the relevant part of the stack
console.error(e.stack.split(/^.*node:internal\/modules/gm)[0].trim())
console.error('')
}
})
}
module.exports = {
devServer: whenDev(() => ({
setupMiddlewares: (_, { app }) => {
try {
require('express-async-errors')
useHotReload(app, './src/server')
app.use(function (err, req, res, next) {
if (err) {
console.error('')
console.error(chalk.bgRed('Unhandled Error:') + ' [', req && req.url, ']')
console.error(String(err.stack).split(/^.*express-async-errors/gm)[0].trim())
console.error('')
return res.status(err.statusCode || 500).type('text').end(err.stack);
}
next()
})
} catch (e) {
console.error('')
console.error(chalk.bgRed('An error occurred when trying to start the API server:'))
console.error(e.stack.split(/^.*node:internal\/modules/gm)[0].trim())
console.error('')
process.exit(1)
}
return _;
}
}))
};