Skip to content

Commit d3bd933

Browse files
authored
refactor: change mock files to commonjs (#3246)
1 parent 0bf61aa commit d3bd933

9 files changed

+74
-25
lines changed

mock/article.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Mock from 'mockjs'
1+
const Mock = require('mockjs')
22

33
const List = []
44
const count = 100
@@ -27,7 +27,7 @@ for (let i = 0; i < count; i++) {
2727
}))
2828
}
2929

30-
export default [
30+
module.exports = [
3131
{
3232
url: '/vue-element-admin/article/list',
3333
type: 'get',

mock/index.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import Mock from 'mockjs'
2-
import { param2Obj } from '../src/utils'
1+
const Mock = require('mockjs')
2+
const { param2Obj } = require('./utils')
33

4-
import user from './user'
5-
import role from './role'
6-
import article from './article'
7-
import search from './remote-search'
4+
const user = require('./user')
5+
const role = require('./role')
6+
const article = require('./article')
7+
const search = require('./remote-search')
88

99
const mocks = [
1010
...user,
@@ -16,7 +16,7 @@ const mocks = [
1616
// for front mock
1717
// please use it cautiously, it will redefine XMLHttpRequest,
1818
// which will cause many of your third-party libraries to be invalidated(like progress event).
19-
export function mockXHR() {
19+
function mockXHR() {
2020
// mock patch
2121
// https://github.com/nuysoft/Mock/issues/300
2222
Mock.XHR.prototype.proxy_send = Mock.XHR.prototype.send
@@ -54,4 +54,7 @@ export function mockXHR() {
5454
}
5555
}
5656

57-
export default mocks
57+
module.exports = {
58+
mocks,
59+
mockXHR
60+
}

mock/mock-server.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const mockDir = path.join(process.cwd(), 'mock')
88

99
function registerRoutes(app) {
1010
let mockLastIndex
11-
const { default: mocks } = require('./index.js')
11+
const { mocks } = require('./index.js')
1212
const mocksForServer = mocks.map(route => {
1313
return responseFake(route.url, route.type, route.response)
1414
})
@@ -44,9 +44,6 @@ const responseFake = (url, type, respond) => {
4444
}
4545

4646
module.exports = app => {
47-
// es6 polyfill
48-
require('@babel/register')
49-
5047
// parse app.body
5148
// https://expressjs.com/en/4x/api.html#req.body
5249
app.use(bodyParser.json())

mock/remote-search.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Mock from 'mockjs'
1+
const Mock = require('mockjs')
22

33
const NameList = []
44
const count = 100
@@ -10,7 +10,7 @@ for (let i = 0; i < count; i++) {
1010
}
1111
NameList.push({ name: 'mock-Pan' })
1212

13-
export default [
13+
module.exports = [
1414
// username search
1515
{
1616
url: '/vue-element-admin/search/user',

mock/role/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import Mock from 'mockjs'
2-
import { deepClone } from '../../src/utils/index.js'
3-
import { asyncRoutes, constantRoutes } from './routes.js'
1+
const Mock = require('mockjs')
2+
const { deepClone } = require('../utils')
3+
const { asyncRoutes, constantRoutes } = require('./routes.js')
44

55
const routes = deepClone([...constantRoutes, ...asyncRoutes])
66

@@ -35,7 +35,7 @@ const roles = [
3535
}
3636
]
3737

38-
export default [
38+
module.exports = [
3939
// mock get all routes form server
4040
{
4141
url: '/vue-element-admin/routes',

mock/role/routes.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Just a mock data
22

3-
export const constantRoutes = [
3+
const constantRoutes = [
44
{
55
path: '/redirect',
66
component: 'layout/Layout',
@@ -72,7 +72,7 @@ export const constantRoutes = [
7272
}
7373
]
7474

75-
export const asyncRoutes = [
75+
const asyncRoutes = [
7676
{
7777
path: '/permission',
7878
component: 'layout/Layout',
@@ -523,3 +523,8 @@ export const asyncRoutes = [
523523

524524
{ path: '*', redirect: '/404', hidden: true }
525525
]
526+
527+
module.exports = {
528+
constantRoutes,
529+
asyncRoutes
530+
}

mock/user.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const users = {
2323
}
2424
}
2525

26-
export default [
26+
module.exports = [
2727
// user login
2828
{
2929
url: '/vue-element-admin/user/login',

mock/utils.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @param {string} url
3+
* @returns {Object}
4+
*/
5+
function param2Obj(url) {
6+
const search = url.split('?')[1]
7+
if (!search) {
8+
return {}
9+
}
10+
return JSON.parse(
11+
'{"' +
12+
decodeURIComponent(search)
13+
.replace(/"/g, '\\"')
14+
.replace(/&/g, '","')
15+
.replace(/=/g, '":"')
16+
.replace(/\+/g, ' ') +
17+
'"}'
18+
)
19+
}
20+
21+
/**
22+
* This is just a simple version of deep copy
23+
* Has a lot of edge cases bug
24+
* If you want to use a perfect deep copy, use lodash's _.cloneDeep
25+
* @param {Object} source
26+
* @returns {Object}
27+
*/
28+
function deepClone(source) {
29+
if (!source && typeof source !== 'object') {
30+
throw new Error('error arguments', 'deepClone')
31+
}
32+
const targetObj = source.constructor === Array ? [] : {}
33+
Object.keys(source).forEach(keys => {
34+
if (source[keys] && typeof source[keys] === 'object') {
35+
targetObj[keys] = deepClone(source[keys])
36+
} else {
37+
targetObj[keys] = source[keys]
38+
}
39+
})
40+
return targetObj
41+
}
42+
43+
module.exports = {
44+
param2Obj,
45+
deepClone
46+
}

package.json

-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@
7272
"xlsx": "0.14.1"
7373
},
7474
"devDependencies": {
75-
"@babel/core": "7.0.0",
76-
"@babel/register": "7.0.0",
7775
"@vue/cli-plugin-babel": "3.5.3",
7876
"@vue/cli-plugin-eslint": "^3.9.1",
7977
"@vue/cli-plugin-unit-jest": "3.5.3",

0 commit comments

Comments
 (0)