17.3.0 • Published 7 days ago

ng-packagr v17.3.0

Weekly downloads
278,358
License
MIT
Repository
github
Last release
7 days ago

ng-packagr

Transpile your libraries to Angular Package Format

npm npm License Conventional Commits CircleCI Travis

GitHub contributors GitHub PR Stats GitHub Issue Stats

GitHub stars npm Downloads Renovate enabled

Usage Example

Let's talk us 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: 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).

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

  • :gift: Implements Angular Package Format
    • :checkered_flag: Bundles your library in FESM2015, FESM5, and UMD formats
    • :school_satchel: npm package can be consumed by Angular CLI, Webpack, or SystemJS
    • :dancer: Creates type definitions (.d.ts)
    • :runner: Generates Ahead-of-Time metadata (.metadata.json)
    • :trophy: Auto-discovers and bundles secondary entry points such as @my/foo, @my/foo/testing, @my/foo/bar
  • :mag_right: Creates scoped and non-scoped packages for publishing to npm registry
  • :surfer: Inlines Templates and Stylesheets
  • :sparkles: CSS Features
    • :camel: Runs SCSS preprocessor, supporting the relative ~ import syntax and custom include paths
    • :elephant: Runs less preprocessor
    • :snake: Runs Stylus preprocessor, resolves relative paths relative to ng-package.json
    • :monkey: Adds vendor-specific prefixes w/ autoprefixer and browserslist just tell your desired .browserslistrc
    • :tiger: Embed assets data w/ postcss-url

Advanced Use Cases

Examples and Tutorials

A great step-by-step example of making an Angular CLI project with a library distributed separate from the app, by Jason Aden

Nikolas LeBlanc wrote a tutorial on building an Angular 4 Component Library with the Angular CLI and ng-packagr

Here is a demo repository showing ng-packagr and Angular CLI in action.

What about ng-packagr alongside Nx Workspace? Well, they work well together!

Configuration Locations

Configuration is picked up from the project file given by the -p CLI option. The -poption may refer to a package.json (with custom ngPackage property), an ng-package.json, or an ng-package.js file. When the -p option refers to a directory, the configuration is picked up from the first matching source; locations are tried in the above-mentioned order.

To configure with a package.json, put the configuration in the ngPackage custom property:

{
  "$schema": "./node_modules/ng-packagr/package.schema.json",
  "ngPackage": {
    "lib": {
      "entryFile": "public_api.ts"
    }
  }
}

To configure with a ng-package.json or ng-package.js, keep the library's package.json in the same folder next to ng-package.json or ng-package.js.

Example of ng-package.json:

{
  "$schema": "./node_modules/ng-packagr/ng-package.schema.json",
  "lib": {
    "entryFile": "public_api.ts"
  }
}

Example of ng-package.js:

module.exports = {
  lib: {
    entryFile: 'public_api.ts'
  }
};

Note: referencing the $schema enables JSON editing support (auto-completion for configuration) in IDEs like VSCode.

Secondary Entry Points

Besides the primary entry point, a package can contain one or more secondary entry points (e.g. @angular/core/testing, @angular/cdk/a11y, …). These contain symbols that we don't want to group together with the symbols in the main entry point. The module id of a secondary entry directs the module loader to a sub-directory by the secondary's name. For instance, @angular/core/testing resolves to a directory under node_modules/@angular/core/testing containing a package.json file that references the correct bundle files for what an application's build system is looking for.

For library developers, secondary entry points are dynamically discovered by searching for package.json files within subdirectories of the main package.json file's folder!

Tell me now, how do I use secondary entry points (a.k.a. sub-packages)?

All you have to do is create a package.json file and put it where you want a secondary entry point to be created. One way this can be done is by mimicking the folder structure of the following example which has a testing entry point in addition to its main entry point.

my_package
├── src
|   └── *.ts
├── public_api.ts
├── ng-package.json
├── package.json
└── testing
    ├── src
    |   └── *.ts
    ├── public_api.ts
    └── package.json

The contents of my_package/testing/package.json can be as simple as:

{
  "ngPackage": {}
}

