提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
3.0 KiB

  1. # 🛣️ pathe
  2. > Universal filesystem path utils
  3. [![version][npm-v-src]][npm-v-href]
  4. [![downloads][npm-d-src]][npm-d-href]
  5. [![size][size-src]][size-href]
  6. ## ❓ Why
  7. For [historical reasons](https://docs.microsoft.com/en-us/archive/blogs/larryosterman/why-is-the-dos-path-character), windows followed MS-DOS and used backslash for separating paths rather than slash used for macOS, Linux, and other Posix operating systems. Nowadays, [Windows](https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file?redirectedfrom=MSDN) supports both Slash and Backslash for paths. [Node.js's built-in `path` module](https://nodejs.org/api/path.html) in the default operation of the path module varies based on the operating system on which a Node.js application is running. Specifically, when running on a Windows operating system, the path module will assume that Windows-style paths are being used. **This makes inconsistent code behavior between Windows and POSIX.**
  8. Compared to popular [upath](https://github.com/anodynos/upath), pathe provides **identical exports** of Node.js with normalization on **all operations** and is written in modern **ESM/TypeScript** and has **no dependency on Node.js**!
  9. This package is a drop-in replacement of the Node.js's [path module](https://nodejs.org/api/path.html) module and ensures paths are normalized with slash `/` and work in environments including Node.js.
  10. ## 💿 Usage
  11. Install using npm or yarn:
  12. ```bash
  13. # npm
  14. npm i pathe
  15. # yarn
  16. yarn add pathe
  17. # pnpm
  18. pnpm i pathe
  19. ```
  20. Import:
  21. ```js
  22. // ESM / Typescript
  23. import { resolve, matchesGlob } from "pathe";
  24. // CommonJS
  25. const { resolve, matchesGlob } = require("pathe");
  26. ```
  27. Read more about path utils from [Node.js documentation](https://nodejs.org/api/path.html) and rest assured behavior is consistently like POSIX regardless of your input paths format and running platform (the only exception is `delimiter` constant export, it will be set to `;` on windows platform).
  28. ### Extra utilities
  29. Pathe exports some extra utilities that do not exist in standard Node.js [path module](https://nodejs.org/api/path.html).
  30. In order to use them, you can import from `pathe/utils` subpath:
  31. ```js
  32. import {
  33. filename,
  34. normalizeAliases,
  35. resolveAlias,
  36. reverseResolveAlias,
  37. } from "pathe/utils";
  38. ```
  39. ## License
  40. Made with 💛 Published under the [MIT](./LICENSE) license.
  41. Some code was used from the Node.js project. Glob supported is powered by [zeptomatch](https://github.com/fabiospampinato/zeptomatch).
  42. <!-- Refs -->
  43. [npm-v-src]: https://img.shields.io/npm/v/pathe?style=flat-square
  44. [npm-v-href]: https://npmjs.com/package/pathe
  45. [npm-d-src]: https://img.shields.io/npm/dm/pathe?style=flat-square
  46. [npm-d-href]: https://npmjs.com/package/pathe
  47. [github-actions-src]: https://img.shields.io/github/workflow/status/unjs/pathe/ci/main?style=flat-square
  48. [github-actions-href]: https://github.com/unjs/pathe/actions?query=workflow%3Aci
  49. [size-src]: https://packagephobia.now.sh/badge?p=pathe
  50. [size-href]: https://packagephobia.now.sh/result?p=pathe