Skip to content

Commit 9c07c99

Browse files
authored
Merge pull request #90 from pengzhanbo/improve-package
perf: improve code structure
2 parents 72c288e + 1aa7d33 commit 9c07c99

Some content is hidden

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

44 files changed

+883
-647
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ type defineMockData<T> = (
549549
) => [getter, setter] & { value: T }
550550
```
551551
552-
### Expamples
552+
### Examples
553553
554554
`data.ts`
555555

docs/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "docs",
3+
"type": "module",
4+
"version": "1.6.1",
5+
"private": true,
6+
"scripts": {
7+
"docs:build": "vitepress build .",
8+
"docs:dev": "vitepress dev .",
9+
"docs:preview": "vitepress preview ."
10+
},
11+
"devDependencies": {
12+
"vitepress": "^1.3.1"
13+
}
14+
}

example/index.html

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,39 @@
66
<meta name="viewport" content="width=device-width, initial-scale=1.0">
77
<title>Mock Server Example</title>
88
<style>
9+
.app { max-width: 1200px; margin: 0 auto; padding-bottom: 48px; }
910
.container * { margin: 0; padding: 0;}
1011
.container { margin: 10px 20px; padding: 10px 20px; border-bottom: solid 1px #ccc; color: #333; }
1112
.container p { line-height: 1.7; }
1213
.container p b { padding-right: 20px; }
1314
.container pre { margin-left: 100px; padding:10px 0 10px 20px; border-left: solid 1px #ccc; }
14-
.container pre, .container code {font-size: 1.1em;}
15+
.container pre, .container code {font-size: 1.1em; line-height: 1.7;}
1516
</style>
1617
</head>
1718
<body>
18-
<h1>Mock Example</h1>
19-
<section style="border:solid 1px #ccc; padding: 10px 20px; margin-bottom: 20px;">
20-
<h4>mock upload file:</h4>
21-
<form action="/api/upload" method="post" enctype="multipart/form-data">
22-
<p>
23-
<span>file: </span>
24-
<input type="file" name="files" multiple>
25-
</p>
26-
<p>
27-
<span>name:</span>
28-
<input type="text" name="name" value="mark">
29-
</p>
30-
<p>
31-
<input type="submit" value="submit">
32-
</p>
33-
</form>
34-
</section>
35-
<a href="/api/download" download="shared.ts">Mock Download File</a>
36-
<br>
37-
<h3>Please open the browser Devtools and switch to the network tab to view the interface information.</h3>
38-
<h3>请打开浏览器 开发者工具 并切换到 network 查看接口信息</h3>
19+
<div class="app">
20+
<h1>Mock Example</h1>
21+
<section style="border:solid 1px #ccc; padding: 10px 20px; margin-bottom: 20px;">
22+
<h4>mock upload file:</h4>
23+
<form action="/api/upload" method="post" enctype="multipart/form-data">
24+
<p>
25+
<span>file: </span>
26+
<input type="file" name="files" multiple>
27+
</p>
28+
<p>
29+
<span>name:</span>
30+
<input type="text" name="name" value="mark">
31+
</p>
32+
<p>
33+
<input type="submit" value="submit">
34+
</p>
35+
</form>
36+
</section>
37+
<a href="/api/download" download="shared.ts">Mock Download File</a>
38+
<br>
39+
<h3>Please open the browser Devtools and switch to the network tab to view the interface information.</h3>
40+
<h3>请打开浏览器 开发者工具 并切换到 network 查看接口信息</h3>
41+
</div>
3942
<script type="module" src="./src/main.ts"></script>
4043
</body>
4144
</html>

example/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "example",
3+
"type": "module",
4+
"version": "1.6.1",
5+
"private": true,
6+
"scripts": {
7+
"dev": "vite dev",
8+
"build": "vite build"
9+
},
10+
"devDependencies": {
11+
"@types/mockjs": "^1.0.10",
12+
"mockjs": "^1.1.0",
13+
"vite": "^5.4.0",
14+
"vite-plugin-mock-dev-server": "workspace:*"
15+
}
16+
}

example/src/request.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ async function renderResponse(url: string, method: string, response: Response) {
3232
content += `<div><p><b>Response Body: </b></p><pre><code>${str}</code></pre></div>`
3333
}
3434
container.innerHTML = content
35-
document.body.appendChild(container)
35+
document.querySelector('.app')!.appendChild(container)
3636
}
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
{
22
"compilerOptions": {
3-
"composite": true,
43
"target": "ESNext",
54
"jsx": "preserve",
65
"lib": ["DOM", "ESNext"],
6+
"baseUrl": ".",
77
"module": "ESNext",
88
"moduleResolution": "node",
9-
"paths": {
10-
"vite-plugin-mock-dev-server": ["./src/index.ts"]
11-
},
129
"resolveJsonModule": true,
1310
"allowJs": true,
1411
"strict": true,
@@ -18,6 +15,6 @@
1815
"forceConsistentCasingInFileNames": true,
1916
"skipLibCheck": true
2017
},
21-
"include": ["./package.json", "./example", "./src"],
22-
"exclude": ["./example/**/*.mjs", "./example/**/*.cjs", "./example/**/*.js"]
18+
"include": ["./src", "./mock"],
19+
"exclude": ["./**/*.mjs", "./**/*.cjs", "./**/*.js"]
2320
}

example/vite.config.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import path from 'node:path'
22
import process from 'node:process'
33
import { defineConfig } from 'vite'
4-
import mockServer from '../src'
4+
import { mockDevServerPlugin } from 'vite-plugin-mock-dev-server'
55

66
export default defineConfig(({ mode }) => ({
77
plugins: [
8-
mockServer({
8+
mockDevServerPlugin({
99
prefix: '^/api-dev/',
1010
wsPrefix: ['/socket.io'],
11-
include: 'example/mock/**/*.mock.{ts,js,cjs,mjs,json,json5}',
1211
log: 'debug',
1312
formidableOptions: {
1413
// 配置上传资源存放目录
15-
uploadDir: path.join(process.cwd(), 'example/uploads'),
14+
uploadDir: path.join(process.cwd(), '/uploads'),
1615
// 可修改上传资源名称
1716
filename: (name, ext, part) => {
1817
return part.originalFilename!

netlify.toml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
[build]
22
publish = "docs/.vitepress/dist"
3-
command = "pnpm install && pnpm docs:build"
4-
5-
[build.environment]
6-
NODE_VERSION = "18" # prevent Netlify npm install
7-
NPM_FLAGS = "--version"
3+
command = "pnpm docs:build"

package.json

Lines changed: 10 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,23 @@
11
{
2-
"name": "vite-plugin-mock-dev-server",
2+
"name": "mock-dev-server-monorepo",
33
"type": "module",
44
"version": "1.6.1",
5+
"private": true,
56
"packageManager": "[email protected]",
67
"author": "pengzhanbo <[email protected]> (https://github.com/pengzhanbo)",
78
"license": "MIT",
8-
"homepage": "https://vite-plugin-mock-dev-server.netlify.app",
9-
"repository": {
10-
"type": "git",
11-
"url": "https://github.com/pengzhanbo/vite-plugin-mock-dev-server"
12-
},
13-
"keywords": [
14-
"vite",
15-
"plugin",
16-
"vite-plugin",
17-
"mock",
18-
"mock-server",
19-
"development"
20-
],
21-
"exports": {
22-
".": {
23-
"import": {
24-
"types": "./dist/index.d.ts",
25-
"default": "./dist/index.js"
26-
},
27-
"require": {
28-
"types": "./dist/index.d.cts",
29-
"default": "./dist/index.cjs"
30-
}
31-
},
32-
"./package.json": "./package.json"
33-
},
34-
"main": "dist/index.js",
35-
"types": "dist/index.d.ts",
36-
"files": [
37-
"dist"
38-
],
39-
"engines": {
40-
"node": "^18 || >= 20"
41-
},
429
"scripts": {
43-
"build": "tsup && node ./deprecate.mjs",
44-
"dev": "DEBUG=vite:mock-dev-server vite example --config ./example/vite.config.ts",
45-
"example:build": "DEBUG=vite:mock-dev-server vite build example --config ./example/vite.config.ts",
46-
"docs:build": "vitepress build docs",
47-
"docs:dev": "vitepress dev docs",
48-
"docs:preview": "vitepress preview docs",
10+
"build": "pnpm -F vite-plugin-mock-dev-server build",
11+
"dev": "pnpm -F example dev",
12+
"example:build": "pnpm -F example build",
13+
"docs:build": "pnpm -F docs docs:build",
14+
"docs:dev": "pnpm -F docs docs:dev",
15+
"docs:preview": "pnpm -F docs docs:preview",
4916
"lint": "eslint .",
5017
"test": "vitest",
5118
"prepublishOnly": "vitest run && pnpm run build",
5219
"release:changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
53-
"release": "bumpp package.json --execute=\"pnpm release:changelog\" --commit --all --push --tag && pnpm publish --access public"
54-
},
55-
"peerDependencies": {
56-
"vite": ">=3.0.0"
57-
},
58-
"dependencies": {
59-
"@pengzhanbo/utils": "^1.1.2",
60-
"@rollup/pluginutils": "^5.1.0",
61-
"chokidar": "^3.6.0",
62-
"co-body": "^6.2.0",
63-
"cookies": "^0.9.1",
64-
"cors": "^2.8.5",
65-
"debug": "^4.3.6",
66-
"esbuild": "^0.23.0",
67-
"fast-glob": "^3.3.2",
68-
"formidable": "2.1.2",
69-
"http-status": "^1.7.4",
70-
"is-core-module": "^2.15.0",
71-
"json5": "^2.2.3",
72-
"mime-types": "^2.1.35",
73-
"path-to-regexp": "6.2.2",
74-
"picocolors": "^1.0.1",
75-
"ws": "^8.18.0"
20+
"release": "bumpp package.json --execute=\"pnpm release:changelog\" --commit --all --push --tag && pnpm -r publish --access public"
7621
},
7722
"devDependencies": {
7823
"@pengzhanbo/eslint-config": "^1.12.0",
@@ -88,11 +33,8 @@
8833
"bumpp": "^9.4.1",
8934
"conventional-changelog-cli": "^5.0.0",
9035
"eslint": "^9.8.0",
91-
"mockjs": "^1.1.0",
92-
"tsup": "^8.2.3",
36+
"tsup": "^8.2.4",
9337
"typescript": "^5.5.4",
94-
"vite": "^5.3.5",
95-
"vitepress": "^1.3.1",
9638
"vitest": "^2.0.4"
9739
}
9840
}

plugin/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 pengzhanbo
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

plugin/README.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# vite-plugin-mock-dev-server
2+
3+
Vite Plugin for API mock dev server.
4+
5+
<p align="center">
6+
<a href="https://www.npmjs.com/package/vite-plugin-mock-dev-server"><img alt="npm" src="https://img.shields.io/npm/v/vite-plugin-mock-dev-server?style=flat-square"></a>
7+
<img alt="node-current" src="https://img.shields.io/node/v/vite-plugin-mock-dev-server?style=flat-square">
8+
<img alt="npm peer dependency version" src="https://img.shields.io/npm/dependency-version/vite-plugin-mock-dev-server/peer/vite?style=flat-square">
9+
<img alt="npm" src="https://img.shields.io/npm/dm/vite-plugin-mock-dev-server?style=flat-square">
10+
<br>
11+
<img alt="GitHub Workflow Status" src="https://img.shields.io/github/actions/workflow/status/pengzhanbo/vite-plugin-mock-dev-server/lint.yml?style=flat-square">
12+
<a href="https://app.fossa.com/projects/git%2Bgithub.com%2Fpengzhanbo%2Fvite-plugin-mock-dev-server?ref=badge_shield"><img alt="fossa status" src="https://app.fossa.com/api/projects/git%2Bgithub.com%2Fpengzhanbo%2Fvite-plugin-mock-dev-server.svg?type=shield"></a>
13+
</p>
14+
15+
## Features
16+
17+
- ⚡️ Lightweight, Flexible, Fast.
18+
- 🧲 Not injection-based, non-intrusive to client code.
19+
- 💡 ESModule/commonjs.
20+
- 🦾 Typescript.
21+
- 🔥 HMR
22+
- 🏷 Support `.[cm]?js` / `.ts` / `.json` / `.json5`.
23+
- 📦 Auto import mock file.
24+
- 🎨 Support any lib, like `mockjs`, or do not use it.
25+
- 📥 Path rule matching, request parameter matching.
26+
- ⚙️ Support Enabled/Disabled any one of the API mock.
27+
- 📀 Supports response body content type such as `text/json/buffer/stream`.
28+
- ⚖️ Use `server.proxy`
29+
- 🍕 Support `viteConfig.define` and `env` in the mock file.
30+
- ⚓️ Support `viteConfig.resolve.alias` in the mock file.
31+
- 🌈 Support `vite preview` mode.
32+
- 📤 Support `multipart` content-type, mock upload file.
33+
- 📥 Support mock download file.
34+
- ⚜️ Support `WebSocket Mock`
35+
- 🗂 Support building small independent deployable mock services.
36+
37+
## Documentation
38+
39+
See the [documentation](https://vite-plugin-mock-dev-server.netlify.app/en/) for more details.
40+
41+
[![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)
42+
43+
## Install
44+
45+
``` sh
46+
# npm
47+
npm i -D vite-plugin-mock-dev-server
48+
# yarn
49+
yarn add vite-plugin-mock-dev-server
50+
# pnpm
51+
pnpm add -D vite-plugin-mock-dev-server
52+
```
53+
54+
## Usage
55+
56+
`vite.config.ts`
57+
58+
``` ts
59+
import { defineConfig } from 'vite'
60+
import mockDevServerPlugin from 'vite-plugin-mock-dev-server'
61+
62+
export default defineConfig({
63+
plugins: [
64+
mockDevServerPlugin(/* plugin options */),
65+
],
66+
// The fields defined here can also be used in mock.
67+
define: {},
68+
server: {
69+
// plugin will read `server.proxy`
70+
proxy: {
71+
'^/api': { target: 'http://example.com' }
72+
}
73+
}
74+
})
75+
```
76+
77+
The plugin will read the configuration of `server.proxy` or `options.prefix`, and enable mock matching for matched URLs.
78+
79+
The plugin will also read the `define` configuration, which supports direct use in mock files.
80+
81+
## Edit Mock File
82+
83+
By default, write mock data in the `mock` directory of your project's root directory:
84+
85+
`mock/**/*.mock.ts` :
86+
87+
``` ts
88+
import { defineMock } from 'vite-plugin-mock-dev-server'
89+
90+
export default defineMock({
91+
url: '/api/test',
92+
body: { a: 1, b: 2 }
93+
})
94+
```
95+
96+
## Links
97+
98+
- [vite](https://vitejs.dev/)
99+
- [awesome-vite](https://github.com/vitejs/awesome-vite#helpers)
100+
- [rspack-plugin-mock](https://github.com/pengzhanbo/rspack-plugin-mock) - **Rspack** and **Rsbuild** plugin for API mock server
101+
102+
## LICENSE
103+
104+
The plugin is licensed under the [MIT License](./LICENSE)
105+
106+
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fpengzhanbo%2Fvite-plugin-mock-dev-server.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fpengzhanbo%2Fvite-plugin-mock-dev-server?ref=badge_large)
File renamed without changes.

0 commit comments

Comments
 (0)