No, that is not a typo. No name is required. No version is required. It's all handled for you by ng-packagr! When built, the primary entry point is imported by import {..} from '@my/library' and the secondary entry point with import {..} from '@my/library/testing'.

Changing the entry file: What if I don't like public_api.ts?

You can change the entry point file by using the ngPackage configuration field in package.json (or ng-package.json). For example, the following would use index.ts as the entry point:

{
  "ngPackage": {
    "lib": {
      "entryFile": "index.ts"
    }
  }
}

Language Level: How do I use es2016 or es2017 features in my TypeScript library?

You can change the TypeScript language level support in tsconfig by setting lib.languageLevel property in the ngPackage section: For example:

{
  "ngPackage": {
    "lib": {
      "languageLevel": ["dom", "es2017"]
    }
  }
}

How to embed assets in CSS?

You can embed assets such as font and images inside the outputted css. More information in the CSS tricks website

Valid values: none or inline.

{
  "ngPackage": {
    "lib": {
      "cssUrl": "inline"
    }
  }
}

What if I have multiple SASS/SCSS/Less/Stylus include paths?

In case you have multiple include paths for @import statements (e.g., when setting the stylePreprocessorOptions in .angular-cli.json), the additional paths may be configured through the styleIncludePaths option.

{
  "ngPackage": {
    "lib": {
      "styleIncludePaths": ["./src/assets/styles"]
    }
  }
}

By default, ng-packagr will treat dependencies as external dependencies. When writing the UMD bundle, ng-packagr does its best to provide common default values for the UMD module identifiers and rollup will also do its best to guess the module ID of an external dependency. Even then, you should make sure that the UMD module identifiers of the external dependencies are correct. In case ng-packagr doesn't provide a default and rollup is unable to guess the correct identifier, you should explicitly provide the module identifier by using umdModuleIds in the library's package file section like so:

{
  "$schema": "../../../src/ng-package.schema.json",
  "lib": {
    "umdModuleIds": {
      "lodash": "_",
      "date-fns": "DateFns"
    }
  }
}

React loves Angular, Angular loves React

What if I want to use React Components in Angular?

If you have React Components that you're using in your library, and want to use proper JSX/TSX syntax in order to construct them, you can set the jsx flag for your library through ng-package.json like so:

{
  "$schema": "../../../src/ng-package.schema.json",
  "lib": {
    "entryFile": "public_api.ts",
    "umdModuleIds": {
      "react": "React",
      "react-dom": "ReactDOM"
    },
    "jsx": "react"
  }
}

The jsx flag will accept what the corresponding tsconfig accepts, more information in the TypeScript Handbook chaper on JSX.

Note: Don't forget to include react and react-dom in umdModuleIds so that you're shipping a correct UMD bundle!

Further documentation

We keep track of user questions in GitHub's issue tracker and try to build a documentation from it. Explore issues w/ label documentation.

Knowledge

Angular Package Format v6.0, design document at Google Docs

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)

Juri Strumpflohner - Create and publish Angular libs like a Pro

Packaging Angular - Jason Aden at ng-conf 2017 (28min talk)

Packaging Angular - Jason Aden

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)

Juri Strumpflohner - Create & Publish Angular Libs like a PRO at ngVikings

