Build a library compatible with Angular, AoT compilation & Tree shaking.
This starter allows you to create a library for Angular 2+ apps written in TypeScript, ES6 or ES5. The project is based on the official Angular modules.
Get the Changelog.
- 1 Project structure
- 2 Customizing
- 3 Unit testing
- 4 Building
- 5 Publishing
- 6 Using the library
- 7 What it is important to know
- Library:
- src folder for the classes
- index.ts entry point for all public APIs of the package
- package.json npm options
- rollup.config.js Rollup configuration for building the bundle
- tsconfig-build.json ngc compiler options for AoT compilation
- build.js commands to build the library using ShellJS
- Unit testing:
- tests folder for unit testing
- karma.conf.js Karma configuration that uses webpack
- spec.bundle.js defines the files used by webpack
- tsconfig.json TypeScript compiler options
- Extra:
- tslint.json TypeScript linter rules with Codelyzer
- travis.yml Travis CI configuration
-
Update Node & npm.
-
Rename
angular-library-startereverywhere tomy-library. -
Update in
package.jsonfile:- version: Semantic Versioning
- description
- urls
- packages
and run
npm install. -
Create your classes in
srcfolder, and export public classes inmy-library.ts. -
You can create only one module for the whole library: I suggest you create different modules for different functions, so that the user can import only those he needs and optimize Tree shaking of his app.
-
Update in
rollup.config.jsfileexternal&globalslibraries with those that actually you use. -
Create unit tests in
testsfolder. Karma is configured to use webpack only for*.tsfiles: if you need to test different formats, you have to update it.
npm test The following command:
npm run build- starts TSLint with Codelyzer
- starts AoT compilation using ngc compiler
- creates
umdbundle using Rollup - minimizes
umdbundle using UglifyJS - creates
distfolder with all the files of distribution
To test locally the npm package:
npm run pack-libThen you can install it in an app to test it:
npm install [path]my-library-[version].tgzBefore publishing the first time:
- you can register your library on Travis CI: you have already configured
.travis.ymlfile - you must have a user on the npm registry: Publishing npm packages
npm run publish-libnpm 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.
The library is compatible with AoT compilation, just import it in your code.
Include the umd bundle in your index.html:
<script src="node_modules/my-library/bundles/my-library.umd.js"></script>and use global ng.my-library namespace.
-
package.json"module": "index.js"to useimport&exportwith ES2015 module bundlers"peerDependencies"the packages and their versions required by the library when it will be installed
-
tsconfig-build.jsonfile used by ngc compiler-
Compiler options:
"declaration": trueto emit TypeScript declaration files"module": "es2015"for compatibility with AoT compilation & Tree shaking"target": "es5"for browsers compatibility
-
Angular Compiler Options:
"genDir": "aot"generates folder for compiled files"annotateForClosureCompiler": truefor compatibility with Google Closure compiler"strictMetadataEmit": truewithout emitting metadata files, the library will not compatible with AoT compilation
-
-
rollup.config.jsfile used to build the bundleformat: 'umd'the Universal Module Definition pattern is used by Angular for its bundlesmoduleName: 'ng.angular-library-starter'defines the global namespace used by JavaScript appsexternal&globalsdeclare the external packages
##License MIT