-
Notifications
You must be signed in to change notification settings - Fork 26
/
build.js
143 lines (134 loc) · 3.7 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
const babel = require('@babel/core');
const fs = require('fs/promises');
const path = require('path');
const webpack = require('webpack');
const DIST = path.join(__dirname, 'dist');
const SRC = path.join(__dirname, 'src');
const babelOptions = {
babelrc: false,
plugins: [
[
'minify-replace',
{
replacements: [
{
identifierName: '__DEV__',
replacement: {
type: 'booleanLiteral',
value: false,
},
},
],
},
],
[
'./scripts/babel/invariant',
{
strip: true,
},
],
'relay',
'@babel/plugin-proposal-object-rest-spread',
'@babel/plugin-proposal-class-properties',
'@babel/plugin-proposal-optional-chaining',
'@babel/plugin-proposal-nullish-coalescing-operator',
],
presets: [
[
'@babel/preset-env',
{
targets: {
node: '10.13.0',
},
},
],
'@babel/preset-flow',
'@babel/preset-react',
],
sourceMaps: true,
};
/**
* Transform all ".js" files under "src/" using Babel, excluding "__tests__" and
* "__mocks__", writing into "dist/".
*/
async function runBabel() {
async function traverse(input, output) {
const entries = await fs.readdir(input, {withFileTypes: true});
for (const entry of entries) {
if (entry.isDirectory()) {
if (entry.name !== '__tests__' && entry.name !== '__mocks__') {
await traverse(
path.join(input, entry.name),
path.join(output, entry.name),
);
}
} else if (entry.name.endsWith('.js')) {
const file = path.join(input, entry.name);
const {code, map} = await babel.transformFileAsync(file, babelOptions);
await fs.mkdir(output, {recursive: true});
await fs.writeFile(path.join(output, entry.name), code);
if (map) {
await fs.writeFile(path.join(output, entry.name + '.map'), JSON.stringify(map));
}
}
}
}
console.log('[start] Babel');
await traverse(SRC, DIST);
console.log('[finish] Babel');
}
/**
* Copy "src/__generated__/*.txt" to "dist/__generated__/".
*/
async function runGraphQL() {
console.log('[start] GraphQL');
const input = path.join(SRC, '__generated__');
const output = path.join(DIST, '__generated__');
await fs.mkdir(output, {recursive: true});
const files = await fs.readdir(input);
for (const file of files) {
if (file.endsWith('.txt')) {
// TODO: use `fs.cp` (added in Node 16.7.0).
// await fs.cp(path.join(input, file), path.join(output, file));
await fs.writeFile(
path.join(output, file),
await fs.readFile(path.join(input, file)),
);
}
}
console.log('[finish] GraphQL');
}
/**
* Run webpack with webpack.production.config.
*/
function runWebpack() {
console.log('[start] webpack');
return new Promise((resolve, reject) => {
webpack(require('./webpack.production.config'), (error, stats) => {
if (error) {
reject(error);
} else if (stats.hasErrors()) {
const errors = stats.flatMap(({compilation}) => {
if (compilation.errors) {
return compiliation.errors;
} else {
return [];
}
});
reject(new Error(errors.map((error) => error.toString()).join('\n\n')));
} else {
console.log(stats.toString({colors: true}));
console.log('[finish] webpack');
resolve();
}
});
});
}
console.log('Starting build');
Promise.all([runBabel(), runGraphQL(), runWebpack()])
.then(() => console.log('Done'))
.catch((error) => {
// Ring terminal bell.
process.stderr.write('\x07');
console.log(`Error: ${error}`);
});