-
Notifications
You must be signed in to change notification settings - Fork 51k
Fix UMD bundles, making safe to use as required modules #7840
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be define, not require.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think what I have is right.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looked in the docs and you're right. 😅 |
||
|
|
||
| // <script> | ||
| } else { | ||
| var g; | ||
| if (typeof window !== "undefined") { | ||
| g = window; | ||
| } else if (typeof global !== "undefined") { | ||
| g = global; | ||
| } else if (typeof self !== "undefined") { | ||
| g = self; | ||
| } else { | ||
| // works providing we're not in "use strict"; | ||
| // needed for Java 8 Nashorn | ||
| // see https://github.com/facebook/react/issues/3037 | ||
| g = this; | ||
| } | ||
| f(g.React) | ||
| } | ||
| })(function(React) { | ||
| ${src} | ||
| }); | ||
| `; | ||
| } | ||
|
|
||
| // Our basic config which we'll add to to make our other builds | ||
| var basic = { | ||
| entries: [ | ||
|
|
@@ -137,7 +183,7 @@ var dom = { | |
| transforms: [shimSharedModules], | ||
| globalTransforms: [envifyDev], | ||
| plugins: [collapser], | ||
| after: [derequire, simpleBannerify], | ||
| after: [derequire, wrapperify, simpleBannerify], | ||
| }; | ||
|
|
||
| var domMin = { | ||
|
|
@@ -155,7 +201,7 @@ var domMin = { | |
| // No need to derequire because the minifier will mangle | ||
| // the "require" calls. | ||
|
|
||
| after: [minify, bannerify], | ||
| after: [wrapperify, minify, bannerify], | ||
| }; | ||
|
|
||
| var domServer = { | ||
|
|
@@ -169,7 +215,7 @@ var domServer = { | |
| transforms: [shimSharedModules], | ||
| globalTransforms: [envifyDev], | ||
| plugins: [collapser], | ||
| after: [derequire, simpleBannerify], | ||
| after: [derequire, wrapperify, simpleBannerify], | ||
| }; | ||
|
|
||
| var domServerMin = { | ||
|
|
@@ -187,7 +233,7 @@ var domServerMin = { | |
| // No need to derequire because the minifier will mangle | ||
| // the "require" calls. | ||
|
|
||
| after: [minify, bannerify], | ||
| after: [wrapperify, minify, bannerify], | ||
| }; | ||
|
|
||
| var domFiber = { | ||
|
|
@@ -201,7 +247,7 @@ var domFiber = { | |
| transforms: [shimSharedModules], | ||
| globalTransforms: [envifyDev], | ||
| plugins: [collapser], | ||
| after: [derequire, simpleBannerify], | ||
| after: [derequire, wrapperify, simpleBannerify], | ||
| }; | ||
|
|
||
| var domFiberMin = { | ||
|
|
@@ -219,7 +265,7 @@ var domFiberMin = { | |
| // No need to derequire because the minifier will mangle | ||
| // the "require" calls. | ||
|
|
||
| after: [minify, bannerify], | ||
| after: [wrapperify, minify, bannerify], | ||
| }; | ||
|
|
||
| module.exports = { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It became really what?! 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Too late! I pressed merge.
It became really "hard". Or maybe I wrote "annoying"? I'm going to leave the mystery in there for now :)