Skip to content

Commit

Permalink
chore: initial commit!
Browse files Browse the repository at this point in the history
  • Loading branch information
pengzhanbo committed Aug 2, 2024
0 parents commit 18b5ad7
Show file tree
Hide file tree
Showing 52 changed files with 9,273 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .gitignore
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*
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cSpell.words": [
"rsbuild",
"rspack"
]
}
91 changes: 91 additions & 0 deletions README.md
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)
3 changes: 3 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import config from '@pengzhanbo/eslint-config'

export default config()
13 changes: 13 additions & 0 deletions examples/rsbuild-starter/.gitignore
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
9 changes: 9 additions & 0 deletions examples/rsbuild-starter/mock/file-extension/api.mock.cjs
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.',
},
}
8 changes: 8 additions & 0 deletions examples/rsbuild-starter/mock/file-extension/api.mock.js
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.',
},
})
9 changes: 9 additions & 0 deletions examples/rsbuild-starter/mock/file-extension/api.mock.json
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."
}
}
9 changes: 9 additions & 0 deletions examples/rsbuild-starter/mock/file-extension/api.mock.json5
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."
}
}
8 changes: 8 additions & 0 deletions examples/rsbuild-starter/mock/file-extension/api.mock.mjs
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.',
},
})
10 changes: 10 additions & 0 deletions examples/rsbuild-starter/mock/file-extension/api.mock.ts
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.',
},
},
])
16 changes: 16 additions & 0 deletions examples/rsbuild-starter/package.json
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"
}
}
13 changes: 13 additions & 0 deletions examples/rsbuild-starter/rsbuild.config.ts
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(),
],
})
26 changes: 26 additions & 0 deletions examples/rsbuild-starter/src/index.css
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;
}
8 changes: 8 additions & 0 deletions examples/rsbuild-starter/src/index.ts
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>
`;
16 changes: 16 additions & 0 deletions examples/rsbuild-starter/tsconfig.json
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"]
}
13 changes: 13 additions & 0 deletions examples/rspack-starter/.gitignore
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
11 changes: 11 additions & 0 deletions examples/rspack-starter/index.html
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>
9 changes: 9 additions & 0 deletions examples/rspack-starter/mock/file-extension/api.mock.cjs
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.',
},
}
8 changes: 8 additions & 0 deletions examples/rspack-starter/mock/file-extension/api.mock.js
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.',
},
})
9 changes: 9 additions & 0 deletions examples/rspack-starter/mock/file-extension/api.mock.json
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."
}
}
9 changes: 9 additions & 0 deletions examples/rspack-starter/mock/file-extension/api.mock.json5
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."
}
}
8 changes: 8 additions & 0 deletions examples/rspack-starter/mock/file-extension/api.mock.mjs
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.',
},
})
10 changes: 10 additions & 0 deletions examples/rspack-starter/mock/file-extension/api.mock.ts
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.',
},
},
])
16 changes: 16 additions & 0 deletions examples/rspack-starter/package.json
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:*"
}
}
Loading

0 comments on commit 18b5ad7

Please sign in to comment.