-
Notifications
You must be signed in to change notification settings - Fork 0
/
vue.config.js
40 lines (37 loc) · 1.09 KB
/
vue.config.js
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
35
36
37
38
39
40
const path = require('path')
const { pathToRegexp } = require('path-to-regexp')
const isMock = process.env.MOCK
const webpackConfig = {
devServer: {}
}
// mock API response
if (isMock) {
const mockDir = path.resolve(__dirname, './mock')
webpackConfig.devServer.before = app => {
app.use((req, res, next) => {
// TODO: need to optimize
// remove require cache for every request
Object.keys(require.cache).forEach(file => {
if (file.startsWith(mockDir)) delete require.cache[file]
})
const mocks = require('./mock')
const matched = Object.keys(mocks).find(key => {
const [method, url] = key.split(/\s+/)
const isMethodMatch = req.method === method
const isUrlMath = pathToRegexp(url).test(req.path)
return isMethodMatch && isUrlMath
})
if (matched) {
const mock = mocks[matched]
const mockData =
typeof mock === 'function' ? mock(req.query, req.params) : mock
res.send(mockData)
} else {
next()
}
})
}
}
module.exports = {
configureWebpack: webpackConfig
}