ng-packagr
Compile and package Angular libraries in Angular Package Format (APF)
Installation
npm install -D ng-packagr
Usage Example
Let's walk through a getting started that'll build an Angular library from TypeScript sources and create a distribution-ready npm package:
create a package.json
file, add the custom ngPackage
property, and eventually run ng-packagr -p package.json
β Here we go:
{
"$schema": "./node_modules/ng-packagr/package.schema.json",
"name": "@my/foo",
"version": "1.0.0",
"ngPackage": {
"lib": {
"entryFile": "public_api.ts"
}
}
}
Note 1: Paths in the ngPackage
section are resolved relative to the location of the package.json
file.
In the above example, public_api.ts
is the entry file to the library's sources and must be placed next to package.json
(a sibling in the same folder).
Note 2: referencing the $schema
enables JSON editing support (auto-completion for configuration) in IDEs like VSCode.
You can easily run ng-packagr through a npm/yarn script:
{
"scripts": {
"build": "ng-packagr -p package.json"
}
}
Now, execute the build with the following command:
$ yarn build
The build output is written to the dist
folder, containing all those binaries to meet the Angular Package Format specification.
You'll now be able to go ahead and npm publish dist
your Angular library to the npm registry.
Do you like to publish more libraries?
Is your code living in a monorepo?
Create one package.json
per npm package, run ng-packagr for each!
Features
- π Implements Angular Package Format
- π Bundles your library in FESM2020
- π npm package can be consumed by Angular CLI, Webpack, or SystemJS
- π Creates type definitions (
.d.ts
) - π Auto-discovers and bundles secondary entry points such as
@my/foo
,@my/foo/testing
,@my/foo/bar
- π Creates scoped and non-scoped packages for publishing to npm registry
- π Inlines Templates and Stylesheets
- β¨ CSS Features
- π« Runs SCSS preprocessor, supporting the relative
~
import syntax and custom include paths - π Runs Stylus preprocessor, resolves relative paths relative to ng-package.json
- π Adds vendor-specific prefixes w/ autoprefixer and browserslist β just tell your desired
.browserslistrc
- π― Embed assets data w/ postcss-url
- π« Runs SCSS preprocessor, supporting the relative
How toβ¦
- Copy Assets
- Embed Assets in CSS
- Managing Dependencies
- Change the Entry File of a Library
- Change Configuration Locations
- Override tsconfig
- Add Style Include Paths
- Package Secondary Entrypoints (sub packages)
Knowledge
Angular Package Format documentation
Packaging Angular Libraries - Jason Aden at Angular Mountain View Meetup (Jan 2018, 45min talk)
Create and publish Angular libs like a Pro - Juri Strumpflohner at NG-BE (Dec 2017, 30min talk)
Packaging Angular - Jason Aden at ng-conf 2017 (28min talk)
Create and publish Angular libs like a Pro - Juri Strumpflohner at ngVikings, this time demoing building Angular libraries with ng-packagr, with NX as well as Bazel (March 2018, 30min talk)
Contributing to ng-packagr
General contribution guidelines
If you like to submit a pull request, you'll find it helpful to take a look at the initial design document where it all started.
To orchestrate the different tools, ng-packagr features a custom transformation pipeline. The transformation pipeline is built on top of RxJS and Angular Dependency Injection concepts.