๐พ Seed project for Angular libraries that are AOT/JIT compatible and that use external SCSS-styles and HTML-templates
https://github.com/trekhleb/angular-library-seed.git
angular-library-seed - the starter for Angular librariesSeed project for Angular libraries that are AOT/JIT compatible and that use external SCSS-styles and HTML-templates.
This project contains TickTock library example. The library itself is small and the only thing it does is displaying current time (Plunker example). But what most important is that the project contains reusable environment for the libraries that allows to build, test, lint, document, explore and publish them.
Read more about architectural challenges and solutions used in this repository.
# Clone the repository
git clone https://github.com/trekhleb/angular-library-seed.git
# Go to repository folder
cd angular-library-seed
# Install all dependencies
yarn install
# Build the library
yarn build
angular-library-seed
โโ demo * Folder for demo applications (MAY BE DELETED if not required)
| โโ esm * AOT/JIT demo project
| โโ umd * UMD demo project
| โโ ... * More details about this folder may be found in demo folder README file.
|
โโ src * Library sources home folder (THE PLACE FOR YOUR LIBRARY SOURCES)
| โโ components * Example of library components with tests
| โโ services * Example of library services with tests
| โโ index.ts * Library entry point that is used by builders
| โโ tick-tock.module.ts * Example of library module
|
โโ .editorconfig * Common IDE configuration
โโ .gitignore * List of files that are ignored while publishing to git repo
โโ .npmignore * List of files that are ignored while publishing to npm
โโ .travis.yml * Travic CI configuration
โโ LICENSE * License details
โโ README.md * README for you library
โโ gulpfile.js * Gulp helper scripts
โโ karma-test-entry.ts * Entry script for Karma tests
โโ karma.conf.ts * Karma configuration for our unit tests
โโ package.json * NPM dependencies, scripts and package configuration
โโ tsconfig-aot.json * TypeScript configuration for AOT build
โโ tsconfig.json * TypeScript configuration for UMD and Test builds
โโ tslint.json * TypeScript linting configuration
โโ webpack-test.config.ts * Webpack configuration for building test version of the library
โโ webpack-umd.config.ts * Webpack configuration for building UMD bundle
โโ yarn.lock * Yarn lock file that locks dependency versions
brew install node for Mac.npm directly you may ignore this step.
Yarn installs library dependencies faster and also locks theirs versions. It has more advantages but these two are already pretty attractive.
Install Yarn by following the instructions.
brew install yarn for Mac.fork this repository.clone your fork to your local environment.yarn install to install required dependencies (or npm i).TickTock library with your own libraryOnce you're ready to develop your own library you should do the following.
package.json fields like name, version, keywords, description etc. You may read about specifics of npm's package.json handling to do that.src folder with your library sources. Your library must have index.ts file as an entry point for further building.demo sources to make them consume your library in case if you want to keep the demo folder.yarn build for building the library once (both ESM and AOT versions).yarn build:watch for building the library (both ESM and AOT versions) and watch for file changes.yarn build:esm - for building AOT/JIT compatible versions of files.yarn build:esm:watch - the same as previous command but in watch-mode.yarn build:umd - for building UMD bundle only.yarn build:umd:watch - the same as previous command but in watch-mode.yarn lint for performing static code analysis.yarn test for running all your *.spec.ts tests once. Generated code coverage report may be found in coverage folder.yarn test:watch for running all you *.spec.ts and watch for file changes.yarn docs for generating documentation locally.yarn gh-pages for generating documentation and uploading it to GitHub Pages. Documentation example.yarn explorer to find out where all your code in bundle is coming from.npm version patch to increase library version. More on bumping.preversion script in this case will automatically run project testing and linting in prior in order to check that the library is ready for publishing.
npm publish to publish your library sources on npmjs.com. Once the library is published it will be available for usage in npm packages.prepublishOnly script in this case will automatically run project testing and linting in prior in order to check that the library is ready for publishing.
yarn clean:tmp command will clean up all temporary files like docs, dist, coverage etc.yarn clean:all command will clean up all temporary files along with node_modules folder. In order to debug your library in browser you need to have Angular project that will consume your library, build the application and display it. For your convenience all of that should happen automatically in background so once you change library source code you should instantly see the changes in browser.
There are several ways to go here:
yarn link command (see below).yarn link to link your library to it.You may take advantage of watch-modes for both library build and demo-projects builds in order to see changes to your library's source code immediately in your browser.
To do so you need to:
yarn build:watch (assuming that you're in angular-library-seed root folder).yarn start in second console instance (assuming that you're in angular-library-seed/demo folder).For more details about demo projects, their folder structure and npm commands please take a look at demo projects README.
yarn linkIn you library root folder:
# Create symbolic link
yarn link
# Build library in watch mode
yarn build:watch
In you project folder that should consume the library:
# Link you library to the project
yarn link "angular-library-seed"
# Build your project. In case of Angular-CLI use the following command.
ng serve --aot
Then you need to import your library into your project's source code.
Now, once you update your library source code it will automatically be re-compiled and your project will be re-built so you may see library changes instantly.
More information about yarn link command.
At the moment of publishing this project there is a bug exists when usingyarn linkin combination with Angular CLI. The issue is caused by havingnode_modulesfolder inside linked library. There is a workaround has been provided that suggests to add apathsproperty with all Angular dependencies to thetsconfig.jsonfile of the Angular CLI project like it is shown below:
{
"compilerOptions": {
"paths": { "@angular/*": ["../node_modules/@angular/*"] }
}
}