Angular 作为 Webpack 外部
Angular as Webpack External
我目前正在尝试通过 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
垫片库,以便 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
中为 jit 添加这个 polyfill。但是 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 bundle(@esm-bundle/rxjs@6.5.4/system/rxjs.min.js
) 这意味着这个包没有正确构建以在浏览器中使用它(更多关于这个错误的信息 ) .
您可以为 rxjs 使用 umd bundle
"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>
我目前正在尝试通过 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
垫片库,以便 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
.
例如你也可以使用<script src='https://unpkg.com/core-js-bundle@3.1.4/minified.js'></script>
Angular CLI 默认在 polyfills.ts
中为 jit 添加这个 polyfill。但是 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 bundle(@esm-bundle/rxjs@6.5.4/system/rxjs.min.js
) 这意味着这个包没有正确构建以在浏览器中使用它(更多关于这个错误的信息
您可以为 rxjs 使用 umd bundle
"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>