Skip to content

Commit 1156bef

Browse files
filipesilvasumitarora
authored andcommitted
docs: add linked library story (#6292)
Blocked on #6276.
1 parent 5cac1f0 commit 1156bef

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

docs/documentation/stories.md

+1
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@
2626
- [Application Environments](stories/application-environments)
2727
- [Autoprefixer Configuration](stories/autoprefixer)
2828
- [Deploy to GitHub Pages](stories/githug-pages)
29+
- [Linked Library](stories/linked-library)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Linked libraries
2+
3+
While working on a library, it is common to use [npm link](https://docs.npmjs.com/cli/link) to
4+
avoid reinstalling the library on every build.
5+
6+
While this is very useful there are a few caveats to keep in mind.
7+
8+
## The library needs to be AOT compatible
9+
10+
Angular CLI does static analysis even without the `--aot` flag in order to detect lazy-loade routes.
11+
If your library is not AOT compatible, you will likely get a static analysis error.
12+
13+
## The library still needs to be rebuilt on every change
14+
15+
Angular libraries are usually built using TypeScript and thus require to be built before they
16+
are published.
17+
For simple cases, a linked library might work even without a build step, but this is the exception
18+
rather than the norm.
19+
20+
If a library is not being built using its own build step, then it is being compiled by the
21+
Angular CLI build system and there is no guarantee that it will be correctly built.
22+
Even if it works on development it might not work when deployed.
23+
24+
When linking a library remember to have your build step running in watch mode and the library's
25+
`package.json` pointing at the correct entry points (e.g. 'main' should point at a `.js` file, not
26+
a `.ts` file).
27+
28+
## Use TypesScript path mapping for Peer Dependencies
29+
30+
Angular libraries should list all `@angular/*` dependencies as
31+
[Peer Dependencies](https://nodejs.org/en/blog/npm/peer-dependencies/).
32+
This insures that, when modules ask for Angular, they all get the exact same module.
33+
If a library lists `@angular/core` in `dependencies` instead of `peerDependencies` then it might
34+
get a *different* Angular module instead, which will cause your application to break.
35+
36+
While developing a library, you'll need to have all of your peer dependencies also installed
37+
via `devDependencies` - otherwise you could not compile.
38+
A linked library will then have it's own set of Angular libraries that it is using for building,
39+
located in it's `node_modules` folder.
40+
This can cause problems while building or running your application.
41+
42+
To get around this problem you can use the TypeScript
43+
[path mapping](https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping).
44+
With it, you can tell TypeScript that it should load some modules from a specific location.
45+
46+
You should list all the peer dependencies that your library uses in `./tsconfig.json`, pointing
47+
them at the local copy in the apps `node_modules` folder.
48+
This ensures that you all will always load the local copies of the modules your library asks for.
49+
50+
```
51+
{
52+
"compilerOptions": {
53+
// ...
54+
// Note: these paths are relative to `baseUrl` path.
55+
"paths": {
56+
"@angular/*": [
57+
"../node_modules/@angular/*"
58+
]
59+
}
60+
}
61+
}
62+
```

0 commit comments

Comments
 (0)