@eui/deps-baseng-tongbao-coreweb-components-reekew-listkew-list-viewkew-list-view-packageticker-angular-componentng-horo@invoicecloud/headernitor-custom-login-test-two@charcode/charcode-paginatorlibs-ionicv4ng-tlab-libmallumonk-my-libmallumonk-my-lib1mallumonk-my-lib2@autovance/customer-translatorkup.common.utilserengeti-commonserengeti-marketnpm-ntk-cms-angular@produbanco/styleguide@everything-registry/sub-chunk-2274naava-coreruan-number-keyboardangular-international-phone-input-app@lutterotti/-customer-builder@lutterotti/customer-builderdiscord-countingdmy-component-librarydevicewisedharma-ui-analyticsdhanushuicolor.dove-headercolor_cat-ngx-treetablecounting-discord-jscpm-librarydd-flexboxdotcms-uidp-ui-components-angular-libngx-http-clientngx-http-client-new-ngngx-hm-dndnine-mayunified-kgcunified-kgc-v2navigaze-pannellum-reactek-stylengx-modal-hxngx-doc-gennitor-custom-login-testnitor-firebase-login-testnitor-sample-loginnitrozenucs-nota-alunoucs-notificacaoucs-menu-portalucs-modal-testedynamic-ng-zorro-form-builderuilibrary-demoui-library-penpencilupload-file-imageuserchat-libraryuserchat-library-ionicuserchat-library-ionic1userchat-library1ng2-accordionsng2-button-loaderng2-telefone-maskngu-carouselngx-alan-libng2-pdfjs-viewer-ng17ng2-preloaderngx-classificationngrx-store-libtibatoastpackage123my-map-componentmitchrw-ng-libraryng-nocng-number-formatter-supng-prac-billingng-prac-claimsng-prac-doc-viewerng-prac-documentsng-prac-policydetailsng-prac-profileng-prac-registrationng-world-map-svgng-zorro-jalali-date-pickerng-adalng-td-libng-search-dropdownng-flex-gridng-form-control-errorsngx-prosemirrorngx-radio-testngx-smart-smooth-dndtwog-cube-carouselngx-cms
18.0.0-next.2

7 days ago

18.0.0-next.3

7 days ago

18.0.0-next.1

1 month ago

18.0.0-next.0

1 month ago

17.3.0

1 month ago

17.3.0-rc.0

2 months ago

17.2.1

2 months ago

17.2.0

2 months ago

17.2.0-rc.0

3 months ago

17.2.0-next.0

3 months ago

17.1.2

3 months ago

17.1.1

3 months ago

17.1.0

3 months ago

17.1.0-next.5

4 months ago

17.1.0-next.4

4 months ago

17.0.3

4 months ago

17.1.0-next.3

4 months ago

17.1.0-next.0

5 months ago

17.1.0-next.2

5 months ago

17.1.0-next.1

5 months ago

17.0.0-rc.1

6 months ago

17.0.0-rc.0

6 months ago

17.0.0-next.2

7 months ago

17.0.2

5 months ago

17.0.1

5 months ago

17.0.0

6 months ago

17.1.0-next

5 months ago

17.0.0-next.1

8 months ago

16.2.3

8 months ago

16.2.0-next.1

10 months ago

16.2.0-next.0

10 months ago

17.0.0-next.0

8 months ago

16.1.0

11 months ago

16.1.0-rc.0

11 months ago

16.2.0

9 months ago

16.2.2

8 months ago

16.2.1

8 months ago

16.0.0-rc.0

1 year ago

16.0.0-rc.1

1 year ago

16.1.0-next.0

12 months ago

16.0.1

12 months ago

16.0.0

12 months ago

16.0.0-next.0

1 year ago

16.0.0-next.1

1 year ago

16.0.0-next.2

1 year ago

15.1.1

1 year ago

15.1.2

1 year ago

15.1.0

1 year ago

15.2.0

1 year ago

15.2.1

1 year ago

15.2.2

1 year ago

15.0.2

1 year ago

15.0.3

1 year ago

15.0.0

1 year ago

15.0.1

1 year ago

14.2.2

2 years ago

15.1.0-next.0

1 year ago

15.0.0-rc.0

2 years ago

15.0.0-next.4

2 years ago

14.2.0

2 years ago

14.2.1

2 years ago

14.3.0

2 years ago

15.0.0-next.3

2 years ago

15.0.0-next.2

2 years ago

15.0.0-next.1

2 years ago

15.0.0-next.0

2 years ago

14.1.0

2 years ago

14.1.0-next.0

2 years ago

14.0.0

2 years ago

14.0.1

2 years ago

14.0.2

2 years ago

14.0.3

2 years ago

14.0.4

2 years ago

14.0.0-next.10

2 years ago

14.0.0-rc.0

2 years ago

14.0.0-next.9

2 years ago

14.0.0-next.8

2 years ago

14.0.0-next.7

2 years ago

