From 92c84a6f37245f4fbe3ce5dc50845856201637fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20O=E2=80=99Shannessy?= Date: Mon, 3 Oct 2016 15:49:10 -0700 Subject: [PATCH] Fix UMD bundles, making safe to use as required modules (#7840) --- grunt/config/browserify.js | 58 ++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/grunt/config/browserify.js b/grunt/config/browserify.js index 0ef4c5f8ac22f..282c003d61b6b 100644 --- a/grunt/config/browserify.js +++ b/grunt/config/browserify.js @@ -62,6 +62,52 @@ function simpleBannerify(src) { ); } +// What is happening here??? +// I'm glad you asked. It became really to make our bundle splitting work. +// Everything is fine in node and when bundling with those packages, but when +// using our pre-packaged files, the splitting didn't work. Specifically due to +// the UMD wrappers defining their own require and creating their own encapsulated +// "registry" scope, we couldn't require across the boundaries. Webpack tries to +// be smart and looks for top-level requires (even when aliasing to a bundle), +// but since we didn't have those, we couldn't require 'react' from 'react-dom'. +// But we are already shimming in some modules that look for a global React +// variable. So we write a wrapper around the UMD bundle that browserify creates, +// and define a React variable that will require across Webpack-boundaries or fall +// back to the global, just like it would previously. +function wrapperify(src) { + return ` +;(function(f) { + // CommonJS + if (typeof exports === "object" && typeof module !== "undefined") { + f(require('react')); + + // RequireJS + } else if (typeof define === "function" && define.amd) { + require(['react'], f); + + //