|
1 | 1 | 'use strict';
|
2 | 2 |
|
| 3 | +function testMinificationUsedDCE() { |
| 4 | + if (process.env.NODE_ENV === 'development') { |
| 5 | + // We expect this method only to be called in production. |
| 6 | + throw new Error('This is unreachable'); |
| 7 | + } |
| 8 | + try { |
| 9 | + // use scoped variable for our initial test, in case |
| 10 | + // 'top-level' mangling is not enabled. |
| 11 | + const source = testMinificationUsedDCE.toString(); |
| 12 | + const longVariableName = true; |
| 13 | + if (longVariableName && source.match(/longVariableName/g).length === 3) { |
| 14 | + // We are not minified. |
| 15 | + // This might be a Node environment where DCE is not expected anyway. |
| 16 | + return; |
| 17 | + } |
| 18 | + if (source.match(/toString/g).length !== 2) { |
| 19 | + // We always look for two matches: |
| 20 | + // The actual occurence and then the call to 'match' |
| 21 | + // |
| 22 | + // We know for a fact the above line exists so there should be 2 |
| 23 | + // matches. |
| 24 | + // Therefore the browser gave us invalid source. |
| 25 | + return; |
| 26 | + } |
| 27 | + if (source.match(/unreachable/g).length === 2) { |
| 28 | + // We always look for two matches: |
| 29 | + // The actual occurence and then the call to 'match' |
| 30 | + |
| 31 | + // Dead code elimination would have stripped that branch |
| 32 | + // because it is impossible to reach in production. |
| 33 | + setTimeout(function() { |
| 34 | + // Ensure it gets reported to production logging |
| 35 | + throw new Error( |
| 36 | + 'React is running in production mode, but dead code ' + |
| 37 | + 'elimination has not been applied. Read how to correctly ' + |
| 38 | + 'configure React for production: ' + |
| 39 | + 'https://fburl.com/react-perf-use-the-production-build' |
| 40 | + ); |
| 41 | + }); |
| 42 | + } |
| 43 | + } catch (e) {} |
| 44 | +} |
| 45 | + |
3 | 46 | if (process.env.NODE_ENV === 'production') {
|
| 47 | + testMinificationUsedDCE(); |
4 | 48 | module.exports = require('./cjs/react.production.min.js');
|
5 | 49 | } else {
|
6 | 50 | module.exports = require('./cjs/react.development.js');
|
|
0 commit comments