Skip to content

Commit 01b1023

Browse files
committed
docs: add library creation
1 parent 24dc9b0 commit 01b1023

File tree

3 files changed

+117
-63
lines changed

3 files changed

+117
-63
lines changed

docs/documentation/stories.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
- [Application Environments](stories/application-environments)
2929
- [Autoprefixer Configuration](stories/autoprefixer)
3030
- [Deploy to GitHub Pages](stories/github-pages)
31-
- [Linked Library](stories/linked-library)
31+
- [Create a Library](stories/create-library)
3232
- [Multiple apps](stories/multiple-apps)
3333
- [Continuous Integration](stories/continuous-integration)
3434
- [Universal Rendering](stories/universal-rendering)
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Library support in Angular CLI 6
2+
3+
Angular CLI v6 comes with library support via `ng-packagr` plugged into the build system we use in
4+
Angular CLI, together with schematics for generating a library.
5+
6+
7+
## Generating a library
8+
9+
You can create a library in a existing workspace by running the following commands:
10+
11+
```
12+
ng generate library my-lib
13+
```
14+
15+
You should now have a library inside `projects/my-lib`.
16+
It contains a component and a service inside a NgModule.
17+
18+
19+
## Building your library
20+
21+
You can build this library via `ng build my-lib`, and also unit test it and lint it by replacing
22+
`build` with `test` or `lint`.
23+
24+
## Using your library inside your apps
25+
26+
Before you use your library, it's important to understand the mental model of how libraries are
27+
used in general.
28+
29+
When you want to use a library from `npm`, you must:
30+
31+
- install the library into node_modules via `npm install library-name`
32+
- import it in your application by name `import { something } from 'library-name';`
33+
34+
This works because importing a library in Angular CLI looks for a mapping between library name
35+
and location on disk.
36+
37+
Angular CLI first looks in your tsconfig paths, then in the node_modules folder.
38+
39+
When you build your own libraries it doesn't go into node_modules so we use the tsconfig paths
40+
to tell the build system where it is.
41+
Generating a library automatically adds its path to the tsconfig file.
42+
43+
Using your own library follows a similar pattern:
44+
45+
- build the library via `ng build my-lib`
46+
- import it in your application by name `import { something } from 'my-lib';`
47+
48+
It's important to note that your app can never use your library before it is built.
49+
50+
For instance, if you clone your git repository and run `npm install`, your editor will show
51+
the `my-lib` imports as missing.
52+
This is because you haven't yet built your library.
53+
54+
Another common problem is changes to your library not being reflected in your app.
55+
This is often because your app is using an old build of your library.
56+
If this happens just rebuild your library.
57+
58+
59+
## Publishing your library
60+
61+
To publish your library follow these steps:
62+
63+
```
64+
ng build my-lib --prod
65+
cd dist/my-lib
66+
npm publish
67+
```
68+
69+
If you've never published a package in npm before, you will need to create a user account.
70+
You can read more about publishing on npm here:
71+
https://docs.npmjs.com/getting-started/publishing-npm-packages
72+
73+
The `--prod` flag should be used when building to publish because it will completely clean the build
74+
directory for the library beforehand, removing old code leftover code from previous versions.
75+
76+
77+
## Why do I need to build the library everytime I make changes?
78+
79+
Running `ng build my-lib` every time you change a file is bothersome and takes time.
80+
81+
Some similar setups instead add the path to the source code directly inside tsconfig.
82+
This makes seeing changes in your app faster.
83+
84+
But doing that is risky.
85+
When you do that, the build system for your app is building the library as well.
86+
But your library is built using a different build system than your app.
87+
88+
Those two build systems can build things slightly different, or support completely different
89+
features.
90+
91+
This leads to subtle bugs where your published library behaves differently from the one in your
92+
development setup.
93+
94+
For this reason we decided to err on the side of caution, and make the recommended usage
95+
the safe one.
96+
97+
In the future we want to add watch support to building libraries so it is faster to see changes.
98+
99+
We are also planning to add internal dependency support to Angular CLI.
100+
This means that Angular CLI would know your app depends on your library, and automatically rebuilds
101+
the library when the app needs it.
102+
103+
104+
## Note for upgraded projects
105+
106+
If you are using an upgraded project, there are some additional changes you have to make to support
107+
monorepo (a workspace with multiple projects) setups:
108+
109+
- in `angular.json`, change the `outputPath` to `dist/project-name` for your app
110+
- remove `baseUrl` in `src/tsconfig.app.json` and `src/tsconfig.spec.json`
111+
- add `"baseUrl": "./"` in `./tsconfig.json`
112+
- change any absolute path imports in your app to be absolute from the root (including `src/`),
113+
or make them relative
114+
115+
This is necessary to support multiple projects in builds and in your editor.
116+
New projects come with this configuration by default.

docs/documentation/stories/linked-library.md

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)