module-federation-examples: Initialization of sharing external failed: ScriptExternalLoadError: Loading script failed.
webpack version: 5.20.0
error message
warining: Initialization of sharing external failed: ScriptExternalLoadError: Loading script failed. error: Uncaught (in promise) ScriptExternalLoadError: Loading script failed.
similar issue: (#307)
I already used syntax like app2: "app2@http://localhost:3002/remoteEntry.js"
, but the browser still report the error.
Here’s my config:
app1
app2
other
I am sure I have read shared-routes2
folder in this repo.
The difference between my demo and shared-routes
is this different framework.
I use vue-cli in my demo.
I inspect the generated webpack config:
/* config.plugin('mf') */
// app1
new ModuleFederationPlugin(
{
name: 'app1',
filename: 'remoteEntry.js',
remotes: {
app2: 'app2@http://localhost:8082/remoteEntry.js'
},
exposes: {},
shared: {
vue: {
eager: true,
singleton: true,
requiredVersion: '^2.6.11'
},
'vue-router': {
eager: true,
singleton: true,
requiredVersion: '^3.4.3'
}
}
}
)
// app2
/* config.plugin('mf') */
new ModuleFederationPlugin(
{
name: 'app2',
filename: 'remoteEntry.js',
exposes: {
'./routes': './src/router/index.ts'
},
shared: {
vue: {
eager: true,
singleton: true,
requiredVersion: '^2.6.11'
},
'vue-router': {
eager: true,
singleton: true,
requiredVersion: '^3.4.3'
}
}
}
)
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 4
- Comments: 18 (6 by maintainers)
As mentioned by nrz2000 disabling splitChunks worked, but I didn’t understand why, so I checked vue-cli code to see how they were configuring SplitChunks plugin.
And here it is what I got so far:
Comparing the config from vue-cli and the default webpack config from the official website we can get some answers.
Code copied from vue-cli:
https://github.com/vuejs/vue-cli/blob/dev/packages/%40vue/cli-service/lib/config/app.js#L41-L60
Code copied from webpack docs:
https://webpack.js.org/plugins/split-chunks-plugin/#optimizationsplitchunks
So the main difference between the two configs is that webpack uses by default
chunks: 'async'
while vue-cli useschunks: initial
, and this seems to be the reason behind the error with module federation. I changed my vue-config to the following and it solved the problem:Note that it’s the same configuration as vue-cli, but just changing the
chunks
property on both cache groups frominitial
toasync
Still, I don’t know the specific reason, in this stackoverflow thread they explain the differences between the three possible split chunk values:
async
,all
andinitial
. I tried with ‘all’ and the error persists. I think it’s related to the nature of module federation that relies on async chunks, so trying optimizing static chunks throw errors, but I’m just speculating here. Hope someone has some answers.Public path is probably incorrect
兄弟,有发现问题吗?我现在也遇到同样的问题
我这边发现问题了,是qiannkun沙箱问题,暂时通过配置library: { type: ‘window’, name: ‘xxx’ } 来解决
based on @gabrielmaschke and @ScriptedAlchemy 's solution, the following worked for me
I am not sure how to fix this in webpack. since all module types are derived classes of Module, they get sent to the SplitChunksPlugin. It does not make sense to add a condition in the SplitChunksPlugin to check for this but I cant find where else to fix it @ScriptedAlchemy any help here? If we cant fix this, I am willing to send a PR to the docs to add this to the troubleshooting section because this is important from a loading time pov
Chunks also accepts function. So all, initial, async or function. Use function and test chunks to see if they contain remote-module or provide-module type, return false for those and true for any others. That lets you exclude MF manages chunks from split chunks algorithms
I’m using vue-cli with vue3 and disabling splitChunks was the solution for me. #https://github.com/vuejs/vue-cli/issues/6318#issuecomment-786603459