2017-04-05 16:47:29 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
2019-04-25 07:55:44 -04:00
|
|
|
const rollup = require('rollup');
|
2017-04-05 16:47:29 +01:00
|
|
|
const babel = require('rollup-plugin-babel');
|
2018-05-14 17:49:41 +01:00
|
|
|
const closure = require('./plugins/closure-plugin');
|
2017-04-05 16:47:29 +01:00
|
|
|
const commonjs = require('rollup-plugin-commonjs');
|
2023-01-05 15:41:49 -05:00
|
|
|
const flowRemoveTypes = require('flow-remove-types');
|
2017-11-08 22:37:11 +00:00
|
|
|
const prettier = require('rollup-plugin-prettier');
|
2017-04-05 16:47:29 +01:00
|
|
|
const replace = require('rollup-plugin-replace');
|
2017-11-08 22:37:11 +00:00
|
|
|
const stripBanner = require('rollup-plugin-strip-banner');
|
2017-04-05 16:47:29 +01:00
|
|
|
const chalk = require('chalk');
|
2017-11-30 12:11:00 +00:00
|
|
|
const resolve = require('rollup-plugin-node-resolve');
|
2017-04-05 16:47:29 +01:00
|
|
|
const fs = require('fs');
|
|
|
|
|
const argv = require('minimist')(process.argv.slice(2));
|
|
|
|
|
const Modules = require('./modules');
|
|
|
|
|
const Bundles = require('./bundles');
|
2017-12-06 20:11:32 +00:00
|
|
|
const Stats = require('./stats');
|
|
|
|
|
const Sync = require('./sync');
|
2017-04-05 16:47:29 +01:00
|
|
|
const sizes = require('./plugins/sizes-plugin');
|
2017-11-30 12:11:00 +00:00
|
|
|
const useForks = require('./plugins/use-forks-plugin');
|
2018-09-01 12:00:00 -07:00
|
|
|
const stripUnusedImports = require('./plugins/strip-unused-imports');
|
2017-04-05 16:47:29 +01:00
|
|
|
const Packaging = require('./packaging');
|
2020-04-02 17:52:32 -07:00
|
|
|
const {asyncRimRaf} = require('./utils');
|
2023-01-06 04:56:31 +08:00
|
|
|
const codeFrame = require('@babel/code-frame');
|
2017-11-08 22:37:11 +00:00
|
|
|
const Wrappers = require('./wrappers');
|
2017-04-05 16:47:29 +01:00
|
|
|
|
2019-10-18 19:35:33 -07:00
|
|
|
const RELEASE_CHANNEL = process.env.RELEASE_CHANNEL;
|
|
|
|
|
|
|
|
|
|
// Default to building in experimental mode. If the release channel is set via
|
|
|
|
|
// an environment variable, then check if it's "experimental".
|
|
|
|
|
const __EXPERIMENTAL__ =
|
|
|
|
|
typeof RELEASE_CHANNEL === 'string'
|
|
|
|
|
? RELEASE_CHANNEL === 'experimental'
|
|
|
|
|
: true;
|
2019-10-18 14:36:59 -07:00
|
|
|
|
2017-12-06 20:11:32 +00:00
|
|
|
// Errors in promises should be fatal.
|
|
|
|
|
let loggedErrors = new Set();
|
|
|
|
|
process.on('unhandledRejection', err => {
|
|
|
|
|
if (loggedErrors.has(err)) {
|
|
|
|
|
// No need to print it twice.
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
throw err;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const {
|
2020-05-28 15:56:34 -07:00
|
|
|
NODE_ES2015,
|
2020-11-13 08:57:45 -05:00
|
|
|
NODE_ESM,
|
2017-12-06 20:11:32 +00:00
|
|
|
UMD_DEV,
|
|
|
|
|
UMD_PROD,
|
2018-09-13 17:44:08 -07:00
|
|
|
UMD_PROFILING,
|
2017-12-06 20:11:32 +00:00
|
|
|
NODE_DEV,
|
|
|
|
|
NODE_PROD,
|
2018-06-11 13:16:27 -07:00
|
|
|
NODE_PROFILING,
|
2022-11-17 13:15:56 -08:00
|
|
|
BUN_DEV,
|
|
|
|
|
BUN_PROD,
|
2018-04-18 13:16:50 -07:00
|
|
|
FB_WWW_DEV,
|
|
|
|
|
FB_WWW_PROD,
|
2018-06-26 13:28:41 -07:00
|
|
|
FB_WWW_PROFILING,
|
2018-04-18 13:16:50 -07:00
|
|
|
RN_OSS_DEV,
|
|
|
|
|
RN_OSS_PROD,
|
2018-06-11 13:16:27 -07:00
|
|
|
RN_OSS_PROFILING,
|
2018-04-18 13:16:50 -07:00
|
|
|
RN_FB_DEV,
|
|
|
|
|
RN_FB_PROD,
|
2018-06-26 13:28:41 -07:00
|
|
|
RN_FB_PROFILING,
|
2022-10-14 23:29:17 -04:00
|
|
|
BROWSER_SCRIPT,
|
2017-12-06 20:11:32 +00:00
|
|
|
} = Bundles.bundleTypes;
|
2017-04-05 16:47:29 +01:00
|
|
|
|
2020-05-28 15:04:25 -07:00
|
|
|
const {getFilename} = Bundles;
|
|
|
|
|
|
2018-11-07 20:46:41 -08:00
|
|
|
function parseRequestedNames(names, toCase) {
|
|
|
|
|
let result = [];
|
|
|
|
|
for (let i = 0; i < names.length; i++) {
|
|
|
|
|
let splitNames = names[i].split(',');
|
|
|
|
|
for (let j = 0; j < splitNames.length; j++) {
|
|
|
|
|
let name = splitNames[j].trim();
|
|
|
|
|
if (!name) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (toCase === 'uppercase') {
|
|
|
|
|
name = name.toUpperCase();
|
|
|
|
|
} else if (toCase === 'lowercase') {
|
|
|
|
|
name = name.toLowerCase();
|
|
|
|
|
}
|
|
|
|
|
result.push(name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const requestedBundleTypes = argv.type
|
|
|
|
|
? parseRequestedNames([argv.type], 'uppercase')
|
|
|
|
|
: [];
|
|
|
|
|
const requestedBundleNames = parseRequestedNames(argv._, 'lowercase');
|
2017-12-08 00:41:25 +00:00
|
|
|
const forcePrettyOutput = argv.pretty;
|
2019-04-25 07:55:44 -04:00
|
|
|
const isWatchMode = argv.watch;
|
2017-12-06 20:11:32 +00:00
|
|
|
const syncFBSourcePath = argv['sync-fbsource'];
|
|
|
|
|
const syncWWWPath = argv['sync-www'];
|
2017-04-05 16:47:29 +01:00
|
|
|
|
2020-04-01 12:08:37 -07:00
|
|
|
// Non-ES2015 stuff applied before closure compiler.
|
|
|
|
|
const babelPlugins = [
|
|
|
|
|
// These plugins filter out non-ES2015.
|
|
|
|
|
['@babel/plugin-proposal-class-properties', {loose: true}],
|
|
|
|
|
'syntax-trailing-function-commas',
|
|
|
|
|
// These use loose mode which avoids embedding a runtime.
|
|
|
|
|
// TODO: Remove object spread from the source. Prefer Object.assign instead.
|
|
|
|
|
[
|
|
|
|
|
'@babel/plugin-proposal-object-rest-spread',
|
|
|
|
|
{loose: true, useBuiltIns: true},
|
|
|
|
|
],
|
|
|
|
|
['@babel/plugin-transform-template-literals', {loose: true}],
|
|
|
|
|
// TODO: Remove for...of from the source. It requires a runtime to be embedded.
|
|
|
|
|
'@babel/plugin-transform-for-of',
|
|
|
|
|
// TODO: Remove array spread from the source. Prefer .apply instead.
|
|
|
|
|
['@babel/plugin-transform-spread', {loose: true, useBuiltIns: true}],
|
|
|
|
|
'@babel/plugin-transform-parameters',
|
|
|
|
|
// TODO: Remove array destructuring from the source. Requires runtime.
|
|
|
|
|
['@babel/plugin-transform-destructuring', {loose: true, useBuiltIns: true}],
|
2022-02-23 19:34:24 -05:00
|
|
|
// Transform Object spread to shared/assign
|
|
|
|
|
require('../babel/transform-object-assign'),
|
2020-04-01 12:08:37 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const babelToES5Plugins = [
|
|
|
|
|
// These plugins transform DEV mode. Closure compiler deals with these in PROD.
|
|
|
|
|
'@babel/plugin-transform-literals',
|
|
|
|
|
'@babel/plugin-transform-arrow-functions',
|
|
|
|
|
'@babel/plugin-transform-block-scoped-functions',
|
|
|
|
|
'@babel/plugin-transform-shorthand-properties',
|
|
|
|
|
'@babel/plugin-transform-computed-properties',
|
|
|
|
|
['@babel/plugin-transform-block-scoping', {throwIfClosureRequired: true}],
|
|
|
|
|
];
|
|
|
|
|
|
2019-12-14 18:09:25 +00:00
|
|
|
function getBabelConfig(
|
|
|
|
|
updateBabelOptions,
|
|
|
|
|
bundleType,
|
|
|
|
|
packageName,
|
|
|
|
|
externals,
|
[RFC] Codemod invariant -> throw new Error (#22435)
* Hoist error codes import to module scope
When this code was written, the error codes map (`codes.json`) was
created on-the-fly, so we had to lazily require from inside the visitor.
Because `codes.json` is now checked into source, we can import it a
single time in module scope.
* Minify error constructors in production
We use a script to minify our error messages in production. Each message
is assigned an error code, defined in `scripts/error-codes/codes.json`.
Then our build script replaces the messages with a link to our
error decoder page, e.g. https://reactjs.org/docs/error-decoder.html/?invariant=92
This enables us to write helpful error messages without increasing the
bundle size.
Right now, the script only works for `invariant` calls. It does not work
if you throw an Error object. This is an old Facebookism that we don't
really need, other than the fact that our error minification script
relies on it.
So, I've updated the script to minify error constructors, too:
Input:
Error(`A ${adj} message that contains ${noun}`);
Output:
Error(formatProdErrorMessage(ERR_CODE, adj, noun));
It only works for constructors that are literally named Error, though we
could add support for other names, too.
As a next step, I will add a lint rule to enforce that errors written
this way must have a corresponding error code.
* Minify "no fallback UI specified" error in prod
This error message wasn't being minified because it doesn't use
invariant. The reason it didn't use invariant is because this particular
error is created without begin thrown — it doesn't need to be thrown
because it's located inside the error handling part of the runtime.
Now that the error minification script supports Error constructors, we
can minify it by assigning it a production error code in
`scripts/error-codes/codes.json`.
To support the use of Error constructors more generally, I will add a
lint rule that enforces each message has a corresponding error code.
* Lint rule to detect unminified errors
Adds a lint rule that detects when an Error constructor is used without
a corresponding production error code.
We already have this for `invariant`, but not for regular errors, i.e.
`throw new Error(msg)`. There's also nothing that enforces the use of
`invariant` besides convention.
There are some packages where we don't care to minify errors. These are
packages that run in environments where bundle size is not a concern,
like react-pg. I added an override in the ESLint config to ignore these.
* Temporarily add invariant codemod script
I'm adding this codemod to the repo temporarily, but I'll revert it
in the same PR. That way we don't have to check it in but it's still
accessible (via the PR) if we need it later.
* [Automated] Codemod invariant -> Error
This commit contains only automated changes:
npx jscodeshift -t scripts/codemod-invariant.js packages --ignore-pattern="node_modules/**/*"
yarn linc --fix
yarn prettier
I will do any manual touch ups in separate commits so they're easier
to review.
* Remove temporary codemod script
This reverts the codemod script and ESLint config I added temporarily
in order to perform the invariant codemod.
* Manual touch ups
A few manual changes I made after the codemod ran.
* Enable error code transform per package
Currently we're not consistent about which packages should have their
errors minified in production and which ones should.
This adds a field to the bundle configuration to control whether to
apply the transform. We should decide what the criteria is going
forward. I think it's probably a good idea to minify any package that
gets sent over the network. So yes to modules that run in the browser,
and no to modules that run on the server and during development only.
2021-09-30 15:01:28 -04:00
|
|
|
isDevelopment,
|
|
|
|
|
bundle
|
2019-12-14 18:09:25 +00:00
|
|
|
) {
|
|
|
|
|
const canAccessReactObject =
|
|
|
|
|
packageName === 'react' || externals.indexOf('react') !== -1;
|
2017-10-25 02:55:00 +03:00
|
|
|
let options = {
|
2018-03-23 19:51:04 +00:00
|
|
|
exclude: '/**/node_modules/**',
|
2020-04-01 12:08:37 -07:00
|
|
|
babelrc: false,
|
|
|
|
|
configFile: false,
|
2017-10-25 02:55:00 +03:00
|
|
|
presets: [],
|
2020-04-01 12:08:37 -07:00
|
|
|
plugins: [...babelPlugins],
|
2017-10-25 02:55:00 +03:00
|
|
|
};
|
2019-12-14 18:09:25 +00:00
|
|
|
if (isDevelopment) {
|
|
|
|
|
options.plugins.push(
|
2020-04-01 12:08:37 -07:00
|
|
|
...babelToES5Plugins,
|
2019-12-14 18:09:25 +00:00
|
|
|
// Turn console.error/warn() into a custom wrapper
|
|
|
|
|
[
|
|
|
|
|
require('../babel/transform-replace-console-calls'),
|
|
|
|
|
{
|
|
|
|
|
shouldError: !canAccessReactObject,
|
|
|
|
|
},
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
2017-10-25 02:55:00 +03:00
|
|
|
if (updateBabelOptions) {
|
|
|
|
|
options = updateBabelOptions(options);
|
|
|
|
|
}
|
2021-10-31 18:37:32 -04:00
|
|
|
// Controls whether to replace error messages with error codes in production.
|
|
|
|
|
// By default, error messages are replaced in production.
|
|
|
|
|
if (!isDevelopment && bundle.minifyWithProdErrorCodes !== false) {
|
|
|
|
|
options.plugins.push(require('../error-codes/transform-error-messages'));
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 19:34:24 -05:00
|
|
|
return options;
|
2017-04-05 16:47:29 +01:00
|
|
|
}
|
|
|
|
|
|
2018-05-22 08:16:59 -07:00
|
|
|
function getRollupOutputOptions(
|
|
|
|
|
outputPath,
|
|
|
|
|
format,
|
|
|
|
|
globals,
|
|
|
|
|
globalName,
|
|
|
|
|
bundleType
|
|
|
|
|
) {
|
|
|
|
|
const isProduction = isProductionBundleType(bundleType);
|
|
|
|
|
|
2020-02-25 13:54:27 -08:00
|
|
|
return {
|
|
|
|
|
file: outputPath,
|
|
|
|
|
format,
|
|
|
|
|
globals,
|
|
|
|
|
freeze: !isProduction,
|
|
|
|
|
interop: false,
|
|
|
|
|
name: globalName,
|
|
|
|
|
sourcemap: false,
|
|
|
|
|
esModule: false,
|
|
|
|
|
};
|
2017-04-05 16:47:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getFormat(bundleType) {
|
|
|
|
|
switch (bundleType) {
|
|
|
|
|
case UMD_DEV:
|
|
|
|
|
case UMD_PROD:
|
2018-09-13 17:44:08 -07:00
|
|
|
case UMD_PROFILING:
|
2017-04-05 16:47:29 +01:00
|
|
|
return `umd`;
|
2020-05-28 15:56:34 -07:00
|
|
|
case NODE_ES2015:
|
2017-04-05 16:47:29 +01:00
|
|
|
case NODE_DEV:
|
|
|
|
|
case NODE_PROD:
|
2018-06-11 13:16:27 -07:00
|
|
|
case NODE_PROFILING:
|
2022-11-17 13:15:56 -08:00
|
|
|
case BUN_DEV:
|
|
|
|
|
case BUN_PROD:
|
2018-04-18 13:16:50 -07:00
|
|
|
case FB_WWW_DEV:
|
|
|
|
|
case FB_WWW_PROD:
|
2018-06-26 13:28:41 -07:00
|
|
|
case FB_WWW_PROFILING:
|
2018-04-18 13:16:50 -07:00
|
|
|
case RN_OSS_DEV:
|
|
|
|
|
case RN_OSS_PROD:
|
2018-06-11 13:16:27 -07:00
|
|
|
case RN_OSS_PROFILING:
|
2018-04-18 13:16:50 -07:00
|
|
|
case RN_FB_DEV:
|
|
|
|
|
case RN_FB_PROD:
|
2018-06-26 13:28:41 -07:00
|
|
|
case RN_FB_PROFILING:
|
2017-04-05 16:47:29 +01:00
|
|
|
return `cjs`;
|
2020-11-13 08:57:45 -05:00
|
|
|
case NODE_ESM:
|
|
|
|
|
return `es`;
|
2022-10-14 23:29:17 -04:00
|
|
|
case BROWSER_SCRIPT:
|
|
|
|
|
return `iife`;
|
2017-04-05 16:47:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-08 22:37:11 +00:00
|
|
|
function isProductionBundleType(bundleType) {
|
|
|
|
|
switch (bundleType) {
|
2020-05-28 15:56:34 -07:00
|
|
|
case NODE_ES2015:
|
2020-11-13 08:57:45 -05:00
|
|
|
case NODE_ESM:
|
2023-02-09 19:45:05 -05:00
|
|
|
return true;
|
2017-11-08 22:37:11 +00:00
|
|
|
case UMD_DEV:
|
|
|
|
|
case NODE_DEV:
|
2022-11-17 13:15:56 -08:00
|
|
|
case BUN_DEV:
|
2018-04-18 13:16:50 -07:00
|
|
|
case FB_WWW_DEV:
|
|
|
|
|
case RN_OSS_DEV:
|
|
|
|
|
case RN_FB_DEV:
|
2017-11-08 22:37:11 +00:00
|
|
|
return false;
|
|
|
|
|
case UMD_PROD:
|
|
|
|
|
case NODE_PROD:
|
2022-11-17 13:15:56 -08:00
|
|
|
case BUN_PROD:
|
2018-09-13 17:44:08 -07:00
|
|
|
case UMD_PROFILING:
|
2018-06-11 13:16:27 -07:00
|
|
|
case NODE_PROFILING:
|
2018-04-18 13:16:50 -07:00
|
|
|
case FB_WWW_PROD:
|
2018-06-26 13:28:41 -07:00
|
|
|
case FB_WWW_PROFILING:
|
2018-04-18 13:16:50 -07:00
|
|
|
case RN_OSS_PROD:
|
2018-06-11 13:16:27 -07:00
|
|
|
case RN_OSS_PROFILING:
|
2018-04-18 13:16:50 -07:00
|
|
|
case RN_FB_PROD:
|
2018-06-26 13:28:41 -07:00
|
|
|
case RN_FB_PROFILING:
|
2022-10-14 23:29:17 -04:00
|
|
|
case BROWSER_SCRIPT:
|
2017-11-08 22:37:11 +00:00
|
|
|
return true;
|
|
|
|
|
default:
|
|
|
|
|
throw new Error(`Unknown type: ${bundleType}`);
|
|
|
|
|
}
|
2017-04-05 16:47:29 +01:00
|
|
|
}
|
|
|
|
|
|
2018-06-11 13:16:27 -07:00
|
|
|
function isProfilingBundleType(bundleType) {
|
|
|
|
|
switch (bundleType) {
|
2020-05-28 15:56:34 -07:00
|
|
|
case NODE_ES2015:
|
2020-11-13 08:57:45 -05:00
|
|
|
case NODE_ESM:
|
2018-06-11 13:16:27 -07:00
|
|
|
case FB_WWW_DEV:
|
|
|
|
|
case FB_WWW_PROD:
|
|
|
|
|
case NODE_DEV:
|
|
|
|
|
case NODE_PROD:
|
2022-11-17 13:15:56 -08:00
|
|
|
case BUN_DEV:
|
|
|
|
|
case BUN_PROD:
|
2018-06-11 13:16:27 -07:00
|
|
|
case RN_FB_DEV:
|
|
|
|
|
case RN_FB_PROD:
|
|
|
|
|
case RN_OSS_DEV:
|
|
|
|
|
case RN_OSS_PROD:
|
|
|
|
|
case UMD_DEV:
|
|
|
|
|
case UMD_PROD:
|
2022-10-14 23:29:17 -04:00
|
|
|
case BROWSER_SCRIPT:
|
2018-06-11 13:16:27 -07:00
|
|
|
return false;
|
2018-06-26 13:28:41 -07:00
|
|
|
case FB_WWW_PROFILING:
|
2018-06-11 13:16:27 -07:00
|
|
|
case NODE_PROFILING:
|
2018-06-26 13:28:41 -07:00
|
|
|
case RN_FB_PROFILING:
|
2018-06-11 13:16:27 -07:00
|
|
|
case RN_OSS_PROFILING:
|
2018-09-13 17:44:08 -07:00
|
|
|
case UMD_PROFILING:
|
2018-06-11 13:16:27 -07:00
|
|
|
return true;
|
|
|
|
|
default:
|
|
|
|
|
throw new Error(`Unknown type: ${bundleType}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-09 16:54:28 +01:00
|
|
|
function forbidFBJSImports() {
|
2018-06-19 16:03:45 +01:00
|
|
|
return {
|
2018-09-09 16:54:28 +01:00
|
|
|
name: 'forbidFBJSImports',
|
2018-06-19 16:03:45 +01:00
|
|
|
resolveId(importee, importer) {
|
|
|
|
|
if (/^fbjs\//.test(importee)) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`Don't import ${importee} (found in ${importer}). ` +
|
|
|
|
|
`Use the utilities in packages/shared/ instead.`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-05 16:47:29 +01:00
|
|
|
function getPlugins(
|
|
|
|
|
entry,
|
2017-10-25 02:55:00 +03:00
|
|
|
externals,
|
|
|
|
|
updateBabelOptions,
|
2017-04-05 16:47:29 +01:00
|
|
|
filename,
|
2017-12-23 14:22:59 -05:00
|
|
|
packageName,
|
2017-04-05 16:47:29 +01:00
|
|
|
bundleType,
|
2017-10-25 02:55:00 +03:00
|
|
|
globalName,
|
2017-10-11 14:29:26 -04:00
|
|
|
moduleType,
|
2020-03-12 11:38:32 -07:00
|
|
|
pureExternalModules,
|
|
|
|
|
bundle
|
2017-04-05 16:47:29 +01:00
|
|
|
) {
|
2020-03-12 11:38:32 -07:00
|
|
|
const forks = Modules.getForks(bundleType, entry, moduleType, bundle);
|
2017-11-08 22:37:11 +00:00
|
|
|
const isProduction = isProductionBundleType(bundleType);
|
2018-06-11 13:16:27 -07:00
|
|
|
const isProfiling = isProfilingBundleType(bundleType);
|
2018-09-13 17:44:08 -07:00
|
|
|
const isUMDBundle =
|
|
|
|
|
bundleType === UMD_DEV ||
|
|
|
|
|
bundleType === UMD_PROD ||
|
|
|
|
|
bundleType === UMD_PROFILING;
|
2020-07-20 13:37:04 -04:00
|
|
|
const isFBWWWBundle =
|
2018-06-26 13:28:41 -07:00
|
|
|
bundleType === FB_WWW_DEV ||
|
|
|
|
|
bundleType === FB_WWW_PROD ||
|
|
|
|
|
bundleType === FB_WWW_PROFILING;
|
2018-04-18 13:16:50 -07:00
|
|
|
const isRNBundle =
|
|
|
|
|
bundleType === RN_OSS_DEV ||
|
|
|
|
|
bundleType === RN_OSS_PROD ||
|
2018-06-11 13:16:27 -07:00
|
|
|
bundleType === RN_OSS_PROFILING ||
|
2018-04-18 13:16:50 -07:00
|
|
|
bundleType === RN_FB_DEV ||
|
2018-06-26 13:28:41 -07:00
|
|
|
bundleType === RN_FB_PROD ||
|
|
|
|
|
bundleType === RN_FB_PROFILING;
|
2020-07-20 13:37:04 -04:00
|
|
|
const shouldStayReadable = isFBWWWBundle || isRNBundle || forcePrettyOutput;
|
2017-11-08 22:37:11 +00:00
|
|
|
return [
|
2023-01-05 15:41:49 -05:00
|
|
|
{
|
|
|
|
|
name: 'rollup-plugin-flow-remove-types',
|
|
|
|
|
transform(code) {
|
|
|
|
|
const transformed = flowRemoveTypes(code);
|
|
|
|
|
return {
|
|
|
|
|
code: transformed.toString(),
|
|
|
|
|
map: transformed.generateMap(),
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
},
|
2017-11-30 12:11:00 +00:00
|
|
|
// Shim any modules that need forking in this environment.
|
|
|
|
|
useForks(forks),
|
2018-06-19 17:52:37 +01:00
|
|
|
// Ensure we don't try to bundle any fbjs modules.
|
2018-09-09 16:54:28 +01:00
|
|
|
forbidFBJSImports(),
|
2017-11-02 19:50:03 +00:00
|
|
|
// Use Node resolution mechanism.
|
2017-11-30 12:11:00 +00:00
|
|
|
resolve({
|
2017-10-25 02:55:00 +03:00
|
|
|
skip: externals,
|
|
|
|
|
}),
|
2017-11-08 22:37:11 +00:00
|
|
|
// Remove license headers from individual modules
|
|
|
|
|
stripBanner({
|
|
|
|
|
exclude: 'node_modules/**/*',
|
|
|
|
|
}),
|
2020-04-01 12:08:37 -07:00
|
|
|
// Compile to ES2015.
|
2019-12-14 18:09:25 +00:00
|
|
|
babel(
|
|
|
|
|
getBabelConfig(
|
|
|
|
|
updateBabelOptions,
|
|
|
|
|
bundleType,
|
|
|
|
|
packageName,
|
|
|
|
|
externals,
|
[RFC] Codemod invariant -> throw new Error (#22435)
* Hoist error codes import to module scope
When this code was written, the error codes map (`codes.json`) was
created on-the-fly, so we had to lazily require from inside the visitor.
Because `codes.json` is now checked into source, we can import it a
single time in module scope.
* Minify error constructors in production
We use a script to minify our error messages in production. Each message
is assigned an error code, defined in `scripts/error-codes/codes.json`.
Then our build script replaces the messages with a link to our
error decoder page, e.g. https://reactjs.org/docs/error-decoder.html/?invariant=92
This enables us to write helpful error messages without increasing the
bundle size.
Right now, the script only works for `invariant` calls. It does not work
if you throw an Error object. This is an old Facebookism that we don't
really need, other than the fact that our error minification script
relies on it.
So, I've updated the script to minify error constructors, too:
Input:
Error(`A ${adj} message that contains ${noun}`);
Output:
Error(formatProdErrorMessage(ERR_CODE, adj, noun));
It only works for constructors that are literally named Error, though we
could add support for other names, too.
As a next step, I will add a lint rule to enforce that errors written
this way must have a corresponding error code.
* Minify "no fallback UI specified" error in prod
This error message wasn't being minified because it doesn't use
invariant. The reason it didn't use invariant is because this particular
error is created without begin thrown — it doesn't need to be thrown
because it's located inside the error handling part of the runtime.
Now that the error minification script supports Error constructors, we
can minify it by assigning it a production error code in
`scripts/error-codes/codes.json`.
To support the use of Error constructors more generally, I will add a
lint rule that enforces each message has a corresponding error code.
* Lint rule to detect unminified errors
Adds a lint rule that detects when an Error constructor is used without
a corresponding production error code.
We already have this for `invariant`, but not for regular errors, i.e.
`throw new Error(msg)`. There's also nothing that enforces the use of
`invariant` besides convention.
There are some packages where we don't care to minify errors. These are
packages that run in environments where bundle size is not a concern,
like react-pg. I added an override in the ESLint config to ignore these.
* Temporarily add invariant codemod script
I'm adding this codemod to the repo temporarily, but I'll revert it
in the same PR. That way we don't have to check it in but it's still
accessible (via the PR) if we need it later.
* [Automated] Codemod invariant -> Error
This commit contains only automated changes:
npx jscodeshift -t scripts/codemod-invariant.js packages --ignore-pattern="node_modules/**/*"
yarn linc --fix
yarn prettier
I will do any manual touch ups in separate commits so they're easier
to review.
* Remove temporary codemod script
This reverts the codemod script and ESLint config I added temporarily
in order to perform the invariant codemod.
* Manual touch ups
A few manual changes I made after the codemod ran.
* Enable error code transform per package
Currently we're not consistent about which packages should have their
errors minified in production and which ones should.
This adds a field to the bundle configuration to control whether to
apply the transform. We should decide what the criteria is going
forward. I think it's probably a good idea to minify any package that
gets sent over the network. So yes to modules that run in the browser,
and no to modules that run on the server and during development only.
2021-09-30 15:01:28 -04:00
|
|
|
!isProduction,
|
|
|
|
|
bundle
|
2019-12-14 18:09:25 +00:00
|
|
|
)
|
|
|
|
|
),
|
2017-11-08 22:37:11 +00:00
|
|
|
// Remove 'use strict' from individual source files.
|
|
|
|
|
{
|
|
|
|
|
transform(source) {
|
2019-08-29 15:49:27 +02:00
|
|
|
return source.replace(/['"]use strict["']/g, '');
|
2017-11-08 22:37:11 +00:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
// Turn __DEV__ and process.env checks into constants.
|
|
|
|
|
replace({
|
|
|
|
|
__DEV__: isProduction ? 'false' : 'true',
|
2018-06-11 13:16:27 -07:00
|
|
|
__PROFILE__: isProfiling || !isProduction ? 'true' : 'false',
|
2018-09-01 12:00:00 -07:00
|
|
|
__UMD__: isUMDBundle ? 'true' : 'false',
|
2017-11-08 22:37:11 +00:00
|
|
|
'process.env.NODE_ENV': isProduction ? "'production'" : "'development'",
|
2019-10-18 14:36:59 -07:00
|
|
|
__EXPERIMENTAL__,
|
2017-11-08 22:37:11 +00:00
|
|
|
}),
|
2020-02-28 16:46:17 -08:00
|
|
|
// The CommonJS plugin *only* exists to pull "art" into "react-art".
|
|
|
|
|
// I'm going to port "art" to ES modules to avoid this problem.
|
|
|
|
|
// Please don't enable this for anything else!
|
|
|
|
|
isUMDBundle && entry === 'react-art' && commonjs(),
|
2017-11-08 22:37:11 +00:00
|
|
|
// Apply dead code elimination and/or minification.
|
2023-02-09 19:45:05 -05:00
|
|
|
// closure doesn't yet support leaving ESM imports intact
|
2017-11-08 22:37:11 +00:00
|
|
|
isProduction &&
|
2023-02-09 19:45:05 -05:00
|
|
|
bundleType !== NODE_ESM &&
|
2022-10-14 23:29:17 -04:00
|
|
|
closure({
|
|
|
|
|
compilation_level: 'SIMPLE',
|
2023-02-09 19:45:05 -05:00
|
|
|
language_in: 'ECMASCRIPT_2018',
|
2022-10-14 23:29:17 -04:00
|
|
|
language_out:
|
2023-02-09 19:45:05 -05:00
|
|
|
bundleType === NODE_ES2015
|
|
|
|
|
? 'ECMASCRIPT_2018'
|
|
|
|
|
: bundleType === BROWSER_SCRIPT
|
|
|
|
|
? 'ECMASCRIPT5'
|
|
|
|
|
: 'ECMASCRIPT5_STRICT',
|
2022-10-14 23:29:17 -04:00
|
|
|
env: 'CUSTOM',
|
|
|
|
|
warning_level: 'QUIET',
|
|
|
|
|
apply_input_source_maps: false,
|
|
|
|
|
use_types_for_optimization: false,
|
|
|
|
|
process_common_js_modules: false,
|
|
|
|
|
rewrite_polyfills: false,
|
|
|
|
|
inject_libraries: false,
|
|
|
|
|
|
|
|
|
|
// Don't let it create global variables in the browser.
|
|
|
|
|
// https://github.com/facebook/react/issues/10909
|
|
|
|
|
assume_function_wrapper: !isUMDBundle,
|
|
|
|
|
renaming: !shouldStayReadable,
|
|
|
|
|
}),
|
2018-09-01 12:00:00 -07:00
|
|
|
// HACK to work around the fact that Rollup isn't removing unused, pure-module imports.
|
|
|
|
|
// Note that this plugin must be called after closure applies DCE.
|
|
|
|
|
isProduction && stripUnusedImports(pureExternalModules),
|
2017-11-08 22:37:11 +00:00
|
|
|
// Add the whitespace back if necessary.
|
2020-02-20 23:09:30 +01:00
|
|
|
shouldStayReadable &&
|
|
|
|
|
prettier({
|
2023-01-31 08:25:05 -05:00
|
|
|
parser: 'flow',
|
2020-02-20 23:09:30 +01:00
|
|
|
singleQuote: false,
|
|
|
|
|
trailingComma: 'none',
|
|
|
|
|
bracketSpacing: true,
|
|
|
|
|
}),
|
2017-11-08 22:37:11 +00:00
|
|
|
// License and haste headers, top-level `if` blocks.
|
|
|
|
|
{
|
2020-02-20 23:09:30 +01:00
|
|
|
renderChunk(source) {
|
2017-11-08 22:37:11 +00:00
|
|
|
return Wrappers.wrapBundle(
|
|
|
|
|
source,
|
|
|
|
|
bundleType,
|
|
|
|
|
globalName,
|
|
|
|
|
filename,
|
2021-10-21 14:40:41 -04:00
|
|
|
moduleType,
|
|
|
|
|
bundle.wrapWithModuleBoundaries
|
2017-11-08 22:37:11 +00:00
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
// Record bundle size.
|
2017-04-05 16:47:29 +01:00
|
|
|
sizes({
|
|
|
|
|
getSize: (size, gzip) => {
|
2017-12-24 01:44:08 +00:00
|
|
|
const currentSizes = Stats.currentBuildResults.bundleSizes;
|
|
|
|
|
const recordIndex = currentSizes.findIndex(
|
|
|
|
|
record =>
|
|
|
|
|
record.filename === filename && record.bundleType === bundleType
|
|
|
|
|
);
|
|
|
|
|
const index = recordIndex !== -1 ? recordIndex : currentSizes.length;
|
|
|
|
|
currentSizes[index] = {
|
2017-12-23 14:22:59 -05:00
|
|
|
filename,
|
|
|
|
|
bundleType,
|
|
|
|
|
packageName,
|
2017-04-05 16:47:29 +01:00
|
|
|
size,
|
|
|
|
|
gzip,
|
2017-12-24 01:44:08 +00:00
|
|
|
};
|
2017-04-05 16:47:29 +01:00
|
|
|
},
|
2017-11-08 22:37:11 +00:00
|
|
|
}),
|
|
|
|
|
].filter(Boolean);
|
2017-04-05 16:47:29 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-06 20:11:32 +00:00
|
|
|
function shouldSkipBundle(bundle, bundleType) {
|
2017-04-07 16:17:36 +01:00
|
|
|
const shouldSkipBundleType = bundle.bundleTypes.indexOf(bundleType) === -1;
|
|
|
|
|
if (shouldSkipBundleType) {
|
2017-12-06 20:11:32 +00:00
|
|
|
return true;
|
2017-04-05 16:47:29 +01:00
|
|
|
}
|
2017-04-07 16:17:36 +01:00
|
|
|
if (requestedBundleTypes.length > 0) {
|
|
|
|
|
const isAskingForDifferentType = requestedBundleTypes.every(
|
|
|
|
|
requestedType => bundleType.indexOf(requestedType) === -1
|
|
|
|
|
);
|
|
|
|
|
if (isAskingForDifferentType) {
|
2017-12-06 20:11:32 +00:00
|
|
|
return true;
|
2017-04-07 16:17:36 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (requestedBundleNames.length > 0) {
|
2022-04-11 21:01:48 -04:00
|
|
|
// If the name ends with `something/index` we only match if the
|
|
|
|
|
// entry ends in something. Such as `react-dom/index` only matches
|
|
|
|
|
// `react-dom` but not `react-dom/server`. Everything else is fuzzy
|
|
|
|
|
// search.
|
|
|
|
|
const entryLowerCase = bundle.entry.toLowerCase() + '/index.js';
|
2017-04-07 16:17:36 +01:00
|
|
|
const isAskingForDifferentNames = requestedBundleNames.every(
|
2022-04-11 21:01:48 -04:00
|
|
|
requestedName => {
|
|
|
|
|
const matchEntry = entryLowerCase.indexOf(requestedName) !== -1;
|
|
|
|
|
if (!bundle.name) {
|
|
|
|
|
return !matchEntry;
|
|
|
|
|
}
|
|
|
|
|
const matchName =
|
|
|
|
|
bundle.name.toLowerCase().indexOf(requestedName) !== -1;
|
|
|
|
|
return !matchEntry && !matchName;
|
|
|
|
|
}
|
2017-04-07 16:17:36 +01:00
|
|
|
);
|
|
|
|
|
if (isAskingForDifferentNames) {
|
2017-12-06 20:11:32 +00:00
|
|
|
return true;
|
2017-04-07 16:17:36 +01:00
|
|
|
}
|
|
|
|
|
}
|
2017-12-06 20:11:32 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-25 13:54:27 -08:00
|
|
|
function resolveEntryFork(resolvedEntry, isFBBundle) {
|
|
|
|
|
// Pick which entry point fork to use:
|
|
|
|
|
// .modern.fb.js
|
|
|
|
|
// .classic.fb.js
|
|
|
|
|
// .fb.js
|
|
|
|
|
// .stable.js
|
|
|
|
|
// .experimental.js
|
|
|
|
|
// .js
|
|
|
|
|
|
|
|
|
|
if (isFBBundle) {
|
|
|
|
|
const resolvedFBEntry = resolvedEntry.replace(
|
|
|
|
|
'.js',
|
|
|
|
|
__EXPERIMENTAL__ ? '.modern.fb.js' : '.classic.fb.js'
|
|
|
|
|
);
|
|
|
|
|
if (fs.existsSync(resolvedFBEntry)) {
|
|
|
|
|
return resolvedFBEntry;
|
|
|
|
|
}
|
|
|
|
|
const resolvedGenericFBEntry = resolvedEntry.replace('.js', '.fb.js');
|
|
|
|
|
if (fs.existsSync(resolvedGenericFBEntry)) {
|
|
|
|
|
return resolvedGenericFBEntry;
|
|
|
|
|
}
|
|
|
|
|
// Even if it's a FB bundle we fallthrough to pick stable or experimental if we don't have an FB fork.
|
|
|
|
|
}
|
|
|
|
|
const resolvedForkedEntry = resolvedEntry.replace(
|
|
|
|
|
'.js',
|
|
|
|
|
__EXPERIMENTAL__ ? '.experimental.js' : '.stable.js'
|
|
|
|
|
);
|
|
|
|
|
if (fs.existsSync(resolvedForkedEntry)) {
|
|
|
|
|
return resolvedForkedEntry;
|
|
|
|
|
}
|
|
|
|
|
// Just use the plain .js one.
|
|
|
|
|
return resolvedEntry;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-06 20:11:32 +00:00
|
|
|
async function createBundle(bundle, bundleType) {
|
|
|
|
|
if (shouldSkipBundle(bundle, bundleType)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-04-05 16:47:29 +01:00
|
|
|
|
2020-05-28 15:04:25 -07:00
|
|
|
const filename = getFilename(bundle, bundleType);
|
2017-04-20 11:18:33 -07:00
|
|
|
const logKey =
|
|
|
|
|
chalk.white.bold(filename) + chalk.dim(` (${bundleType.toLowerCase()})`);
|
2017-04-05 16:47:29 +01:00
|
|
|
const format = getFormat(bundleType);
|
2017-10-25 02:55:00 +03:00
|
|
|
const packageName = Packaging.getPackageName(bundle.entry);
|
|
|
|
|
|
2020-07-20 13:37:04 -04:00
|
|
|
const isFBWWWBundle =
|
2018-06-26 13:28:41 -07:00
|
|
|
bundleType === FB_WWW_DEV ||
|
|
|
|
|
bundleType === FB_WWW_PROD ||
|
2019-09-17 20:22:09 +02:00
|
|
|
bundleType === FB_WWW_PROFILING;
|
2020-02-25 13:54:27 -08:00
|
|
|
|
2020-07-20 13:37:04 -04:00
|
|
|
const isFBRNBundle =
|
|
|
|
|
bundleType === RN_FB_DEV ||
|
|
|
|
|
bundleType === RN_FB_PROD ||
|
|
|
|
|
bundleType === RN_FB_PROFILING;
|
|
|
|
|
|
2020-02-25 13:54:27 -08:00
|
|
|
let resolvedEntry = resolveEntryFork(
|
|
|
|
|
require.resolve(bundle.entry),
|
2020-07-20 13:37:04 -04:00
|
|
|
isFBWWWBundle || isFBRNBundle
|
2020-02-25 13:54:27 -08:00
|
|
|
);
|
2017-10-25 02:55:00 +03:00
|
|
|
|
|
|
|
|
const shouldBundleDependencies =
|
2018-09-13 17:44:08 -07:00
|
|
|
bundleType === UMD_DEV ||
|
|
|
|
|
bundleType === UMD_PROD ||
|
|
|
|
|
bundleType === UMD_PROFILING;
|
2018-05-18 00:16:03 +08:00
|
|
|
const peerGlobals = Modules.getPeerGlobals(bundle.externals, bundleType);
|
2017-10-25 02:55:00 +03:00
|
|
|
let externals = Object.keys(peerGlobals);
|
|
|
|
|
if (!shouldBundleDependencies) {
|
|
|
|
|
const deps = Modules.getDependencies(bundleType, bundle.entry);
|
|
|
|
|
externals = externals.concat(deps);
|
|
|
|
|
}
|
2017-04-05 16:47:29 +01:00
|
|
|
|
2017-11-02 19:50:03 +00:00
|
|
|
const importSideEffects = Modules.getImportSideEffects();
|
|
|
|
|
const pureExternalModules = Object.keys(importSideEffects).filter(
|
|
|
|
|
module => !importSideEffects[module]
|
|
|
|
|
);
|
|
|
|
|
|
2017-12-06 20:11:32 +00:00
|
|
|
const rollupConfig = {
|
|
|
|
|
input: resolvedEntry,
|
2017-12-11 16:54:12 +00:00
|
|
|
treeshake: {
|
2023-02-20 13:04:26 +08:00
|
|
|
moduleSideEffects: (id, external) =>
|
|
|
|
|
!(external && pureExternalModules.includes(id)),
|
2017-12-11 16:54:12 +00:00
|
|
|
},
|
2017-12-06 20:11:32 +00:00
|
|
|
external(id) {
|
|
|
|
|
const containsThisModule = pkg => id === pkg || id.startsWith(pkg + '/');
|
|
|
|
|
const isProvidedByDependency = externals.some(containsThisModule);
|
|
|
|
|
if (!shouldBundleDependencies && isProvidedByDependency) {
|
2021-03-24 02:43:55 +00:00
|
|
|
if (id.indexOf('/src/') !== -1) {
|
|
|
|
|
throw Error(
|
|
|
|
|
'You are trying to import ' +
|
|
|
|
|
id +
|
|
|
|
|
' but ' +
|
|
|
|
|
externals.find(containsThisModule) +
|
|
|
|
|
' is one of npm dependencies, ' +
|
|
|
|
|
'so it will not contain that source file. You probably want ' +
|
|
|
|
|
'to create a new bundle entry point for it instead.'
|
|
|
|
|
);
|
|
|
|
|
}
|
2017-12-06 20:11:32 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return !!peerGlobals[id];
|
|
|
|
|
},
|
|
|
|
|
onwarn: handleRollupWarning,
|
|
|
|
|
plugins: getPlugins(
|
|
|
|
|
bundle.entry,
|
|
|
|
|
externals,
|
|
|
|
|
bundle.babel,
|
|
|
|
|
filename,
|
2017-12-23 14:22:59 -05:00
|
|
|
packageName,
|
2017-12-06 20:11:32 +00:00
|
|
|
bundleType,
|
|
|
|
|
bundle.global,
|
|
|
|
|
bundle.moduleType,
|
2020-03-12 11:38:32 -07:00
|
|
|
pureExternalModules,
|
|
|
|
|
bundle
|
2017-12-06 20:11:32 +00:00
|
|
|
),
|
2020-02-20 23:09:30 +01:00
|
|
|
output: {
|
|
|
|
|
externalLiveBindings: false,
|
|
|
|
|
freeze: false,
|
|
|
|
|
interop: false,
|
|
|
|
|
esModule: false,
|
|
|
|
|
},
|
2017-12-06 20:11:32 +00:00
|
|
|
};
|
2020-04-02 17:52:32 -07:00
|
|
|
const mainOutputPath = Packaging.getBundleOutputPath(
|
2022-10-14 23:29:17 -04:00
|
|
|
bundle,
|
2017-12-06 20:11:32 +00:00
|
|
|
bundleType,
|
|
|
|
|
filename,
|
|
|
|
|
packageName
|
|
|
|
|
);
|
2022-11-17 13:15:56 -08:00
|
|
|
|
2017-12-06 20:11:32 +00:00
|
|
|
const rollupOutputOptions = getRollupOutputOptions(
|
|
|
|
|
mainOutputPath,
|
|
|
|
|
format,
|
|
|
|
|
peerGlobals,
|
2018-05-22 08:16:59 -07:00
|
|
|
bundle.global,
|
|
|
|
|
bundleType
|
2017-12-06 20:11:32 +00:00
|
|
|
);
|
|
|
|
|
|
2019-04-25 07:55:44 -04:00
|
|
|
if (isWatchMode) {
|
|
|
|
|
rollupConfig.output = [rollupOutputOptions];
|
|
|
|
|
const watcher = rollup.watch(rollupConfig);
|
|
|
|
|
watcher.on('event', async event => {
|
|
|
|
|
switch (event.code) {
|
|
|
|
|
case 'BUNDLE_START':
|
|
|
|
|
console.log(`${chalk.bgYellow.black(' BUILDING ')} ${logKey}`);
|
|
|
|
|
break;
|
|
|
|
|
case 'BUNDLE_END':
|
|
|
|
|
console.log(`${chalk.bgGreen.black(' COMPLETE ')} ${logKey}\n`);
|
|
|
|
|
break;
|
|
|
|
|
case 'ERROR':
|
|
|
|
|
case 'FATAL':
|
|
|
|
|
console.log(`${chalk.bgRed.black(' OH NOES! ')} ${logKey}\n`);
|
|
|
|
|
handleRollupError(event.error);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
console.log(`${chalk.bgYellow.black(' BUILDING ')} ${logKey}`);
|
|
|
|
|
try {
|
|
|
|
|
const result = await rollup.rollup(rollupConfig);
|
|
|
|
|
await result.write(rollupOutputOptions);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(`${chalk.bgRed.black(' OH NOES! ')} ${logKey}\n`);
|
|
|
|
|
handleRollupError(error);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
console.log(`${chalk.bgGreen.black(' COMPLETE ')} ${logKey}\n`);
|
2017-12-06 20:11:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleRollupWarning(warning) {
|
|
|
|
|
if (warning.code === 'UNUSED_EXTERNAL_IMPORT') {
|
|
|
|
|
const match = warning.message.match(/external module '([^']+)'/);
|
|
|
|
|
if (!match || typeof match[1] !== 'string') {
|
|
|
|
|
throw new Error(
|
|
|
|
|
'Could not parse a Rollup warning. ' + 'Fix this method.'
|
2017-11-27 17:57:28 +00:00
|
|
|
);
|
|
|
|
|
}
|
2017-12-06 20:11:32 +00:00
|
|
|
const importSideEffects = Modules.getImportSideEffects();
|
|
|
|
|
const externalModule = match[1];
|
|
|
|
|
if (typeof importSideEffects[externalModule] !== 'boolean') {
|
|
|
|
|
throw new Error(
|
|
|
|
|
'An external module "' +
|
|
|
|
|
externalModule +
|
|
|
|
|
'" is used in a DEV-only code path ' +
|
|
|
|
|
'but we do not know if it is safe to omit an unused require() to it in production. ' +
|
|
|
|
|
'Please add it to the `importSideEffects` list in `scripts/rollup/modules.js`.'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
// Don't warn. We will remove side effectless require() in a later pass.
|
|
|
|
|
return;
|
2017-11-27 17:57:28 +00:00
|
|
|
}
|
2018-05-21 15:38:46 +01:00
|
|
|
|
2020-02-20 23:09:30 +01:00
|
|
|
if (warning.code === 'CIRCULAR_DEPENDENCY') {
|
|
|
|
|
// Ignored
|
|
|
|
|
} else if (typeof warning.code === 'string') {
|
2018-05-21 15:38:46 +01:00
|
|
|
// This is a warning coming from Rollup itself.
|
|
|
|
|
// These tend to be important (e.g. clashes in namespaced exports)
|
|
|
|
|
// so we'll fail the build on any of them.
|
|
|
|
|
console.error();
|
|
|
|
|
console.error(warning.message || warning);
|
|
|
|
|
console.error();
|
|
|
|
|
process.exit(1);
|
|
|
|
|
} else {
|
|
|
|
|
// The warning is from one of the plugins.
|
|
|
|
|
// Maybe it's not important, so just print it.
|
|
|
|
|
console.warn(warning.message || warning);
|
|
|
|
|
}
|
2017-04-05 16:47:29 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-06 20:11:32 +00:00
|
|
|
function handleRollupError(error) {
|
|
|
|
|
loggedErrors.add(error);
|
|
|
|
|
if (!error.code) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
console.error(
|
|
|
|
|
`\x1b[31m-- ${error.code}${error.plugin ? ` (${error.plugin})` : ''} --`
|
|
|
|
|
);
|
2018-05-14 17:49:41 +01:00
|
|
|
console.error(error.stack);
|
|
|
|
|
if (error.loc && error.loc.file) {
|
|
|
|
|
const {file, line, column} = error.loc;
|
2017-12-06 20:11:32 +00:00
|
|
|
// This looks like an error from Rollup, e.g. missing export.
|
|
|
|
|
// We'll use the accurate line numbers provided by Rollup but
|
|
|
|
|
// use Babel code frame because it looks nicer.
|
|
|
|
|
const rawLines = fs.readFileSync(file, 'utf-8');
|
|
|
|
|
// column + 1 is required due to rollup counting column start position from 0
|
|
|
|
|
// whereas babel-code-frame counts from 1
|
|
|
|
|
const frame = codeFrame(rawLines, line, column + 1, {
|
|
|
|
|
highlightCode: true,
|
|
|
|
|
});
|
|
|
|
|
console.error(frame);
|
2018-05-14 17:49:41 +01:00
|
|
|
} else if (error.codeFrame) {
|
2017-12-06 20:11:32 +00:00
|
|
|
// This looks like an error from a plugin (e.g. Babel).
|
|
|
|
|
// In this case we'll resort to displaying the provided code frame
|
|
|
|
|
// because we can't be sure the reported location is accurate.
|
|
|
|
|
console.error(error.codeFrame);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-05 16:47:29 +01:00
|
|
|
|
2017-12-06 20:11:32 +00:00
|
|
|
async function buildEverything() {
|
2019-11-08 19:41:40 +00:00
|
|
|
if (!argv['unsafe-partial']) {
|
|
|
|
|
await asyncRimRaf('build');
|
|
|
|
|
}
|
2019-05-29 21:30:16 -07:00
|
|
|
|
2017-12-06 20:11:32 +00:00
|
|
|
// Run them serially for better console output
|
|
|
|
|
// and to avoid any potential race conditions.
|
2019-05-29 14:34:50 -07:00
|
|
|
|
|
|
|
|
let bundles = [];
|
2018-02-09 16:11:22 +00:00
|
|
|
// eslint-disable-next-line no-for-of-loops/no-for-of-loops
|
2017-12-06 20:11:32 +00:00
|
|
|
for (const bundle of Bundles.bundles) {
|
2019-05-29 14:34:50 -07:00
|
|
|
bundles.push(
|
2020-05-28 15:56:34 -07:00
|
|
|
[bundle, NODE_ES2015],
|
2020-11-13 08:57:45 -05:00
|
|
|
[bundle, NODE_ESM],
|
2019-05-29 14:34:50 -07:00
|
|
|
[bundle, UMD_DEV],
|
|
|
|
|
[bundle, UMD_PROD],
|
|
|
|
|
[bundle, UMD_PROFILING],
|
|
|
|
|
[bundle, NODE_DEV],
|
|
|
|
|
[bundle, NODE_PROD],
|
|
|
|
|
[bundle, NODE_PROFILING],
|
2022-11-17 13:15:56 -08:00
|
|
|
[bundle, BUN_DEV],
|
|
|
|
|
[bundle, BUN_PROD],
|
2020-02-13 20:33:53 +00:00
|
|
|
[bundle, FB_WWW_DEV],
|
|
|
|
|
[bundle, FB_WWW_PROD],
|
|
|
|
|
[bundle, FB_WWW_PROFILING],
|
2019-05-29 14:34:50 -07:00
|
|
|
[bundle, RN_OSS_DEV],
|
|
|
|
|
[bundle, RN_OSS_PROD],
|
2020-05-28 15:04:25 -07:00
|
|
|
[bundle, RN_OSS_PROFILING],
|
|
|
|
|
[bundle, RN_FB_DEV],
|
|
|
|
|
[bundle, RN_FB_PROD],
|
2022-10-14 23:29:17 -04:00
|
|
|
[bundle, RN_FB_PROFILING],
|
|
|
|
|
[bundle, BROWSER_SCRIPT]
|
2019-05-29 14:34:50 -07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-31 18:37:32 -04:00
|
|
|
if (process.env.CIRCLE_NODE_TOTAL) {
|
2019-05-29 14:34:50 -07:00
|
|
|
// In CI, parallelize bundles across multiple tasks.
|
|
|
|
|
const nodeTotal = parseInt(process.env.CIRCLE_NODE_TOTAL, 10);
|
|
|
|
|
const nodeIndex = parseInt(process.env.CIRCLE_NODE_INDEX, 10);
|
|
|
|
|
bundles = bundles.filter((_, i) => i % nodeTotal === nodeIndex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-for-of-loops/no-for-of-loops
|
|
|
|
|
for (const [bundle, bundleType] of bundles) {
|
|
|
|
|
await createBundle(bundle, bundleType);
|
2017-12-06 20:11:32 +00:00
|
|
|
}
|
2017-04-05 16:47:29 +01:00
|
|
|
|
2017-12-06 20:11:32 +00:00
|
|
|
await Packaging.copyAllShims();
|
|
|
|
|
await Packaging.prepareNpmPackages();
|
2017-04-05 16:47:29 +01:00
|
|
|
|
2017-12-06 20:11:32 +00:00
|
|
|
if (syncFBSourcePath) {
|
2018-04-18 13:16:50 -07:00
|
|
|
await Sync.syncReactNative(syncFBSourcePath);
|
2017-12-06 20:11:32 +00:00
|
|
|
} else if (syncWWWPath) {
|
|
|
|
|
await Sync.syncReactDom('build/facebook-www', syncWWWPath);
|
2017-04-05 16:47:29 +01:00
|
|
|
}
|
2017-12-06 20:11:32 +00:00
|
|
|
|
|
|
|
|
console.log(Stats.printResults());
|
2017-12-07 22:47:56 +00:00
|
|
|
if (!forcePrettyOutput) {
|
|
|
|
|
Stats.saveResults();
|
|
|
|
|
}
|
2017-12-06 20:11:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buildEverything();
|