From cb5dd34e17c06316906d38c7910bf5adaaa1d3e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E4=BC=A0=E9=BE=99?= Date: Sun, 26 Jun 2022 18:42:07 +0800 Subject: [PATCH] refactor: simplify mock setup --- build/plugin/mock.js | 5 ++--- mock/{_create-prod-server.js => index.js} | 5 +---- mock/{auth/index.js => modules/auth.js} | 2 +- mock/{post/index.js => modules/post.js} | 0 mock/{user/index.js => modules/user.js} | 2 +- mock/{_utils.js => utils.js} | 0 6 files changed, 5 insertions(+), 9 deletions(-) rename mock/{_create-prod-server.js => index.js} (74%) rename mock/{auth/index.js => modules/auth.js} (94%) rename mock/{post/index.js => modules/post.js} (100%) rename mock/{user/index.js => modules/user.js} (94%) rename mock/{_utils.js => utils.js} (100%) diff --git a/build/plugin/mock.js b/build/plugin/mock.js index 6c2d583..d598b2c 100644 --- a/build/plugin/mock.js +++ b/build/plugin/mock.js @@ -2,12 +2,11 @@ import { viteMockServe } from 'vite-plugin-mock' export function configMockPlugin(isBuild) { return viteMockServe({ - ignore: /^\_/, - mockPath: 'mock', + mockPath: 'mock/modules', localEnabled: !isBuild, prodEnabled: isBuild, injectCode: ` - import { setupProdMockServer } from '../mock/_create-prod-server'; + import { setupProdMockServer } from '../mock'; setupProdMockServer(); `, }) diff --git a/mock/_create-prod-server.js b/mock/index.js similarity index 74% rename from mock/_create-prod-server.js rename to mock/index.js index 4e0522a..13e5de9 100644 --- a/mock/_create-prod-server.js +++ b/mock/index.js @@ -1,11 +1,8 @@ import { createProdMockServer } from 'vite-plugin-mock/es/createProdMockServer' -const modules = import.meta.globEager('./**/*.js') +const modules = import.meta.globEager('./modules/*.js') const mockModules = [] Object.keys(modules).forEach((key) => { - if (key.includes('/_')) { - return - } mockModules.push(...modules[key].default) }) diff --git a/mock/auth/index.js b/mock/modules/auth.js similarity index 94% rename from mock/auth/index.js rename to mock/modules/auth.js index c8fa18e..fac75b4 100644 --- a/mock/auth/index.js +++ b/mock/modules/auth.js @@ -1,4 +1,4 @@ -import { resolveToken } from '../_utils' +import { resolveToken } from '../utils' const token = { admin: 'admin', diff --git a/mock/post/index.js b/mock/modules/post.js similarity index 100% rename from mock/post/index.js rename to mock/modules/post.js diff --git a/mock/user/index.js b/mock/modules/user.js similarity index 94% rename from mock/user/index.js rename to mock/modules/user.js index b637880..178380d 100644 --- a/mock/user/index.js +++ b/mock/modules/user.js @@ -1,4 +1,4 @@ -import { resolveToken } from '../_utils' +import { resolveToken } from '../utils' const users = { admin: { diff --git a/mock/_utils.js b/mock/utils.js similarity index 100% rename from mock/_utils.js rename to mock/utils.js