-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.js
executable file
·164 lines (139 loc) · 3.81 KB
/
build.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env node
// Based on https://esbuild.github.io/getting-started/#build-scripts
// Assumes modern Node.js
// Usage:
// ./build.js # Build client and server code
// ./build.js --watch # Build, watch for changes and rebuild, and lint
// ./build.js --serve # Build, watch and also serve the fixture in test/fixtures/server.js (implies --watch)
import { fork } from 'child_process';
import { watch, promises as fs } from 'fs';
import { join } from 'path';
import { argv, cwd, exit } from 'process';
import esbuild from 'esbuild';
import { ESLint } from 'eslint';
const { readFile } = fs;
const options = {
serve: argv.includes('--serve'),
};
options.watch = options.serve || argv.includes('--watch');
options.lint = options.watch;
const entryPoints = {
client: 'src/client/index.ts',
server: 'src/server/index.ts',
};
let eslint, eslintFormatter, eslintOptions, eslintFiles, tscPath, tsFiles;
if (options.lint) {
eslint = new ESLint();
eslintFormatter = await eslint.loadFormatter('stylish');
eslintOptions = JSON.parse(await readFile(join(cwd(), '.eslintrc.json')));
eslintFiles = {
buildClient: eslintOptions.overrides.find(el => el.env.browser).files,
buildServer: eslintOptions.overrides.find(el => el.env.node).files,
build: eslintOptions.overrides.flatMap(el => el.files)
};
tscPath = join(cwd(), 'node_modules', '.bin', 'tsc');
// eslint-disable-next-line @typescript-eslint/no-unused-vars
tsFiles = { // TODO: Run tsc only on subset of files that just changed
buildClient: [entryPoints.client],
buildServer: [entryPoints.server],
build: [entryPoints.client, entryPoints.server],
};
}
let serverProcess;
const serverPath = join(cwd(), 'test', 'fixtures', 'server.js');
function buildClient() {
return esbuild.build({
bundle: true,
define: {
'process.env.NODE_ENV': "'production'" // msw/lib/esm/graphql.js references process.env.NODE_ENV
},
entryPoints: [entryPoints.client],
format: 'esm',
outfile: 'dist/client.js',
sourcemap: true,
target: ['es2020'],
})
}
function buildServer() {
return esbuild.build({
bundle: true,
entryPoints: [entryPoints.server],
external: [
'express',
'js-yaml',
'msw',
'msw/lib/esm/mockServiceWorker.js',
'node-fetch',
],
outfile: 'dist/server.cjs',
platform: 'node',
sourcemap: true,
target: ['node14'],
});
}
function build() {
return Promise.all([
buildClient(),
buildServer()
]);
}
let debounce = false;
async function wrapBuildFunction(message, fn) {
if (debounce) {
return;
}
debounce = setTimeout(() => {
debounce = false;
}, 50);
console.clear();
console.log(message);
try {
await fn();
} catch (exception) {
console.error(exception);
if (!options.watch) {
exit(1);
}
}
if (options.serve) {
serve();
}
if (options.lint) {
lint(fn);
}
}
async function lint(fn) {
// https://eslint.org/docs/developer-guide/nodejs-api#eslint-class
try {
const lintResults = await eslint.lintFiles(eslintFiles[fn.name]);
console.log(eslintFormatter.format(lintResults));
} catch (exception) {
console.error(exception);
}
// TODO: Lint only the files that just changed
fork(tscPath, ['--noEmit']);
}
function serve() {
if (serverProcess) {
console.log('Restarting server...');
serverProcess.kill();
} else {
console.log('Starting server...');
}
serverProcess = fork(serverPath)
}
if (options.watch) {
watch('src/client', { recursive: true }, async () => {
await wrapBuildFunction('Rebuilding client...', buildClient);
});
watch('src/server', { recursive: true }, async () => {
await wrapBuildFunction('Rebuilding server...', buildServer);
});
watch('src/shared', { recursive: true }, async () => {
await wrapBuildFunction('Rebuilding client and server...', build);
});
if (options.serve) {
watch(serverPath, serve);
}
}
wrapBuildFunction('Building client and server...', build);