Skip to content

Commit

Permalink
Wrap contents of if-DEV condition in an IIFE (#10361)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bvaughn authored Aug 2, 2017
1 parent f3e502c commit 630afb3
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions scripts/rollup/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,24 @@ function getBanner(bundleType, hasteName, filename) {
return Header.getUMDHeader(filename, reactVersion);
// CommonJS DEV bundle is guarded to help weak dead code elimination.
case NODE_DEV:
return `'use strict';\n\n\nif (process.env.NODE_ENV !== "production") {\n`;
// Wrap the contents of the if-DEV check with an IIFE.
// Block-level function definitions can cause problems for strict mode.
return `'use strict';\n\n\nif (process.env.NODE_ENV !== "production") {\n(function() {\n`;
case NODE_PROD:
return '';
// All FB and RN bundles need Haste headers.
// FB DEV bundles are also guarded;
// RN bundles are not b'c of an older JSC packaged for Android.
// See github.com/facebook/react-native/issues/14995
// DEV bundle is guarded to help weak dead code elimination.
case FB_DEV:
case FB_PROD:
case RN_DEV:
case RN_PROD:
const isDev = bundleType === FB_DEV || bundleType === RN_DEV;
const hasteFinalName = hasteName + (isDev ? '-dev' : '-prod');
// Wrap the contents of the if-DEV check with an IIFE.
// Block-level function definitions can cause problems for strict mode.
return (
Header.getProvidesHeader(hasteFinalName) +
(bundleType === FB_DEV ? `\n\n'use strict';\n\n\nif (__DEV__) {\n` : '')
(isDev ? `\n\n'use strict';\n\n\nif (__DEV__) {\n(function() {\n` : '')
);
default:
throw new Error('Unknown type.');
Expand All @@ -110,7 +112,8 @@ function getFooter(bundleType) {
// Non-UMD DEV bundles need conditions to help weak dead code elimination.
case NODE_DEV:
case FB_DEV:
return '\n}\n';
case RN_DEV:
return '\n})();\n}\n';
default:
return '';
}
Expand Down

0 comments on commit 630afb3

Please sign in to comment.