Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 使用vite-plugin-fake-server替换vite-plugin-mock,使用@faker-js/faker替换mockjs #763

Merged
merged 9 commits into from
Nov 10, 2023
1 change: 0 additions & 1 deletion build/cdn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Plugin as importToCDN } from "vite-plugin-cdn-import";
/**
* @description 打包时采用`cdn`模式,仅限外网使用(默认不采用,如果需要采用cdn模式,请在 .env.production 文件,将 VITE_CDN 设置成true)
* 平台采用国内cdn:https://www.bootcdn.cn,当然你也可以选择 https://unpkg.com 或者 https://www.jsdelivr.com
* 提醒:mockjs不能用cdn模式引入,会报错。正确的方式是,生产环境删除mockjs,使用真实的后端请求
* 注意:上面提到的仅限外网使用也不是完全肯定的,如果你们公司内网部署的有相关js、css文件,也可以将下面配置对应改一下,整一套内网版cdn
*/
export const cdn = importToCDN({
Expand Down
17 changes: 6 additions & 11 deletions build/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@ import vue from "@vitejs/plugin-vue";
import { viteBuildInfo } from "./info";
import svgLoader from "vite-svg-loader";
import vueJsx from "@vitejs/plugin-vue-jsx";
import { viteMockServe } from "vite-plugin-mock";
import { configCompressPlugin } from "./compress";
import { visualizer } from "rollup-plugin-visualizer";
import removeConsole from "vite-plugin-remove-console";
import { themePreprocessorPlugin } from "@pureadmin/theme";
import VueI18nPlugin from "@intlify/unplugin-vue-i18n/vite";
import { genScssMultipleScopeVars } from "../src/layout/theme";
import { vitePluginFakeServer } from "vite-plugin-fake-server";

export function getPluginsList(
command: string,
VITE_CDN: boolean,
VITE_COMPRESSION: ViteCompression
) {
const prodMock = true;
const lifecycle = process.env.npm_lifecycle_event;
return [
vue(),
Expand All @@ -43,15 +42,11 @@ export function getPluginsList(
// svg组件化支持
svgLoader(),
// mock支持
viteMockServe({
mockPath: "mock",
localEnabled: command === "serve",
prodEnabled: command !== "serve" && prodMock,
injectCode: `
import { setupProdMockServer } from './mockProdServer';
setupProdMockServer();
`,
logger: false
vitePluginFakeServer({
logger: false,
include: "mock",
infixName: false,
enableProd: true
}),
// 打包分析
lifecycle === "report"
Expand Down
6 changes: 3 additions & 3 deletions mock/asyncRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// 模拟后端动态生成路由
import { MockMethod } from "vite-plugin-mock";
import { defineFakeRoute } from "vite-plugin-fake-server/client";
import { system, permission, frame, tabs } from "@/router/enums";

/**
Expand Down Expand Up @@ -198,7 +198,7 @@ const tabsRouter = {
]
};

export default [
export default defineFakeRoute([
{
url: "/get-async-routes",
method: "get",
Expand All @@ -209,4 +209,4 @@ export default [
};
}
}
] as MockMethod[];
]);
6 changes: 3 additions & 3 deletions mock/list.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MockMethod } from "vite-plugin-mock";
import { defineFakeRoute } from "vite-plugin-fake-server/client";

export default [
export default defineFakeRoute([
{
url: "/get-card-list",
method: "post",
Expand Down Expand Up @@ -676,4 +676,4 @@ export default [
};
}
}
] as MockMethod[];
]);
6 changes: 3 additions & 3 deletions mock/login.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// 根据角色动态生成路由
import { MockMethod } from "vite-plugin-mock";
import { defineFakeRoute } from "vite-plugin-fake-server/client";

export default [
export default defineFakeRoute([
{
url: "/login",
method: "post",
Expand Down Expand Up @@ -33,4 +33,4 @@ export default [
}
}
}
] as MockMethod[];
]);
28 changes: 16 additions & 12 deletions mock/map.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
import { MockMethod } from "vite-plugin-mock";
import { defineFakeRoute } from "vite-plugin-fake-server/client";
import { faker } from "@faker-js/faker/locale/zh_CN";

type mapType = {
plateNumber: string;
driver: string;
"orientation|1-360": number;
"lng|113-114.1-10": number;
"lat|34-35.1-10": number;
orientation: number;
lng: number;
lat: number;
};

// http://mockjs.com/examples.html#Object
const mapList = (): Array<mapType> => {
const result: Array<mapType> = [];
for (let index = 0; index < 200; index++) {
result.push({
plateNumber: "豫A@natural(11111, 99999)@character('upper')",
driver: "@cname()",
"orientation|1-360": 100,
"lng|113-114.1-10": 1,
"lat|34-35.1-10": 1
plateNumber: `豫A${faker.string.numeric({
length: 5
})}${faker.string.alphanumeric({
casing: "upper"
})}`,
driver: faker.person.firstName(),
orientation: faker.number.int({ min: 1, max: 360 }),
lng: faker.location.latitude({ max: 114.1, min: 113 }),
lat: faker.location.latitude({ max: 35.1, min: 34 })
});
}
return result;
};

export default [
export default defineFakeRoute([
{
url: "/get-map-info",
method: "get",
Expand All @@ -34,4 +38,4 @@ export default [
};
}
}
] as MockMethod[];
]);
6 changes: 3 additions & 3 deletions mock/refreshToken.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MockMethod } from "vite-plugin-mock";
import { defineFakeRoute } from "vite-plugin-fake-server/client";

// 模拟刷新token接口
export default [
export default defineFakeRoute([
{
url: "/refresh-token",
method: "post",
Expand All @@ -24,4 +24,4 @@ export default [
}
}
}
] as MockMethod[];
]);
71 changes: 36 additions & 35 deletions mock/system.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MockMethod } from "vite-plugin-mock";
import { defineFakeRoute } from "vite-plugin-fake-server/client";
import { faker } from "@faker-js/faker/locale/zh_CN";

export default [
export default defineFakeRoute([
// 用户管理
{
url: "/user",
Expand All @@ -12,7 +13,7 @@ export default [
nickname: "admin",
avatar: "https://avatars.githubusercontent.com/u/44761321",
phone: "15888886789",
email: "@email",
email: faker.internet.email(),
sex: 0,
id: 1,
status: 1,
Expand All @@ -30,7 +31,7 @@ export default [
nickname: "common",
avatar: "https://avatars.githubusercontent.com/u/52823142",
phone: "18288882345",
email: "@email",
email: faker.internet.email(),
sex: 1,
id: 2,
status: 1,
Expand Down Expand Up @@ -153,132 +154,132 @@ export default [
id: 100,
sort: 0,
phone: "15888888888",
principal: "@cname()",
email: "@email",
principal: faker.person.firstName(),
email: faker.internet.email(),
status: 1, // 状态 1 启用 0 停用
type: 1, // 1 公司 2 分公司 3 部门
createTime: 1605456000000,
remark: "@cparagraph(1, 3)"
remark: "这里是备注信息这里是备注信息这里是备注信息这里是备注信息"
},
{
name: "郑州分公司",
parentId: 100,
id: 101,
sort: 1,
phone: "15888888888",
principal: "@cname()",
email: "@email",
principal: faker.person.firstName(),
email: faker.internet.email(),
status: 1,
type: 2,
createTime: 1605456000000,
remark: "@cparagraph(1, 3)"
remark: "这里是备注信息这里是备注信息这里是备注信息这里是备注信息"
},
{
name: "研发部门",
parentId: 101,
id: 103,
sort: 1,
phone: "15888888888",
principal: "@cname()",
email: "@email",
principal: faker.person.firstName(),
email: faker.internet.email(),
status: 1,
type: 3,
createTime: 1605456000000,
remark: "@cparagraph(1, 3)"
remark: "这里是备注信息这里是备注信息这里是备注信息这里是备注信息"
},
{
name: "市场部门",
parentId: 102,
id: 108,
sort: 1,
phone: "15888888888",
principal: "@cname()",
email: "@email",
principal: faker.person.firstName(),
email: faker.internet.email(),
status: 1,
type: 3,
createTime: 1605456000000,
remark: "@cparagraph(1, 3)"
remark: "这里是备注信息这里是备注信息这里是备注信息这里是备注信息"
},
{
name: "深圳分公司",
parentId: 100,
id: 102,
sort: 2,
phone: "15888888888",
principal: "@cname()",
email: "@email",
principal: faker.person.firstName(),
email: faker.internet.email(),
status: 1,
type: 2,
createTime: 1605456000000,
remark: "@cparagraph(1, 3)"
remark: "这里是备注信息这里是备注信息这里是备注信息这里是备注信息"
},
{
name: "市场部门",
parentId: 101,
id: 104,
sort: 2,
phone: "15888888888",
principal: "@cname()",
email: "@email",
principal: faker.person.firstName(),
email: faker.internet.email(),
status: 1,
type: 3,
createTime: 1605456000000,
remark: "@cparagraph(1, 3)"
remark: "这里是备注信息这里是备注信息这里是备注信息这里是备注信息"
},
{
name: "财务部门",
parentId: 102,
id: 109,
sort: 2,
phone: "15888888888",
principal: "@cname()",
email: "@email",
principal: faker.person.firstName(),
email: faker.internet.email(),
status: 1,
type: 3,
createTime: 1605456000000,
remark: "@cparagraph(1, 3)"
remark: "这里是备注信息这里是备注信息这里是备注信息这里是备注信息"
},
{
name: "测试部门",
parentId: 101,
id: 105,
sort: 3,
phone: "15888888888",
principal: "@cname()",
email: "@email",
principal: faker.person.firstName(),
email: faker.internet.email(),
status: 0,
type: 3,
createTime: 1605456000000,
remark: "@cparagraph(1, 3)"
remark: "这里是备注信息这里是备注信息这里是备注信息这里是备注信息"
},
{
name: "财务部门",
parentId: 101,
id: 106,
sort: 4,
phone: "15888888888",
principal: "@cname()",
email: "@email",
principal: faker.person.firstName(),
email: faker.internet.email(),
status: 1,
type: 3,
createTime: 1605456000000,
remark: "@cparagraph(1, 3)"
remark: "这里是备注信息这里是备注信息这里是备注信息这里是备注信息"
},
{
name: "运维部门",
parentId: 101,
id: 107,
sort: 5,
phone: "15888888888",
principal: "@cname()",
email: "@email",
principal: faker.person.firstName(),
email: faker.internet.email(),
status: 0,
type: 3,
createTime: 1605456000000,
remark: "@cparagraph(1, 3)"
remark: "这里是备注信息这里是备注信息这里是备注信息这里是备注信息"
}
]
};
}
}
] as MockMethod[];
]);
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
"md-editor-v3": "2.7.2",
"mint-filter": "^4.0.3",
"mitt": "^3.0.1",
"mockjs": "^1.1.0",
"nprogress": "^0.2.0",
"path": "^0.12.7",
"pinia": "^2.1.7",
Expand Down Expand Up @@ -105,14 +104,14 @@
"devDependencies": {
"@commitlint/cli": "^17.7.2",
"@commitlint/config-conventional": "^17.7.0",
"@faker-js/faker": "^8.2.0",
"@iconify-icons/ep": "^1.2.12",
"@iconify-icons/ri": "^1.2.10",
"@iconify/vue": "^4.1.1",
"@intlify/unplugin-vue-i18n": "^1.4.0",
"@pureadmin/theme": "^3.2.0",
"@types/intro.js": "^5.1.2",
"@types/js-cookie": "^3.0.4",
"@types/mockjs": "^1.0.8",
"@types/node": "^20.8.2",
"@types/nprogress": "0.2.0",
"@types/qrcode": "^1.5.2",
Expand Down Expand Up @@ -160,7 +159,7 @@
"vite": "^4.5.0",
"vite-plugin-cdn-import": "^0.3.5",
"vite-plugin-compression": "^0.5.1",
"vite-plugin-mock": "2.9.6",
"vite-plugin-fake-server": "^2.0.0",
"vite-plugin-remove-console": "^2.1.1",
"vite-svg-loader": "^4.0.0",
"vue-eslint-parser": "^9.3.2",
Expand Down
Loading