| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | 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' })) { |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | 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) }) |
| | ); |
| |
|
| | |
| | const reactErrorIdentfier = addDefault( |
| | path, |
| | paths.appRoot + '/errors/ErrorDev.js', |
| | { |
| | nameHint: 'InvariantError', |
| | } |
| | ); |
| |
|
| | |
| | |
| | const devThrow = t.throwStatement( |
| | t.callExpression(reactErrorIdentfier, [ |
| | t.templateLiteral(errorMsgQuasis, errorMsgExpressions), |
| | ]) |
| | ); |
| |
|
| | if (noMinify) { |
| | |
| | |
| | |
| | |
| | |
| | |
| | path.replaceWith( |
| | t.ifStatement( |
| | t.unaryExpression('!', condition), |
| | t.blockStatement([devThrow]) |
| | ) |
| | ); |
| | return; |
| | } |
| |
|
| | |
| | const existingErrorMap = JSON.parse( |
| | fs.readFileSync(paths.appErrorsJson, 'utf-8') |
| | ); |
| | const errorMap = invertObject(existingErrorMap); |
| |
|
| | let prodErrorId = errorMap[errorMsgLiteral]; |
| |
|
| | if (prodErrorId === undefined) { |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | 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); |
| |
|
| | |
| | const reactErrorProdIdentfier = addDefault( |
| | path, |
| | paths.appRoot + '/errors/ErrorProd.js', |
| | { |
| | nameHint: 'InvariantErrorProd', |
| | } |
| | ); |
| |
|
| | |
| | |
| | const prodThrow = t.throwStatement( |
| | t.callExpression(reactErrorProdIdentfier, [ |
| | t.numericLiteral(prodErrorId), |
| | ...errorMsgExpressions, |
| | ]) |
| | ); |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | path.replaceWith( |
| | t.ifStatement( |
| | t.unaryExpression('!', condition), |
| | t.blockStatement([ |
| | t.ifStatement( |
| | DEV_EXPRESSION, |
| | t.blockStatement([devThrow]), |
| | t.blockStatement([prodThrow]) |
| | ), |
| | ]) |
| | ) |
| | ); |
| | } |
| | }, |
| | }, |
| | }; |
| | } |
| |
|