-
Notifications
You must be signed in to change notification settings - Fork 24.2k
/
build.js
374 lines (314 loc) · 10.4 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
* @oncall react_native
*/
const {PACKAGES_DIR} = require('../consts');
const {
buildConfig,
getBabelConfig,
getBuildOptions,
getTypeScriptCompilerOptions,
} = require('./config');
const babel = require('@babel/core');
const {parseArgs} = require('@pkgjs/parseargs');
const chalk = require('chalk');
const translate = require('flow-api-translator');
const {accessSync, constants, promises: fs, readFileSync} = require('fs');
const glob = require('glob');
const micromatch = require('micromatch');
const path = require('path');
const prettier = require('prettier');
const ts = require('typescript');
const SRC_DIR = 'src';
const BUILD_DIR = 'dist';
const JS_FILES_PATTERN = '**/*.js';
const IGNORE_PATTERN = '**/__{tests,mocks,fixtures}__/**';
const config = {
allowPositionals: true,
options: {
help: {type: 'boolean'},
},
};
async function build() {
const {
positionals: packageNames,
values: {help},
} = parseArgs(config);
if (help) {
console.log(`
Usage: node ./scripts/build/build.js <packages>
Build packages (shared monorepo build setup).
By default, builds all packages defined in ./scripts/build/config.js. If a
a package list is provided, builds only those specified.
`);
process.exitCode = 0;
return;
}
console.log('\n' + chalk.bold.inverse('Building packages') + '\n');
const packagesToBuild = packageNames.length
? packageNames.filter(packageName => packageName in buildConfig.packages)
: Object.keys(buildConfig.packages);
for (const packageName of packagesToBuild) {
await buildPackage(packageName);
}
process.exitCode = 0;
}
function invert(map /*: Map<string, string>*/) /*: Map<string, string> */ {
const result /*: Map<string, string>*/ = new Map();
for (const [key, value] of map.entries()) {
result.set(value, key);
}
return result;
}
async function buildPackage(packageName /*: string */) {
const {emitTypeScriptDefs} = getBuildOptions(packageName);
const entryPointRewrites = getEntryPoints(packageName);
const files = glob
.sync(path.resolve(PACKAGES_DIR, packageName, SRC_DIR, '**/*'), {
nodir: true,
})
.filter(file => !entryPointRewrites.has(file));
process.stdout.write(
`${packageName} ${chalk.dim('.').repeat(72 - packageName.length)} `,
);
const invertedEntryPointRewrites = invert(entryPointRewrites);
// Build all files matched for package
for (const file of files) {
await buildFile(path.normalize(file), {
silent: true,
destPath: invertedEntryPointRewrites.get(file),
});
}
// Validate program for emitted .d.ts files
if (emitTypeScriptDefs) {
validateTypeScriptDefs(packageName);
}
// Rewrite package.json "exports" field (src -> dist)
await rewritePackageExports(packageName);
process.stdout.write(chalk.reset.inverse.bold.green(' DONE ') + '\n');
}
async function buildFile(
file /*: string */,
options /*: {silent?: boolean, destPath?: string}*/ = {},
) {
const {silent, destPath} = {silent: false, ...options};
const packageName = getPackageName(file);
const buildPath = getBuildPath(destPath ?? file);
const {emitFlowDefs, emitTypeScriptDefs} = getBuildOptions(packageName);
const logResult = ({copied, desc} /*: {copied: boolean, desc?: string} */) =>
silent ||
console.log(
chalk.dim(' - ') +
path.relative(PACKAGES_DIR, file) +
(copied ? ' -> ' + path.relative(PACKAGES_DIR, buildPath) : ' ') +
(desc != null ? ' (' + desc + ')' : ''),
);
if (micromatch.isMatch(file, IGNORE_PATTERN)) {
logResult({copied: false, desc: 'ignore'});
return;
}
await fs.mkdir(path.dirname(buildPath), {recursive: true});
if (!micromatch.isMatch(file, JS_FILES_PATTERN)) {
await fs.copyFile(file, buildPath);
logResult({copied: true, desc: 'copy'});
return;
}
const source = await fs.readFile(file, 'utf-8');
const prettierConfig = {parser: 'babel'};
// Transform source file using Babel
const transformed = prettier.format(
(await babel.transformFileAsync(file, getBabelConfig(packageName))).code,
prettierConfig,
);
await fs.writeFile(buildPath, transformed);
// Translate source Flow types for each type definition target
if (/@flow/.test(source)) {
await Promise.all([
emitFlowDefs
? fs.writeFile(
buildPath + '.flow',
await translate.translateFlowToFlowDef(source, prettierConfig),
)
: null,
emitTypeScriptDefs
? fs.writeFile(
buildPath.replace(/\.js$/, '') + '.d.ts',
await translate.translateFlowToTSDef(source, prettierConfig),
)
: null,
]);
}
logResult({copied: true});
}
/*::
type PackageJson = {
name: string,
exports?: {[lookup: string]: string},
}
*/
// As a convention, we use a .js/.js.flow file pair for each package
// entry point, with the .js file being a Babel wrapper that can be
// used directly in the monorepo. On build, we drop this wrapper and
// emit a single file from the .js.flow contents.
// can be used directly within the repo. When built, this needs to be rewritten
// and the wrapper dropped:
//
// index.js ──────►{remove wrapper}
// ┌─►index.js
// index.flow.js├─►index.d.ts
// └─►index.flow.js
function getEntryPoints(packageName /*: string*/) /*: Map<string, string> */ {
const pkg /*: PackageJson */ = JSON.parse(
readFileSync(
path.resolve(PACKAGES_DIR, packageName, 'package.json'),
'utf8',
),
);
// Flow files we want transpiled in place of the wrapper js files
const pathMap /*: Map<string, string>*/ = new Map();
for (const packagePath in pkg.exports) {
const original = revertRewriteExportsTarget(pkg.exports[packagePath]);
// Exported json files shouldn't be considered
if (!original.endsWith('.js')) {
continue;
}
if (original.endsWith('.flow.js')) {
throw new Error(
`${chalk.bold(packageName)} has ${chalk.bold(
'exports.' + packagePath + ' = "' + original + '"',
)}. Expecting a .js wrapper file. See other monorepo packages for examples.`,
);
}
// Our special case for wrapper files that need to be stripped
const entryPoint = path.resolve(PACKAGES_DIR, packageName, original);
const {dir, name} = path.parse(entryPoint);
const entryPointFlow = path.join(dir, name + '.flow.js');
try {
accessSync(entryPointFlow, constants.F_OK);
} catch {
throw new Error(
`${chalk.bold(
entryPointFlow,
)} does not exist when building ${chalk.bold(packageName)}.
The ${chalk.bold("package.json's")} ${chalk.bold(
'exports["' + packagePath + '"]',
)}:
- found: ${chalk.bold.green(entryPoint)}
- missing: ${chalk.bold.red(entryPointFlow)}
This is needed so users can directly import the file from the monorepo using Node.`,
);
}
pathMap.set(entryPoint, entryPointFlow);
}
return pathMap;
}
function getPackageName(file /*: string */) /*: string */ {
return path.relative(PACKAGES_DIR, file).split(path.sep)[0];
}
function getBuildPath(file /*: string */) /*: string */ {
const packageDir = path.join(PACKAGES_DIR, getPackageName(file));
return path.join(
packageDir,
file.replace(path.join(packageDir, SRC_DIR), BUILD_DIR),
);
}
async function rewritePackageExports(packageName /*: string */) {
const packageJsonPath = path.join(PACKAGES_DIR, packageName, 'package.json');
const pkg = JSON.parse(await fs.readFile(packageJsonPath, 'utf8'));
if (pkg.exports == null) {
throw new Error(
packageName +
' does not define an "exports" field in its package.json. As part ' +
'of the build setup, this field must be used in order to rewrite ' +
'paths to built files in production.',
);
}
pkg.exports = rewriteExportsField(pkg.exports);
if (pkg.main != null) {
pkg.main = rewriteExportsTarget(pkg.main);
}
await fs.writeFile(packageJsonPath, JSON.stringify(pkg, null, 2) + '\n');
}
/*::
type ExportsField = {
[subpath: string]: ExportsField | string,
} | string;
*/
function rewriteExportsField(
exportsField /*: ExportsField */,
) /*: ExportsField */ {
if (typeof exportsField === 'string') {
return rewriteExportsTarget(exportsField);
}
for (const key in exportsField) {
if (typeof exportsField[key] === 'string') {
exportsField[key] = rewriteExportsTarget(exportsField[key]);
} else if (typeof exportsField[key] === 'object') {
exportsField[key] = rewriteExportsField(exportsField[key]);
}
}
return exportsField;
}
function rewriteExportsTarget(target /*: string */) /*: string */ {
return target.replace('./' + SRC_DIR + '/', './' + BUILD_DIR + '/');
}
function revertRewriteExportsTarget(target /*: string */) /*: string */ {
return target.replace('./' + BUILD_DIR + '/', './' + SRC_DIR + '/');
}
function validateTypeScriptDefs(packageName /*: string */) {
const files = glob.sync(
path.resolve(PACKAGES_DIR, packageName, BUILD_DIR, '**/*.d.ts'),
);
const compilerOptions = {
...getTypeScriptCompilerOptions(packageName),
noEmit: true,
skipLibCheck: false,
};
const program = ts.createProgram(files, compilerOptions);
const emitResult = program.emit();
if (emitResult.diagnostics.length) {
for (const diagnostic of emitResult.diagnostics) {
if (diagnostic.file != null) {
let {line, character} = ts.getLineAndCharacterOfPosition(
diagnostic.file,
diagnostic.start,
);
let message = ts.flattenDiagnosticMessageText(
diagnostic.messageText,
'\n',
);
console.log(
// $FlowIssue[incompatible-use] Type refined above
`${diagnostic.file.fileName} (${line + 1},${
character + 1
}): ${message}`,
);
} else {
console.log(
ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'),
);
}
}
throw new Error(
'Failing build because TypeScript errors were encountered for ' +
'generated type definitions.',
);
}
}
module.exports = {
buildFile,
getBuildPath,
BUILD_DIR,
PACKAGES_DIR,
SRC_DIR,
};
if (require.main === module) {
// eslint-disable-next-line no-void
void build();
}