Skip to content

JitCompilerFactory Bug at Angular 5 #20639

Closed

Description

@jackzard

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

JitCompilerFactory already exported to public api from this #20478

but when i install Angular 5 then start using it got some error in production

my code
export function compilerFactory() { return new JitCompilerFactory([{useJit:true}]).createCompiler() }

ERROR in src/app/app.module.ts(11,9): error TS2554: Expected 0 arguments, but got 1. src/app/dynamic/dynamic.component.ts(17,8): error TS2339: Property 'component' does not exist on type 'DynamicComponent'.

it seems JitCompilerFactory no need arguments, then i tried without arguments

export function compilerFactory() { return new JitCompilerFactory().createCompiler() }

then i got new error

ERROR TypeError: Cannot read property 'useJit' of undefined

i tried manually edit compiler_factory.d.ts

export declare class JitCompilerFactory implements CompilerFactory { private _defaultOptions; constructor(options: CompilerOptions[]) <<< i add this constructor createCompiler(options?: CompilerOptions[]): Compiler; }

after that im trying compile again but got new error :

Uncaught (in promise): Error: Unexpected value 't' imported by the module 'function (){}'. Please add a @NgModule annotation.

Expected behavior

how to fix this, Thanks

@ocombe

Minimal reproduction of the problem with instructions

https://github.com/jackzard/Angular5JitCompilerFactory

Environment


Angular version: 5.1.0-beta.2


For Tooling issues:
- Node version: 8.9  
- Platform:  Mac 

Others:

