gpt4 book ai didi

Angular 作为 Webpack 外部

转载 作者:行者123 更新时间:2023-12-03 16:36:36 32 4
gpt4 key购买 nike

我目前正在尝试通过 Webpack 外部对象外部化 Angular 依赖项来缩短构建时间。到目前为止,我已经为 React 和其他小库实现了这一目标。

如果我只是移动 '@angular/compiler' 它也可以工作,但是当外部化 '@angular/core' 和其他人时,我会收到 Can't resolve all parameters for ApplicationModule: (?) at at syntaxError (util.ts:100) at CompileMetadataResolver._getDependenciesMetadata (metadata_resolver.ts:957) ...引导应用程序时出错。

我使用的文件是您可以在 unpkg.com 中找到的 8.2.14 UMD 包,我的外部文件是:

[
/^@example\,
/^rxjs$/,
/^rxjs\/operators$/,
/^@angular\/core$/,
/^@angular\/compiler$/,
/^@angular\/common$/,
/^@angular\/common\/http$/,
/^@angular\/forms$/,
/^@angular\/platform-browser$/,
/^@angular\/platform-browser-dynamic$/,
/^@angular\/router$/,
/^zone\.js$/,
/^single-spa$/
]

rxjs 包是在这里找到的 https://cdn.jsdelivr.net/npm/@esm-bundle .

您可以在此 repo 上看到错误: https://github.com/OriolInvernonLlaneza/test-share-angular

提前致谢!

编辑:多亏了 yurzui 的回答,这个错误得到了解决。但是,我现在收到另一个错误:
Class constructor I cannot be invoked without 'new'
at new EventEmitter (event_emitter.ts:78)
at new NgZone (ng_zone.ts:97)
at getNgZone (application_ref.ts:358)
at PlatformRef.bootstrapModuleFactory (application_ref.ts:255)
at application_ref.ts:308

最佳答案

您需要包含 core-js shim 库,以便 Angular 代码可以使用 Reflect.getOwnMetadata从装饰类中读取元数据的方法

index.ejs

<% if (isLocal) { %>
<script src="https://unpkg.com/core-js@2.4.1/client/shim.min.js"></script> <== add this
<script type="systemjs-importmap">
....

您将使用哪个 core-js 版本并不重要,但它应该包含 reflect.get-own-metadata 的 polyfill .

例如,您也可以使用 <script src='https://unpkg.com/core-js-bundle@3.1.4/minified.js'></script>
Angular CLI 默认在 polyfills.ts 中添加了这个 polyfill对于 jit。但是 single-spa-angular删除这个 polyfill。

请注意,您还应该将 zone.js 包含到您的文件中,以便完整的代码看起来像

index.ejs
<script src='https://unpkg.com/core-js-bundle@3.1.4/minified.js'></script>
<script src="https://unpkg.com/zone.js"></script>

更新

错误 Class constructor I cannot be invoked without 'new'来自 rxjs 包( @esm-bundle/rxjs@6.5.4/system/rxjs.min.js ),这意味着这个包没有正确构建以在浏览器中使用它(更多关于这个错误的信息在这里 Javascript ES6 TypeError: Class constructor Client cannot be invoked without 'new' )。

您可以将 umd 包用于 rxjs
"rxjs": "https://cdn.jsdelivr.net/npm/rxjs@6.5.4/bundles/rxjs.umd.min.js",
"rxjs/operators": "https://cdn.jsdelivr.net/npm/@esm-bundle/rxjs@6.5.4/system/rxjs-operators.min.js",

我会分享整个 inject.ejs下面的文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Root Config</title>
<meta name="importmap-type" content="systemjs-importmap" />
<% if (isLocal) { %>
<!--<script src="https://unpkg.com/core-js@2.6/client/shim.min.js"></script>-->
<script type="systemjs-importmap">
{
"imports": {
"@example/root-config": "http://localhost:9000/root-config.js",
"@example/angular": "http://localhost:4200/main.js",

"single-spa": "https://cdnjs.cloudflare.com/ajax/libs/single-spa/5.1.0/system/single-spa.dev.js",

"rxjs": "https://cdn.jsdelivr.net/npm/rxjs@6.5.4/bundles/rxjs.umd.min.js",
"rxjs/operators": "https://cdn.jsdelivr.net/npm/@esm-bundle/rxjs@6.5.4/system/rxjs-operators.min.js",

"@angular/core": "https://unpkg.com/@angular/core@8.2.14/bundles/core.umd.js",
"@angular/compiler": "https://unpkg.com/@angular/compiler@8.2.14/bundles/compiler.umd.js",
"@angular/common": "https://unpkg.com/@angular/common@8.2.14/bundles/common.umd.min.js",
"@angular/common/http": "https://unpkg.com/@angular/common@8.2.14/bundles/common-http.umd.min.js",

"@angular/platform-browser-dynamic": "https://unpkg.com/@angular/platform-browser-dynamic@8.2.14/bundles/platform-browser-dynamic.umd.min.js",
"@angular/platform-browser": "https://unpkg.com/@angular/platform-browser@8.2.14/bundles/platform-browser.umd.min.js",
"@angular/platform-browser/animations": "https://unpkg.com/@angular/platform-browser@8.2.14/bundles/platform-browser-animations.umd.min.js",

"@angular/forms": "https://unpkg.com/@angular/forms@8.2.14/bundles/forms.umd.min.js",
"@angular/router": "https://unpkg.com/@angular/router@8.2.14/bundles/router.umd.min.js",
"@angular/animations": "https://unpkg.com/@angular/animations@8.2.14/bundles/animations.umd.js",
"@angular/animations/browser": "https://unpkg.com/@angular/animations@8.2.14/bundles/animations-browser.umd.min.js",

"tslib": "https://cdnjs.cloudflare.com/ajax/libs/tslib/1.10.0/tslib.min.js",
"zone.js": "https://cdnjs.cloudflare.com/ajax/libs/zone.js/0.9.1/zone.min.js"
}
}
</script>
<% } %>
<script src='https://unpkg.com/core-js-bundle@3.1.4/minified.js'></script>
<script src="https://unpkg.com/zone.js"></script>
<script src="https://cdn.jsdelivr.net/npm/import-map-overrides/dist/import-map-overrides.js"></script>
<script src="https://cdn.jsdelivr.net/npm/systemjs/dist/system.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/systemjs/dist/extras/amd.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/systemjs/dist/extras/named-exports.min.js"></script>
</head>
<body>
<main id="main"></main>
<script>
System.import("@example/root-config");
</script>
<import-map-overrides-full show-when-local-storage="devtools" dev-libs></import-map-overrides-full>
</body>
</html>

关于Angular 作为 Webpack 外部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60505372/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com