Search code examples
angulartsconfigangular2-aot

How to publish package for use in a AoT angular2 app


I have a code written in angular2 typescript , this is a module exported with services and components

@NgModule({
    declarations:[
        ...DECLARATIONS
    ],
    imports: [
        CommonModule,
        ChartModule
    ],
    exports:[
        ...DECLARATIONS,
        CommonModule,
        ChartModule
    ],
    providers:[
        AlertErrorHandleService
        , AuthenticationModelService
        , AuthConnectionBackend
    ],
    schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
})
export class MyModule {
    static forRoot():ModuleWithProviders {
        return {
            ngModule: StmsModule,
            providers: [AlertErrorHandleService
                , AuthenticationModelService
                , AuthConnectionBackend
            ]
        };
    }
}

I want to use this MyModule in different angular2 applications , so I've published it with this tsconfig and scripts

{
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "removeComments": true,
    "module": "es2015",
    "target": "es5",
    "moduleResolution": "node",
    "sourceMap": true,
    "declaration": true,
    "noImplicitAny": false,
    "experimentalDecorators": true,
    "lib": ["dom", "es2015"],
    "outDir": "dist",
    "types": [
      "node"
    ]
  },
  "exclude": [
    "node_modules",
    "dist",
    "scripts"
  ],
  "angularCompilerOptions": {
    "genDir": "aot"
  }
}

packages.json

"name": "my-lib",
  "version": "0.2.1",
  "description": "Shared lib angular2",
  "main": "index.js",
  "typings": "index.d.ts",
  "scripts": {
    "ngc": "ngc",
    "lint": "tslint src/**/*.ts",
    "copyPackage":"node ./scripts/copy-package",
    "build": "rimraf -rf aot dist && npm run ngc && npm run copyPackage",
    "publishPackage": "npm run build && cd dist && npm publish"
  },
  "keywords": [
    "angular",
    "angular2"
  ],
  "author": "Haddar Macdasi",
  "license": "MIT",
  "dependencies": {
    "@angular/core": "2.1.1",
    "@angular/common": "2.1.1",
    "@angular/compiler": "2.1.1",
    "@angular/compiler-cli": "2.1.1",
    "@angular/http":"2.1.1",
    "@angular/platform-browser": "2.1.1",
    "rxjs": "5.0.0-beta.12",
    "angular2-highcharts": "0.4.1",
    "highcharts": "5.0.2"
  },
  "devDependencies": {
    "@types/chai": "^3.4.34",
    "@types/node": "^6.0.41",
    "typescript": "^2.0.3"
  }

in other angular2 app that was created using seed from angular2 seed I've installed the package , trying to use it all works as I run the app not in AOT but in AoT it has error

Error: Unexpected value 'MyModule' imported by the module 'AboutModule'

I have couple of questions :

  1. how should I publish the package / what is the differents inside tsconfig "module" = es2015 / systemjs / commonjs
  2. Is the module publishe type has anything to do with how the package can be used inside angular2 - i.e , If i use systemjs publish can I this package in an angular2 AoT app ?
  3. Is there any kind of best practice how to publish an angular2 shared module and use it in diff apps ?

Solution

  • I plead ignorance of the actual issue, but recognised the error message from a blog post I was just reading for a different reason.

    This appears to be related to needing to include the *.metadata.json files in your npm package.

    https://medium.com/@isaacplmann/getting-your-angular-2-library-ready-for-aot-90d1347bcad#.p7i6p63e8