-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
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
Bug: webpack's UMD global detection is incompatible with strict mode JavaScript #5843
Comments
Yep, send a PR. |
cdata
pushed a commit
to cdata/webpack
that referenced
this issue
Oct 18, 2017
Merged
sokra
added a commit
that referenced
this issue
Nov 28, 2017
microbit-matt-hillsdon
added a commit
to microbit-matt-hillsdon/react-carousel
that referenced
this issue
Mar 16, 2020
See e.g. webpack/webpack#6522 / webpack/webpack#5843 and the docs at https://webpack.js.org/configuration/output/#outputglobalobject This was broken by the Webpack 4 upgrade in brainhubeu#273. This fix effectively reinstates Webpack 3 behaviour in the browser. Note the component still doesn't really support SSR, but its calls to Browser APIs are in componentDidMount and similar so are delayed until client side.
2 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you want to request a feature or report a bug?
This is a bug.
What is the current behavior?
Please consider the following contrived webpack config:
When webpack creates
my-bundle.js
, it wraps the contents ofmy-lib.js
in a UMD template. The template used by webpack is out of date, and uses an obsolete technique to detect the global object.If the current behavior is a bug, please provide the steps to reproduce.
npm install
webpack
my-bundle.js
artifactThe current technique for global object detection in use by webpack is visible on line 10 of
my-bundle.js
:What is the expected behavior?
The UMD template used to wrap
my-lib.js
should use the global object detection technique used in the most up-to-date UMD reference templates. For example, the webpack UMD wrapper aroundmy-bundle.js
should look like:If this is a feature request, what is motivation or use case for changing the behavior?
The old UMD wrapper template used by webpack is not compatible with strict mode JavaScript.
Historically, it was not possible to load non-strict JavaScript as strict mode JavaScript without the script deliberately using the
"use strict";
pragma. However, it is now possible to do this in browsers with<script type="module" src="my-bundle.js>
.UMD cannot dynamically import and export via ECMAScript Modules (ESM). However, it is still possible and useful to import UMD-wrapped libraries as leaf-nodes in an ESM dependency graph (accepting that they will probably export their modules to the detected global object). This is particularly helpful during the current transitional period, where the broader JavaScript ecosystem begins to adopt the new language-native module syntax.
The change required to make UMD-wrapped bundles compatible with strict mode is very small and is known to be compatible with a wide variety of scenarios. If webpack were to use the latest UMD templates, it would unlock authors to make use of a significant number of existing JavaScript libraries as leaf nodes in an ESM dependency graph without modification.
Please mention other relevant information such as the browser version, Node.js version, webpack version and Operating System.
I tested the example project mentioned above with:
The text was updated successfully, but these errors were encountered: