Skip to content
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

32 changes: 32 additions & 0 deletions packages/react/index.js
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) {

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 with const source = String(testMinification); and this check would still work.

// We know for a fact the above line exists.
// Therefore the browser gave us invalid source.
return;
}
if (source.indexOf('unreachable') !== -1) {

Choose a reason for hiding this comment

The 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.

'UNREACHABLE'.toLowerCase() appears to work for now, but probably not in the future, as Babili currently evaluates some other String.prototype methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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');
Expand Down