在具有自定义webpack配置的monorepo中使用模块联合与Angular和React时,出现“ScriptExternalLoadError:Loading script failed”

jgovgodb  于 2023-11-19  发布在  Webpack
关注(0)|答案(1)|浏览(491)

我尝试使用Angular 13和React,使用Webpack 5和Module Federation插件实现Micro Frontends。我有一个远程Angular应用程序,我试图导入到我的主机React应用程序中。我已经遵循了文档和指南,但我得到一个错误ScriptExternalLoadError:Loading script failed.当尝试加载远程Angular应用程序时。
下面是我的实现细节:

  • 我有两个独立的应用程序,一个Angular,一个React。
  • 在我的Angular应用程序中,我已经将@module-federation/client包添加到我的依赖项中,并配置webpack.js.js文件以公开所需的模块。

webpack-config Angular Application

  1. const path = require('path');
  2. const ModuleFederationPlugin = require('webpack/lib/container/ModuleFederationPlugin');
  3. module.exports = {
  4. mode: 'development',
  5. devtool: 'source-map',
  6. entry: './src/bootstrap.ts',
  7. optimization: {
  8. splitChunks: false,
  9. },
  10. output: {
  11. path: path.resolve(__dirname, 'dist'),
  12. filename: '[name].js',
  13. chunkFilename: '[id].chunk.js',
  14. scriptType: 'text/javascript'
  15. },
  16. resolve: {
  17. extensions: ['.ts', '.js'],
  18. },
  19. module: {
  20. rules: [
  21. {
  22. test: /\.ts$/,
  23. use: [
  24. {
  25. loader: 'ts-loader',
  26. options: {
  27. transpileOnly: true,
  28. },
  29. },
  30. ],
  31. },
  32. {
  33. test: /\.html$/,
  34. loader: 'html-loader',
  35. },
  36. {
  37. test: /\.css$/,
  38. use: ['style-loader', 'css-loader'],
  39. },
  40. ],
  41. },
  42. devServer: {
  43. static: {
  44. directory: path.join(__dirname, 'dist'),
  45. },
  46. compress: true,
  47. port: 8082,
  48. hot: true,
  49. historyApiFallback: true,
  50. },
  51. plugins : [
  52. new ModuleFederationPlugin({
  53. name : 'cubs',
  54. library: { type: 'var', name: 'profile' },
  55. filename : 'remoteEntry.js',
  56. exposes : {
  57. './angular' : './src/bootstrap'
  58. }
  59. }),
  60. ]
  61. };

字符串
package-json

  1. {
  2. "version": "0.0.0",
  3. "scripts": {
  4. "ng": "ng",
  5. "start": "ng serve",
  6. "build": "ng build",
  7. "watch": "ng build --watch --configuration development",
  8. "test": "ng test"
  9. },
  10. "private": true,
  11. "dependencies": {
  12. "@angular-builders/custom-webpack": "^13.1.0",
  13. "@angular/animations": "~13.3.0",
  14. "@angular/common": "~13.3.0",
  15. "@angular/compiler": "~13.3.0",
  16. "@angular/core": "~13.3.0",
  17. "@angular/forms": "~13.3.0",
  18. "@angular/platform-browser": "~13.3.0",
  19. "@angular/platform-browser-dynamic": "~13.3.0",
  20. "@angular/router": "~13.3.0",
  21. "clean-webpack-plugin": "^4.0.0",
  22. "html-webpack-plugin": "^5.5.1",
  23. "i": "^0.3.7",
  24. "npm": "^9.6.6",
  25. "rxjs": "~7.5.0",
  26. "tslib": "^2.3.0",
  27. "zone.js": "~0.11.4"
  28. },
  29. "devDependencies": {
  30. "@angular-devkit/build-angular": "~13.3.11",
  31. "@angular/cli": "~13.3.11",
  32. "@angular/compiler-cli": "~13.3.0",
  33. "@types/jasmine": "~3.10.0",
  34. "@types/node": "^12.11.1",
  35. "jasmine-core": "~4.0.0",
  36. "karma": "~6.3.0",
  37. "karma-chrome-launcher": "~3.1.0",
  38. "karma-coverage": "~2.1.0",
  39. "karma-jasmine": "~4.0.0",
  40. "karma-jasmine-html-reporter": "~1.7.0",
  41. "style-loader": "^3.3.2",
  42. "ts-loader": "^9.4.2",
  43. "typescript": "~4.6.2"
  44. }
  45. }


