-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.mjs
executable file
Β·152 lines (133 loc) Β· 3.82 KB
/
build.mjs
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
#!/usr/bin/env node
import { build } from 'esbuild';
import browserslist from 'browserslist';
import { resolveToEsbuildTarget } from 'esbuild-plugin-browserslist';
import { mkdirSync } from 'fs';
import { replace } from 'esbuild-plugin-replace';
import ts from 'typescript';
import { context } from 'esbuild';
console.log(ts.version);
const production = !!process.env.production;
if (production) {
console.log('π§π§π§ BUILDING FOR PRODUCTION! π§π§π§');
}
else {
console.log('π π π Building for local dev! π π π ');
}
const watch = process.env.WATCH === 'true';
const buildSuccessNotifierPlugin = appName => ({
name: 'Build success notifier',
setup(build) {
build.onEnd(result => {
if (result.errors.length > 0) {
// esbuild has already logged the error.
return;
}
console.log(appName + ' build succeeded at ' + new Date().toLocaleTimeString());
});
},
});
checkTypes();
bundle();
function checkTypes() {
const configPath = ts.findConfigFile(
'./',
ts.sys.fileExists,
'tsconfig.json'
);
if (!configPath) {
throw new Error('Could not find a valid \'tsconfig.json\'.');
}
if (watch) {
const createProgram = ts.createSemanticDiagnosticsBuilderProgram;
const host = ts.createWatchCompilerHost(
configPath,
{},
ts.sys,
createProgram,
printDiag,
printDiag,
);
ts.createWatchProgram(host);
}
else {
console.log('Checking types');
const { options, errors: parseConfigFileErrors } = ts.convertCompilerOptionsFromJson(
ts.parseConfigFileTextToJson(
configPath,
ts.readJsonConfigFile(configPath, ts.sys.readFile).text,
).config.compilerOptions,
);
if (parseConfigFileErrors.length > 0) {
for (const error of parseConfigFileErrors) printDiag(error);
process.exit(1);
}
const program = ts.createProgram({
rootNames: ['server/main.ts', 'client/main.tsx'],
options,
});
const diags = ts.getPreEmitDiagnostics(program);
if (diags.length === 0) {
console.log('No type errors');
}
else {
for (const diag of diags) printDiag(diag)
process.exit(1);
}
}
function printDiag(diag) {
if (diag.file) {
let { line, character } = ts.getLineAndCharacterOfPosition(diag.file, diag.start);
let message = ts.flattenDiagnosticMessageText(diag.messageText, "\n");
console.log(`${diag.file.fileName} (${line + 1},${character + 1}): ${message}`);
} else {
console.log(ts.flattenDiagnosticMessageText(diag.messageText, "\n"));
}
}
}
function bundle() {
mkdirSync('out', { recursive: true });
const buildConfig = {
server: {
entryPoints: ['server/main.ts'],
platform: 'node',
bundle: true,
minify: production,
outfile: 'out/server.js',
format: 'iife',
sourcemap: true,
target: resolveToEsbuildTarget(browserslist('> 0.5%, last 2 versions, not dead'), {
printUnknownTargets: false,
}),
plugins: [
buildSuccessNotifierPlugin('Server')
]
},
client: {
entryPoints: ['client/main.tsx'],
bundle: true,
minify: production,
outfile: `out/client.js`,
format: 'iife',
sourcemap: true,
banner: { js: 'const global = window;' },
target: resolveToEsbuildTarget(browserslist('> 0.5%, last 2 versions, not dead'), {
printUnknownTargets: false,
}),
plugins: [
replace({
'process.env.NODE_ENV': JSON.stringify(production ? 'production' : 'development'),
}),
buildSuccessNotifierPlugin('Client'),
]
},
};
if (watch) {
context(buildConfig.server).then(ctx => ctx.watch());
context(buildConfig.client).then(ctx => ctx.watch());
}
else {
build(buildConfig.server);
build(buildConfig.client);
}
}