2017-10-13 11 views
2

JitCompilerFactoryを使用してランタイムコンパイラとカスタムデコレータをロードしてコンポーネントとモジュールのメタデータを保存するというソリューションhereに従っています。しかし、角度-CLI --build-optimizerフラグで私が取得:ビルドオプティマイザを使用したAOTとJIT

ERROR Error: Cannot resolve all parameters for 'Parser'(?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'Parser' is decorated with Injectable.

Hereは私の最小限の再現、実行ng serve --aot --build-optimizerです。コントロールとしてng serve --aotが機能します。

答えて

0

オプション--build-optimizerを使用すると、@angular-devkit/build-optimizer/webpack-loaderローダーがwebpackローダーリストに追加されます。私はそれが正しい行動だと思う

const platformWhitelist = [ 
    'PlatformRef_', 
    'TestabilityRegistry', 
    'Console', 
    'BrowserPlatformLocation', 
]; 

@angular-devkit/build-optimizer/webpack-loaderplatformWhitelistの一部(source)ではない種類からctorParameters定義を削除します。これを回避するには

あなたはJitCompilerFactoryis created前に手動ctorParametersを初期化することができます:

import { 
    Compiler, ɵConsole as Console, 
    Optional, Inject, Injector, 
    PACKAGE_ROOT_URL 
} from '@angular/core'; 

import { 
    JitCompilerFactory, 
    TemplateParser, CompilerConfig, CompileReflector, 
    ElementSchemaRegistry, I18NHtmlParser, TEMPLATE_TRANSFORMS, DirectiveNormalizer, 
    ResourceLoader, UrlResolver, HtmlParser, CompileMetadataResolver, NgModuleResolver, 
    DirectiveResolver, SummaryResolver, PipeResolver, StaticSymbolCache, 
    ERROR_COLLECTOR_TOKEN, 
    StyleCompiler, ViewCompiler, NgModuleCompiler, JitCompiler 
} from '@angular/compiler'; 

import * as compiler from '@angular/compiler'; 

export function compilerFactory() { 
    restoreDecorators(); 

    return new JitCompilerFactory([{ useDebug: false, useJit: true }]).createCompiler(); 
} 

function restoreDecorators() { 
    (compiler.Parser as any).parameters = [compiler.Lexer]; 
    (TemplateParser as any).parameters = [ 
    CompilerConfig, CompileReflector, compiler.Parser, ElementSchemaRegistry, 
    I18NHtmlParser, Console, [new Optional(), new Inject(TEMPLATE_TRANSFORMS)] 
    ]; 
    (DirectiveNormalizer as any).parameters = [ 
    ResourceLoader, UrlResolver, 
    HtmlParser, CompilerConfig 
    ]; 
    (CompileMetadataResolver as any).parameters = [ 
    CompilerConfig, NgModuleResolver, DirectiveResolver, PipeResolver, SummaryResolver, 
    ElementSchemaRegistry, 
    DirectiveNormalizer, Console, 
    [new Optional(), StaticSymbolCache], 
    CompileReflector, 
    [new Optional(), new Inject(ERROR_COLLECTOR_TOKEN)] 
    ]; 
    (StyleCompiler as any).parameters = [UrlResolver]; 
    (ViewCompiler as any).parameters = [CompileReflector]; 
    (NgModuleCompiler as any).parameters = [CompileReflector]; 
    (NgModuleResolver as any).parameters = [CompileReflector]; 
    (DirectiveResolver as any).parameters = [CompileReflector]; 
    (PipeResolver as any).parameters = [CompileReflector]; 

    (JitCompiler as any).parameters = [ 
    Injector, 
    CompileMetadataResolver, 
    TemplateParser, 
    StyleCompiler, 
    ViewCompiler, 
    NgModuleCompiler, 
    SummaryResolver, 
    CompilerConfig, 
    Console 
    ]; 
    (UrlResolver as any).parameters = [[new Inject(PACKAGE_ROOT_URL)]]; 
} 

そしてコンパイラが内部変更の対象となるように危険であると思われます。 @angular/[email protected]で動作しますが、他のリリースでは動作しない可能性があります。

+0

私は 'ctorParameters'が何であるかわからないがコンテキストから私はDIがサービスの(そしてその他の)コンストラクタに何を注入するのかを知っているということを集めている。もしあなたがhttps://github.com/angular/devkitをフォークしたり、機能リクエストをした場合、 'platformWhitelist'に' Compiler'を追加するのと同じくらい単純かもしれませんか? –

+0

私たちはそれをするべきではありません。 – yurzui

関連する問題