diff --git a/src/constants.ts b/src/constants.ts index f47db0a60..a83adb24d 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -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'), }; diff --git a/src/errors/extractErrors.ts b/src/errors/extractErrors.ts index 3a3e06f00..751bb40c6 100644 --- a/src/errors/extractErrors.ts +++ b/src/errors/extractErrors.ts @@ -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 { @@ -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); @@ -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]); } diff --git a/src/errors/transformErrorMessages.ts b/src/errors/transformErrorMessages.ts index 193185823..08d144234 100644 --- a/src/errors/transformErrorMessages.ts +++ b/src/errors/transformErrorMessages.ts @@ -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));