14.0.0-next.6

2 years ago

13.3.1

2 years ago

13.3.0

2 years ago

14.0.0-next.5

2 years ago

14.0.0-next.4

2 years ago

14.0.0-next.3

2 years ago

14.0.0-next.2

2 years ago

13.1.3

2 years ago

13.1.1

2 years ago

13.1.2

2 years ago

13.1.0

2 years ago

13.2.0

2 years ago

13.2.1

2 years ago

12.2.6

2 years ago

12.2.7

2 years ago

14.0.0-next.1

2 years ago

14.0.0-next.0

2 years ago

13.0.8

2 years ago

13.0.6

2 years ago

13.0.7

2 years ago

13.0.5

2 years ago

13.0.2

2 years ago

13.0.3

2 years ago

13.0.0

2 years ago

13.0.1

2 years ago

13.0.0-rc.3

2 years ago

12.2.5

3 years ago

12.2.4

3 years ago

13.0.0-rc.2

3 years ago

13.0.0-rc.1

3 years ago

13.0.0-next.8

3 years ago

12.2.3

3 years ago

13.0.0-rc.0

3 years ago

13.0.0-next.6

3 years ago

13.0.0-next.5

3 years ago

13.0.0-next.4

3 years ago

13.0.0-next.3

3 years ago

12.2.2

3 years ago

13.0.0-next.2

3 years ago

13.0.0-next.1

3 years ago

13.0.0-next.0

3 years ago

12.2.1

3 years ago

12.2.0

3 years ago

12.2.0-next.0

3 years ago

12.1.2

3 years ago

12.1.1

3 years ago

12.1.0

3 years ago

12.0.7

3 years ago

12.0.8

3 years ago

12.1.0-next.0

3 years ago

12.0.6

3 years ago

12.0.3

3 years ago

12.0.4

3 years ago

12.0.5

3 years ago

12.0.1

3 years ago

12.0.2

3 years ago

12.0.0

3 years ago

12.0.0-rc.3

3 years ago

12.0.0-rc.1

3 years ago

12.0.0-rc.2

3 years ago

12.0.0-rc.0

3 years ago

12.0.0-next.9

3 years ago

12.0.0-next.8

3 years ago

12.0.0-next.7

3 years ago

12.0.0-next.6

3 years ago

12.0.0-next.5

3 years ago

12.0.0-next.4

3 years ago

11.2.4

3 years ago

11.2.3

3 years ago

12.0.0-next.3

3 years ago

11.2.2

3 years ago

12.0.0-next.2

3 years ago

11.2.1

3 years ago

12.0.0-next.1

3 years ago

12.0.0-next.0

3 years ago

11.2.0

3 years ago

11.1.4

3 years ago

11.1.3

3 years ago

11.1.2

3 years ago

11.1.0

3 years ago

11.0.3

3 years ago

11.0.2

3 years ago

11.0.1

3 years ago

11.0.0

3 years ago

11.0.0-next.3

3 years ago

11.0.0-next.2

4 years ago

11.0.0-next.1

4 years ago

11.0.0-next.0

4 years ago

10.1.2

4 years ago

10.1.1

4 years ago

10.1.0

4 years ago

10.0.4

4 years ago

10.0.3

4 years ago

10.0.2

4 years ago

10.0.1

4 years ago

10.0.0

4 years ago

10.0.0-rc.2

4 years ago

10.0.0-rc.1

4 years ago

10.0.0-rc.0

4 years ago

10.0.0-next.2

4 years ago

9.1.5

4 years ago

9.1.4

4 years ago

10.0.0-next.1

4 years ago

10.0.0-next.0

4 years ago

9.1.3

4 years ago

9.1.2

4 years ago

9.1.1

4 years ago

9.1.0

4 years ago

9.0.3

4 years ago

9.0.2

4 years ago

9.0.1

4 years ago

9.0.0

4 years ago

9.0.0-rc.9

4 years ago

9.0.0-rc.8

4 years ago

9.0.0-rc.7

4 years ago

9.0.0-rc.6

4 years ago

9.0.0-rc.5

4 years ago

