File size: 5,442 Bytes
1e92f2d |
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 |
// largely borrowed from https://github.com/facebook/react/blob/2c8832075b05009bd261df02171bf9888ac76350/scripts/error-codes/transform-error-messages.js
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import fs from 'fs';
import { invertObject } from './invertObject';
import { evalToString } from './evalToString';
import { addDefault } from '@babel/helper-module-imports';
import { paths } from '../constants';
export default function transformErrorMessages(babel: any) {
const t = babel.types;
const DEV_EXPRESSION = t.identifier('__DEV__');
return {
visitor: {
CallExpression(path: any, file: any) {
const node = path.node;
const noMinify = file.opts.noMinify;
if (path.get('callee').isIdentifier({ name: 'invariant' })) {
// Turns this code:
//
// invariant(condition, 'A %s message that contains %s', adj, noun);
//
// into this:
//
// if (!condition) {
// if (__DEV__) {
// throw ReactError(`A ${adj} message that contains ${noun}`);
// } else {
// throw ReactErrorProd(ERR_CODE, adj, noun);
// }
// }
//
// where ERR_CODE is an error code: a unique identifier (a number
// string) that references a verbose error message. The mapping is
// stored in `paths.appErrorsJson`.
const condition = node.arguments[0];
const errorMsgLiteral = evalToString(node.arguments[1]);
const errorMsgExpressions = Array.from(node.arguments.slice(2));
const errorMsgQuasis = errorMsgLiteral
.split('%s')
.map((raw: any) =>
t.templateElement({ raw, cooked: String.raw({ raw } as any) })
);
// Import ReactError
const reactErrorIdentfier = addDefault(
path,
paths.appRoot + '/errors/ErrorDev.js',
{
nameHint: 'InvariantError',
}
);
// Outputs:
// throw ReactError(`A ${adj} message that contains ${noun}`);
const devThrow = t.throwStatement(
t.callExpression(reactErrorIdentfier, [
t.templateLiteral(errorMsgQuasis, errorMsgExpressions),
])
);
if (noMinify) {
// Error minification is disabled for this build.
//
// Outputs:
// if (!condition) {
// throw ReactError(`A ${adj} message that contains ${noun}`);
// }
path.replaceWith(
t.ifStatement(
t.unaryExpression('!', condition),
t.blockStatement([devThrow])
)
);
return;
}
// Avoid caching because we write it as we go.
const existingErrorMap = JSON.parse(
fs.readFileSync(paths.appErrorsJson, 'utf-8')
);
const errorMap = invertObject(existingErrorMap);
let prodErrorId = errorMap[errorMsgLiteral];
if (prodErrorId === undefined) {
// There is no error code for this message. Add an inline comment
// that flags this as an unminified error. This allows the build
// to proceed, while also allowing a post-build linter to detect it.
//
// Outputs:
// /* FIXME (minify-errors-in-prod): Unminified error message in production build! */
// if (!condition) {
// throw ReactError(`A ${adj} message that contains ${noun}`);
// }
path.replaceWith(
t.ifStatement(
t.unaryExpression('!', condition),
t.blockStatement([devThrow])
)
);
path.addComment(
'leading',
'FIXME (minify-errors-in-prod): Unminified error message in production build!'
);
return;
}
prodErrorId = parseInt(prodErrorId, 10);
// Import ReactErrorProd
const reactErrorProdIdentfier = addDefault(
path,
paths.appRoot + '/errors/ErrorProd.js',
{
nameHint: 'InvariantErrorProd',
}
);
// Outputs:
// throw ReactErrorProd(ERR_CODE, adj, noun);
const prodThrow = t.throwStatement(
t.callExpression(reactErrorProdIdentfier, [
t.numericLiteral(prodErrorId),
...errorMsgExpressions,
])
);
// Outputs:
// if (!condition) {
// if (__DEV__) {
// throw ReactError(`A ${adj} message that contains ${noun}`);
// } else {
// throw ReactErrorProd(ERR_CODE, adj, noun);
// }
// }
path.replaceWith(
t.ifStatement(
t.unaryExpression('!', condition),
t.blockStatement([
t.ifStatement(
DEV_EXPRESSION,
t.blockStatement([devThrow]),
t.blockStatement([prodThrow])
),
])
)
);
}
},
},
};
}
|