From 8aa8e357d93de68652ca875f21b43acda083e803 Mon Sep 17 00:00:00 2001 From: Pan Date: Mon, 8 Apr 2019 10:36:30 +0800 Subject: [PATCH] refine --- mock/index.js | 2 +- mock/mock-server.js | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/mock/index.js b/mock/index.js index 7d9609ab..5501742d 100644 --- a/mock/index.js +++ b/mock/index.js @@ -2,7 +2,7 @@ import Mock from 'mockjs' import mocks from './mocks' import { param2Obj } from '../src/utils' -const MOCK_API_BASE = '/mock' +export const MOCK_API_BASE = '/mock' export function mockXHR() { // 修复在使用 MockJS 情况下,设置 withCredentials = true,且未被拦截的跨域请求丢失 Cookies 的问题 diff --git a/mock/mock-server.js b/mock/mock-server.js index e5966d6f..d9f084fb 100644 --- a/mock/mock-server.js +++ b/mock/mock-server.js @@ -3,20 +3,20 @@ const bodyParser = require('body-parser') const chalk = require('chalk') function registerRoutes(app) { - const { default: mocks } = require('./index.js') + const { default: mocks, MOCK_API_BASE } = require('./index.js') for (const mock of mocks) { app[mock.type](mock.url, mock.response) } return { mockRoutesLength: Object.keys(mocks).length, - caches: require.cache + MOCK_API_BASE: MOCK_API_BASE } } -function unregisterRoutes(caches) { - Object.keys(caches).forEach(i => { - if (i.includes('/mock')) { - delete caches[require.resolve(i)] +function unregisterRoutes(MOCK_API_BASE) { + Object.keys(require.cache).forEach(i => { + if (i.includes(MOCK_API_BASE)) { + delete require.cache[require.resolve(i)] } }) } @@ -54,7 +54,7 @@ module.exports = app => { extended: true })) - const { mockRoutesLength, caches } = registerRoutes(app) + const { mockRoutesLength, MOCK_API_BASE } = registerRoutes(app) // watch files, hot reload mock server chokidar.watch(('./mock'), { @@ -70,7 +70,7 @@ module.exports = app => { app._router.stack.splice(index, mockRoutesLength) // clear routes cache - unregisterRoutes(caches) + unregisterRoutes(MOCK_API_BASE) registerRoutes(app)