support hmr
This commit is contained in:
parent
7492e2097f
commit
5e8bc40509
|
@ -0,0 +1,69 @@
|
||||||
|
|
||||||
|
const chokidar = require('chokidar')
|
||||||
|
const bodyParser = require('body-parser')
|
||||||
|
|
||||||
|
function getPath(path) {
|
||||||
|
var match = path.toString()
|
||||||
|
.replace('\\/?', '')
|
||||||
|
.replace('(?=\\/|$)', '$')
|
||||||
|
.match(/^\/\^((?:\\[.*+?^${}()|[\]\\\/]|[^.*+?^${}()|[\]\\\/])*)\$\//)
|
||||||
|
return match
|
||||||
|
? match[1].replace(/\\(.)/g, '$1').split('/')
|
||||||
|
: path.toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
function registerRoutes(app) {
|
||||||
|
const { default: mocks } = require('./index.js')
|
||||||
|
for (const mock of mocks) {
|
||||||
|
app[mock.type](mock.url, mock.response)
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
mockRoutesLength: Object.keys(mocks).length,
|
||||||
|
caches: require.cache
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function unregisterRoutes(caches) {
|
||||||
|
Object.keys(caches).forEach(i => {
|
||||||
|
if (i.includes('/mock')) {
|
||||||
|
delete caches[require.resolve(i)]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function getMockRoutesIndex(app) {
|
||||||
|
for (let index = 0; index <= app._router.stack.length; index++) {
|
||||||
|
const r = app._router.stack[index]
|
||||||
|
if (r.route && r.route.path) {
|
||||||
|
const path = getPath(r.route.path)
|
||||||
|
if (path.includes('mock')) {
|
||||||
|
return index
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = app => {
|
||||||
|
require('@babel/register')
|
||||||
|
|
||||||
|
app.use(bodyParser.json())
|
||||||
|
app.use(bodyParser.urlencoded({
|
||||||
|
extended: true
|
||||||
|
}))
|
||||||
|
|
||||||
|
const { mockRoutesLength, caches } = registerRoutes(app)
|
||||||
|
|
||||||
|
chokidar.watch(('./mock'), { ignored: 'mock/mock-server.js', persistent: true, ignoreInitial: false }).on('all', (event, path) => {
|
||||||
|
if (event === 'change' || event === 'add') {
|
||||||
|
const index = getMockRoutesIndex(app)
|
||||||
|
|
||||||
|
app._router.stack.splice(index, mockRoutesLength)
|
||||||
|
console.log('caches')
|
||||||
|
console.log(index)
|
||||||
|
// console.log(path)
|
||||||
|
unregisterRoutes(caches)
|
||||||
|
// console.log(app)
|
||||||
|
registerRoutes(app)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
|
@ -82,6 +82,7 @@
|
||||||
"babel-eslint": "10.0.1",
|
"babel-eslint": "10.0.1",
|
||||||
"babel-jest": "23.6.0",
|
"babel-jest": "23.6.0",
|
||||||
"chalk": "2.4.2",
|
"chalk": "2.4.2",
|
||||||
|
"chokidar": "2.1.5",
|
||||||
"connect": "3.6.6",
|
"connect": "3.6.6",
|
||||||
"eslint": "5.15.3",
|
"eslint": "5.15.3",
|
||||||
"eslint-plugin-vue": "5.2.2",
|
"eslint-plugin-vue": "5.2.2",
|
||||||
|
|
|
@ -41,22 +41,7 @@ module.exports = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
after(app) {
|
after: require('./mock/mock-server.js')
|
||||||
require('@babel/register')
|
|
||||||
const bodyParser = require('body-parser')
|
|
||||||
|
|
||||||
// parse app.body
|
|
||||||
// http://expressjs.com/en/4x/api.html#req.body
|
|
||||||
app.use(bodyParser.json())
|
|
||||||
app.use(bodyParser.urlencoded({
|
|
||||||
extended: true
|
|
||||||
}))
|
|
||||||
|
|
||||||
const { default: mocks } = require('./mock')
|
|
||||||
for (const mock of mocks) {
|
|
||||||
app[mock.type](mock.url, mock.response)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
configureWebpack: {
|
configureWebpack: {
|
||||||
// provide the app's title in webpack's name field, so that
|
// provide the app's title in webpack's name field, so that
|
||||||
|
|
Loading…
Reference in New Issue