Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler-cli): better detect classes that are indirectly exported #42207

Closed
wants to merge 1 commit into from

Commits on May 20, 2021

  1. fix(compiler-cli): better detect classes that are indirectly exported

    The compiler flag `compileNonExportedClasses` allows the Angular compiler to
    process classes which are not exported at the top level of a source file.
    This is often used to allow for AOT compilation of test classes inside
    `it()` test blocks, for example.
    
    Previously, the compiler would identify exported classes by looking for an
    `export` modifier on the class declaration itself. This works for the
    trivial case, but fails for indirectly exported classes:
    
    ```typescript
    // Component is declared unexported.
    @component({...})
    class FooCmp {...}
    
    // Indirect export of FooCmp
    export {FooCmp};
    ```
    
    This is not an immediate problem for most application builds, since the
    default value for `compileNonExportedClasses` is `true` and therefore such
    classes get compiled regardless.
    
    However, in the Angular Language Service now, `compileNonExportedClasses` is
    forcibly overridden to `false`. That's because the tsconfig used by the IDE
    and Language Service is often far broader than the application build's
    configuration, and pulls in test files that can contain unexported classes
    not designed with AOT compilation in mind.
    
    Therefore, the Language Service has trouble working with such structures.
    
    In this commit, the `ReflectionHost` gains a new API for detecting whether a
    class is exported. The implementation of this method now not only considers
    the `export` modifier, but also scans the `ts.SourceFile` for indirect
    exports like the example above. This ensures the above case will be
    processed directly in the Language Service.
    
    This new operation is cached using an expando symbol on the `ts.SourceFile`,
    ensuring good performance even when scanning large source files with lots of
    exports (e.g. a FESM file under `ngcc`).
    
    Fixes angular#42184.
    alxhub committed May 20, 2021
    Configuration menu
    Copy the full SHA
    c8b1bc6 View commit details
    Browse the repository at this point in the history