Skip to content

Commit 8be5db5

Browse files
committed
add example and README_zh.md file
1 parent 29384cc commit 8be5db5

10 files changed

Lines changed: 198 additions & 4 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/node_modules/
1+
node_modules
22
/dist/
33
/coverage/
44
npm-debug.*.log

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Happy-Mock-Webpack--Plugin
22

3+
[中文文档](./README_zh.md)
4+
35
This plugin provides a simple way to mock your data via webpack and webapck-dev-server. You can easily mock your own data
46
with just a little configuration.
57

@@ -13,6 +15,8 @@ npm install happy-mock-webpack-plugin
1315

1416
## Usage
1517

18+
[Example](./examples/base/README.md)
19+
1620
In webpack config file, add this plugin instance:
1721

1822
```js
@@ -108,7 +112,7 @@ Other files will be returned as it is.
108112

109113
If you set it to `true`, every time you change your mock files, the browser will automatically refresh to make sure you can get the lateset data. If it's `false`, you need to refresh the browser by yourself to make it.
110114

111-
> Note: this field works only when `lazy=true`, otherwise the cache will take priority
115+
> Note: this field works only when `lazy=true`, otherwise the cache will take priority and you need to restart the app to get the updated mock files.
112116
113117

114118
## LICENSE

README_zh.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Happy-Mock-Webpack--Plugin
2+
3+
这个插件提供了一个简单的方式,只需要一点配置,你就可以在基于`webpack``webpack-dev-server`的项目中轻松的mock数据。
4+
5+
> 插件当前只支持`GET`请求的数据mock.
6+
7+
## 安装
8+
9+
```bash
10+
npm install happy-mock-webpack-plugin
11+
```
12+
13+
## 使用
14+
15+
[例子](./examples/base/README.md)
16+
17+
在webpack的配置文件中,加入插件:
18+
19+
```js
20+
const HappyMockMockPlugin = require('happy-mock-webpack-plugin')
21+
const path = require('path')
22+
23+
module.exports = {
24+
// other configs
25+
plugins: [
26+
// other plugins...
27+
new HappyMockMockPlugin({
28+
root: path.resolve(__dirname, 'mock'),
29+
lazy: true,
30+
autoRefresh: true
31+
})
32+
]
33+
}
34+
```
35+
36+
### Mock 文件
37+
38+
目录结构:
39+
40+
```
41+
- src
42+
- app.js
43+
- mock
44+
- user
45+
- profile.json
46+
- orders.js
47+
- menu.js
48+
```
49+
50+
这里的目录路径就是请求的路径,比如,如果你使用了`axios`,那么`axios.get('/user/profile')`将会请求`mock/user/profile.json`文件。
51+
下面提供几种mock文件的例子:
52+
53+
- /mock/user/orders.js
54+
55+
这个js文件导出一个函数,这个函数接收两个参数: `request``response`,这两个对象来自于`webpack-dev-server`中,即是`express`中的请求和响应对象。你可以根据请求参数的不同来返回不同的结果。
56+
57+
```js
58+
module.exports = (request, response) => {
59+
// handle request ...
60+
// modify response ...
61+
return {
62+
data: []
63+
}
64+
}
65+
```
66+
67+
- /mock/user/profile.json
68+
69+
```json
70+
{
71+
"name": "John Doe"
72+
}
73+
74+
```
75+
76+
- /mock/menu.js
77+
```js
78+
module.exports = {
79+
items: [],
80+
from: 'xx'
81+
}
82+
83+
```
84+
85+
其他文件都会按原样返回。
86+
87+
88+
### 配置
89+
90+
- **root**
91+
92+
`String`
93+
94+
mock文件夹的路径,**必须是一个绝对路径**
95+
96+
97+
- **lazy**
98+
99+
`Boolean`
100+
101+
如果设置成true的话,文件将会在接收到对应请求时被加载,在这种情况下,请求处理时间可能会稍微长一点,但是项目启动的时间相对设置成false时短了。
102+
103+
如果设置成false的话,项目会在启动的时候就加载好所有的mock文件放入缓存中,所以项目启动时间会有所增加,但是请求的响应速度会加快。
104+
105+
106+
- **autoRefresh**
107+
108+
`Boolean`
109+
110+
设置成true的话,只要你修改了mock文件夹下的文件,浏览器都会自动刷新以获取最新的数据。设置成false的话,你只能手动刷新来打到目的。
111+
112+
> 注意:这个配置只有在`lazy`设置成true的时候起作用。如果`lazy``false`的话,那么优先会加载缓存,你需要重启项目才能获得更新后的mock数据。
113+
114+
115+
## LICENSE
116+
117+
[MIT](./LICENSE)

examples/base/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
1. install packages
3+
```bash
4+
npm i
5+
```
6+
7+
2. run the app
8+
```bash
9+
npm run dev
10+
```

examples/base/mock/person.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "hello999"
3+
}

examples/base/mock/user/profile.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = () => ({
2+
name: 'John',
3+
age: 18,
4+
});

examples/base/package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "test-mocker-plugin",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"dev": "webpack-dev-server"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC",
13+
"devDependencies": {
14+
"happy-mock-webpack-plugin": "^0.0.13",
15+
"webpack": "^4.41.2",
16+
"webpack-cli": "^3.3.10",
17+
"webpack-dev-server": "^3.9.0"
18+
},
19+
"dependencies": {
20+
"axios": "^0.19.0",
21+
"html-webpack-plugin": "^3.2.0"
22+
}
23+
}

examples/base/src/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import axios from 'axios';
2+
3+
axios.get('/user/profile').then((res) => {
4+
const { data } = res;
5+
document.writeln(JSON.stringify(data));
6+
document.close();
7+
});

examples/base/webpack.config.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const path = require('path');
2+
const HappyMockMockPlugin = require('happy-mock-webpack-plugin');
3+
const HtmlWebpackPlugin = require('html-webpack-plugin');
4+
5+
module.exports = {
6+
entry: './src/index.js',
7+
output: {
8+
filename: 'bundle.js',
9+
path: path.resolve(__dirname, 'dist'),
10+
},
11+
devServer: {
12+
noInfo: true,
13+
quiet: true,
14+
before: () => {
15+
// console.log('outer before');
16+
},
17+
},
18+
plugins: [
19+
new HtmlWebpackPlugin(),
20+
new HappyMockMockPlugin({
21+
root: path.resolve(__dirname, 'mock'),
22+
lazy: true,
23+
autoRefresh: true, // set to false to disable auto refresh
24+
}),
25+
],
26+
};

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"name": "happy-mock-webpack-plugin",
3-
"version": "0.0.13",
3+
"version": "0.0.14",
44
"description": "A simple data mock plugin for webpack based on webpack-dev-server",
55
"main": "index.js",
66
"directories": {
77
"lib": "lib"
88
},
99
"scripts": {
1010
"test": "echo \"Error: no test specified\" && exit 1",
11-
"lint": "eslint lib/*.js"
11+
"lint": "eslint lib/*.js examples/**/*.js"
1212
},
1313
"keywords": [
1414
"mock",

0 commit comments

Comments
 (0)