Bläddra i källkod

refactor: change mock files to commonjs

花裤衩 4 år sedan
förälder
incheckning
5c87772025
6 ändrade filer med 37 tillägg och 15 borttagningar
  1. 10 6
      mock/index.js
  2. 1 4
      mock/mock-server.js
  3. 2 2
      mock/table.js
  4. 1 1
      mock/user.js
  5. 23 0
      mock/utils.js
  6. 0 2
      package.json

+ 10 - 6
mock/index.js

@@ -1,8 +1,8 @@
1
-import Mock from 'mockjs'
2
-import { param2Obj } from '../src/utils'
1
+const Mock = require('mockjs')
2
+const { param2Obj } = require('./utils')
3 3
 
4
-import user from './user'
5
-import table from './table'
4
+const user = require('./user')
5
+const table = require('./table')
6 6
 
7 7
 const mocks = [
8 8
   ...user,
@@ -12,7 +12,7 @@ const mocks = [
12 12
 // for front mock
13 13
 // please use it cautiously, it will redefine XMLHttpRequest,
14 14
 // which will cause many of your third-party libraries to be invalidated(like progress event).
15
-export function mockXHR() {
15
+function mockXHR() {
16 16
   // mock patch
17 17
   // https://github.com/nuysoft/Mock/issues/300
18 18
   Mock.XHR.prototype.proxy_send = Mock.XHR.prototype.send
@@ -50,4 +50,8 @@ export function mockXHR() {
50 50
   }
51 51
 }
52 52
 
53
-export default mocks
53
+module.exports = {
54
+  mocks,
55
+  mockXHR
56
+}
57
+

+ 1 - 4
mock/mock-server.js

@@ -8,7 +8,7 @@ const mockDir = path.join(process.cwd(), 'mock')
8 8
 
9 9
 function registerRoutes(app) {
10 10
   let mockLastIndex
11
-  const { default: mocks } = require('./index.js')
11
+  const { mocks } = require('./index.js')
12 12
   const mocksForServer = mocks.map(route => {
13 13
     return responseFake(route.url, route.type, route.response)
14 14
   })
@@ -44,9 +44,6 @@ const responseFake = (url, type, respond) => {
44 44
 }
45 45
 
46 46
 module.exports = app => {
47
-  // es6 polyfill
48
-  require('@babel/register')
49
-
50 47
   // parse app.body
51 48
   // https://expressjs.com/en/4x/api.html#req.body
52 49
   app.use(bodyParser.json())

+ 2 - 2
mock/table.js

@@ -1,4 +1,4 @@
1
-import Mock from 'mockjs'
1
+const Mock = require('mockjs')
2 2
 
3 3
 const data = Mock.mock({
4 4
   'items|30': [{
@@ -11,7 +11,7 @@ const data = Mock.mock({
11 11
   }]
12 12
 })
13 13
 
14
-export default [
14
+module.exports = [
15 15
   {
16 16
     url: '/vue-admin-template/table/list',
17 17
     type: 'get',

+ 1 - 1
mock/user.js

@@ -23,7 +23,7 @@ const users = {
23 23
   }
24 24
 }
25 25
 
26
-export default [
26
+module.exports = [
27 27
   // user login
28 28
   {
29 29
     url: '/vue-admin-template/user/login',

+ 23 - 0
mock/utils.js

@@ -0,0 +1,23 @@
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
+module.exports = {
22
+  param2Obj
23
+}

+ 0 - 2
package.json

@@ -26,8 +26,6 @@
26 26
     "vuex": "3.1.0"
27 27
   },
28 28
   "devDependencies": {
29
-    "@babel/core": "7.0.0",
30
-    "@babel/register": "7.0.0",
31 29
     "@vue/cli-plugin-babel": "3.6.0",
32 30
     "@vue/cli-plugin-eslint": "^3.9.1",
33 31
     "@vue/cli-plugin-unit-jest": "3.6.3",