9.0.0-rc.4

4 years ago

9.0.0-rc.3

4 years ago

9.0.0-rc.2

4 years ago

9.0.0-rc.1

4 years ago

5.7.1

4 years ago

9.0.0-rc.0

5 years ago

5.7.0

5 years ago

5.6.1

5 years ago

5.6.0

5 years ago

5.5.1

5 years ago

5.5.0

5 years ago

5.4.3

5 years ago

5.4.2

5 years ago

5.4.1

5 years ago

5.4.0

5 years ago

5.3.0

5 years ago

5.2.0

5 years ago

5.1.0

5 years ago

5.0.1

5 years ago

5.0.0

5 years ago

4.7.1

5 years ago

4.7.0

5 years ago

4.6.0

5 years ago

4.5.0

5 years ago

4.4.5

5 years ago

4.4.0

6 years ago

4.3.1

6 years ago

4.3.0

6 years ago

4.2.0

6 years ago

4.1.1

6 years ago

4.1.0

6 years ago

3.0.6

6 years ago

4.0.1

6 years ago

4.0.0

6 years ago

3.0.5

6 years ago

4.0.0-rc.3

6 years ago

4.0.0-rc.2

6 years ago

3.0.3

6 years ago

4.0.0-rc.1

6 years ago

3.0.2

6 years ago

3.0.1

6 years ago

4.0.0-rc.0

6 years ago

3.0.0

6 years ago

2.4.5

6 years ago

3.0.0-rc.5

6 years ago

3.0.0-rc.4

6 years ago

2.4.4

6 years ago

2.4.3

6 years ago

3.0.0-rc.3

6 years ago

3.0.0-rc.2

6 years ago

3.0.0-rc.1

6 years ago

3.0.0-rc.0

6 years ago

2.4.2

6 years ago

2.4.1

6 years ago

2.4.0

6 years ago

2.3.0

6 years ago

2.2.0

6 years ago

2.1.0

6 years ago

2.0.0

6 years ago

1.7.0

6 years ago

2.0.0-rc.13

6 years ago

2.0.0-rc.12

6 years ago

2.0.0-rc.11

6 years ago

2.0.0-rc.10

6 years ago

2.0.0-rc.9

6 years ago

2.0.0-rc.8

6 years ago

2.0.0-rc.7

6 years ago

2.0.0-rc.6

6 years ago

2.0.0-rc.5

6 years ago

2.0.0-rc.4

6 years ago

2.0.0-rc.3

6 years ago

2.0.0-rc.2

6 years ago

2.0.0-rc.0

6 years ago

1.6.0

6 years ago

1.5.2

6 years ago

1.6.0-rc.0

6 years ago

1.5.1

6 years ago

1.5.0

6 years ago

1.5.0-rc.1

7 years ago

1.5.0-rc.0

7 years ago

1.4.1

7 years ago

1.4.0

7 years ago

1.3.0

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.1

7 years ago

1.0.0-pre.17

7 years ago

1.0.0-pre.16

7 years ago

1.0.0-pre.15

7 years ago

1.0.0-pre.14

7 years ago

1.0.0-pre.13

7 years ago

1.0.0-pre.12

7 years ago

1.0.0-pre.11

7 years ago

1.0.0-pre.10

7 years ago

1.0.0-pre.9

7 years ago

1.0.0-pre.8

7 years ago

1.0.0-pre.7

7 years ago

1.0.0-pre.6

7 years ago

1.0.0-pre.5

7 years ago

1.0.0-pre.4

7 years ago

1.0.0-pre.3

7 years ago

1.0.0-pre.2

7 years ago

1.0.0-pre.1

7 years ago

1.0.0-pre.0

7 years ago

1.0.0-alpha.5

7 years ago

1.0.0-alpha.4

7 years ago

1.0.0-alpha.7

7 years ago

1.0.0-alpha.6

7 years ago

1.0.0-alpha.3

7 years ago

1.0.0-alpha.2

7 years ago

1.0.0-alpha.1

7 years ago

1.0.0

7 years ago

1.0.0-alpha.0

7 years ago