-
Notifications
You must be signed in to change notification settings - Fork 47.3k
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
Throw error to warn of mistaken loading of prod + dev React bundles #10446
Changes from 2 commits
8e280c8
441d661
05be49a
8fab885
9a73704
5204310
26ce0ac
498f8f5
ab6c1f0
8a1c36b
0eecddc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,38 @@ | ||
'use strict'; | ||
|
||
function testMinification() { | ||
if (testMinification.name === 'testMinification') { | ||
// We are not minified. | ||
// This might be a Node environment where DCE is not expected anyway. | ||
return; | ||
} | ||
if (process.env.NODE_ENV === 'development') { | ||
// We expect this method only to be called in production. | ||
throw new Error('This is unreachable'); | ||
} | ||
try { | ||
const source = testMinification.toString(); | ||
if (source.indexOf('toString') === -1) { | ||
// We know for a fact the above line exists. | ||
// Therefore the browser gave us invalid source. | ||
return; | ||
} | ||
if (source.indexOf('unreachable') !== -1) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is always true because it will find its own search string, i.e. (function foo() { return foo.toString().indexOf('any arbitrary string') !== -1; })(); so this error will be thrown in correctly minified code. Extracting it to a variable might work: var searchString = 'any arbitrary string';
(function foo() { return foo.toString().indexOf(searchString) !== -1; })(); but Babili will inline it, recreating the original problem.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ! Good catch! I'll update it. Maybe we can have it look for two occurrences of the search string. |
||
// Dead code elimination would have stripped that branch | ||
// because it is impossible to reach in production. | ||
setTimeout(function() { | ||
// Ensure it gets reported to production logging | ||
throw new Error('React is running in production mode, but dead code ' | ||
+ 'elimination has not been applied. Read how to correctly ' | ||
+ 'configure React for production: ' | ||
+ 'https://fburl.com/react-perf-use-the-production-build'); | ||
}); | ||
} | ||
} catch (e) {} | ||
} | ||
|
||
if (process.env.NODE_ENV === 'production') { | ||
testMinification(); | ||
module.exports = require('./cjs/react.production.min.js'); | ||
} else { | ||
module.exports = require('./cjs/react.development.js'); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar thing here, this works, but is kinda misleading.
const source = testMinification.toString();
could be replaced withconst source = String(testMinification);
and this check would still work.