From 5c87772025d626762e8b5115efe29c691424bdbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8A=B1=E8=A3=A4=E8=A1=A9?= Date: Mon, 15 Jun 2020 17:07:49 +0800 Subject: [PATCH] refactor: change mock files to commonjs --- mock/index.js | 16 ++++++++++------ mock/mock-server.js | 5 +---- mock/table.js | 4 ++-- mock/user.js | 2 +- mock/utils.js | 23 +++++++++++++++++++++++ package.json | 2 -- 6 files changed, 37 insertions(+), 15 deletions(-) create mode 100644 mock/utils.js diff --git a/mock/index.js b/mock/index.js index aca99f4..c514c13 100644 --- a/mock/index.js +++ b/mock/index.js @@ -1,8 +1,8 @@ -import Mock from 'mockjs' -import { param2Obj } from '../src/utils' +const Mock = require('mockjs') +const { param2Obj } = require('./utils') -import user from './user' -import table from './table' +const user = require('./user') +const table = require('./table') const mocks = [ ...user, @@ -12,7 +12,7 @@ const mocks = [ // for front mock // please use it cautiously, it will redefine XMLHttpRequest, // which will cause many of your third-party libraries to be invalidated(like progress event). -export function mockXHR() { +function mockXHR() { // mock patch // https://github.com/nuysoft/Mock/issues/300 Mock.XHR.prototype.proxy_send = Mock.XHR.prototype.send @@ -50,4 +50,8 @@ export function mockXHR() { } } -export default mocks +module.exports = { + mocks, + mockXHR +} + diff --git a/mock/mock-server.js b/mock/mock-server.js index 806fdac..8941ec0 100644 --- a/mock/mock-server.js +++ b/mock/mock-server.js @@ -8,7 +8,7 @@ const mockDir = path.join(process.cwd(), 'mock') function registerRoutes(app) { let mockLastIndex - const { default: mocks } = require('./index.js') + const { mocks } = require('./index.js') const mocksForServer = mocks.map(route => { return responseFake(route.url, route.type, route.response) }) @@ -44,9 +44,6 @@ const responseFake = (url, type, respond) => { } module.exports = app => { - // es6 polyfill - require('@babel/register') - // parse app.body // https://expressjs.com/en/4x/api.html#req.body app.use(bodyParser.json()) diff --git a/mock/table.js b/mock/table.js index ba95f76..bd0e013 100644 --- a/mock/table.js +++ b/mock/table.js @@ -1,4 +1,4 @@ -import Mock from 'mockjs' +const Mock = require('mockjs') const data = Mock.mock({ 'items|30': [{ @@ -11,7 +11,7 @@ const data = Mock.mock({ }] }) -export default [ +module.exports = [ { url: '/vue-admin-template/table/list', type: 'get', diff --git a/mock/user.js b/mock/user.js index f007cd9..7555338 100644 --- a/mock/user.js +++ b/mock/user.js @@ -23,7 +23,7 @@ const users = { } } -export default [ +module.exports = [ // user login { url: '/vue-admin-template/user/login', diff --git a/mock/utils.js b/mock/utils.js new file mode 100644 index 0000000..9eb4c78 --- /dev/null +++ b/mock/utils.js @@ -0,0 +1,23 @@ +/** + * @param {string} url + * @returns {Object} + */ +function param2Obj(url) { + const search = url.split('?')[1] + if (!search) { + return {} + } + return JSON.parse( + '{"' + + decodeURIComponent(search) + .replace(/"/g, '\\"') + .replace(/&/g, '","') + .replace(/=/g, '":"') + .replace(/\+/g, ' ') + + '"}' + ) +} + +module.exports = { + param2Obj +} diff --git a/package.json b/package.json index 6f5189d..a9fc8fc 100644 --- a/package.json +++ b/package.json @@ -26,8 +26,6 @@ "vuex": "3.1.0" }, "devDependencies": { - "@babel/core": "7.0.0", - "@babel/register": "7.0.0", "@vue/cli-plugin-babel": "3.6.0", "@vue/cli-plugin-eslint": "^3.9.1", "@vue/cli-plugin-unit-jest": "3.6.3",