This project's aim is to store reusable, versionable components that can be packaged and shared (privately) via NPM.
Install ng-packager
-
npm install
(it's in package.json otherwisenpm install ng-packagr --save-dev
) - It's already declared as
adevDependency
- cd to root and use the CLI to initialise a new module:
-
ng generate module modules/<your_MODULE_name>
- Creates:
- header.component.css
- header.component.html
- header.component.spec.ts
- header.component.ts
- header.module.ts
- Create the component (a module can have multiple components!)
ng generate component modules/<your_MODULE_name>/<your_COMPONENT_name>
- Open the module ts file
<your_MODULE_name>.module.ts
and add an exports array to the@NgModule
block:exports: [ YourComponentName ]
-
Important! CD into the module's folder and configure the
scope
so that the package will be private (otherwise anyone can download via npm!)npm init --scope=appscatter
It is vital that you leave the name as is (the@appscatter/
marks it as private) - Make sure your module can be exported in
public_api.ts
:export * from './src/app/modules/<your_MODULE_name>/<your_COMPONENT_name>.module'
- Package the modules/components:
npm run packagr
(run in root)
- This builds packages into the
/dist
directory. Packages here are fully self sufficient & packaged according to best practices; they can be used as per any typicalnpm install abc-123
package!
- cd to
/dist
and runnpm pack
. This will create<your_COMPONENT_name>-0.0.0.tgz
. (The0.0.0
part comes from the component's package.json).
- If not logged in to NPM via terminal, do so:
npm login
- Publish it!
npm publish
Root level ng-package.json
configures n-packagr, specifically where to find public_api.ts
{ "$schema": "./node_modules/ng-packagr/ng-package.schema.json", "lib": { "entryFile": "public_api.ts" } }
Root level package.json
contains a packager script that we can use to tell ng-packagr to package our libraries according to the configuration of ng-package.json.Note that private
is set to false
to allowing library publication.
"scripts": { ... "packagr": "ng-packagr -p ng-package.json" }, "private": false
This project was generated with Angular CLI version 6.0.8.
Run ng serve
for a dev server. Navigate to http://localhost:4200/
. The app will automatically reload if you change any of the source files.
Run ng generate component component-name
to generate a new component. You can also use ng generate directive|pipe|service|class|guard|interface|enum|module
.
Run ng build
to build the project. The build artifacts will be stored in the dist/
directory. Use the --prod
flag for a production build.
Run ng test
to execute the unit tests via Karma.
Run ng e2e
to execute the end-to-end tests via Protractor.
To get more help on the Angular CLI use ng help
or go check out the Angular CLI README.