5

我目前正在尝试通过 Webpack 外部外部化 Angular 依赖项来缩短构建时间。到目前为止,我已经为 React 和其他小型库实现了这一点,没有出现任何问题。

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

我使用的文件是您可以在 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
4

1 回答 1

4

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

索引.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.

例如,您还可以使用<script src='https://unpkg.com/core-js-bundle@3.1.4/minified.js'></script>

默认情况下,Angular CLI 在 jit 中添加了这个polyfills.tspolyfill。但是single-spa-angular删除了这个 polyfill。

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

索引.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 bundle( @esm-bundle/rxjs@6.5.4/system/rxjs.min.js),这意味着该 bundle 没有正确构建以在浏览器中使用它(更多关于此错误的信息,请参见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>
于 2020-03-05T19:38:27.970 回答