-
Notifications
You must be signed in to change notification settings - Fork 46.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFC] Codemod invariant -> throw new Error #22435
Commits on Sep 30, 2021
-
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.
Configuration menu - View commit details
-
Copy full SHA for e365ca3 - Browse repository at this point
Copy the full SHA e365ca3View commit details -
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.
Configuration menu - View commit details
-
Copy full SHA for b8555b3 - Browse repository at this point
Copy the full SHA b8555b3View commit details -
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.
Configuration menu - View commit details
-
Copy full SHA for ec82a89 - Browse repository at this point
Copy the full SHA ec82a89View commit details -
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.
Configuration menu - View commit details
-
Copy full SHA for 5c18239 - Browse repository at this point
Copy the full SHA 5c18239View commit details -
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.
Configuration menu - View commit details
-
Copy full SHA for b472c83 - Browse repository at this point
Copy the full SHA b472c83View commit details -
[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.
Configuration menu - View commit details
-
Copy full SHA for 9657c1d - Browse repository at this point
Copy the full SHA 9657c1dView commit details -
Remove temporary codemod script
This reverts the codemod script and ESLint config I added temporarily in order to perform the invariant codemod.
Configuration menu - View commit details
-
Copy full SHA for 3f3eabd - Browse repository at this point
Copy the full SHA 3f3eabdView commit details -
Configuration menu - View commit details
-
Copy full SHA for a6a6466 - Browse repository at this point
Copy the full SHA a6a6466View commit details -
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.
Configuration menu - View commit details
-
Copy full SHA for 3ed2d03 - Browse repository at this point
Copy the full SHA 3ed2d03View commit details