-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwebpack.mock.js
More file actions
34 lines (32 loc) · 1015 Bytes
/
webpack.mock.js
File metadata and controls
34 lines (32 loc) · 1015 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import webpackMockServer from "webpack-mock-server";
import path from "path";
import glob from "glob";
export default webpackMockServer.add((app, helper) => {
app.all('/api/*', function(req, res) {
function getMockApiPath() {
let mockPaths = glob.sync("**/mock/**/*.js", {
cwd: path.resolve(__dirname, "./src")
});
let mockApiPath = {};
mockPaths.forEach(mockPath => {
const fileIndex = /mock(.+)\.js/.exec(mockPath)[1];
const filepath = path.resolve(
path.resolve(__dirname, "./src"),
mockPath
);
mockApiPath[fileIndex] = filepath;
});
return mockApiPath;
}
const mockApiPath = getMockApiPath();
const url = req.url
Object.keys(mockApiPath).forEach(item => {
const strPath = mockApiPath[item];
if (url.indexOf(item) > 0) {
// removing NodeJs require-cache
delete require.cache[require.resolve(strPath)];
res.json(require(strPath));
}
});
});
});