Skip to content

Commit 3c00296

Browse files
committed
chore: format
1 parent fd585f6 commit 3c00296

14 files changed

+19805
-3002
lines changed

.prettierignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lib/
2+
*.tsbuildinfo

.prettierrc.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
trailingComma: all
2+
tabWidth: 2
3+
semi: true
4+
singleQuote: false
5+
bracketSpacing: true
6+
printWidth: 120
7+
arrowParens: avoid

README.md

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# How to build TypeScript mono-repo project
2+
3+
## Tools
4+
5+
- npm (v7 or later)
6+
- Lerna: Multiple packages management tool.
7+
8+
## Directory Structure
9+
10+
Put each package under the `packages` directory.
11+
12+
```
13+
.
14+
├── node_modules/
15+
├── README.md
16+
├── lerna.json
17+
├── package-lock.json
18+
├── package.json
19+
├── packages
20+
│   ├── x-cli
21+
│   │   ├── lib
22+
│   │   │   ├── cli.d.ts
23+
│   │   │   ├── cli.js
24+
│   │   │   ├── cli.js.map
25+
│   │   │   ├── main.d.ts
26+
│   │   │   ├── main.js
27+
│   │   │   └── main.js.map
28+
│   │   ├── package.json
29+
│   │   ├── src
30+
│   │   │   ├── cli.ts
31+
│   │   │   └── main.ts
32+
│   │   └── tsconfig.json
33+
│   └── x-core
34+
│   ├── lib
35+
│   │   ├── index.d.ts
36+
│   │   ├── index.js
37+
│   │   └── index.js.map
38+
│   ├── package.json
39+
│   ├── src
40+
│   │   └── index.ts
41+
│   └── tsconfig.json
42+
├── tsconfig.build.json
43+
└── tsconfig.json
44+
```
45+
46+
## Workspaces
47+
48+
Using [NPM workspace feature](https://github.com/npm/rfcs/blob/latest/implemented/0026-workspaces.md), configure the following files:
49+
50+
- /package.json
51+
52+
Append the `workspaces` key.
53+
54+
```json
55+
{
56+
"private": true,
57+
"workspaces": ["packages/*"]
58+
}
59+
```
60+
61+
- lerna.json
62+
63+
Set `npmClient` `"yarn"` and turn `useWorkspaces` on.
64+
65+
```json
66+
{
67+
"lerna": "2.2.0",
68+
"packages": ["packages/*"],
69+
"npmClient": "yarn",
70+
"useWorkspaces": true,
71+
"version": "1.0.0"
72+
}
73+
```
74+
75+
Exec `yarn install`(or `lerna bootstrap`). After successful running, all dependency packages are downloaded under the repository root `node_modules` directory.
76+
77+
### Dependencies between packages
78+
79+
In this example, the `x-cli` package depends on another package, `x-core`. So to execute (or test) `x-cli`, `x-core` packages should be installed.
80+
But in development the `x-core` package is not published so you can't install it.
81+
82+
`yarn` solves this problem. This command creates sim-links of each package into the top-level `node_modules` dir.
83+
84+
## Resolve Dependencies as TypeScript Modules
85+
86+
As mentioned above, Lerna resolves dependencies between packages. It's enough for "runtime". However considering TypeScript sources, in other words "static", it's not.
87+
88+
For example, the following code depends a module `x-core` located at other package.
89+
90+
```ts
91+
/* packages/x-cli/src/main.ts */
92+
import { awesomeFn } from '@quramy/x-core';
93+
94+
export function cli() {
95+
awesomeFn();
96+
return Promise.resolve(true);
97+
}
98+
```
99+
100+
If you compile this code, TypeScript compiler emits a "Cannot find module" error until building `x-core` package and creating `x-core/index.d.ts`. And it's silly to compile dependent packages(e.g. `x-core`) in the same repository after each editing them.
101+
102+
[TypeScript's path mapping](https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping) is the best solution. Path mappings are declared such as:
103+
104+
```js
105+
/* tsconfig.json */
106+
{
107+
"extends": "./tsconfig.base.json",
108+
"compilerOptions": {
109+
"baseUrl": "./packages",
110+
"paths": {
111+
"@quramy/*": ["./*/src"]
112+
}
113+
}
114+
}
115+
```
116+
117+
The above setting means `import { awesomeFn } from "@quramy/x-core"` is mapped to `import { awesomeFn } from "../../x-core/src"`(it's relative from "packages/x-cli/src/main.ts"). In other words, path mapping allows to treat developing packages' sources as published(compiled) modules.
118+
119+
## License
120+
121+
The MIT License (MIT)
122+
123+
Copyright 2017 Quramy
124+
125+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
126+
127+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
128+
129+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

lerna.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"lerna": "2.2.0",
3+
"packages": ["packages/*"],
4+
"npmClient": "npm",
5+
"version": "1.0.0"
6+
}

0 commit comments

Comments
 (0)