Skip to content

Commit 1cf4bf7

Browse files
authored
feat: v2 ! pure esm, rolldown support, upgrade path-to-regexp to v8 (#124)
1 parent 1fb7d2e commit 1cf4bf7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+3245
-3372
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"picomatch",
1111
"proxied",
1212
"referer",
13+
"rolldown",
1314
"tsdown"
1415
],
1516
"typescript.tsdk": "node_modules/typescript/lib",

README.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
- ⚡️ Lightweight, Flexible, Fast.
3030
- 🧲 Not injection-based, non-intrusive to client code.
31-
- 💡 ESModule/commonjs.
31+
- 💡 Pure ESModule.
3232
- 🦾 Typescript.
3333
- 🔥 HMR
3434
- 🏷 Support `.[cm]?js` / `.ts` / `.json` / `.json5`.
@@ -52,6 +52,14 @@ See the [documentation](https://vite-plugin-mock-dev-server.netlify.app/) for mo
5252

5353
[![Netlify Status](https://api.netlify.com/api/v1/badges/9ccda610-2c6a-4cd0-aeaa-a8932f2b477c/deploy-status)](https://app.netlify.com/sites/vite-plugin-mock-dev-server/deploys)
5454

55+
> [!IMPORTANT]
56+
> The plugin no longer supports `CommonJS` imports. Please use `ESModule` to import the plugin.
57+
58+
----
59+
60+
> [!IMPORTANT]
61+
> The current document is for the `v2` version of the plugin. If you are using the `v1` version, please refer to the [Migration Guide](https://vite-plugin-mock-dev-server.netlify.app/guide/migrate-v2).
62+
5563
## Install
5664

5765
``` sh
@@ -100,7 +108,7 @@ By default, write mock data in the `mock` directory of your project's root direc
100108
import { defineMock } from 'vite-plugin-mock-dev-server'
101109

102110
export default defineMock({
103-
url: '/api/test',
111+
url: '/api/user/:id',
104112
body: { a: 1, b: 2 }
105113
})
106114
```
@@ -205,18 +213,26 @@ export default defineMock({
205213

206214
Configure the matching context for `include` and `exclude`.
207215

216+
### dir
217+
218+
- **Type:** `string`
219+
- **Default:** `'mock'`
220+
- **Details:**
221+
222+
Configure the directory to read mock files.
223+
208224
### include
209225

210226
- **Type:** `string | string[]`
211-
- **Default:** `['mock/**/*.mock.{js,ts,cjs,mjs,json,json5}']` (Relative to the root directory)
227+
- **Default:** `['**/*.mock.{js,ts,cjs,mjs,json,json5}']`
212228
- **Details:**
213229

214230
Configure to read mock files, which can be a directory, glob, or an array.
215231

216232
### exclude
217233

218234
- **Type:** `string | string[]`
219-
- **Default:** `['**/node_modules/**','**/.vscode/**','**/.git/**']`
235+
- **Default:** `['**/node_modules/**']`
220236
- **Details:**
221237

222238
When reading mock files for configuration, the files that need to be excluded can be a directory, glob, or array.

README.zh-CN.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
- ⚡️ 轻量,灵活,快速
3131
- 🧲 非注入式,对客户端代码无侵入
32-
- 💡 ESModule/commonjs
32+
- 💡 ESModule
3333
- 🦾 Typescript
3434
- 🔥 热更新
3535
- 🏷 支持 `.[cm]?js` / `.ts` / `.json` / `.json5` 编写 mock 数据
@@ -53,6 +53,14 @@
5353

5454
[![Netlify Status](https://api.netlify.com/api/v1/badges/9ccda610-2c6a-4cd0-aeaa-a8932f2b477c/deploy-status)](https://app.netlify.com/sites/vite-plugin-mock-dev-server/deploys)
5555

56+
> [!IMPORTANT]
57+
> 插件不再支持 `CommonJS` 导入使用,请使用 `ESModule` 导入插件
58+
59+
----
60+
61+
> [!IMPORTANT]
62+
> 当前文档为插件的 `v2` 版本,如果您正在使用 `v1` 版本,请参考 [迁移文档](https://vite-plugin-mock-dev-server.netlify.app/zh/guide/migrate-v2)
63+
5664
## 安装
5765

5866
```sh
@@ -101,7 +109,7 @@ export default defineConfig({
101109
import { defineMock } from 'vite-plugin-mock-dev-server'
102110

103111
export default defineMock({
104-
url: '/api/test',
112+
url: '/api/user/:id',
105113
body: { a: 1, b: 2 }
106114
})
107115
```
@@ -205,18 +213,26 @@ export default defineMock({
205213

206214
配置 `include``exclude` 的匹配上下文
207215

216+
### dir
217+
218+
- **类型:** `string`
219+
- **默认值:** `'mock'`
220+
- **详情:**
221+
222+
配置mock数据的目录,相对于 `cwd`
223+
208224
### include
209225

210226
- **类型:** `string | string[]`
211-
- **默认值:** `['mock/**/*.mock.{js,ts,cjs,mjs,json,json5}']` (相对于根目录)
227+
- **默认值:** `['**/*.mock.{js,ts,cjs,mjs,json,json5}']`
212228
- **详情:**
213229

214230
配置读取 mock文件,可以是一个 目录,glob,或者一个数组
215231

216232
### exclude
217233

218234
- **类型:** `string | string[]`
219-
- **默认值:** `['**/node_modules/**', '**/.vscode/**', '**/.git/**']`
235+
- **默认值:** `['**/node_modules/**']`
220236
- **详情:**
221237

222238
配置读取 mock文件时,需要排除的文件, 可以是一个 目录、glob、或者一个数组

docs/.vitepress/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const vitepressConfig: UserConfig<DefaultTheme.Config> = defineConfig({
5151
},
5252
vite: {
5353
plugins: [
54-
groupIconVitePlugin(),
54+
groupIconVitePlugin() as any,
5555
prod && llmstxt({
5656
workDir: 'en',
5757
ignoreFiles: ['index.md'],

docs/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ function sidebarGuide(): DefaultTheme.SidebarItem[] {
5757
{ text: 'Introduce', link: 'introduce' },
5858
{ text: 'Install', link: 'install' },
5959
{ text: 'Usage', link: 'usage' },
60+
{ text: 'Migrate from v1.0', link: 'migrate-v2' },
6061
],
6162
},
6263
{

docs/en/examples/static.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Static resource service
22

3-
Retrieve the file path from the request through dynamic route matching using `:filepath(.*)`, obtain the file path from the request, return it as a file stream in the body, and set the correct `Content-Type` in the headers.
3+
Retrieve the file path from the request through dynamic route matching using `*filepath`, obtain the file path from the request, return it as a file stream in the body, and set the correct `Content-Type` in the headers.
44

55
```ts
66
import { createReadStream } from 'node:fs'
@@ -12,18 +12,18 @@ import { defineMock } from 'vite-plugin-mock-dev-server'
1212
const __dirname = path.dirname(fileURLToPath(import.meta.url))
1313

1414
export default defineMock({
15-
url: '/mockData/:filepath(.*)',
15+
url: '/mock-static/*filepath',
1616
method: 'GET',
1717
headers(request) {
18-
const { filepath } = request.params
18+
const filepath = request.params.filepath.join('/')
1919
const filename = path.basename(filepath)
2020
return {
2121
'Content-Type': mime.lookup(filename) || 'text/plain',
2222
}
2323
},
2424
body(request) {
2525
const { filepath } = request.params
26-
return createReadStream(path.join(__dirname, 'mockData', filepath))
26+
return createReadStream(path.join(__dirname, 'mock-static', filepath))
2727
},
2828
})
2929
```

docs/en/guide/migrate-v2.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Migrate from v1.x
2+
3+
## Pure ESM Support
4+
5+
The plugin no longer provides `CommonJS` code exports. Please use the ESM mode to import the plugin.
6+
7+
```ts
8+
import { mockDevServerPlugin } from 'vite-plugin-mock-dev-server' // ✔️
9+
```
10+
11+
```js
12+
const { mockDevServerPlugin } = require('vite-plugin-mock-dev-server') // ✖️ // [!code error]
13+
```
14+
15+
## Rolldown & Esbuild Support
16+
17+
The plugin now automatically selects different compilers to process mock files based on different versions of Vite.
18+
19+
For the `vite@npm:rolldown-vite@latest` version, `rolldown` is used as the compiler.
20+
21+
## path-to-regexp Upgraded from v6 to v8
22+
23+
The plugin now uses [`path-to-regexp@v8`](https://github.com/pillarjs/path-to-regexp#readme) as the path matching engine.
24+
25+
Please refer to the [official documentation](https://github.com/pillarjs/path-to-regexp#unexpected--or-) for adaptation and upgrade.
26+
27+
## Plugin Configuration
28+
29+
Added a new `dir` configuration option to specify the directory for mock files, relative to `cwd`. The matching context for `include` and `exclude` is `cwd + dir`.
30+
31+
::: details Why was this adjustment made?
32+
33+
The plugin uses `chokidar` to monitor changes in mock files, starting the matching from the `cwd` directory by default. The search scope may be too large for medium to large projects,
34+
35+
which could lead to unnecessary performance overhead. Therefore, the `dir` configuration option has been added to further narrow down the search scope.
36+
37+
:::

docs/en/guide/plugin-config.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
`type: MockServerPluginOptions`
44

55
``` ts
6+
import process from 'node:process'
67
import { defineConfig } from 'vite'
78
import { mockDevServerPlugin } from 'vite-plugin-mock-dev-server'
89

@@ -11,7 +12,9 @@ export default defineConfig({
1112
mockDevServerPlugin({
1213
prefix: [],
1314
wsPrefix: [],
14-
include: ['mock/**/*.mock.{js,ts,cjs,mjs,json,json5}'],
15+
cwd: process.cwd(),
16+
dir: 'mock',
17+
include: ['**/*.mock.{js,ts,cjs,mjs,json,json5}'],
1518
exclude: '',
1619
log: 'info',
1720
reload: false,
@@ -31,6 +34,8 @@ export default defineConfig({
3134
interface MockServerPluginOptions {
3235
prefix?: string | string[]
3336
wsPrefix?: string | string[]
37+
cwd?: string
38+
dir?: string
3439
include?: string | string[]
3540
exclude?: string | string[]
3641
reload?: boolean
@@ -74,11 +79,18 @@ This conflict is not a problem with Vite or the plugin itself; it is a reasonabl
7479

7580
Configure the matching context for `include` and `exclude`.
7681

82+
## dir
83+
84+
- **Type**`string`
85+
- **Default**`'mock'`
86+
87+
The directory for configuring mock data, relative to `cwd`.
88+
7789
## include
7890

7991
**Type**`string | string[]`
8092

81-
- **Default**:relative to [`cwd`](#cwd)
93+
- **Default**:relative to [`cwd`](#cwd) + [`dir`](#dir)
8294

8395
```ts
8496
['mock/**/*.mock.{js,ts,cjs,mjs,json,json5}']
@@ -90,10 +102,10 @@ Configure the reading of mock files, which can be a directory, a glob pattern, o
90102

91103
- **Type**`string | string[]`
92104

93-
- **Default**:relative to [`cwd`](#cwd)
105+
- **Default**:relative to [`cwd`](#cwd) + [`dir`](#dir)
94106

95107
```ts
96-
['**/node_modules/**', '**/test/**', '**/cypress/**', 'src/**', '**/.vscode/**', '**/.git/**', '**/dist/**']
108+
['**/node_modules/**']
97109
```
98110

99111
Specifies the files to be excluded when reading mock files. It can be a directory, glob pattern, or an array.

docs/zh/config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ function sidebarGuide(): DefaultTheme.SidebarItem[] {
110110
{ text: '介绍', link: 'introduce' },
111111
{ text: '安装', link: 'install' },
112112
{ text: '使用', link: 'usage' },
113+
{ text: '从 v1.x 迁移', link: 'migrate-v2' },
113114
],
114115
},
115116
{
@@ -125,8 +126,8 @@ function sidebarGuide(): DefaultTheme.SidebarItem[] {
125126
{
126127
text: '配置',
127128
items: [
128-
{ text: 'pluginConfig', link: 'plugin-config' },
129-
{ text: 'mockConfig', link: 'mock-config' },
129+
{ text: '插件配置', link: 'plugin-config' },
130+
{ text: 'mock 配置', link: 'mock-config' },
130131
],
131132
},
132133
{

docs/zh/examples/static.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 静态资源服务
22

3-
通过 动态路由匹配,获取请求的文件路径 `:filepath(.*)`, 获取请求中的文件路径,
3+
通过 动态路由匹配,获取请求的文件路径 `*filepath`, 获取请求中的文件路径,
44
`body` 中以文件流的方式返回,并在 `headers` 中设置正确的 `Content-Type`
55

66
```ts
@@ -13,18 +13,18 @@ import { defineMock } from 'vite-plugin-mock-dev-server'
1313
const __dirname = path.dirname(fileURLToPath(import.meta.url))
1414

1515
export default defineMock({
16-
url: '/mockData/:filepath(.*)',
16+
url: '/mock-static/*filepath',
1717
method: 'GET',
1818
headers(request) {
19-
const { filepath } = request.params
19+
const filepath = request.params.filepath.join('/')
2020
const filename = path.basename(filepath)
2121
return {
2222
'Content-Type': mime.lookup(filename) || 'text/plain',
2323
}
2424
},
2525
body(request) {
2626
const { filepath } = request.params
27-
return createReadStream(path.join(__dirname, 'mockData', filepath))
27+
return createReadStream(path.join(__dirname, 'mock-static', filepath))
2828
},
2929
})
3030
```

0 commit comments

Comments
 (0)