{"id":19848205,"url":"https://github.com/starlederer/vite-plugin-fs","last_synced_at":"2025-05-01T22:30:27.918Z","repository":{"id":42505007,"uuid":"422625483","full_name":"StarLederer/vite-plugin-fs","owner":"StarLederer","description":"Fetch network requests to interact with fs in dev mode","archived":false,"fork":false,"pushed_at":"2023-11-25T13:16:49.000Z","size":273,"stargazers_count":22,"open_issues_count":1,"forks_count":3,"subscribers_count":1,"default_branch":"develop","last_synced_at":"2025-04-21T20:44:36.125Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/vite-plugin-fs","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/StarLederer.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","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},"funding":{"github":["StarLederer"]}},"created_at":"2021-10-29T15:28:11.000Z","updated_at":"2025-01-22T18:07:25.000Z","dependencies_parsed_at":"2023-02-13T00:46:29.237Z","dependency_job_id":"8b37cae7-d3a5-431c-8cf4-8f33b314332d","html_url":"https://github.com/StarLederer/vite-plugin-fs","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StarLederer%2Fvite-plugin-fs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StarLederer%2Fvite-plugin-fs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StarLederer%2Fvite-plugin-fs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StarLederer%2Fvite-plugin-fs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/StarLederer","download_url":"https://codeload.github.com/StarLederer/vite-plugin-fs/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251954655,"owners_count":21670845,"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":[],"created_at":"2024-11-12T13:16:35.908Z","updated_at":"2025-05-01T22:30:27.528Z","avatar_url":"https://github.com/StarLederer.png","language":"TypeScript","funding_links":["https://github.com/sponsors/StarLederer"],"categories":[],"sub_categories":[],"readme":"[npm-version-src]: https://img.shields.io/npm/v/vite-plugin-fs/latest.svg\n[npm-version-href]: https://npmjs.com/package/vite-plugin-fs\n[npm-downloads-src]: https://img.shields.io/npm/dm/vite-plugin-fs.svg\n[npm-downloads-href]: https://npmjs.com/package/vite-plugin-fs\n[license-src]: https://img.shields.io/npm/l/nuxt-content-writer.svg\n[license-href]: https://npmjs.com/package/nuxt-content-writer\n[libera-src]: https://img.shields.io/badge/libera-manifesto-lightgrey.svg\n[libera-href]: https://liberamanifesto.com\n\n[issue-tracker]: https://github.com/StarLederer/vite-plugin-fs/issues\n[show-n-tell]: https://github.com/StarLederer/vite-plugin-fs/discussions/categories/show-and-tell\n[github]: https://github.com/StarLederer/vite-plugin-fs\n[changelog]: https://github.com/StarLederer/vite-plugin-fs/tree/master/CHANGELOG.md\n\n# [vite-plugin-fs](https://npmjs.com/package/vite-plugin-fs)\n\n[![npm version][npm-version-src]][npm-version-href]\n[![npm downloads][npm-downloads-src]][npm-downloads-href]\n[![License][license-src]][license-href]\n[![libera manifesto][libera-src]][libera-href]\n\nInteract with fs from the browser in dev mode.\n\n\u003e **News:** v1.1.0 is out and v2.0.0 is already in the works! See [changelog][changelog] for updates since v1.0.0 and keep and eye on the [issue tracker][issue-tracker] for future improvement plans.\n\n## What's supported by the relay server\n\n- readdir\n- readFile (utf-8 only)\n- rm\n- stat\n- writeFile (writes strings only; creates directories automatically)\n- [something else?][issue-tracker]\n\n## Setup\n\n1. Add `vite-plugin-fs` to your project\n\n```bash\npnpm install vite-plugin-fs\n```\n\n2. Add the plugin to `vite.config.js`\n\n```ts\n// vite.config.js\nimport fs from \"vite-plugin-fs\";\n\nexport default {\n  plugins: [fs()],\n};\n```\n\n## Options\n\n```ts\ninterface UserOptions {\n  /**\n   * Port to serve the API at.\n   * Chooses a random one if the one specified is taken.\n   * Usually you don't need to configure this.\n   *\n   * @default 7070\n   */\n  port?: number;\n\n  /**\n   * Root directory visible to browser.\n   *\n   * @default '/'\n   *\n   * @example\n   * './src/content'\n   */\n  rootDir?: string;\n}\n```\n\n## Usage\n\nThe relay server only works in dev mode but the abstraction module can still be imported in production, it is up to you to ensure it is not. The abstraction layer is not buildable in production since it relies on fs relay server port, which does not exist at buid time. Importhing the abstraction layer at build time will casue an error.\n\nThis plugin runs a relay server that allows the browser to communicate with node. There are two ways to interact with the relay server:\n\n- use the abstraction API (**recommended**),\n- fetch network requests (tinkering only).\n\n### Abstraction API\n\nThe abstraction API is designed to act as much like node fs as possible.\n\nImport the abstraction API in your browser code\n\n```ts\nimport fs from 'vite-plugin-fs/browser';\n\n// To read a file\nconst file = await fs.readFile('path/to/somewhere');\n\n// To read a directory\nconst dir = await fs.readdir('path/to/somewhere');\n\n// To stat a path\nconst stats = await fs.stat('path/to/somewhere');\n\n// To write a file\n// Currently only strings are supported as the second argument\nawait fs.writeFile('path/to/somewhere', 'File content');\n\n// To delete a file\nawait fs.rm('path/to/somewhere');\n\n// To delete a file or directory (rm -r)\nawait fs.rm('path/to/somewhere', { recursive: true });\n```\n\n### Network requests\n\n*Do not use this method in production!*\n\nThis is a more direct way to interact with the relay server, however, it is inconvenient, error-prone and does not have a stable API. Breaking changes to this method are not documented. **This method is documented purely for educational reasons.** Only use this method if you want to play around with the plugin and better understand what it does in the background.\n\n```ts\nimport { activePort } from 'virtual:fs';\n\nconst url = `http://localhost:${activePort}`;\n\n// To read a file\nconst fileData = await fetch(`${url}/path/to/somewhere?cmd=readFile`);\n\n// To read a directory\nconst directoryEntries = await fetch(`${url}/path/to/somewhere?cmd=readdir`);\n\n// To execute fs.stat\nconst stats = await fetch(`${url}/path/to/somewhere?cmd=stat`);\n\n// To write a file\n// (Creates the parent directories if they don't already exist automatically)\nawait fetch(`${url}/path/to/somewhere?cmd?=writeFile`, {\n  method: 'POST',\n  headers: {\n    'Content-Type': 'application/json',\n  },\n  body: JSON.stringify({data}),\n});\n\n// To delete a file\nawait fetch(`${url}/path/to/somewhere?cmd=rm`, { method: 'DELETE' });\n\n// To delete a file or directory (rm -rf)\nawait fetch(`${url}/path/to/somewhere?cmd=rm\u0026recursive=true\u0026force=true`, { method: 'DELETE' });\n```\n\n## Sponsors\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://starlederer.github.io/sponsors/sponsors.svg\"\u003e\n    \u003cimg src=\"https://starlederer.github.io/sponsors/sponsors.svg\"/\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstarlederer%2Fvite-plugin-fs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstarlederer%2Fvite-plugin-fs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstarlederer%2Fvite-plugin-fs/lists"}