Embarking on a fresh new project, I utilized angular-cli 8.1.2 for the generation process. The goal is to establish a shared library that caters to multiple microservices (apps). This particular library should remain separate from the applications folder, constituting a distinct project with its own git repository. However, upon attempting this setup, the application encounters compilation issues in aot/production mode, despite functioning fine in jit.
The vision involves having two projects housed in two directories within the application:
/test-lib
/test-app
The initial step entails generating the library using angular-cli:
ng new test-lib --create-application=false
(using defaults)
cd test-lib/
ng generate library test-lib --prefix=test
ng build test-lib
This procedure yields the creation of the library folder along with a project residing at /test-lib/projects/test-lib
Subsequently, the (first) application is generated as follows:
cd ..
ng new test-app
(using defaults)
To link the library with this app:
"paths" must be added to tsconfig.json:
"paths": {
"test-lib": [
"../test-lib/dist/test-lib"
],
"test-lib/*": [
"../test-lib/dist/test-lib/*"
]
}
In addition, an import statement must be included in app.module.ts:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { TestLibModule } from 'test-lib';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
TestLibModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Furthermore, the following tag needs to be inserted into app.component.html:
<test-test-lib></test-test-lib>
Upon executing "ng serve," the operation proceeds smoothly without any hitches.
However, running "ng build" within the test-app directory leads to failure and displays the following error message:
ERROR in : Unexpected value 'TestLibModule in C:/Users/.../test-lib/dist/test-lib/test-lib.d.ts' imported by the module 'AppModule in C:/Users/.../test-app/src/app/app.module.ts'. Please add a @NgModule annotation.
enter code here
An additional issue surfaces:
: 'test-test-lib' is not a known element:
1. If 'test-test-lib' is an Angular component, then verify that it is part of this module.
2. If 'test-test-lib' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to
the '@NgModule.schemas' of this component to suppress this message. ("
</ul>
[ERROR ->]<test-test-lib></test-test-lib>")
Attempts to import the component in app.component.ts also proved unsuccessful (component not found).
Shouldn't there be a straightforward way to develop a library externally in a separate folder using angular-cli?