{"id":19778616,"url":"https://github.com/condorheroblog/vite-plugin-fake-server","last_synced_at":"2026-04-02T11:35:38.808Z","repository":{"id":196813980,"uuid":"697090690","full_name":"condorheroblog/vite-plugin-fake-server","owner":"condorheroblog","description":"A fake server plugin for Vite.","archived":false,"fork":false,"pushed_at":"2025-01-09T06:28:20.000Z","size":3532,"stargazers_count":71,"open_issues_count":0,"forks_count":6,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-03-28T20:05:55.748Z","etag":null,"topics":["fake-dev-server","fake-server","fakerjs","mock-dev-server","mock-server","mockjs","vite-plugin-fake-server","vite-plugin-mock","vite-plugin-mock-server"],"latest_commit_sha":null,"homepage":"https://condorheroblog.github.io/vite-plugin-fake-server/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/condorheroblog.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-09-27T03:16:44.000Z","updated_at":"2025-03-26T08:28:47.000Z","dependencies_parsed_at":null,"dependency_job_id":"992cdc92-8e22-48a7-b4a8-db4ddf019da1","html_url":"https://github.com/condorheroblog/vite-plugin-fake-server","commit_stats":{"total_commits":136,"total_committers":3,"mean_commits":"45.333333333333336","dds":"0.014705882352941124","last_synced_commit":"c981a3a0d05c5d31fd38d91cb8db3c8647b4bd89"},"previous_names":["condorheroblog/vite-plugin-faker","condorheroblog/vite-plugin-fake-server"],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/condorheroblog%2Fvite-plugin-fake-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/condorheroblog%2Fvite-plugin-fake-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/condorheroblog%2Fvite-plugin-fake-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/condorheroblog%2Fvite-plugin-fake-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/condorheroblog","download_url":"https://codeload.github.com/condorheroblog/vite-plugin-fake-server/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247249524,"owners_count":20908212,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fake-dev-server","fake-server","fakerjs","mock-dev-server","mock-server","mockjs","vite-plugin-fake-server","vite-plugin-mock","vite-plugin-mock-server"],"created_at":"2024-11-12T05:30:08.684Z","updated_at":"2026-04-02T11:35:33.781Z","avatar_url":"https://github.com/condorheroblog.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Vite-plugin-fake-server\n\n[![NPM version](https://img.shields.io/npm/v/vite-plugin-fake-server)](https://www.npmjs.com/package/vite-plugin-fake-server)\n[![Downloads](https://img.shields.io/npm/dw/vite-plugin-fake-server)](https://www.npmjs.com/package/vite-plugin-fake-server)\n[![License](https://img.shields.io/npm/l/vite-plugin-fake-server)](https://github.com/condorheroblog/vite-plugin-fake-server/blob/main/LICENSE)\n[![node compatibility](https://img.shields.io/node/v/vite.svg)](https://nodejs.org/en/about/previous-releases)\n[![GitHub stars](https://img.shields.io/github/stars/condorheroblog/vite-plugin-fake-server)](https://github.com/condorheroblog/vite-plugin-fake-server/blob/main/packages/vite-plugin-fake-server)\n\nA fake server plugin for Vite. [Live Demo](https://condorheroblog.github.io/vite-plugin-fake-server/)\n\n## Features\n\n- Simple to use, configuration file driven.\n- Support ECMAScript modules(ESM) and CommonJS modules(CommonJS).\n- Friendly type prompt - defineFakeRoute.\n- No reliance on fake library - you can use [@faker-js/faker](https://github.com/faker-js/faker) or [mockjs](https://github.com/nuysoft/Mock) and so on.\n- Support HTTP/1 and HTTP/2 - [http2 option](https://github.com/condorheroblog/vite-plugin-fake-server#http2).\n- Support `ts`, `js`, `mjs`, `cjs`, `cts`, `mts` files.\n- Support multiple response methods - [responseType](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType).\n- Support custom response headers.\n- Support development and production environments.\n- Support exporting independent deployment services - [build option](https://github.com/condorheroblog/vite-plugin-fake-server#build).\n- Support interception XHR and Fetch request - [XHook](https://github.com/jpillora/xhook).\n- Support requesting real URL.\n\n## Install\n\n```bash\nnpm install --save-dev vite-plugin-fake-server\n```\n\n## Usage\n\nConfigure plugins in the configuration file of [Vite](https://vitejs.dev/config/), such as `vite.config.ts`:\n\n```ts\n// vite.config.ts\nimport { defineConfig } from \"vite\";\nimport { vitePluginFakeServer } from \"vite-plugin-fake-server\";\n\n// https://vitejs.dev/config/\nexport default defineConfig({\n\tplugins: [\n\t\tvitePluginFakeServer(),\n\t],\n});\n```\n\nYou can create a new file in the **`fake`** folder in the directory where the Vite configuration file is located, such as `user.fake.ts`:\n\n```ts\nimport { faker } from \"@faker-js/faker\";\n// fake/user.fake.ts\nimport Mock from \"mockjs\";\nimport { defineFakeRoute } from \"vite-plugin-fake-server/client\";\n\nexport default defineFakeRoute([\n\t{\n\t\turl: \"/mock/get-user-info\",\n\t\tresponse: () =\u003e {\n\t\t\treturn Mock.mock({\n\t\t\t\tid: \"@guid\",\n\t\t\t\tusername: \"@first\",\n\t\t\t\temail: \"@email\",\n\t\t\t\tavatar: \"@image(\\\"200x200\\\")\",\n\t\t\t\trole: \"admin\",\n\t\t\t});\n\t\t},\n\t},\n\t{\n\t\turl: \"/fake/get-user-info\",\n\t\tresponse: () =\u003e {\n\t\t\treturn {\n\t\t\t\tid: faker.string.uuid(),\n\t\t\t\tavatar: faker.image.avatar(),\n\t\t\t\tbirthday: faker.date.birthdate(),\n\t\t\t\temail: faker.internet.email(),\n\t\t\t\tfirstName: faker.person.firstName(),\n\t\t\t\tlastName: faker.person.lastName(),\n\t\t\t\tsex: faker.person.sexType(),\n\t\t\t\trole: \"admin\",\n\t\t\t};\n\t\t},\n\t},\n]);\n```\n\nAfter starting the project through Vite, you can request the custom URL above through XHR or Fetch.\n\nFor case details, please click this link to view [some examples](https://github.com/condorheroblog/vite-plugin-fake-server/tree/main/packages/playground/react-sample/fake) or experience it directly online - [Live Demo](https://condorheroblog.github.io/vite-plugin-fake-server/)\n\n## defineFakeRoute\n\nThe defineFakeRoute function parameters require the user to enter the route type as follows:\n\n```ts\nexport interface FakeRoute {\n\turl: string\n\tmethod?: HttpMethodType\n\ttimeout?: number\n\tstatusCode?: number\n\tstatusText?: string\n\theaders?: OutgoingHttpHeaders\n\tresponse?: (processedRequest: ProcessedRequest, req: IncomingMessage, res: ServerResponse) =\u003e any\n\trawResponse?: (req: IncomingMessage, res: ServerResponse) =\u003e void\n}\n\nexport function defineFakeRoute(config: FakeRoute | FakeRoute[]) {\n\treturn config;\n}\n```\n\n## API\n\n### vitePluginFakeServer(options?)\n\n#### options\n\n##### include\n\nType: `string | string[]`\\\nDefault: `\"fake\"`\n\nSet the folder where the fake `xxx.fake.{ts,js,mjs,cjs,cts,mts}` files is stored.\n\n##### exclude\n\nType: `string | string[]`\\\nDefault: `[]`\n\nExclude files in the `include` directory.\n\ndocument: https://github.com/mrmlnc/fast-glob#ignore\n\n##### infixName\n\nType: `string ｜ boolean`\\\nDefault: `\"fake\"`\n\nSet the infix name used in the fake file name.\n\n##### watch\n\nType: `boolean`\\\nDefault: `true`\n\nSet whether to listen to `include` files.\n\n##### logger\n\nType: `boolean`\\\nDefault: `true`\n\nSet whether to display the request log on the console.\n\nIn order to maintain logs and screen clearing strategies consistent with Vite style on the terminal, the vite-plugin-fake-server plugin will automatically read the following three parameters in the Vite configuration file or in the Vite command line.\n\n- [loglevel](https://vitejs.dev/config/shared-options.html#loglevel)\n- [customlogger](https://vitejs.dev/config/shared-options.html#customlogger)\n- [clearScreen](https://vitejs.dev/config/shared-options.html#clearScreen)\n\nA preview image:\n\n![shapes at 23-10-24 15 23 18](https://github.com/condorheroblog/vite-plugin-fake-server/assets/47056890/a6b11fcb-ee30-40a5-9beb-b1d9a0a51b39)\n\n##### extensions\n\nType: `string[]`\\\nDefault: `[\"ts\", \"js\", \"mjs\", \"cjs\", \"cts\", \"mts\"]`\n\nSet the fake files extensions.\n\n##### timeout\n\nType: `number`\\\nDefault: `undefined`\n\nSet default response delay time.\n\n##### basename\n\nType: `string`\\\nDefault: `\"\"`\n\nSet the root address of the request URL.\n\n##### headers\n\nType: `OutgoingHttpHeaders`\\\nDefault: `{}`\n\nSet default headers for responses.\n\n##### enableDev\n\nType: `boolean`\\\nDefault: `true`\n\nSet up the service simulator in the development environment.\n\nPowered by [Connect](https://github.com/senchalabs/connect) technology.\n\n##### enableProd\n\nType: `boolean`\\\nDefault: `false`\n\nSet up the service simulator in the production environment.\n\nPowered by [XHook](https://github.com/jpillora/xhook) technology.\n\n\u003e ⚠️ The node module cannot be used in the fake file, otherwise the production environment will fail.As an alternative to keep consistent with the development environment, you can build a standalone deployment server, see [build option](https://github.com/condorheroblog/vite-plugin-fake-server#build).\n\nCompared with the development environment, the API interface defined in the production environment does not have a `rawResponse` function. The response function does not have the second parameter `request` and the third parameter `response`.\n\n```ts\nexport interface FakeRoute {\n\turl: string\n\tmethod?: HttpMethodType\n\ttimeout?: number\n\tstatusCode?: number\n\tstatusText?: string\n\theaders?: OutgoingHttpHeaders\n\tresponse?: (processedRequest: ProcessedRequest) =\u003e any\n}\n```\n\n##### http2\n\nType: `boolean`\n\nClarify that the plugin runs on the HTTP/2 protocol. By default, automatically follow Vite's [server-https](https://vitejs.dev/config/server-options.html#server-https) configuration.\n\n\u003e The status text in HTTP/2 is always an empty string.\n\n##### build\n\nType: `boolean | ServerBuildOptions`\\\nDefault: `false`\n\nSet whether to export a independently deployable fake service(only valid in [build](https://vitejs.dev/guide/cli.html#build) mode).\n\n```ts\ninterface ServerBuildOptions {\n\t/**\n\t * @description Server port\n\t * @default 8888\n\t */\n\tport?: number\n\t/**\n\t * Directory relative from `root` where build output will be placed. If the\n\t * directory exists, it will be removed before the build.\n\t * @default \"fakeServer\"\n\t */\n\toutDir?: string\n}\n```\n\n## Principle\n\n![vite-plugin-fake-server](https://github.com/condorheroblog/vite-plugin-fake-server/assets/47056890/141278b0-8b72-4eb5-8b33-2dd68497e1c0)\n\n## Appreciation\n\n- [vite-plugin-mock](https://github.com/vbenjs/vite-plugin-mock)\n- [XHook](https://github.com/jpillora/xhook)\n- [connect](https://github.com/senchalabs/connect#readme)\n\n## Contributing\n\n1. Fork it!\n2. Create your feature branch: git checkout -b my-new-feature\n\n### Debugging\n\n#### In development environment\n\n```\n# packages/vite-plugin-fake-server\nnpm run build:watch\n\n# packages/playground/react-sample\nnpm run dev\n```\n\n#### In production environment\n\n```\n# packages/vite-plugin-fake-server\nnpm run build:watch\n\n# packages/playground/react-sample\nnpm run build\nnpm run preview\n```\n\n3. Commit your changes: git commit -am \"Add some feature\"\n4. Push to the branch: git push origin my-new-feature\n5. Submit a pull request :D\n\n## License\n\n[MIT](https://github.com/condorheroblog/vite-plugin-fake-server/blob/main/LICENSE) License © 2023-Present [Condor Hero](https://github.com/condorheroblog)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcondorheroblog%2Fvite-plugin-fake-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcondorheroblog%2Fvite-plugin-fake-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcondorheroblog%2Fvite-plugin-fake-server/lists"}