Prevent to remove script section of package.json (library angular)

Viewed 777

I made a library with angular 6. When I build the library, script section of package.json will be removed. How can I prevent this?(I need script section after library was built)

How to build: ng build --prod MyLibraryName

3 Answers

Angular library uses ng-packagr package. When you generete a library by cli, ng-package.json and ng-package.prod.json will be added into your library.(for setting of the package)

If you add "keepLifecycleScripts": true into ng-package.prod.json, the script section of the package.json won't be removed in building the library.

Since version 2.3.0 of the ng-packagr, you need to add the keepLifecycleScripts-flag to the package.json, not to the ng-package.prod.json:

{
  "name": "xyz",
  "version": "1.0.0",
  "scripts": {
    "prepare": "..."
  },
  "ngPackage": {
    "keepLifecycleScripts": true
  }
}

Just to help if someone had the same problem I had:

After I've edited the package.json as described by Morzeta, executing ng build --prod resulted on a different destination path for the final build. The default is {workspace}/dist/{projectName}, after the change this became projects/{projectName}/dist.

I had some hard time trying to find why this happened. Debugging ng-packagr on node_modules, I came to find that when you configure your package.json as described the dest property is resolved to projects/{projectName}/dist, the solution is to place the dest property on package.json again:

{
  "name": "xyz",
  "version": "1.0.0",
  "scripts": {
    "prepare": "..."
  },
  "ngPackage": {
    "dest": "../../dist/{your-project-name-here}",
    "keepLifecycleScripts": true
  }
}
Related