Skip to content

Commit a65a8ab

Browse files
authored
Use async/await in Rollup scripts (#11669)
1 parent 0182769 commit a65a8ab

File tree

3 files changed

+194
-269
lines changed

3 files changed

+194
-269
lines changed

scripts/rollup/build.js

Lines changed: 115 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,8 @@ function getBabelConfig(updateBabelOptions, bundleType, filename) {
8989
plugins: options.plugins.concat([
9090
// Use object-assign polyfill in open source
9191
resolve('./scripts/babel/transform-object-assign-require'),
92-
9392
// Minify invariant messages
9493
require('../error-codes/replace-invariant-error-codes'),
95-
9694
// Wrap warning() calls in a __DEV__ check so they are stripped from production.
9795
require('./plugins/wrap-warning-with-env-check'),
9896
]),
@@ -310,25 +308,25 @@ function getPlugins(
310308
].filter(Boolean);
311309
}
312310

313-
function createBundle(bundle, bundleType) {
311+
async function createBundle(bundle, bundleType) {
314312
const shouldSkipBundleType = bundle.bundleTypes.indexOf(bundleType) === -1;
315313
if (shouldSkipBundleType) {
316-
return Promise.resolve();
314+
return;
317315
}
318316
if (requestedBundleTypes.length > 0) {
319317
const isAskingForDifferentType = requestedBundleTypes.every(
320318
requestedType => bundleType.indexOf(requestedType) === -1
321319
);
322320
if (isAskingForDifferentType) {
323-
return Promise.resolve();
321+
return;
324322
}
325323
}
326324
if (requestedBundleNames.length > 0) {
327325
const isAskingForDifferentNames = requestedBundleNames.every(
328326
requestedName => bundle.label.indexOf(requestedName) === -1
329327
);
330328
if (isAskingForDifferentNames) {
331-
return Promise.resolve();
329+
return;
332330
}
333331
}
334332

@@ -364,160 +362,125 @@ function createBundle(bundle, bundleType) {
364362
);
365363

366364
console.log(`${chalk.bgYellow.black(' BUILDING ')} ${logKey}`);
367-
return rollup({
368-
input: resolvedEntry,
369-
pureExternalModules,
370-
external(id) {
371-
const containsThisModule = pkg => id === pkg || id.startsWith(pkg + '/');
372-
const isProvidedByDependency = externals.some(containsThisModule);
373-
if (!shouldBundleDependencies && isProvidedByDependency) {
374-
return true;
375-
}
376-
return !!peerGlobals[id];
377-
},
378-
onwarn: handleRollupWarnings,
379-
plugins: getPlugins(
380-
bundle.entry,
381-
externals,
382-
bundle.babel,
383-
filename,
384-
bundleType,
385-
bundle.global,
386-
bundle.moduleType,
387-
bundle.modulesToStub,
388-
bundle.featureFlags
389-
),
390-
// We can't use getters in www.
391-
legacy: bundleType === FB_DEV || bundleType === FB_PROD,
392-
})
393-
.then(result =>
394-
result.write(
395-
getRollupOutputOptions(
396-
filename,
397-
format,
398-
bundleType,
399-
peerGlobals,
400-
bundle.global,
401-
bundle.moduleType
402-
)
403-
)
404-
)
405-
.then(() => Packaging.createNodePackage(bundleType, packageName, filename))
406-
.then(() => {
407-
console.log(`${chalk.bgGreen.black(' COMPLETE ')} ${logKey}\n`);
408-
})
409-
.catch(error => {
410-
if (error.code) {
411-
console.error(
412-
`\x1b[31m-- ${error.code}${
413-
error.plugin ? ` (${error.plugin})` : ''
414-
} --`
415-
);
416-
console.error(error.message);
417-
418-
const {file, line, column} = error.loc;
419-
if (file) {
420-
// This looks like an error from Rollup, e.g. missing export.
421-
// We'll use the accurate line numbers provided by Rollup but
422-
// use Babel code frame because it looks nicer.
423-
const rawLines = fs.readFileSync(file, 'utf-8');
424-
// column + 1 is required due to rollup counting column start position from 0
425-
// whereas babel-code-frame counts from 1
426-
const frame = codeFrame(rawLines, line, column + 1, {
427-
highlightCode: true,
428-
});
429-
console.error(frame);
430-
} else {
431-
// This looks like an error from a plugin (e.g. Babel).
432-
// In this case we'll resort to displaying the provided code frame
433-
// because we can't be sure the reported location is accurate.
434-
console.error(error.codeFrame);
365+
try {
366+
const result = await rollup({
367+
input: resolvedEntry,
368+
pureExternalModules,
369+
external(id) {
370+
const containsThisModule = pkg =>
371+
id === pkg || id.startsWith(pkg + '/');
372+
const isProvidedByDependency = externals.some(containsThisModule);
373+
if (!shouldBundleDependencies && isProvidedByDependency) {
374+
return true;
435375
}
376+
return !!peerGlobals[id];
377+
},
378+
onwarn: handleRollupWarnings,
379+
plugins: getPlugins(
380+
bundle.entry,
381+
externals,
382+
bundle.babel,
383+
filename,
384+
bundleType,
385+
bundle.global,
386+
bundle.moduleType,
387+
bundle.modulesToStub,
388+
bundle.featureFlags
389+
),
390+
// We can't use getters in www.
391+
legacy: bundleType === FB_DEV || bundleType === FB_PROD,
392+
});
393+
await result.write(
394+
getRollupOutputOptions(
395+
filename,
396+
format,
397+
bundleType,
398+
peerGlobals,
399+
bundle.global,
400+
bundle.moduleType
401+
)
402+
);
403+
await Packaging.createNodePackage(bundleType, packageName, filename);
404+
console.log(`${chalk.bgGreen.black(' COMPLETE ')} ${logKey}\n`);
405+
} catch (error) {
406+
if (error.code) {
407+
console.error(
408+
`\x1b[31m-- ${error.code}${error.plugin ? ` (${error.plugin})` : ''} --`
409+
);
410+
console.error(error.message);
411+
const {file, line, column} = error.loc;
412+
if (file) {
413+
// This looks like an error from Rollup, e.g. missing export.
414+
// We'll use the accurate line numbers provided by Rollup but
415+
// use Babel code frame because it looks nicer.
416+
const rawLines = fs.readFileSync(file, 'utf-8');
417+
// column + 1 is required due to rollup counting column start position from 0
418+
// whereas babel-code-frame counts from 1
419+
const frame = codeFrame(rawLines, line, column + 1, {
420+
highlightCode: true,
421+
});
422+
console.error(frame);
436423
} else {
437-
console.error(error);
424+
// This looks like an error from a plugin (e.g. Babel).
425+
// In this case we'll resort to displaying the provided code frame
426+
// because we can't be sure the reported location is accurate.
427+
console.error(error.codeFrame);
438428
}
439-
process.exit(1);
440-
});
429+
} else {
430+
console.error(error);
431+
}
432+
process.exit(1);
433+
}
441434
}
442435

443436
// clear the build directory
444-
rimraf('build', () => {
445-
// create a new build directory
446-
fs.mkdirSync('build');
447-
// create the packages folder for NODE+UMD bundles
448-
fs.mkdirSync(join('build', 'packages'));
449-
// create the dist folder for UMD bundles
450-
fs.mkdirSync(join('build', 'dist'));
437+
rimraf('build', async () => {
438+
try {
439+
// create a new build directory
440+
fs.mkdirSync('build');
441+
// create the packages folder for NODE+UMD bundles
442+
fs.mkdirSync(join('build', 'packages'));
443+
// create the dist folder for UMD bundles
444+
fs.mkdirSync(join('build', 'dist'));
451445

452-
const tasks = [
453-
Packaging.createFacebookWWWBuild,
454-
Packaging.createReactNativeBuild,
455-
Packaging.createReactNativeRTBuild,
456-
Packaging.createReactNativeCSBuild,
457-
];
458-
for (const bundle of Bundles.bundles) {
459-
tasks.push(
460-
() => createBundle(bundle, UMD_DEV),
461-
() => createBundle(bundle, UMD_PROD),
462-
() => createBundle(bundle, NODE_DEV),
463-
() => createBundle(bundle, NODE_PROD),
464-
() => createBundle(bundle, FB_DEV),
465-
() => createBundle(bundle, FB_PROD),
466-
() => createBundle(bundle, RN_DEV),
467-
() => createBundle(bundle, RN_PROD)
468-
);
469-
}
470-
if (syncFbsource) {
471-
tasks.push(() =>
472-
syncReactNative(join('build', 'react-native'), syncFbsource)
473-
);
474-
tasks.push(() =>
475-
syncReactNativeRT(join('build', 'react-rt'), syncFbsource)
476-
);
477-
tasks.push(() =>
478-
syncReactNativeCS(join('build', 'react-cs'), syncFbsource)
479-
);
480-
} else if (syncWww) {
481-
tasks.push(() => syncReactDom(join('build', 'facebook-www'), syncWww));
482-
}
483-
// rather than run concurrently, opt to run them serially
484-
// this helps improve console/warning/error output
485-
// and fixes a bunch of IO failures that sometimes occurred
486-
return runWaterfall(tasks)
487-
.then(() => {
488-
// output the results
489-
console.log(Stats.printResults());
490-
// save the results for next run
491-
Stats.saveResults();
492-
if (shouldExtractErrors) {
493-
console.warn(
494-
'\nWarning: this build was created with --extract-errors enabled.\n' +
495-
'this will result in extremely slow builds and should only be\n' +
496-
'used when the error map needs to be rebuilt.\n'
497-
);
498-
}
499-
})
500-
.catch(err => {
501-
console.error(err);
502-
process.exit(1);
503-
});
504-
});
446+
await Packaging.createFacebookWWWBuild();
447+
await Packaging.createReactNativeBuild();
448+
await Packaging.createReactNativeRTBuild();
449+
await Packaging.createReactNativeCSBuild();
505450

506-
function runWaterfall(promiseFactories) {
507-
if (promiseFactories.length === 0) {
508-
return Promise.resolve();
509-
}
451+
// Run them serially for better console output
452+
// and to avoid any potential race conditions.
453+
for (const bundle of Bundles.bundles) {
454+
await createBundle(bundle, UMD_DEV);
455+
await createBundle(bundle, UMD_PROD);
456+
await createBundle(bundle, NODE_DEV);
457+
await createBundle(bundle, NODE_PROD);
458+
await createBundle(bundle, FB_DEV);
459+
await createBundle(bundle, FB_PROD);
460+
await createBundle(bundle, RN_DEV);
461+
await createBundle(bundle, RN_PROD);
462+
}
510463

511-
const head = promiseFactories[0];
512-
const tail = promiseFactories.slice(1);
464+
if (syncFbsource) {
465+
await syncReactNative(join('build', 'react-native'), syncFbsource);
466+
await syncReactNativeRT(join('build', 'react-rt'), syncFbsource);
467+
await syncReactNativeCS(join('build', 'react-cs'), syncFbsource);
468+
} else if (syncWww) {
469+
await syncReactDom(join('build', 'facebook-www'), syncWww);
470+
}
513471

514-
const nextPromiseFactory = head;
515-
const nextPromise = nextPromiseFactory();
516-
if (!nextPromise || typeof nextPromise.then !== 'function') {
517-
throw new Error('runWaterfall() received something that is not a Promise.');
472+
console.log(Stats.printResults());
473+
// save the results for next run
474+
Stats.saveResults();
475+
if (shouldExtractErrors) {
476+
console.warn(
477+
'\nWarning: this build was created with --extract-errors enabled.\n' +
478+
'this will result in extremely slow builds and should only be\n' +
479+
'used when the error map needs to be rebuilt.\n'
480+
);
481+
}
482+
} catch (err) {
483+
console.error(err);
484+
process.exit(1);
518485
}
519-
520-
return nextPromise.then(() => {
521-
return runWaterfall(tail);
522-
});
523-
}
486+
});

0 commit comments

Comments
 (0)