Skip to content

Commit

Permalink
Just default to React's url for errors by default
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredpalmer committed Aug 22, 2019
1 parent 73b152e commit d346cc3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 20 deletions.
4 changes: 3 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export const paths = {
testsSetup: resolveApp('test/setupTests.ts'),
appRoot: resolveApp('.'),
appSrc: resolveApp('src'),
appErrorsJson: resolveApp('./codes.json'),
appErrorsJson: resolveApp('errors/codes.json'),
appErrors: resolveApp('errors'),
appDist: resolveApp('dist'),
appConfig: resolveApp('tsdx.config.js'),
};
26 changes: 8 additions & 18 deletions src/errors/extractErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ export function extractErrors(opts: any) {
throw new Error('Missing options. Ensure you pass --name flag to tsdx');
}

if (typeof opts.extractErrors === 'boolean') {
throw new Error(
'No url passed to extractErrors flag.' +
'Ensure you pass a url, eg. `--extractErrors=https://reactjs.org/docs/error-decoder.html?invariant=`.'
);
}

const errorMapFilePath = opts.errorMapFilePath;
let existingErrorMap: any;
try {
Expand Down Expand Up @@ -98,19 +91,19 @@ export function extractErrors(opts: any) {

function flush(cb?: any) {
const prettyName = pascalCase(safeVariableName(opts.name));
// Output messages to ./codes.json
// Ensure that the ./src/errors directory exists or create it
fs.ensureDirSync(paths.appErrors);

// Output messages to ./errors/codes.json
fs.writeFileSync(
errorMapFilePath,
JSON.stringify(invertObject(existingErrorMap), null, 2) + '\n',
'utf-8'
);

// Ensure that the ./src/errors directory exists or create it
fs.ensureDirSync(paths.appRoot + '/errors');

// Write the error files, unless they already exist
fs.writeFileSync(
paths.appRoot + '/errors/ErrorDev.js',
paths.appErrors + '/ErrorDev.js',
`
function ErrorDev(message) {
const error = new Error(message);
Expand All @@ -124,13 +117,10 @@ export default ErrorDev;
);

fs.writeFileSync(
paths.appRoot + '/errors/ErrorProd.js',
`// Do not require this module directly! Use a normal error constructor with
// template literal strings. The messages will be converted to ErrorProd during
// build, and in production they will be minified.
paths.appErrors + '/ErrorProd.js',
`
function ErrorProd(code) {
let url = '${opts.extractErrors}' + code;
let url = 'https://reactjs.org/docs/error-decoder.html?invariant=' + code;
for (let i = 1; i < arguments.length; i++) {
url += '&args[]=' + encodeURIComponent(arguments[i]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/errors/transformErrorMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default function transformErrorMessages(babel: any) {
//
// 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.appRoot + "/codes.json"`.
// stored in `paths.appErrorsJson`.
const condition = node.arguments[0];
const errorMsgLiteral = evalToString(node.arguments[1]);
const errorMsgExpressions = Array.from(node.arguments.slice(2));
Expand Down

0 comments on commit d346cc3

Please sign in to comment.