-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 18b5ad7
Showing
52 changed files
with
9,273 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
.DS_Store | ||
|
||
node_modules | ||
dist | ||
.idea | ||
docs/.vitepress/cache | ||
|
||
# Log files | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
pnpm-debug.log* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"cSpell.words": [ | ||
"rsbuild", | ||
"rspack" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# rspack-plugin-mock | ||
|
||
[rspack](https://rspack.dev) and [rsbuild](https://rsbuild.dev) plugin for API mock dev server. | ||
|
||
Implement a mock-dev-server in `rspack` and `rsbuild` that is fully consistent with [vite-plugin-mock-dev-server](https://github.com/pengzhanbo/vite-plugin-mock-dev-server). | ||
|
||
> [!IMPORTANT] | ||
> The plugin is not ready yet and is actively being developed. | ||
## Usage | ||
|
||
### Install | ||
|
||
```sh | ||
# npm | ||
npm i -D rspack-plugin-mock | ||
# yarn | ||
yarn add rspack-plugin-mock | ||
# pnp | ||
pnpm add -D rspack-plugin-mock | ||
``` | ||
|
||
### Configuration | ||
|
||
**In Rspack** | ||
|
||
```ts | ||
// rspack.config.js | ||
import { MockServerPlugin } from 'rspack-plugin-mock' | ||
|
||
export default { | ||
devServer: { | ||
// The plugin will read the `proxy` option from the `devServer` | ||
proxy: [ | ||
{ context: '/api', target: 'http://example.com' }, | ||
], | ||
}, | ||
plugins: [ | ||
new MockServerPlugin(/* pluginOptions */), | ||
] | ||
} | ||
``` | ||
|
||
**In Rsbuild** | ||
|
||
```ts | ||
// rsbuild.config.ts | ||
import { defineConfig } from '@rsbuild/core' | ||
import { pluginMockServer } from 'rspack-plugin-mock/rsbuild' | ||
|
||
export default defineConfig({ | ||
server: { | ||
// The plugin will read the `proxy` option from the `server` | ||
proxy: { | ||
'/api': 'http://example.com', | ||
}, | ||
}, | ||
plugins: [ | ||
pluginMockServer(/* pluginOptions */), | ||
], | ||
}) | ||
``` | ||
|
||
### Edit Mock file | ||
|
||
By default, write mock data in the mock directory of your project's root directory: | ||
|
||
`mock/**/*.mock.ts` : | ||
|
||
```ts | ||
import { defineMock } from 'rspack-plugin-mock/helper' | ||
|
||
export default defineMock({ | ||
url: '/api/test', | ||
body: { a: 1, b: 2 } | ||
}) | ||
``` | ||
|
||
## PluginOptions | ||
|
||
TODO... | ||
|
||
## Mock Configuration | ||
|
||
TODO... | ||
|
||
## Redirect | ||
|
||
- [rspack](https://rspack.dev) | ||
- [rsbuild](https://rsbuild.dev) | ||
- [vite-plugin-mock-dev-server](https://github.com/pengzhanbo/vite-plugin-mock-dev-server) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import config from '@pengzhanbo/eslint-config' | ||
|
||
export default config() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Local | ||
.DS_Store | ||
*.local | ||
*.log* | ||
|
||
# Dist | ||
node_modules | ||
dist/ | ||
|
||
# IDE | ||
.vscode/* | ||
!.vscode/extensions.json | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* @type {import('rspack-plugin-mock').MockHttpItem} | ||
*/ | ||
module.exports = { | ||
url: '/api/common-js', | ||
body: { | ||
message: 'Write mock configuration using a CommonJs file.', | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { defineMock } from 'rspack-plugin-mock/helper' | ||
|
||
export default defineMock({ | ||
url: '/api/javascript', | ||
body: { | ||
message: 'Write mock configuration using a js file.', | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"url": "/api/json", | ||
"headers": { | ||
"X-Custom-Header": "your custom header" | ||
}, | ||
"body": { | ||
"message": "Write mock configuration using a json file." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"url": "/api/json5", | ||
"headers": { | ||
"X-Custom-Header": "your custom header" | ||
}, | ||
"body": { | ||
"message": "Write mock configuration using a json5 file." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { defineMock } from 'rspack-plugin-mock/helper' | ||
|
||
export default defineMock({ | ||
url: '/api/es-module-js', | ||
body: { | ||
message: 'Write mock configuration using a ESModule js file.', | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { defineMock } from 'rspack-plugin-mock/helper' | ||
|
||
export default defineMock([ | ||
{ | ||
url: '/api/typescript', | ||
body: { | ||
message: 'Write mock configuration using a typescript file.', | ||
}, | ||
}, | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "rsbuild-starter", | ||
"type": "module", | ||
"version": "1.0.0", | ||
"private": true, | ||
"scripts": { | ||
"dev": "rsbuild dev --open", | ||
"build": "rsbuild build", | ||
"preview": "rsbuild preview" | ||
}, | ||
"devDependencies": { | ||
"@rsbuild/core": "1.0.1-beta.8", | ||
"rspack-plugin-mock": "workspace:*", | ||
"typescript": "^5.5.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { defineConfig } from '@rsbuild/core' | ||
import { pluginMockServer } from 'rspack-plugin-mock/rsbuild' | ||
|
||
export default defineConfig({ | ||
server: { | ||
proxy: { | ||
'/api': 'http://localhost:8080', | ||
}, | ||
}, | ||
plugins: [ | ||
pluginMockServer(), | ||
], | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
body { | ||
margin: 0; | ||
color: #fff; | ||
font-family: Inter, Avenir, Helvetica, Arial, sans-serif; | ||
background-image: linear-gradient(to bottom, #020917, #101725); | ||
} | ||
|
||
.content { | ||
display: flex; | ||
min-height: 100vh; | ||
line-height: 1.1; | ||
text-align: center; | ||
flex-direction: column; | ||
justify-content: center; | ||
} | ||
|
||
.content h1 { | ||
font-size: 3.6rem; | ||
font-weight: 700; | ||
} | ||
|
||
.content p { | ||
font-size: 1.2rem; | ||
font-weight: 400; | ||
opacity: 0.5; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import './index.css'; | ||
|
||
document.querySelector('#root')!.innerHTML = ` | ||
<div class="content"> | ||
<h1>Vanilla Rsbuild</h1> | ||
<p>Start building amazing things with Rsbuild.</p> | ||
</div> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ES2020", | ||
"lib": ["DOM", "ES2020"], | ||
"useDefineForClassFields": true, | ||
"module": "ESNext", | ||
"moduleResolution": "bundler", | ||
"resolveJsonModule": true, | ||
"allowImportingTsExtensions": true, | ||
"strict": true, | ||
"noEmit": true, | ||
"isolatedModules": true, | ||
"skipLibCheck": true | ||
}, | ||
"include": ["src"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Local | ||
.DS_Store | ||
*.local | ||
*.log* | ||
|
||
# Dist | ||
node_modules | ||
dist/ | ||
|
||
# IDE | ||
.vscode/* | ||
!.vscode/extensions.json | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Rspack</title> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* @type {import('rspack-plugin-mock').MockHttpItem} | ||
*/ | ||
module.exports = { | ||
url: '/api/common-js', | ||
body: { | ||
message: 'Write mock configuration using a CommonJs file.', | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { defineMock } from 'rspack-plugin-mock/helper' | ||
|
||
export default defineMock({ | ||
url: '/api/javascript', | ||
body: { | ||
message: 'Write mock configuration using a js file.', | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"url": "/api/json", | ||
"headers": { | ||
"X-Custom-Header": "your custom header" | ||
}, | ||
"body": { | ||
"message": "Write mock configuration using a json file." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"url": "/api/json5", | ||
"headers": { | ||
"X-Custom-Header": "your custom header" | ||
}, | ||
"body": { | ||
"message": "Write mock configuration using a json5 file." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { defineMock } from 'rspack-plugin-mock/helper' | ||
|
||
export default defineMock({ | ||
url: '/api/es-module-js', | ||
body: { | ||
message: 'Write mock configuration using a ESModule js file.', | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { defineMock } from 'rspack-plugin-mock/helper' | ||
|
||
export default defineMock([ | ||
{ | ||
url: '/api/typescript', | ||
body: { | ||
message: 'Write mock configuration using a typescript file.', | ||
}, | ||
}, | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "rspack-starter", | ||
"type": "module", | ||
"version": "1.0.0", | ||
"private": true, | ||
"scripts": { | ||
"dev": "cross-env NODE_ENV=development rspack serve", | ||
"build": "cross-env NODE_ENV=production rspack build" | ||
}, | ||
"devDependencies": { | ||
"@rspack/cli": "1.0.0-beta.1", | ||
"@rspack/core": "1.0.0-beta.1", | ||
"cross-env": "^7.0.3", | ||
"rspack-plugin-mock": "workspace:*" | ||
} | ||
} |
Oops, something went wrong.