在我的配置中,我目前正在使用一个shell应用程序和一个遥控器,它们都是用角12.2.0编写的,而且我面临着一个我不知道如何解决的问题。
外壳webpack.config.js
const ModuleFederationPlugin = require("webpack/lib/container/ModuleFederationPlugin");
const mf = require("@angular-architects/module-federation/webpack");
const path = require("path");
const share = mf.share;
const sharedMappings = new mf.SharedMappings();
sharedMappings.register(
path.join(__dirname, '../../tsconfig.json'),
['auth-lib']
);
module.exports = {
output: {
uniqueName: "portal",
publicPath: "auto"
},
optimization: {
runtimeChunk: false
},
resolve: {
alias: {
...sharedMappings.getAliases(),
}
},
plugins: [
new ModuleFederationPlugin({
// For hosts (please adjust)
remotes: {
"myremote": "myremote@http://localhost:4250/remoteEntry.js"
},
shared: share({
"@angular/core": { singleton: true, strictVersion: true, requiredVersion: 'auto' },
"@angular/common": { singleton: true, strictVersion: true, requiredVersion: 'auto' },
"@angular/common/http": { singleton: true, strictVersion: true, requiredVersion: 'auto' },
"@angular/router": { singleton: true, strictVersion: true, requiredVersion: 'auto' },
...sharedMappings.getDescriptors()
})
}),
sharedMappings.getPlugin()
],
};
远程webpack.config.js
const ModuleFederationPlugin = require("webpack/lib/container/ModuleFederationPlugin");
const mf = require("@angular-architects/module-federation/webpack");
const path = require("path");
const share = mf.share;
const sharedMappings = new mf.SharedMappings();
sharedMappings.register(
path.join(__dirname, 'tsconfig.json'),
[/* mapped paths to share */]
);
module.exports = {
output: {
uniqueName: "interviews",
publicPath: "auto"
},
optimization: {
runtimeChunk: false
},
resolve: {
alias: {
...sharedMappings.getAliases(),
}
},
plugins: [
new ModuleFederationPlugin({
// For remotes (please adjust)
name: "myremote",
filename: "remoteEntry.js",
exposes: {
'./Module': './src/app/myremote/myremote.module.ts'
},
shared: share({
"@angular/core": { singleton: true, strictVersion: true, requiredVersion: 'auto' },
"@angular/common": { singleton: true, strictVersion: true, requiredVersion: 'auto' },
"@angular/common/http": { singleton: true, strictVersion: true, requiredVersion: 'auto' },
"@angular/router": { singleton: true, strictVersion: true, requiredVersion: 'auto' },
...sharedMappings.getDescriptors()
})
}),
sharedMappings.getPlugin()
],
};
当我在shell应用程序中使用对远程模块的引用时,一切都很顺利,如下所示:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HomeComponent } from './components/home/home.component';
const routes: Routes = [{
path: '',
component: HomeComponent,
pathMatch: 'full'
}, {
path: 'myremote',
loadChildren: () => import('myremote/Module').then(m => m.MyRemoteModule)
}];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
当远程模块的组件在其代码中实例化一个新的Web工作人员时会出现这个问题,如下所示:
const myWorker = new Worker(
new URL('../path/to/workers/worker.ts',
import.meta.url
),
{ type: 'module', name: 'my-worker' }
);
// since I'm also using comlink, I also do that, but that's not really relevant here
this.worker = this.createWrapper<WorkerService>(myWorker);
上面的代码( Webpack 5 )已经在我的应用程序my-worker.js中生成了一个单独的包,它既不是组件的一部分,也不是组件的包。因此,将模块公开给module,不会暴露到remoteEntry.js中,也不会暴露到工作人员的代码中。然后尝试使用shell应用程序导航到加载我的遥控器的路由,会出错并通知:
错误: Uncaught (承诺):SecurityError:未能构造'Worker':脚本在'http://localhost:4250/my-worker.js‘不能从原始的http://localhost:5000'访问。错误:未能构造'Worker':在'http://localhost:4250/my-worker.js‘处的脚本无法从原始的'http://localhost:5000'’访问。
所以问题是,如果我想要有一个遥控器,并公开它的模块在一个shell应用程序中使用,而它的一个模块使用一个Web工作者,我如何确保Webpack创建的单独的工人包也被公开,以便shell应用能够实例化它?
发布于 2021-10-01 10:00:33
感谢亚历山大-阿卡伊特,答案就在这里。
https://github.com/webpack/webpack/discussions/14380
假设错误是SecurityError
错误:未知(承诺):SecurityError
该问题与需要在本地服务器中设置CORS有关。
发布于 2022-05-19 11:02:39
我找到了解决办法。
步骤1.在单独的文件中创建自定义工作者类
export class CorsWorker {
private readonly worker: Worker;
constructor(url: string | URL) {
const objectURL = URL.createObjectURL(
new Blob([`importScripts(${JSON.stringify(url.toString())});`], {
type: 'application/javascript'
})
);
this.worker = new Worker(objectURL);
URL.revokeObjectURL(objectURL);
}
getWorker() {
return this.worker;
}
}步骤2-不要使用Webpack工作人员,而是使用CorsWorker导入您的员工文件。
import { CorsWorker as Worker } from './cors-worker';
// aliasing CorsWorker to Worker makes it statically analyzable
const corsWorker = new Worker(new URL('../worker', import.meta.url));
const worker = corsWorker.getWorker();这个例子适用于我。我们可以通过以下GitHub讨论线程https://github.com/webpack/webpack/discussions/14648获得更多信息
https://stackoverflow.com/questions/69361483
复制相似问题