Giter VIP home page Giter VIP logo

ng-packagr's Introduction

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

For publishing your Angular library, create a package.json file and add the custom ngPackage property:

{
  "$schema": "./node_modules/ng-packagr/package.schema.json",
  "name": "@my/foo",
  "version": "1.0.0",
  "ngPackage": {
    "lib": {
      "entryFile": "public_api.ts"
    }
  }
}

Paths in the ngPackage configuration are resolved relative to the location of the package.json file. You should use a npm/yarn script to run ng-packagr:

{
  "scripts": {
    "build": "ng-packagr -p ng-package.json"
  }
}

Now, build with the following command:

$ yarn build

You like to publish more libraries to npm? Create one package.json per npm package, run ng-packagr for each!

Features

  • ๐ŸŽ Implements Angular Package Format
    • ๐Ÿ Bundles your library in FESM2015, FESM5, and UMD formats
    • ๐ŸŽ’ npm package can be consumed by Angular CLI, Webpack, or SystemJS
    • ๐Ÿ’ƒ Creates type definitions (.d.ts)
    • ๐Ÿƒ Generates Ahead-of-Time metadata (.metadata.json)
    • ๐Ÿ† 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
    • ๐Ÿ˜ Runs less preprocessor
    • ๐Ÿ Runs Stylus preprocessor, resolves relative paths relative to ng-package.json
    • ๐Ÿ’ Adds vendor-specific prefixes w/ autoprefixer and browserslist โ€” just tell your desired .browserslistrc

Advanced Use Cases

Examples and Tutorials

Nikolas LeBlanc's story on medium.com: 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 cli -p parameter, then from the default location for ng-package.json, then from package.json.

To configure with a ng-package.json, put the package.json of the library in the same folder next to the ng-package.json. Contents of ng-package.json are for example:

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

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"
    }
  }
}

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

Secondary Entry Points

Beside 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. 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 directs the loader to the correct location for what it's looking for.

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

So how do I use secondary entry points (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 the secondary 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 is imported with @my/library and the secondary entry with @my/library/testing.

What if I don't like public_api.ts?

You can change the entry point file by using the ngPackage configuration field in your secondary package.json. For example, the following would use index.ts as the secondary entry point:

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

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",
    "externals": {
      "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 your externals so that you're not bundling those dependencies!

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 v4.0, design document at Google Docs

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

Packaging Angular - Jason Aden

ng-packagr's People

Contributors

aitboudad avatar alexkpek avatar avatsaev avatar crowmagnumb avatar damiendube avatar davidenke avatar davidparks8 avatar ddeis avatar dherges avatar greenkeeper[bot] avatar joostk avatar kreuzerk avatar kylebastien avatar msusur avatar renovate[bot] avatar sibiraj-s avatar tallyb avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.