initial project from: https://github.com/robisim74/angular-library-starter
Build an Angular library compatible with AoT compilation & Tree shaking like an official package.
This starter allows you to create a library for Angular v5 apps written in TypeScript, ES6 or ES5. The project is based on the official Angular packages.
Get the Changelog.
Update Node & npm.
Rename angular-library-starter
and angularLibraryStarter
everywhere to my-library
and myLibrary
.
Customize the license-banner.txt
file with your library license.
Update in package.json
file:
and run npm install
.
Create your classes in src
folder, and export public classes in my-library.ts
: if you have components, note that this starter supports only inline templates & styles as the official Angular building process.
You can create only one module for the whole library: I suggest you create different modules for different functions, so that the host app can only import the modules it uses, and optimize its Tree shaking.
Update in rollup.config.js
file globals
external dependencies with those that actually you use to build the umd bundle.
Create unit & integration tests in tests
folder, or unit tests next to the things they test in src
folder, always using .spec.ts
extension: note that Karma is configured to use webpack only for *.ts
files.
The following command runs unit & integration tests that are in the tests
folder (you can change the folder in spec.bundle.js
file):
npm test
It also reports coverage using Istanbul.
The following command:
npm run build
dist
folder with all the files of distribution, following Angular Package Format (APF) v5.0:
└── dist
├── bundles
| ├── my-library.umd.js
| ├── my-library.umd.js.map
| ├── my-library.umd.min.js
| └── my-library.umd.min.js.map
├── esm5
| ├── my-library.js
| └── my-library.js.map
├── esm2015
| ├── my-library.js
| └── my-library.js.map
├── src
| └── **/*.d.ts
├── my-library.d.ts
├── my-library.metadata.json
├── LICENSE
├── package.json
├── public_api.d.ts
└── README
To test locally the npm package before publishing:
npm run pack:lib
Then you can install it in an app to test it:
npm install [path]my-library-[version].tgz
Before publishing the first time:
.travis.yml
filenpm run publish:lib
To generate the documentation, this starter uses compodoc:
npm run compodoc
npm run compodoc:serve
npm install my-library --save
System.config({
map: {
'my-library': 'node_modules/my-library/bundles/my-library.umd.js'
}
});
No need to set up anything, just import it in your code.
No need to set up anything, just import it in your code.
Include the umd
bundle in your index.html
:
<script src="https://github.com/nie-ine/npm-package/raw/devel/node_modules/my-library/bundles/my-library.umd.js"></script>
and use global ng.myLibrary
namespace.
The library is compatible with AoT compilation.
package.json
"main": "./bundles/angular-library-starter.umd.js"
legacy module format "module": "./esm5/angular-library-starter.js"
flat ES module, for using module bundlers such as Rollup or webpack:
package module"es2015": "./esm2015/angular-library-starter.js"
ES2015 flat ESM format, experimental ES2015 build"peerDependencies"
the packages and their versions required by the library when it will be installedtsconfig.json
file used by TypeScript compiler
"strict": true
enables TypeScript strict
master optiontsconfig-build.json
file used by ngc compiler
Compiler options:
"declaration": true
to emit TypeScript declaration files"module": "es2015"
& "target": "es2015"
are used by Rollup to create the ES2015 bundleAngular Compiler Options:
"skipTemplateCodegen": true,
skips generating AoT files"annotateForClosureCompiler": true
for compatibility with Google Closure compiler"strictMetadataEmit": true
without emitting metadata files, the library will not be compatible with AoT compilation: it is intended to report syntax errors immediately rather than produce a .metadata.json file with errorsrollup.config.js
file used by Rollup
format: 'umd'
the Universal Module Definition pattern is used by Angular for its bundlesmoduleName: 'ng.angularLibraryStarter'
defines the global namespace used by JavaScript appsexternal
& globals
declare the external packagesServer Side Rendering
If you want the library will be compatible with Server Side Rendering:
window
, document
, navigator
and other browser types do not exist on the serverFrom Angular Package Format v5.0:
Component libraries are typically implemented using stylesheets and html templates stored in separate files. While it's not required, we suggest that component authors inline the templates and stylesheets into their FESM files as well as *.metadata.json files by replacing the stylesheetUrls and templateUrl with stylesheets and template metadata properties respectively. This simplifies consumption of the components by application developers.
ngc compiler still does not support inlining of templates & styles. But if you want, you can follow this suggestion: Inlining of templates and stylesheets.
MIT