angular.json

  1. {
  2. "builder": "@angular-builders/custom-webpack:browser",
  3. "options": {
  4. "customWebpackConfig": {
  5. "path": "./custom-webpack.config.js"
  6. },


bootstrap.ts

  1. import 'zone.js';
  2. import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
  3. import { AppModule } from './app/app.module';
  4. import { AppComponent } from './app/app.component';
  5. export function mount(container: HTMLElement, props: any) {
  6. const appComponent = new AppComponent();
  7. const element = appComponent.getElement();
  8. container.appendChild(element);
  9. console.log("Component mounted")
  10. }
  11. platformBrowserDynamic()
  12. .bootstrapModule(AppModule)
  13. .catch((err) => console.error(err));


在我的React主机应用程序webpack-dev.config中

  1. const { merge } = require('webpack-merge')
  2. const HtmlWebpackPlugin = require('html-webpack-plugin')
  3. const commonConfig = require('./webpack.common')
  4. const ModuleFederationPlugin = require('webpack/lib/container/ModuleFederationPlugin')
  5. const devConfig = {
  6. mode : 'development',
  7. devServer : {
  8. port : 8080,
  9. historyApiFallback : {
  10. index : 'index.html'
  11. }
  12. },
  13. plugins : [
  14. new ModuleFederationPlugin({
  15. name : 'container',
  16. remotes : {
  17. reactapp : 'reactapp@http://localhost:8081/remoteEntry.js',
  18. angular : "angular@http://localhost:8082/remoteEntry.js"
  19. }
  20. }),
  21. new HtmlWebpackPlugin({
  22. template : '/public/index.html'
  23. })
  24. ]
  25. }
  26. module.exports = merge(commonConfig, devConfig)


在App.js中导入的React组件

  1. import React, { useRef, useEffect } from 'react';
  2. import 'angular/angular'
  3. const AngularAppWrapper = () => {
  4. const appRef = useRef(null);
  5. console.log("Making an attempt")
  6. console.log(mount);
  7. useEffect(() => {
  8. if (appRef.current) {
  9. mount(appRef.current, {});
  10. }
  11. }, []);
  12. return <div ref={appRef} />;
  13. };
  14. export default AngularAppWrapper;


主机ReactBootstrap

  1. import React from 'react';
  2. import ReactDOM from 'react-dom/client';
  3. import App from './App';
  4. import reportWebVitals from './reportWebVitals';
  5. const root = ReactDOM.createRoot(document.getElementById('root'));
  6. root.render(
  7. <React.StrictMode>
  8. <App />
  9. </React.StrictMode>
  10. );
  11. reportWebVitals();

  • 我已经验证了remoteEntry.js文件是从Angular应用程序提供的,URL是正确的(http://localhost:8082/remoteEntry.js)。
  • 我已经尝试了网上建议的几种解决方案,比如清除该高速缓存,使用隐身模式,检查网络日志,检查导入模块的路径。
  • 尽管如此,我仍然得到错误.我不知道还有什么可以尝试.有人可以帮助我诊断和解决这个问题?
tp5buhyn

tp5buhyn1#

我在Webpack中使用模块联合时遇到了类似的ScriptExternalLoadError问题。对我有效的解决方案是在Webpack配置中将optimization.minimize设置为false。这实质上是在构建过程中禁用代码最小化步骤。
以下是如何做到这一点:

  1. // webpack.config.js
  2. module.exports = {
  3. // ...other configurations
  4. optimization: {
  5. minimize: false
  6. }
  7. };

字符串
通过这样做,你可能会注意到bundle的大小会增加,因为代码没有被缩小。但是,它应该解决你遇到的ScriptExternalLoadError。请记住,理想情况下,你应该只在调试时这样做,并为生产找到一个更持久的解决方案。

相关问题