webpack: globalObject: "this" work incorrect with ModuleFederation after 5.2.0
Bug report
What is the current behavior?
the ModuleFederation crashed with output.globalObject: "this"
If the current behavior is a bug, please provide the steps to reproduce.
- clone
https://github.com/module-federation/module-federation-examples/tree/master/basic-host-remote
- add
ModuleFederationPlugin
- set
output.globalObject: "this"
- set
package.json
(disablearrowFunctions
)
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
- build and run
the output remoteEntry.js
error:
// this is undefined
var chunkLoadingGlobal = this["webpackChunkreact_app_shared"] = this["webpackChunkreact_app_shared"] || [];
What is the expected behavior?
ModuleFederation
should works correct with: output.globalObject: "this"
Other relevant information: webpack version: 5.2.0~5.5.1
About this issue
- Original URL
- State: open
- Created 4 years ago
- Comments: 24 (21 by maintainers)
My case: First i found complied code contains arrow fucntion. so i add
target: ['web', 'es5']
to solve the problem. Then code became:So i need to add
output.globalObject: 'window'
to solve this problem.But it seems a little weird because once you set
target: ['web', 'es5']
. You need to configureoutput.globalObject: 'window'
too. Which is not mentioned in documentation.It could be indeed a bug. Sounds like the bundle becomes strict mode only and
this
is undefined in strict mode.As workaround avoid this as global object. Using the default should be fine instead.
Still an issue probably.
we use it only if environment supports it.
Yeah I guess that works as a workaround - in my case I went ahead and updated that legacy Worker that caused an issue to be strict mode compliant.
I just wanted to report this as it will probably catch others by surprise too, and takes a while to debug what’s happening.
I think Webpack should either skip non-module usages of Workers, or provide correct environment to them, or at least warn on such usages - current behaviour quietly emits broken code, which seems like the worst of those options DX-wise.