Wrap contents of if-DEV condition in an IIFE (#10361)
This avoids strict mode conflicts for certain browsers wrt functions being defined within an if-block. Also re-added the if-DEV condition for the ReactNative renderer since it was removed for this reason.
Brian Vaughn committed
Aug 2, 2017 at 14:24 UTC
630afb31868fe326563f396d78555482736200e9
1 file changed
+9
-6
scripts/rollup/build.js
+9
-6
@@ -82,22 +82,24 @@ function getBanner(bundleType, hasteName, filename) {
82
return Header.getUMDHeader(filename, reactVersion);
83
// CommonJS DEV bundle is guarded to help weak dead code elimination.
84
case NODE_DEV:
85
- return `'use strict';\n\n\nif (process.env.NODE_ENV !== "production") {\n`;
85
+ // Wrap the contents of the if-DEV check with an IIFE.
86
+ // Block-level function definitions can cause problems for strict mode.
87
+ return `'use strict';\n\n\nif (process.env.NODE_ENV !== "production") {\n(function() {\n`;
88
case NODE_PROD:
89
return '';
90
// All FB and RN bundles need Haste headers.
89
- // FB DEV bundles are also guarded;
90
- // RN bundles are not b'c of an older JSC packaged for Android.
91
- // See github.com/facebook/react-native/issues/14995
91
+ // DEV bundle is guarded to help weak dead code elimination.
92
case FB_DEV:
93
case FB_PROD:
94
case RN_DEV:
95
case RN_PROD:
96
const isDev = bundleType === FB_DEV || bundleType === RN_DEV;
97
const hasteFinalName = hasteName + (isDev ? '-dev' : '-prod');
98
+ // Wrap the contents of the if-DEV check with an IIFE.
99
+ // Block-level function definitions can cause problems for strict mode.
100
return (
101
Header.getProvidesHeader(hasteFinalName) +
100
- (bundleType === FB_DEV ? `\n\n'use strict';\n\n\nif (__DEV__) {\n` : '')
102
+ (isDev ? `\n\n'use strict';\n\n\nif (__DEV__) {\n(function() {\n` : '')
103
);
104
default:
105
throw new Error('Unknown type.');
@@ -110,7 +112,8 @@ function getFooter(bundleType) {
112
// Non-UMD DEV bundles need conditions to help weak dead code elimination.
113
case NODE_DEV:
114
case FB_DEV:
113
- return '\n}\n';
115
+ case RN_DEV:
116
+ return '\n})();\n}\n';
117
default:
118
return '';
119
}