Skip to content

Commit bd62ab8

Browse files
authored
docs: update README to recommend custom source field (#26)
1 parent 711645a commit bd62ab8

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

README.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,18 @@ If the sub-project uses [exports](https://nodejs.org/api/packages.html#package-e
122122
}
123123
```
124124

125+
### Customizing Source Field
126+
127+
Although the plugin uses the `source` field by default to specify the source file, we recommend configuring a custom field through the [sourceField](#sourceField) option (for example, `@custom/source`, where `custom` can be replaced with any scope name).
128+
129+
```ts
130+
pluginSourceBuild({
131+
sourceField: "@custom/source",
132+
});
133+
```
134+
135+
This is because some packages (such as Mobx) also define a `source` field in their `package.json`. If not distinguished, Rsbuild may mistakenly resolve the source files of those dependencies, leading to unexpected build results or type issues. By using a custom field, you can avoid such conflicts and ensure that Rsbuild behaves predictably when resolving dependencies.
136+
125137
## Configure Project Reference
126138

127139
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.
@@ -154,7 +166,7 @@ At the same time, we need to set `composite` to `true` in the lib project's `tsc
154166
{
155167
"compilerOptions": {
156168
"composite": true
157-
},
169+
}
158170
}
159171
```
160172

@@ -173,24 +185,24 @@ Note that the above example is a simplified one. In real monorepo projects, ther
173185

174186
Used to configure the resolve field of the source code files.
175187

176-
For example, when configured as `my-source`:
188+
For example, when configured as `@custom/source`:
177189

178190
```ts
179191
pluginSourceBuild({
180-
sourceField: "my-source",
192+
sourceField: "@custom/source",
181193
});
182194
```
183195

184-
In `package.json`, the source code file path can be specified using `my-source`:
196+
In `package.json`, the source code file path can be specified using `@custom/source`:
185197

186198
```json title="package.json"
187199
{
188200
"name": "lib",
189201
"main": "./dist/index.js",
190-
"my-source": "./src/index.ts",
202+
"@custom/source": "./src/index.ts",
191203
"exports": {
192204
".": {
193-
"my-source": "./src/index.ts",
205+
"@custom/source": "./src/index.ts",
194206
"default": "./dist/index.js"
195207
}
196208
}

README.zh-CN.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,18 @@ monorepo
120120
}
121121
```
122122

123+
### 自定义 source 字段
124+
125+
虽然插件默认使用 `source` 字段来指定源代码文件,但我们更推荐通过 [sourceField](#sourceField) 选项配置一个自定义字段(例如 `@custom/source`,其中 `custom` 可以替换为任意 scope 名称)。
126+
127+
```ts
128+
pluginSourceBuild({
129+
sourceField: "@custom/source",
130+
});
131+
```
132+
133+
这是因为某些三方库(例如 Mobx)的 `package.json` 中也包含 `source` 字段,如果不进行区分,Rsbuild 可能会错误地解析这些库的源文件路径,从而导致意料之外的构建结果或类型问题。使用自定义字段可以避免此类冲突,确保 Rsbuild 在解析依赖关系时保持可控的行为。
134+
123135
## 配置 Project Reference
124136

125137
在 TypeScript 项目中,你需要使用 TypeScript 提供的 [Project Reference](https://typescriptlang.org/docs/handbook/project-references) 能力,它可以帮助你更好地使用源码开发。
@@ -152,7 +164,7 @@ Project reference 提供了以下能力:
152164
{
153165
"compilerOptions": {
154166
"composite": true
155-
},
167+
}
156168
}
157169
```
158170

@@ -171,24 +183,24 @@ Project reference 提供了以下能力:
171183

172184
用于配置源代码文件对应的解析字段。
173185

174-
比如配置为 `my-source`
186+
比如配置为 `@custom/source`
175187

176188
```ts
177189
pluginSourceBuild({
178-
sourceField: "my-source",
190+
sourceField: "@custom/source",
179191
});
180192
```
181193

182-
`package.json` 中,即可通过 `my-source` 指定源代码文件的路径:
194+
`package.json` 中,即可通过 `@custom/source` 指定源代码文件的路径:
183195

184196
```json title="package.json"
185197
{
186198
"name": "lib",
187199
"main": "./dist/index.js",
188-
"my-source": "./src/index.ts",
200+
"@custom/source": "./src/index.ts",
189201
"exports": {
190202
".": {
191-
"my-source": "./src/index.ts",
203+
"@custom/source": "./src/index.ts",
192204
"default": "./dist/index.js"
193205
}
194206
}

0 commit comments

Comments
 (0)