@rsbuild/plugin-source-build 1.0.1-beta.9 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +233 -9
- package/README.zh-CN.md +241 -0
- package/{dist-types → dist}/common/getBaseData.d.ts +2 -2
- package/{dist-types → dist}/common/getProjects.d.ts +2 -2
- package/dist/common/index.d.ts +6 -0
- package/{dist-types → dist}/common/pnpm.d.ts +1 -1
- package/{dist-types → dist}/common/rush.d.ts +1 -1
- package/dist/index.cjs +445 -7781
- package/dist/index.d.ts +4 -0
- package/dist/index.js +358 -7866
- package/{dist-types → dist}/plugin.d.ts +2 -2
- package/{dist-types → dist}/project-utils/filter.d.ts +1 -1
- package/{dist-types → dist}/project-utils/getDependentProjects.d.ts +3 -3
- package/dist/project-utils/index.d.ts +2 -0
- package/{dist-types → dist}/project.d.ts +1 -1
- package/{dist-types → dist}/types/index.d.ts +4 -4
- package/{dist-types → dist}/utils.d.ts +1 -1
- package/package.json +50 -28
- package/dist-types/common/index.d.ts +0 -6
- package/dist-types/index.d.ts +0 -4
- package/dist-types/package.json +0 -1
- package/dist-types/project-utils/index.d.ts +0 -2
- /package/{dist-types → dist}/common/isMonorepo.d.ts +0 -0
- /package/{dist-types → dist}/constants.d.ts +0 -0
- /package/{dist-types → dist}/types/packageJson.d.ts +0 -0
- /package/{dist-types → dist}/types/rushJson.d.ts +0 -0
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c)
|
|
3
|
+
Copyright (c) 2024 Rspack Contrib
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -1,19 +1,243 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
# @rsbuild/plugin-source-build
|
|
2
|
+
|
|
3
|
+
An Rsbuild plugin to provide support for monorepo source code referencing.
|
|
4
|
+
|
|
5
|
+
`@rsbuild/plugin-source-build` allows referencing source code from other subdirectories of monorepo and performs the build and hot updates.
|
|
6
|
+
|
|
7
|
+
<p>
|
|
8
|
+
<a href="https://npmjs.com/package/@rsbuild/plugin-source-build">
|
|
9
|
+
<img src="https://img.shields.io/npm/v/@rsbuild/plugin-source-build?style=flat-square&colorA=564341&colorB=EDED91" alt="npm version" />
|
|
10
|
+
</a>
|
|
11
|
+
<img src="https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="license" />
|
|
3
12
|
</p>
|
|
4
13
|
|
|
5
|
-
|
|
14
|
+
English | [简体中文](./README.zh-CN.md)
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
Install:
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm add @rsbuild/plugin-source-build -D
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Add plugin to your `rsbuild.config.ts`:
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
// rsbuild.config.ts
|
|
28
|
+
import { pluginSourceBuild } from "@rsbuild/plugin-source-build";
|
|
29
|
+
|
|
30
|
+
export default {
|
|
31
|
+
plugins: [pluginSourceBuild()],
|
|
32
|
+
};
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Use Cases
|
|
36
|
+
|
|
37
|
+
In a monorepo, there are two main ways for projects to reference each other: **artifact referencing and source code referencing**. Let's use a simple monorepo as an example to illustrate the use case of source code referencing.
|
|
38
|
+
|
|
39
|
+
For example, the monorepo contains an app application and a lib:
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
monorepo
|
|
43
|
+
├── app
|
|
44
|
+
└── lib
|
|
45
|
+
└── src
|
|
46
|
+
└── index.ts
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
The app is built using Rsbuild and relies on some methods from the lib:
|
|
50
|
+
|
|
51
|
+
```json
|
|
52
|
+
{
|
|
53
|
+
"name": "app",
|
|
54
|
+
"dependencies": {
|
|
55
|
+
"lib": "workspace:*"
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Artifact Referencing
|
|
61
|
+
|
|
62
|
+
**When using artifact referencing, the current project references the artifacts built from other sub-projects.**
|
|
63
|
+
|
|
64
|
+
In the example above, the lib is written in TypeScript. Typically, we need to build the lib's code in advance to generate JavaScript artifacts so that the app can reference it correctly. When the lib's code is updated, we need to rebuild it (or use `tsc --watch`) to ensure that the app can use the latest code.
|
|
65
|
+
|
|
66
|
+
The advantages of this approach are:
|
|
67
|
+
|
|
68
|
+
- The build processes of each sub-project are completely independent and can have different build configurations.
|
|
69
|
+
- Build caching can be applied to individual sub-projects.
|
|
70
|
+
|
|
71
|
+
The disadvantages are:
|
|
72
|
+
|
|
73
|
+
- The HMR chain becomes longer during local development.
|
|
74
|
+
- The process becomes cumbersome when a project contains multiple lib packages.
|
|
75
|
+
|
|
76
|
+
### Source Code Referencing
|
|
77
|
+
|
|
78
|
+
**When using source code referencing, the current project references the source code of other sub-projects for building.**
|
|
79
|
+
|
|
80
|
+
In the example mentioned earlier, when you register the `@rsbuild/plugin-source-build` and add the relevant configuration in the `lib` directory, Rsbuild will automatically reference the `src/index.ts` source code of the lib. This means that you don't need to build the lib's code in advance, and when the source code of the lib is updated, it can trigger automatic hot updates for the app.
|
|
81
|
+
|
|
82
|
+
The advantages of this approach are:
|
|
83
|
+
|
|
84
|
+
- The sub-project does not rely on a build tool and does not require build configurations. The code of the sub-project will be compiled by the build tool of the current project.
|
|
85
|
+
- There is no need to execute the build process for the sub-projects in advance.
|
|
86
|
+
- HMR is more efficient during local development.
|
|
87
|
+
|
|
88
|
+
The disadvantages are:
|
|
89
|
+
|
|
90
|
+
- The current project needs to support syntax features used by sub-projects and follow the same syntax specifications, such as using a consistent version of decorator syntax. If the current project and sub-projects require different build configurations, building from source code may not be suitable.
|
|
91
|
+
- The current project requires compiling more code, which may result in longer build times.
|
|
92
|
+
|
|
93
|
+
### Configuring Sub-projects
|
|
94
|
+
|
|
95
|
+
When the `@rsbuild/plugin-source-build` is registered, the Rsbuild will prioritize reading the file specified in the `source` field of the sub-project during the build process. Therefore, you need to configure the `source` field in the package.json file of the sub-project and point it to the source code file.
|
|
96
|
+
|
|
97
|
+
For example, in the following example, when the lib package is referenced, the `./src/index.ts` file will be read for building:
|
|
98
|
+
|
|
99
|
+
```json title="package.json"
|
|
100
|
+
{
|
|
101
|
+
"name": "lib",
|
|
102
|
+
"main": "./dist/index.js",
|
|
103
|
+
"source": "./src/index.ts"
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
If the sub-project uses [exports](https://nodejs.org/api/packages.html#package-entry-points) field, you also need to add the `source` field in the `exports` field.
|
|
108
|
+
|
|
109
|
+
```json title="package.json"
|
|
110
|
+
{
|
|
111
|
+
"name": "lib",
|
|
112
|
+
"exports": {
|
|
113
|
+
".": {
|
|
114
|
+
"source": "./src/index.ts",
|
|
115
|
+
"default": "./dist/index.js"
|
|
116
|
+
},
|
|
117
|
+
"./features": {
|
|
118
|
+
"source": "./src/features/index.ts",
|
|
119
|
+
"default": "./dist/features/index.js"
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Configure Project Reference
|
|
126
|
+
|
|
127
|
+
In a TypeScript project, you need to use the capability provided by TypeScript called [Project Reference](https://typescriptlang.org/docs/handbook/project-references). It helps you develop source code more effectively.
|
|
128
|
+
|
|
129
|
+
### Introduction
|
|
130
|
+
|
|
131
|
+
Project reference provides the following capabilities:
|
|
132
|
+
|
|
133
|
+
- It allows TypeScript to correctly recognize the types of other sub-projects without the need to build them.
|
|
134
|
+
- When you navigate the code in VS Code, it automatically takes you to the corresponding source code file of the module.
|
|
135
|
+
- Rsbuild reads the project reference configuration and automatically recognizes the `tsconfig.compilerOptions.path` configuration of the sub-project, so that the use of aliases in the sub-project works correctly.
|
|
136
|
+
|
|
137
|
+
### Example
|
|
138
|
+
|
|
139
|
+
In the example mentioned earlier, since the app project references the lib sub-project, we need to configure the `references` options in the app project's `tsconfig.json` to point to the relative directory of the lib:
|
|
140
|
+
|
|
141
|
+
```json title="app/tsconfig.json"
|
|
142
|
+
{
|
|
143
|
+
"references": [
|
|
144
|
+
{
|
|
145
|
+
"path": "../lib"
|
|
146
|
+
}
|
|
147
|
+
]
|
|
148
|
+
}
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
At the same time, we need to set `composite` to `true` in the lib project's `tsconfig.json`:
|
|
152
|
+
|
|
153
|
+
```json title="lib/A/tsconfig.json"
|
|
154
|
+
{
|
|
155
|
+
"compilerOptions": {
|
|
156
|
+
"composite": true
|
|
157
|
+
},
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
After adding these two options, the project reference is already configured. You can restart VS Code to see the effects of the configuration.
|
|
162
|
+
|
|
163
|
+
Note that the above example is a simplified one. In real monorepo projects, there may be more complex dependency relationships. You need to add a complete `references` configuration for the functionality to work correctly.
|
|
164
|
+
|
|
165
|
+
> If you want to learn more about project reference, please refer to the official documentation on [TypeScript - Project References](https://typescriptlang.org/docs/handbook/project-references).
|
|
166
|
+
|
|
167
|
+
## Options
|
|
168
|
+
|
|
169
|
+
### sourceField
|
|
170
|
+
|
|
171
|
+
- **Type:** `string`
|
|
172
|
+
- **Default:** `'source'`
|
|
173
|
+
|
|
174
|
+
Used to configure the resolve field of the source code files.
|
|
175
|
+
|
|
176
|
+
For example, when configured as `my-source`:
|
|
177
|
+
|
|
178
|
+
```ts
|
|
179
|
+
pluginSourceBuild({
|
|
180
|
+
sourceField: "my-source",
|
|
181
|
+
});
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
In `package.json`, the source code file path can be specified using `my-source`:
|
|
185
|
+
|
|
186
|
+
```json title="package.json"
|
|
187
|
+
{
|
|
188
|
+
"name": "lib",
|
|
189
|
+
"main": "./dist/index.js",
|
|
190
|
+
"my-source": "./src/index.ts",
|
|
191
|
+
"exports": {
|
|
192
|
+
".": {
|
|
193
|
+
"my-source": "./src/index.ts",
|
|
194
|
+
"default": "./dist/index.js"
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### resolvePriority
|
|
201
|
+
|
|
202
|
+
- **Type:** `'source' | 'output'`
|
|
203
|
+
- **Default:** `'source'`
|
|
204
|
+
|
|
205
|
+
Used to control the priority of reading the source code or the output code.
|
|
206
|
+
|
|
207
|
+
By default, `@rsbuild/plugin-source-build` will reading the source code first, for example, in the following example, it will read the `source` field.
|
|
208
|
+
|
|
209
|
+
```json title="package.json"
|
|
210
|
+
{
|
|
211
|
+
"name": "lib",
|
|
212
|
+
"main": "./dist/index.js",
|
|
213
|
+
"source": "./src/index.ts"
|
|
214
|
+
}
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
When `resolvePriority` is set to `'output'`, `@rsbuild/plugin-source-build` will read the output code first, i.e., the code from the `main` or `module` field.
|
|
218
|
+
|
|
219
|
+
```ts
|
|
220
|
+
pluginSourceBuild({
|
|
221
|
+
resolvePriority: "output",
|
|
222
|
+
});
|
|
223
|
+
```
|
|
6
224
|
|
|
7
|
-
The
|
|
225
|
+
- The `exports` field in package.json is not affected by `resolvePriority`.
|
|
226
|
+
- The keys order in `exports` determines the resolving order, earlier declared keys having higher priority.
|
|
8
227
|
|
|
9
|
-
##
|
|
228
|
+
## Caveat
|
|
10
229
|
|
|
11
|
-
|
|
230
|
+
When using `@rsbuild/plugin-source-build`, there are a few things to keep in mind:
|
|
12
231
|
|
|
13
|
-
|
|
232
|
+
1. Ensure that the current project can compile the syntax or features used in the sub-project. For example, if the sub-project uses Stylus to write CSS, the current app needs to support Stylus compilation.
|
|
233
|
+
2. Ensure that the current project has the same code syntax and features as the sub-project, such as consistent syntax versions for decorators.
|
|
234
|
+
3. Source code building may have some limitations. When encountering issues, you can remove the `source` field from the sub-project's package.json and debug using the built artifacts of the sub-project.
|
|
235
|
+
4. When `composite: true` is enabled, TypeScript will generate `*.tsbuildinfo` temporary files. You need to add these temporary files to the `.gitignore` file.
|
|
14
236
|
|
|
15
|
-
|
|
237
|
+
```text title=".gitignore"
|
|
238
|
+
*.tsbuildinfo
|
|
239
|
+
```
|
|
16
240
|
|
|
17
241
|
## License
|
|
18
242
|
|
|
19
|
-
|
|
243
|
+
[MIT](./LICENSE).
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
# @rsbuild/plugin-source-build
|
|
2
|
+
|
|
3
|
+
提供对 monorepo 源代码引用的支持。
|
|
4
|
+
|
|
5
|
+
`@rsbuild/plugin-source-build` 用于 monorepo 开发场景,它支持从当前项目引用其他子目录的源代码,并完成构建和热更新。
|
|
6
|
+
|
|
7
|
+
<p>
|
|
8
|
+
<a href="https://npmjs.com/package/@rsbuild/plugin-source-build">
|
|
9
|
+
<img src="https://img.shields.io/npm/v/@rsbuild/plugin-source-build?style=flat-square&colorA=564341&colorB=EDED91" alt="npm version" />
|
|
10
|
+
</a>
|
|
11
|
+
<img src="https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="license" />
|
|
12
|
+
</p>
|
|
13
|
+
|
|
14
|
+
## 使用
|
|
15
|
+
|
|
16
|
+
安装:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm add @rsbuild/plugin-source-build -D
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
在 `rsbuild.config.ts` 里注册插件:
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
// rsbuild.config.ts
|
|
26
|
+
import { pluginSourceBuild } from "@rsbuild/plugin-source-build";
|
|
27
|
+
|
|
28
|
+
export default {
|
|
29
|
+
plugins: [pluginSourceBuild()],
|
|
30
|
+
};
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## 使用场景
|
|
34
|
+
|
|
35
|
+
在 monorepo 中,子项目互相引用主要有两种方式 —— **产物引用和源码引用**。我们以一个最简单的 monorepo 为例子,来介绍源码引用的使用场景。
|
|
36
|
+
|
|
37
|
+
比如 monorepo 中包含了一个 app 应用和一个 lib 包:
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
monorepo
|
|
41
|
+
├── app
|
|
42
|
+
└── lib
|
|
43
|
+
└── src
|
|
44
|
+
└── index.ts
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
其中,app 是基于 Rsbuild 构建的,app 依赖了 lib 中的一些方法:
|
|
48
|
+
|
|
49
|
+
```json
|
|
50
|
+
{
|
|
51
|
+
"name": "app",
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"lib": "workspace:*"
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### 产物引用
|
|
59
|
+
|
|
60
|
+
**产物引用指的是当前项目引用其他子项目构建后的产物。**
|
|
61
|
+
|
|
62
|
+
比如上述例子中的 lib 是使用 TypeScript 编写的,通常情况下,我们需要提前构建 lib 的代码,生成 JavaScript 产物,这样 app 才可以正确引用它。当 lib 代码更新时,还需要重新执行一次构建(或者使用 `tsc --watch`),否则 app 无法引用到最新的代码。
|
|
63
|
+
|
|
64
|
+
这种方式的优势在于:
|
|
65
|
+
|
|
66
|
+
- 各个子项目的构建过程是完全独立的,可以拥有不同的构建配置。
|
|
67
|
+
- 可以针对子项目进行构建缓存。
|
|
68
|
+
|
|
69
|
+
劣势在于:
|
|
70
|
+
|
|
71
|
+
- 本地开发时 HMR 的链路变长。
|
|
72
|
+
- 当一个项目中包含多个 lib 包时,以上过程会显得十分繁琐。
|
|
73
|
+
|
|
74
|
+
### 源码引用
|
|
75
|
+
|
|
76
|
+
**源码引用指的是当前项目引用其他子项目的源码进行构建。**
|
|
77
|
+
|
|
78
|
+
比如上述例子,当你注册了 `@rsbuild/plugin-source-build`,并在 lib 中添加相关配置后,Rsbuild 会自动引用 lib 的 `src/index.ts` 源代码。这意味着,你不需要提前构建 lib 的代码,并且当 lib 的源代码更新时,也可以自动触发 app 的热更新。
|
|
79
|
+
|
|
80
|
+
这种方式的优势在于:
|
|
81
|
+
|
|
82
|
+
- 子项目不依赖构建工具,也不需要添加构建配置,子项目的代码会被当前项目的构建工具编译。
|
|
83
|
+
- 不需要提前执行子项目的构建流程。
|
|
84
|
+
- 本地开发时 HMR 更高效。
|
|
85
|
+
|
|
86
|
+
劣势在于:
|
|
87
|
+
|
|
88
|
+
- 当前项目需要支持子项目用到的语法特性,并且遵循相同的语法规范,比如使用一致的装饰器语法版本。如果当前项目和子项目需要使用不同的编译配置,则不适合使用 `@rsbuild/plugin-source-build`。
|
|
89
|
+
- 当前项目需要编译更多的代码,因此构建时间可能会变长。
|
|
90
|
+
|
|
91
|
+
### 配置子项目
|
|
92
|
+
|
|
93
|
+
当注册 `@rsbuild/plugin-source-build`后,Rsbuild 在构建过程中,会优先读取子项目的 `source` 字段对应的文件。因此,你需要在子项目的 package.json 中配置 `source` 字段,并且指向源码文件路径。
|
|
94
|
+
|
|
95
|
+
比如以下例子,当 lib 包被引用时,会读取 `./src/index.ts` 文件进行构建:
|
|
96
|
+
|
|
97
|
+
```json title="package.json"
|
|
98
|
+
{
|
|
99
|
+
"name": "lib",
|
|
100
|
+
"main": "./dist/index.js",
|
|
101
|
+
"source": "./src/index.ts"
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
如果子项目使用了 [exports](https://nodejs.org/api/packages.html#package-entry-points) 配置,那么你同样需要在 `exports` 中增加 `source` 字段。
|
|
106
|
+
|
|
107
|
+
```json title="package.json"
|
|
108
|
+
{
|
|
109
|
+
"name": "lib",
|
|
110
|
+
"exports": {
|
|
111
|
+
".": {
|
|
112
|
+
"source": "./src/index.ts",
|
|
113
|
+
"default": "./dist/index.js"
|
|
114
|
+
},
|
|
115
|
+
"./features": {
|
|
116
|
+
"source": "./src/features/index.ts",
|
|
117
|
+
"default": "./dist/features/index.js"
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## 配置 Project Reference
|
|
124
|
+
|
|
125
|
+
在 TypeScript 项目中,你需要使用 TypeScript 提供的 [Project Reference](https://typescriptlang.org/docs/handbook/project-references) 能力,它可以帮助你更好地使用源码开发。
|
|
126
|
+
|
|
127
|
+
### 介绍
|
|
128
|
+
|
|
129
|
+
Project reference 提供了以下能力:
|
|
130
|
+
|
|
131
|
+
- 使 TypeScript 可以正确识别其他子项目的类型,而无须对子项目进行构建。
|
|
132
|
+
- 当你在 VS Code 内进行代码跳转时,VS Code 可以自动跳转到对应模块的源代码文件。
|
|
133
|
+
- Rsbuild 会读取 project reference 配置,并自动识别子项目的 `tsconfig.compilerOptions.path` 配置,从而让子项目的别名可以正确生效。
|
|
134
|
+
|
|
135
|
+
### 示例
|
|
136
|
+
|
|
137
|
+
在上文的例子中,由于 app 引用了 lib 子项目,我们需要在 app 的 `tsconfig.json` 内配置 `references`,并指向 lib 对应的相对目录:
|
|
138
|
+
|
|
139
|
+
```json title="app/tsconfig.json"
|
|
140
|
+
{
|
|
141
|
+
"references": [
|
|
142
|
+
{
|
|
143
|
+
"path": "../lib"
|
|
144
|
+
}
|
|
145
|
+
]
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
同时,需要在 lib 子项目的 `tsconfig.json` 内配置 `composite` 为 `true`:
|
|
150
|
+
|
|
151
|
+
```json title="lib/A/tsconfig.json"
|
|
152
|
+
{
|
|
153
|
+
"compilerOptions": {
|
|
154
|
+
"composite": true
|
|
155
|
+
},
|
|
156
|
+
}
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
添加以上两个选项后,project reference 就已经配置完成了,你可以重新启动 VS Code 来查看配置以后的效果。
|
|
160
|
+
|
|
161
|
+
注意以上只是一个最简单的例子,在实际的 monorepo 项目中,可能会有更复杂的依赖关系,你需要添加完整的 `references` 配置,才能使上述功能正确运作。
|
|
162
|
+
|
|
163
|
+
> 如果你想了解更多关于 project reference 的内容,请阅读 [TypeScript - Project References](https://typescriptlang.org/docs/handbook/project-references) 官方文档。
|
|
164
|
+
|
|
165
|
+
## 选项
|
|
166
|
+
|
|
167
|
+
### sourceField
|
|
168
|
+
|
|
169
|
+
- **类型:** `string`
|
|
170
|
+
- **默认值:** `'source'`
|
|
171
|
+
|
|
172
|
+
用于配置源代码文件对应的解析字段。
|
|
173
|
+
|
|
174
|
+
比如配置为 `my-source`:
|
|
175
|
+
|
|
176
|
+
```ts
|
|
177
|
+
pluginSourceBuild({
|
|
178
|
+
sourceField: "my-source",
|
|
179
|
+
});
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
在 `package.json` 中,即可通过 `my-source` 指定源代码文件的路径:
|
|
183
|
+
|
|
184
|
+
```json title="package.json"
|
|
185
|
+
{
|
|
186
|
+
"name": "lib",
|
|
187
|
+
"main": "./dist/index.js",
|
|
188
|
+
"my-source": "./src/index.ts",
|
|
189
|
+
"exports": {
|
|
190
|
+
".": {
|
|
191
|
+
"my-source": "./src/index.ts",
|
|
192
|
+
"default": "./dist/index.js"
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### resolvePriority
|
|
199
|
+
|
|
200
|
+
- **类型:** `'source' | 'output'`
|
|
201
|
+
- **默认值:** `'source'`
|
|
202
|
+
|
|
203
|
+
用于控制优先读取源代码还是产物代码。
|
|
204
|
+
|
|
205
|
+
默认情况下,`@rsbuild/plugin-source-build`会优先读取源代码,比如在下面的例子中,它会读取 `source` 字段。
|
|
206
|
+
|
|
207
|
+
```json title="package.json"
|
|
208
|
+
{
|
|
209
|
+
"name": "lib",
|
|
210
|
+
"main": "./dist/index.js",
|
|
211
|
+
"source": "./src/index.ts"
|
|
212
|
+
}
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
当 `resolvePriority` 设置为 `'output'` 时,`@rsbuild/plugin-source-build`会优先读取产物代码,即 `main` 或 `module` 字段指向的代码。
|
|
216
|
+
|
|
217
|
+
```ts
|
|
218
|
+
pluginSourceBuild({
|
|
219
|
+
resolvePriority: "output",
|
|
220
|
+
});
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
- package.json 中的 `exports` 字段不受 `resolvePriority` 的影响。
|
|
224
|
+
- `exports` 中 key 的声明顺序决定了读取顺序,较早声明的 key 具有更高的优先级。
|
|
225
|
+
|
|
226
|
+
## 注意事项
|
|
227
|
+
|
|
228
|
+
在使用 `@rsbuild/plugin-source-build`的时候,需要注意几点:
|
|
229
|
+
|
|
230
|
+
1. 需要保证当前项目可以编译子项目里使用的语法或特性。比如子项目使用了 Stylus 来编写 CSS 样式,那就需要当前 app 支持 Stylus 编译。
|
|
231
|
+
2. 需要保证当前项目与子项目使用的代码语法特性相同,例如装饰器的语法版本一致。
|
|
232
|
+
3. 源码构建可能存在一些限制。如果在使用中遇到问题,你可以将子项目 package.json 中的 `source` 字段移除,使用子项目的构建产物进行调试。
|
|
233
|
+
4. 开启 `composite: true` 后,TypeScript 会生成 `*.tsbuildinfo` 临时文件,你需要将这些临时文件加入 .gitignore 中。
|
|
234
|
+
|
|
235
|
+
```text title=".gitignore"
|
|
236
|
+
*.tsbuildinfo
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
## License
|
|
240
|
+
|
|
241
|
+
[MIT](./LICENSE).
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { MonorepoAnalyzer } from '../types';
|
|
2
|
-
import type { GetProjectsFunc } from './getProjects';
|
|
1
|
+
import type { MonorepoAnalyzer } from '../types/index.js';
|
|
2
|
+
import type { GetProjectsFunc } from './getProjects.js';
|
|
3
3
|
export interface IMonorepoBaseData {
|
|
4
4
|
isMonorepo: boolean;
|
|
5
5
|
type: string;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Project } from '../project';
|
|
2
|
-
import type { IMonorepoBaseData } from './getBaseData';
|
|
1
|
+
import type { Project } from '../project.js';
|
|
2
|
+
import type { IMonorepoBaseData } from './getBaseData.js';
|
|
3
3
|
export type GetProjectsFunc = (rootPath: string) => Promise<Project[]> | Project[];
|
|
4
4
|
export declare const getMonorepoSubProjects: (monorepoBaseData: IMonorepoBaseData) => Promise<Project[]>;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export * from './getBaseData.js';
|
|
2
|
+
export * from './isMonorepo.js';
|
|
3
|
+
export { getMonorepoSubProjects } from './getProjects.js';
|
|
4
|
+
export { getProjects as getPnpmMonorepoSubProjects } from './pnpm.js';
|
|
5
|
+
export { getProjects as getRushMonorepoSubProjects } from './rush.js';
|
|
6
|
+
export type { GetProjectsFunc } from './getProjects.js';
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { Project } from '../project';
|
|
1
|
+
import { Project } from '../project.js';
|
|
2
2
|
export declare const getProjects: (monorepoRoot: string) => Promise<Project[]>;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { Project } from '../project';
|
|
1
|
+
import { Project } from '../project.js';
|
|
2
2
|
export declare const getProjects: (monorepoRoot: string) => Promise<Project[]>;
|