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

30 changes: 30 additions & 0 deletions packages/react/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
'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 === 'production') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This outer wrapper is probably unnecessary since we are already calling it from the same condition.

if (process.env.NODE_ENV === 'development') {
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) {
// 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.');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably include "Read how to correctly configure React for production: " and an fame link to "Use the Production Build" in "Optimizing Performance" doc.

});
}
} 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