mirror of
https://github.com/facebook/react.git
synced 2026-02-24 20:53:03 +00:00
* Output FIXME during build for unminified errors The invariant Babel transform used to output a FIXME comment if it could not find a matching error code. This could happen if there were a configuration mistake that caused an unminified message to slip through. Linting the compiled bundles is the most reliable way to do it because there's not a one-to-one mapping between source modules and bundles. For example, the same source module may appear in multiple bundles, some which are minified and others which aren't. This updates the transform to output the same messages for Error calls. The source lint rule is still useful for catching mistakes during development, to prompt you to update the error codes map before pushing the PR to CI. * Don't run error transform in development We used to run the error transform in both production and development, because in development it was used to convert `invariant` calls into throw statements. Now that don't use `invariant` anymore, we only have to run the transform for production builds. * Add ! to FIXME comment so Closure doesn't strip it Don't love this solution because Closure could change this heuristic, or we could switch to a differnt compiler that doesn't support it. But it works. Could add a bundle that contains an unminified error solely for the purpose of testing it, but that seems like overkill. * Alternate extract-errors that scrapes artifacts The build script outputs a special FIXME comment when it fails to minify an error message. CI will detect these comments and fail the workflow. The comments also include the expected error message. So I added an alternate extract-errors that scrapes unminified messages from the build artifacts and updates `codes.json`. This is nice because it works on partial builds. And you can also run it after the fact, instead of needing build all over again. * Disable error minification in more bundles Not worth it because the number of errors does not outweight the size of the formatProdErrorMessage runtime. * Run extract-errors script in CI The lint_build job already checks for unminified errors, but the output isn't super helpful. Instead I've added a new job that runs the extract-errors script and fails the build if `codes.json` changes. It also outputs the expected diff so you can easily see which messages were missing from the map. * Replace old extract-errors script with new one Deletes the old extract-errors in favor of extract-errors2
113 lines
2.8 KiB
JavaScript
113 lines
2.8 KiB
JavaScript
/**
|
|
* 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.
|
|
*/
|
|
/* eslint-disable quotes */
|
|
'use strict';
|
|
|
|
let babel = require('@babel/core');
|
|
let devExpressionWithCodes = require('../transform-error-messages');
|
|
|
|
function transform(input, options = {}) {
|
|
return babel.transform(input, {
|
|
plugins: [[devExpressionWithCodes, options]],
|
|
}).code;
|
|
}
|
|
|
|
let oldEnv;
|
|
|
|
describe('error transform', () => {
|
|
beforeEach(() => {
|
|
oldEnv = process.env.NODE_ENV;
|
|
process.env.NODE_ENV = '';
|
|
});
|
|
|
|
afterEach(() => {
|
|
process.env.NODE_ENV = oldEnv;
|
|
});
|
|
|
|
it('should replace error constructors', () => {
|
|
expect(
|
|
transform(`
|
|
new Error('Do not override existing functions.');
|
|
`)
|
|
).toMatchSnapshot();
|
|
});
|
|
|
|
it('should replace error constructors (no new)', () => {
|
|
expect(
|
|
transform(`
|
|
Error('Do not override existing functions.');
|
|
`)
|
|
).toMatchSnapshot();
|
|
});
|
|
|
|
it("should output FIXME for errors that don't have a matching error code", () => {
|
|
expect(
|
|
transform(`
|
|
Error('This is not a real error message.');
|
|
`)
|
|
).toMatchSnapshot();
|
|
});
|
|
|
|
it(
|
|
"should output FIXME for errors that don't have a matching error " +
|
|
'code, unless opted out with a comment',
|
|
() => {
|
|
// TODO: Since this only detects one of many ways to disable a lint
|
|
// rule, we should instead search for a custom directive (like
|
|
// no-minify-errors) instead of ESLint. Will need to update our lint
|
|
// rule to recognize the same directive.
|
|
expect(
|
|
transform(`
|
|
// eslint-disable-next-line react-internal/prod-error-codes
|
|
Error('This is not a real error message.');
|
|
`)
|
|
).toMatchSnapshot();
|
|
}
|
|
);
|
|
|
|
it('should not touch other calls or new expressions', () => {
|
|
expect(
|
|
transform(`
|
|
new NotAnError();
|
|
NotAnError();
|
|
`)
|
|
).toMatchSnapshot();
|
|
});
|
|
|
|
it('should support interpolating arguments with template strings', () => {
|
|
expect(
|
|
transform(`
|
|
new Error(\`Expected \${foo} target to be an array; got \${bar}\`);
|
|
`)
|
|
).toMatchSnapshot();
|
|
});
|
|
|
|
it('should support interpolating arguments with concatenation', () => {
|
|
expect(
|
|
transform(`
|
|
new Error('Expected ' + foo + ' target to be an array; got ' + bar);
|
|
`)
|
|
).toMatchSnapshot();
|
|
});
|
|
|
|
it('should support error constructors with concatenated messages', () => {
|
|
expect(
|
|
transform(`
|
|
new Error(\`Expected \${foo} target to \` + \`be an array; got \${bar}\`);
|
|
`)
|
|
).toMatchSnapshot();
|
|
});
|
|
|
|
it('handles escaped backticks in template string', () => {
|
|
expect(
|
|
transform(`
|
|
new Error(\`Expected \\\`\$\{listener\}\\\` listener to be a function, instead got a value of \\\`\$\{type\}\\\` type.\`);
|
|
`)
|
|
).toMatchSnapshot();
|
|
});
|
|
});
|