Activity

  1. self-assigned this
    on Nov 27, 2017
  2. added
    area: core
    on Nov 27, 2017
  3. ocombe commented on Nov 27, 2017

    @ocombe
    Contributor

    You shouldn't create the compiler yourself, you need to let the injector do the work here. Look at the code that I used to test it: https://github.com/angular/angular/blob/master/integration/dynamic-compiler/src/app.module.ts

    Also I can already tell you that it will fail because of missing metadata when you try to load the lazy module, the reason is that starting from 1.5.1 the cli strips metadata from your bundles when you use aot (to create smaller bundles). I'm not even sure if it'll work in JIT.
    The example that I used to test the JitCompilerFactory uses rollup instead of the cli to bundle the lazy loaded module (don't use my setup, it's not optimized, there is no deduplication of modules).

    If you want to know why the cli does that, @hansl should be able to answer your questions.

    In the mean time, you can use the cli 1.5.0 if you want.

  4. vladuionut commented on Nov 27, 2017

    @vladuionut

    I have a similar issue, I am trying with the configuration from your test ( https://github.com/angular/angular/blob/master/integration/dynamic-compiler/src/app.module.ts ) but now I got the next error
    "Error: No ResourceLoader implementation has been provided. Can't read the url ".
    I tried with angular cli 1.5.0.

  5. jackzard commented on Nov 28, 2017

    @jackzard
    Author

    @ocombe i already use with injector like your test and downgrade to angular cli 1.5.0, but still got metadata error,

    i can get out the metadata error by disabling --build-optimizer false, but it make app size larger

  6. VSmirnov17 commented on Nov 28, 2017

    @VSmirnov17

    selection_153
    selection_154

    Errors occur when I build project with --aot (ng serve --aot)
    If not --aot - all good

  7. ocombe commented on Nov 28, 2017

    @ocombe
    Contributor

    Please read what I wrote above: #20639 (comment)

  8. VSmirnov17 commented on Nov 28, 2017

    @VSmirnov17

    @ocombe but I use CLI 1.5.0

  9. Sky4CE commented on Nov 28, 2017

    @Sky4CE

    @ocombe , same error here, using @ngtools/webpack AngularCompilerPlugin plugin. If lazy loaded module contains imported module which is using JitCompilerFactory in a way exactly as you showed here, then I get error:
    image
    if I build with JIT (at-loader) then everything is working, but I need AOT

    You can reproduce it by using angular2-busy lib forked by me to refactor use of JitCompilerFactory:

    "dependencies": {
        "angular2-busy": "https://github.com/Sky4CE/angular2-busy/tarball/57b532826f0bcc2c473a0f1aea8d5467262e6c5a",
    
  10. claygifford commented on Nov 28, 2017

    @claygifford

    Using your code...

        {provide: COMPILER_OPTIONS, useValue: {}, multi: true},
        {provide: CompilerFactory, useClass: JitCompilerFactory, deps: [COMPILER_OPTIONS]},
        {provide: Compiler, useFactory: createCompiler, deps: [CompilerFactory]},
    

    You get...

    ERROR Error: StaticInjectorError[JitCompiler]:
    StaticInjectorError[JitCompiler]:
    NullInjectorError: No provider for JitCompiler!

  11. mhevery commented on Nov 28, 2017

    @mhevery
    Contributor

    Hello, we reviewed this issue and determined that it doesn't fall into the bug report or feature request category. This issue tracker is not suitable for support requests, please repost your issue on StackOverflow using tag angular.

    If you are wondering why we don't resolve support issues via the issue tracker, please check out this explanation.

  12. sarunint commented on Nov 29, 2017

    @sarunint
    Contributor

    I can confirm that JitCompilerFactory is usable.

    Here is an example plunker: https://embed.plnkr.co/fPLr5wRLJK6CjUjSKoTb/

  13. Sky4CE commented on Nov 29, 2017

    @Sky4CE

    @sarunint , your module is not lazy, this bug appears only in lazy loaded module

  14. sarunint commented on Nov 30, 2017

    @sarunint
    Contributor

    @Sky4CE It is lazy. I modified the example a little bit so that you can inspect it from the browser inspector.

    I've included a screenshot in case you aren't convinced enough.

    ws2

  15. VSmirnov17 commented on Nov 30, 2017

    @VSmirnov17

    @sarunint You use JIT or AOT?

  16. jackzard commented on Nov 30, 2017

    @jackzard
    Author

    @sarunint you can't test with SystemJs, bug appear if using AOT + build-optimizer flag..

  17. ocombe commented on Nov 30, 2017

    @ocombe
    Contributor

    You cannot use the ngtools package (with or without the CLI), they use optimizations that remove metadata when you compile AoT modules.
    If you want to lazy load a module AoT compiled, you have to compile it with ngc directly, not with ngtools, and then bundle it with rollup, webpack, system js bundler or closure compiler

  18. ocombe commented on Nov 30, 2017

    @ocombe
    Contributor

    If you think that the CLI should take this use case into consideration and offer a way to bundle without removing those metadata (and I think that they should) then open an issue on the CLI repository please

  19. Sky4CE commented on Nov 30, 2017

    @Sky4CE

    @sarunint , by lazy I mean that it should be loaded inside loadChildren by router, e.g. inside chunk. So in this case it would not work.

  20. claygifford commented on Nov 30, 2017

    @claygifford

    I was able to get it to work....

    consider this code:
    in your module
    import {
    Compiler,
    CompilerFactory,
    COMPILER_OPTIONS,
    } from '@angular/core';
    import {
    CompileMetadataResolver,
    CompilerConfig,
    JitCompiler,
    NgModuleResolver
    } from "@angular/compiler";

    export function createCompiler(compilerFactory: CompilerFactory) {
    return compilerFactory.createCompiler();
    }
    providers: [
    { provide: COMPILER_OPTIONS, useValue: {}, multi: true},
    { provide: CompilerFactory, useClass: JitCompilerFactory, deps: [COMPILER_OPTIONS]},
    { provide: JitCompiler, useFactory: createCompiler, deps: [CompilerFactory]},
    { provide: CompilerConfig, useValue: new CompilerConfig() }
    ]

    ======

    then later you can do the following:

    constructor(private compiler: JitCompiler) {
    }
    
    ngOnInit() {
           var component = JitComponent.createComponent(this.compiler, type.Template, type.TemplateModules);
            }
            var container = this.viewContainer.createComponent(component);
            container.instance.Property = this.Property;
            container.changeDetectorRef.detectChanges();
        }
    }
    public static createComponent(compiler: JitCompiler, template: string, modules?: any[]): any {
        @Component({ template })
        class TemplateComponent { }
    
        @NgModule({ declarations: [TemplateComponent], imports: modules ? [RouterModule, ControlsModule].concat(modules) : [RouterModule, ControlsModule] })
        class TemplateModule { }
    
        const mod = compiler.compileModuleAndAllComponentsSync(TemplateModule);
        const factory = mod.componentFactories.find((comp: any) =>
            comp.componentType === TemplateComponent
        );
    
        return factory;
    }
    
  21. Torqode commented on Nov 30, 2017

    @Torqode

    @claygifford This works with webpack+JIT, but when I use webpack+AOT I get this error:

    ModuleNotFoundError: Module not found: Error: Can't resolve '@angular/compiler/src/config' in 'c:\project\src\app\lazymodule'
    <...stack data omitted for brevity...>
    resolve '@angular/compiler/src/config' in 'c:\project\src\app\lazymodule'
    Parsed request is a module
    using description file: c:\project\package.json (relative path: ./src/app/lazymodule)
    Field 'browser' doesn't contain a valid alias configuration
    after using description file: c:\project\package.json (relative path: ./src/app/lazymodule)
    resolve as module
    looking for modules in c:\project\node_modules
    using description file: c:\project\package.json (relative path: ./node_modules)
    Field 'browser' doesn't contain a valid alias configuration
    after using description file: c:\project\package.json (relative path: ./node_modules)
    using description file: c:\project\node_modules@angular\compiler\package.json (relative path: ./src/config)
    no extension
    Field 'browser' doesn't contain a valid alias configuration
    c:\project\node_modules@angular\compiler\src\config doesn't exist
    .ts
    Field 'browser' doesn't contain a valid alias configuration
    c:\project\node_modules@angular\compiler\src\config.ts doesn't exist
    .js
    Field 'browser' doesn't contain a valid alias configuration
    c:\project\node_modules@angular\compiler\src\config.js doesn't exist
    .json
    Field 'browser' doesn't contain a valid alias configuration
    c:\project\node_modules@angular\compiler\src\config.json doesn't exist
    as directory
    c:\project\node_modules@angular\compiler\src\config doesn't exist
    etc, etc, repeated multiple times

  22. vladuionut commented on Dec 3, 2017

    @vladuionut

    @claygifford @Torqode same here Webpack + AOT doesn't work

  23. vijayanandkannan commented on Dec 8, 2017

    @vijayanandkannan

    @ocombe, @jackzard Please look into the Issue

    JIT Complier needed with AOT Build for Dynamic Component. #20875

    I need the JIT Complier @ run time for producing the truly dynamic component. We build with AOT Build which do not work in Angular 5.0. With Angular 4.0 it is working. I am not sure why the feature that was given was taken out. Also what is the alternative?. No one answers except everyone loads the that static HTML as dynamic.

    This is the sample of what we do. If you look into app/app.component.ts

    In method ngAfterContentInit() {

    this.addComponent('<div [formGroup]="_parent.myForm" class="form-row"> ....

    http://plnkr.co/edit/rELaWPJ2cDJyCB55deTF?p=preview

    All our validation and controls comes from database which is configured.

    In angular 4.0 we Implement and create the compiler @ run time (This works perfectly with AOT Build)

    import { JitCompilerFactory } from '@angular/compiler';

    private compiler: Compiler = new JitCompilerFactory([{ useDebug: false, useJit: true }]).createCompiler();

    Please bring back the feature to allow to build with AOT. Tell me the alternative to fix the issue.

    Please consider reopening the issue

    -Vijay

  24. pkhruasu-ui commented on Dec 20, 2017

    @pkhruasu-ui

    @vladuionut Have you resolve the issue with resourceLoader? I crawl all over the internet and still could not find a way to make it work besides changing from templateUrl to template which is not practical in my situation (100 big components).

  25. vladuionut commented on Dec 21, 2017

    @vladuionut

    No I don't have a fix for it

  26. Paladinium commented on Jan 24, 2018

    @Paladinium

    @ocombe : referring to your comment on Nov. 30, I created a feature request for the CLI: angular/angular-cli#9306

    It would be nice if you as an Angular contributor could add a comment to the issue to raise the awareness at the CLI team.

    I think it is reality that there are use-cases where applications need AOT for the static parts of the app and JIT for dynamic content from the server. In my view, both the CLI and Angular should support this use case properly. Once that is possible, I guess that the number of AOT+JIT related issues will become less.

  27. angular-automatic-lock-bot commented on Sep 13, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  28. locked and limited conversation to collaborators on Sep 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: core

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions