UNPKG

12.5 kBMarkdownView Raw
1# `angular-library-seed` - the starter for Angular libraries
2
3[![Build Status](https://travis-ci.org/trekhleb/angular-library-seed.svg?branch=master)](https://travis-ci.org/trekhleb/angular-library-seed)
4[![codecov](https://codecov.io/gh/trekhleb/angular-library-seed/branch/master/graph/badge.svg)](https://codecov.io/gh/trekhleb/angular-library-seed)
5[![npm version](https://badge.fury.io/js/angular-library-seed.svg)](https://badge.fury.io/js/angular-library-seed)
6
7> Seed project for [Angular](https://angular.io/) libraries that are [AOT](https://angular.io/docs/ts/latest/cookbook/aot-compiler.html)/[JIT](https://angular.io/docs/ts/latest/cookbook/aot-compiler.html) compatible and that use external [SCSS](http://sass-lang.com/)-styles and HTML-templates.
8
9This project contains TickTock library example. The library itself is small and the only thing it does is [displaying current time](http://embed.plnkr.co/VbO1hldrCfF6ITG6VvGG/) (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.
10
11> [Read more](https://medium.com/@trekhleb/how-to-create-aot-jit-compatible-angular-4-library-with-external-scss-html-templates-9da6e68dac6e) about architectural **challenges** and **solutions** used in this repository.
12
13# You might find this project helpful if
14- You want to create library for **Angular 4**.
15- You want your library to be ready for further **AOT** or **JIT** compilation.
16- You want your library to be ready for further usage **directly in browsers** (let's say [UMD](https://github.com/umdjs/umd) bundle loaded by [SystemJS](https://github.com/systemjs/systemjs)).
17- You want to write component styles in **external SCSS files**.
18- You want to write component templates in **external HTML files**.
19- You want to have **watch-mode** for library builds (including AOT build).
20
21# Main Features
22- **AOT/JIT** compatible library build via [Angular Compiler](https://www.npmjs.com/package/@angular/compiler-cli) (ngc).
23- **UMD** build via [Webpack](https://webpack.js.org/) that allows you to use your library for browser builds. You may play with it on [Plunker](http://embed.plnkr.co/VbO1hldrCfF6ITG6VvGG/).
24- **Testing** with [Karma](https://karma-runner.github.io/1.0/index.html) and [Jasmine](https://jasmine.github.io/).
25- **Test coverage** report via [Istanbul](https://github.com/gotwarlost/istanbul).
26- **Watch modes** for building and testing procedures that makes developing process easier.
27- **Linting** with [TSLint](https://palantir.github.io/tslint/) and [Codelyzer](https://github.com/mgechev/codelyzer) for static code analysis.
28- **Exploring your build** via [Sourcemap Explorer](https://www.npmjs.com/package/source-map-explorer) that shows you a treemap visualization to help you debug where all the code is coming from.
29- **Documentation generation** via [Compodoc](https://github.com/compodoc/compodoc). Take a look at [documentation example](https://trekhleb.github.io/angular-library-seed/).
30- **Documentation hosting** via [GitHub Pages](https://pages.github.com/).
31- **AOT/JIT/UMD demos** via [Webpack](https://webpack.js.org/) and [SystemJS](https://github.com/systemjs/systemjs) that allows you to test library builds.
32- **Continuous integration** with [Travis CI](https://travis-ci.org/).
33- **Code coverage** badge via [Codecov](https://codecov.io) as a reminder to cover code with tests.
34
35# Quick Start
36
37```bash
38# Clone the repository
39git clone https://github.com/trekhleb/angular-library-seed.git
40
41# Go to repository folder
42cd angular-library-seed
43
44# Install all dependencies
45yarn install
46
47# Build the library
48yarn build
49```
50
51# File Structure
52
53```
54angular-library-seed
55 ├─ demo * Folder for demo applications (MAY BE DELETED if not required)
56 | ├─ esm * AOT/JIT demo project
57 | ├─ umd * UMD demo project
58 | └─ ... * More details about this folder may be found in demo folder README file.
59 |
60 ├─ src * Library sources home folder (THE PLACE FOR YOUR LIBRARY SOURCES)
61 | ├─ components * Example of library components with tests
62 | ├─ services * Example of library services with tests
63 | ├─ index.ts * Library entry point that is used by builders
64 | └─ tick-tock.module.ts * Example of library module
65 |
66 ├─ .editorconfig * Common IDE configuration
67 ├─ .gitignore * List of files that are ignored while publishing to git repo
68 ├─ .npmignore * List of files that are ignored while publishing to npm
69 ├─ .travis.yml * Travic CI configuration
70 ├─ LICENSE * License details
71 ├─ README.md * README for you library
72 ├─ gulpfile.js * Gulp helper scripts
73 ├─ karma-test-entry.ts * Entry script for Karma tests
74 ├─ karma.conf.ts * Karma configuration for our unit tests
75 ├─ package.json * NPM dependencies, scripts and package configuration
76 ├─ tsconfig-aot.json * TypeScript configuration for AOT build
77 ├─ tsconfig.json * TypeScript configuration for UMD and Test builds
78 ├─ tslint.json * TypeScript linting configuration
79 ├─ webpack-test.config.ts * Webpack configuration for building test version of the library
80 ├─ webpack-umd.config.ts * Webpack configuration for building UMD bundle
81 └─ yarn.lock * Yarn lock file that locks dependency versions
82```
83
84# Getting Started
85
86## Dependencies
87
88#### Node/NPM
89Install latest Node and NPM following the [instructions](https://nodejs.org/en/download/). Make sure you have Node version ≥ 7.0 and NPM ≥ 4.
90
91- `brew install node` for Mac.
92
93#### Yarn
94[Yarn package manager](https://yarnpkg.com/en/) is optional but highly recommended. If you prefer to work with `npm` directly you may ignore this step.
95
96Yarn installs library dependencies faster and also locks theirs versions. It has [more advantages](https://yarnpkg.com/en/) but these two are already pretty attractive.
97
98Install Yarn by following the [instructions](https://yarnpkg.com/en/docs/install).
99
100- `brew install yarn` for Mac.
101
102## Installing
103- `fork` this repository.
104- `clone` your fork to your local environment.
105- `yarn install` to install required dependencies (or `npm i`).
106
107## Replace `TickTock` library with your own library
108This step may be optional at first since you might just want to play with existing library example.
109
110Once you're ready to develop your own library you should do the following.
111- Check and re-configure `package.json` fields like `name`, `version`, `keywords`, `description` etc. You may read about specifics of npm's [package.json handling](https://docs.npmjs.com/files/package.json) to do that.
112- Replace the content of `src` folder with your library sources. Your library must have `index.ts` file as an entry point for further building.
113- Update `demo` sources to make them consume your library in case if you want to keep the demo folder.
114
115## Build the library
116- `yarn build` for building the library once (both ESM and AOT versions).
117- `yarn build:watch` for building the library (both ESM and AOT versions) and watch for file changes.
118
119You may also build UMD bundle and ESM files separately:
120- `yarn build:esm` - for building AOT/JIT compatible versions of files.
121- `yarn build:esm:watch` - the same as previous command but in watch-mode.
122- `yarn build:umd` - for building UMD bundle only.
123- `yarn build:umd:watch` - the same as previous command but in watch-mode.
124
125## Other commands
126
127#### Lint the code
128- `yarn lint` for performing static code analysis.
129
130#### Test the library
131- `yarn test` for running all your `*.spec.ts` tests once. Generated code coverage report may be found in `coverage` folder.
132- `yarn test:watch` for running all you `*.spec.ts` and watch for file changes.
133
134#### Generate documentation
135- `yarn docs` for generating documentation locally.
136- `yarn gh-pages` for generating documentation and uploading it to GitHub Pages. [Documentation example](https://trekhleb.github.io/angular-library-seed/).
137
138#### Explore the bundle
139- `yarn explorer` to find out where all your code in bundle is coming from.
140
141#### Bump library version
142- `npm version patch` to increase library version. [More on bumping](https://docs.npmjs.com/cli/version).
143
144`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.
145
146#### Publish library to NPM
147- `npm publish` to publish your library sources on [npmjs.com](https://www.npmjs.com/). Once the library is published it will be [available for usage](https://www.npmjs.com/package/angular-library-seed) in npm packages.
148
149`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.
150
151#### Cleaning
152- `yarn clean:tmp` command will clean up all temporary files like `docs`, `dist`, `coverage` etc.
153- `yarn clean:all` command will clean up all temporary files along with `node_modules` folder.
154
155# Library development workflow
156
157In 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.
158
159There are several ways to go here:
160- Use your **real library-consumer project** and link your library to it via `yarn link` command (see below).
161- Use [demo applications](https://github.com/trekhleb/angular-library-seed/tree/master/demo) that are provided for your convenience as a part of this repository.
162- Use [Angular-CLI](https://cli.angular.io/) to generate library-consumer project for you and then use `yarn link` to link your library to it.
163
164### Using demo applications
165
166You may take advantage of watch-modes for both library build and [demo-projects](https://github.com/trekhleb/angular-library-seed/tree/master/demo) builds in order to see changes to your library's source code immediately in your browser.
167
168To do so you need to:
1691. Open two console instances.
1702. Launch library build in watch mode in first console instance by running `yarn build:watch` (assuming that you're in `angular-library-seed` root folder).
1713. Launch demo project build (JIT version) in watch-mode by running `yarn start` in second console instance (assuming that you're in `angular-library-seed/demo` folder).
172
173As a result once you change library source code it will be automatically re-compiled and in turn your JIT demo-project will be automatically re-built and you will be able to see that changes in your browser instantly.
174
175For more details about demo projects, their folder structure and npm commands please take a look at [demo projects README](https://github.com/trekhleb/angular-library-seed/tree/master/demo).
176
177### Using `yarn link`
178
179In you library root folder:
180
181```bash
182# Create symbolic link
183yarn link
184
185# Build library in watch mode
186yarn build:watch
187```
188
189In you project folder that should consume the library:
190
191```bash
192# Link you library to the project
193yarn link "angular-library-seed"
194
195# Build your project. In case of Angular-CLI use the following command.
196ng serve --aot
197```
198
199Then you need to import your library into your project's source code.
200
201Now, 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.
202
203[More information](https://yarnpkg.com/en/docs/cli/link) about `yarn link` command.
204
205> At the moment of publishing this project there is a [bug](https://github.com/angular/angular-cli/issues/3854) exists when using `yarn link` in combination with Angular CLI. The issue is caused by having `node_modules` folder inside linked library. There is a [workaround](https://github.com/angular/angular-cli/issues/3854#issuecomment-274344771) has been provided that suggests to add a `paths` property with all Angular dependencies to the `tsconfig.json` file of the Angular CLI project like so: `"paths": { "@angular/*": ["../node_modules/@angular/*"] }`