Skip to content

Commit 587b4bc

Browse files
authored
docs: add examples for watchOptions (#8532)
1 parent dd77a8b commit 587b4bc

File tree

2 files changed

+148
-8
lines changed

2 files changed

+148
-8
lines changed

website/docs/en/config/watch.mdx

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module.exports = {
2121
```
2222

2323
:::tip
24-
In `@rspack/dev-server` watch mode is enabled by default.
24+
`watch` is enabled by default when using `@rspack/dev-server`.
2525
:::
2626

2727
## watchOptions
@@ -59,19 +59,89 @@ module.exports = {
5959
### watchOptions.ignored
6060

6161
- **Type:** `RegExp`, `string`, `string[]`
62+
- **Default:** `undefined`
6263

63-
The path that matches is excluded while watching.
64+
The path that matches is excluded while watching. Watching many files can result in a lot of CPU or memory usage, it is recommended to exclude a huge folder like `node_modules` and `.git` using a regular expression:
65+
66+
```js title=rspack.config.js
67+
module.exports = {
68+
//...
69+
watchOptions: {
70+
ignored: /[\\/](?:\.git|node_modules)[\\/]/,
71+
},
72+
};
73+
```
74+
75+
Alternatively, a glob pattern may be used:
76+
77+
```js title=rspack.config.js
78+
module.exports = {
79+
//...
80+
watchOptions: {
81+
ignored: '**/node_modules',
82+
},
83+
};
84+
```
85+
86+
It is also possible to use multiple glob patterns:
87+
88+
```js title=rspack.config.js
89+
module.exports = {
90+
//...
91+
watchOptions: {
92+
ignored: ['**/files/**/*.js', '**/node_modules'],
93+
},
94+
};
95+
```
96+
97+
In addition, you can specify one or more absolute paths:
98+
99+
```js title=rspack.config.js
100+
const path = require('node:path');
101+
102+
module.exports = {
103+
//...
104+
watchOptions: {
105+
ignored: [path.posix.resolve(__dirname, './ignored-dir')],
106+
},
107+
};
108+
```
109+
110+
When using glob patterns, Rspack convert them to regular expressions with [glob-to-regexp](https://github.com/fitzgen/glob-to-regexp), so make sure to get yourself familiar with it before you use glob patterns for watchOptions.ignored.
64111

65112
### watchOptions.poll
66113

67114
- **Type:** `boolean`, `number`
68115
- **Default:** `false`
69116

70-
Whether to watch by polling, the default polling interval for `true` is 5007 milliseconds.
117+
Whether to watch by polling.
118+
119+
When set to `true`, the default polling interval is 5007 milliseconds.
120+
121+
```js title=rspack.config.js
122+
module.exports = {
123+
//...
124+
watchOptions: {
125+
poll: true,
126+
},
127+
};
128+
```
129+
130+
It can also set a custom polling interval:
131+
132+
```js title=rspack.config.js
133+
module.exports = {
134+
//...
135+
watchOptions: {
136+
poll: 1000, // Check for changes every second
137+
},
138+
};
139+
```
71140

72141
### watchOptions.followSymlinks
73142

74143
- **Type:** `boolean`
144+
- **Default:** `false`
75145

76146
Follow symbolic links while looking for a file. This is usually not needed as Rspack already resolves symlinks with [resolve.symlinks](/config/resolve#resolvesymlinks).
77147

website/docs/zh/config/watch.mdx

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import WebpackLicense from '@components/WebpackLicense';
44

55
# Watch
66

7-
监听:该选项用于监听文件变更并在变更时触发重新编译
7+
监听:该选项用于监听文件变更,并在变更时触发重新编译
88

99
## watch
1010

@@ -21,7 +21,7 @@ module.exports = {
2121
```
2222

2323
:::tip
24-
在使用 `@rspack/dev-server` 时,watch 默认为 true。
24+
在使用 `@rspack/dev-server` 时,`watch` 默认为 true。
2525
:::
2626

2727
## watchOptions
@@ -59,21 +59,91 @@ module.exports = {
5959
### watchOptions.ignored
6060

6161
- **类型:** `RegExp`, `string`, `string[]`
62+
- **默认值:** `undefined`
6263

63-
监听时排除匹配到的路径。
64+
监听时排除匹配到的路径。监听大量文件可能会导致 CPU 或内存使用率过高,建议使用正则表达式排除像 `node_modules``.git` 这样的大文件夹:
65+
66+
```js title=rspack.config.js
67+
module.exports = {
68+
//...
69+
watchOptions: {
70+
ignored: /[\\/](?:\.git|node_modules)[\\/]/,
71+
},
72+
};
73+
```
74+
75+
此外,可以使用 glob 匹配:
76+
77+
```js title=rspack.config.js
78+
module.exports = {
79+
//...
80+
watchOptions: {
81+
ignored: '**/node_modules',
82+
},
83+
};
84+
```
85+
86+
还可以使用多个 glob 匹配:
87+
88+
```js title=rspack.config.js
89+
module.exports = {
90+
//...
91+
watchOptions: {
92+
ignored: ['**/files/**/*.js', '**/node_modules'],
93+
},
94+
};
95+
```
96+
97+
还可以指定为一个或多个绝对路径:
98+
99+
```js title=rspack.config.js
100+
const path = require('node:path');
101+
102+
module.exports = {
103+
//...
104+
watchOptions: {
105+
ignored: [path.posix.resolve(__dirname, './ignored-dir')],
106+
},
107+
};
108+
```
109+
110+
当使用 glob 匹配时,Rspack 会将它们转换为正则表达式,因此在使用 glob 匹配之前,请确保你熟悉 [glob-to-regexp](https://github.com/fitzgen/glob-to-regexp)
64111

65112
### watchOptions.poll
66113

67114
- **类型:** `boolean`, `number`
68115
- **默认值:** `false`
69116

70-
是否通过轮询进行监听,`true` 时默认的轮询间隔为 5007 毫秒。
117+
是否通过轮询进行监听。
118+
119+
当设置为 `true` 时,默认的轮询间隔为 5007 毫秒。
120+
121+
```js title=rspack.config.js
122+
module.exports = {
123+
//...
124+
watchOptions: {
125+
poll: true,
126+
},
127+
};
128+
```
129+
130+
还可以设置一个自定义的轮询间隔:
131+
132+
```js title=rspack.config.js
133+
module.exports = {
134+
//...
135+
watchOptions: {
136+
poll: 1000, // 每秒检查一次
137+
},
138+
};
139+
```
71140

72141
### watchOptions.followSymlinks
73142

74143
- **类型:** `boolean`
144+
- **默认值:** `false`
75145

76-
根据软链接查找文件。这通常是不需要的,因为 Rspack 已经使用 [resolve.symlinks](/config/resolve#resolvesymlinks) 解析了软链接。
146+
是否根据软链接查找文件。这通常是不需要的,因为 Rspack 已经使用 [resolve.symlinks](/config/resolve#resolvesymlinks) 解析了软链接。
77147

78148
```js title=rspack.config.js
79149
module.exports = {

0 commit comments

Comments
 (0)