版博士V2.0程序
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. # unplugin
  2. [![npm version][npm-version-src]][npm-version-href]
  3. [![npm downloads][npm-downloads-src]][npm-downloads-href]
  4. [![License][license-src]][license-href]
  5. Unified plugin system for build tools.
  6. Currently supports:
  7. - [Vite](https://vitejs.dev/)
  8. - [Rollup](https://rollupjs.org/)
  9. - [Webpack](https://webpack.js.org/)
  10. - [esbuild](https://esbuild.github.io/)
  11. - [Rspack](https://www.rspack.dev/) (⚠️ experimental)
  12. ## Hooks
  13. `unplugin` extends the excellent [Rollup plugin API](https://rollupjs.org/guide/en/#plugins-overview) as the unified plugin interface and provides a compatible layer base on the build tools used with.
  14. ###### Supported
  15. | Hook | Rollup | Vite | Webpack 4 | Webpack 5 | esbuild | Rspack |
  16. | ----------------------------------------------------------------------- | :-------------: | :--: | :-------: | :-------: | :-------------: | :----: |
  17. | [`enforce`](https://rollupjs.org/guide/en/#enforce) | ❌ <sup>1</sup> | ✅ | ✅ | ✅ | ❌ <sup>1</sup> | ❌ |
  18. | [`buildStart`](https://rollupjs.org/guide/en/#buildstart) | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
  19. | [`resolveId`](https://rollupjs.org/guide/en/#resolveid) | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ |
  20. | `loadInclude`<sup>2</sup> | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
  21. | [`load`](https://rollupjs.org/guide/en/#load) | ✅ | ✅ | ✅ | ✅ | ✅ <sup>3</sup> | ✅ |
  22. | `transformInclude`<sup>2</sup> | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
  23. | [`transform`](https://rollupjs.org/guide/en/#transformers) | ✅ | ✅ | ✅ | ✅ | ✅ <sup>3</sup> | ✅ |
  24. | [`watchChange`](https://rollupjs.org/guide/en/#watchchange) | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ |
  25. | [`buildEnd`](https://rollupjs.org/guide/en/#buildend) | ✅ | ✅ | ✅ | ✅ | ❌ | ✅ |
  26. | [`writeBundle`](https://rollupjs.org/guide/en/#writebundle)<sup>4</sup> | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
  27. 1. Rollup and esbuild do not support using `enforce` to control the order of plugins. Users need to maintain the order manually.
  28. 2. Webpack's id filter is outside of loader logic; an additional hook is needed for better perf on Webpack. In Rollup and Vite, this hook has been polyfilled to match the behaviors. See for following usage examples.
  29. 3. Although esbuild can handle both JavaScript and CSS and many other file formats, you can only return JavaScript in `load` and `transform` results.
  30. 4. Currently, `writeBundle` is only serves as a hook for the timing. It doesn't pass any arguments.
  31. > **Warning**: The [Rspack](https://www.rspack.dev/) support is experimental. Future changes to Rspack integrations might not follow semver, please pin `unplugin` in your dependency when using. It's not recommended to use in production.
  32. ### Hook Context
  33. ###### Supported
  34. | Hook | Rollup | Vite | Webpack 4 | Webpack 5 | esbuild | Rspack |
  35. | -------------------------------------------------------------------------- | :----: | :--: | :-------: | :-------: | :-----: | :----: |
  36. | [`this.parse`](https://rollupjs.org/guide/en/#thisparse) | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
  37. | [`this.addWatchFile`](https://rollupjs.org/guide/en/#thisaddwatchfile) | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
  38. | [`this.emitFile`](https://rollupjs.org/guide/en/#thisemitfile)<sup>5</sup> | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
  39. | [`this.getWatchFiles`](https://rollupjs.org/guide/en/#thisgetwatchfiles) | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
  40. | [`this.warn`](https://rollupjs.org/guide/en/#thiswarn) | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
  41. | [`this.error`](https://rollupjs.org/guide/en/#thiserror) | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
  42. 5. Currently, [`this.emitFile`](https://rollupjs.org/guide/en/#thisemitfile) only supports the `EmittedAsset` variant.
  43. ## Usage
  44. ```ts
  45. import { createUnplugin } from 'unplugin'
  46. export const unplugin = createUnplugin((options: UserOptions) => {
  47. return {
  48. name: 'unplugin-prefixed-name',
  49. // webpack's id filter is outside of loader logic,
  50. // an additional hook is needed for better perf on webpack
  51. transformInclude(id) {
  52. return id.endsWith('.vue')
  53. },
  54. // just like rollup transform
  55. transform(code) {
  56. return code.replace(/<template>/, '<template><div>Injected</div>')
  57. },
  58. // more hooks coming
  59. }
  60. })
  61. export const vitePlugin = unplugin.vite
  62. export const rollupPlugin = unplugin.rollup
  63. export const webpackPlugin = unplugin.webpack
  64. export const rspackPlugin = unplugin.rspack
  65. export const esbuildPlugin = unplugin.esbuild
  66. ```
  67. ## Nested Plugins
  68. Since `v0.10.0`, unplugin supports constructing multiple nested plugins to behave like a single one. For example:
  69. ###### Supported
  70. | Rollup | Vite | Webpack 4 | Webpack 5 | Rspack | esbuild |
  71. | :--------------------: | :--: | :-------: | :-------: | :----: | :------------: |
  72. | ✅ `>=3.1`<sup>6</sup> | ✅ | ✅ | ✅ | ✅ | ⚠️<sup>7</sup> |
  73. 6. Rollup supports nested plugins since [v3.1.0](https://github.com/rollup/rollup/releases/tag/v3.1.0). Plugin aurthor should ask users to a have a Rollup version of `>=3.1.0` when using nested plugins. For singe plugin format, unplugin works for any versions of Rollup.
  74. 7. Since esbuild does not have a built-in transform phase, the `transform` hook of nested plugin will not work on esbuild yet. Other hooks like `load` or `resolveId` work fine. We will try to find a way to support it in the future.
  75. ###### Usage
  76. ```ts
  77. import { createUnplugin } from 'unplugin'
  78. export const unplugin = createUnplugin((options: UserOptions) => {
  79. return [
  80. {
  81. name: 'plugin-a',
  82. transform(code) {
  83. // ...
  84. },
  85. },
  86. {
  87. name: 'plugin-b',
  88. resolveId(id) {
  89. // ...
  90. },
  91. },
  92. ]
  93. })
  94. ```
  95. ## Plugin Installation
  96. ###### Vite
  97. ```ts
  98. // vite.config.ts
  99. import UnpluginFeature from './unplugin-feature'
  100. export default {
  101. plugins: [
  102. UnpluginFeature.vite({ /* options */ }),
  103. ],
  104. }
  105. ```
  106. ###### Rollup
  107. ```ts
  108. // rollup.config.js
  109. import UnpluginFeature from './unplugin-feature'
  110. export default {
  111. plugins: [
  112. UnpluginFeature.rollup({ /* options */ }),
  113. ],
  114. }
  115. ```
  116. ###### Webpack
  117. ```ts
  118. // webpack.config.js
  119. module.exports = {
  120. plugins: [
  121. require('./unplugin-feature').webpack({ /* options */ }),
  122. ],
  123. }
  124. ```
  125. ###### esbuild
  126. ```ts
  127. // esbuild.config.js
  128. import { build } from 'esbuild'
  129. build({
  130. plugins: [
  131. require('./unplugin-feature').esbuild({ /* options */ }),
  132. ],
  133. })
  134. ```
  135. ###### Rspack
  136. ```ts
  137. // rspack.config.js
  138. module.exports = {
  139. plugins: [
  140. require('./unplugin-feature').rspack({ /* options */ }),
  141. ],
  142. }
  143. ```
  144. ### Framework-specific Logic
  145. While `unplugin` provides compatible layers for some hooks, the functionality of it is limited to the common subset of the build's plugins capability. For more advanced framework-specific usages, `unplugin` provides an escape hatch for that.
  146. ```ts
  147. export const unplugin = createUnplugin((options: UserOptions, meta) => {
  148. console.log(meta.framework) // 'vite' | 'rollup' | 'webpack' | 'rspack' | 'esbuild'
  149. return {
  150. // common unplugin hooks
  151. name: 'unplugin-prefixed-name',
  152. transformInclude(id) { /* ... */ },
  153. transform(code) { /* ... */ },
  154. // framework specific hooks
  155. vite: {
  156. // Vite plugin
  157. configureServer(server) {
  158. // configure Vite server
  159. },
  160. // ...
  161. },
  162. rollup: {
  163. // Rollup plugin
  164. },
  165. webpack(compiler) {
  166. // configure Webpack compiler
  167. },
  168. rspack(compiler) {
  169. // configure Rspack compiler
  170. },
  171. esbuild: {
  172. // change the filter of onResolve and onLoad
  173. onResolveFilter?: RegExp,
  174. onLoadFilter?: RegExp,
  175. // or you can completely replace the setup logic
  176. setup?: EsbuildPlugin.setup,
  177. },
  178. }
  179. })
  180. ```
  181. ## Conventions
  182. - Plugins powered by unplugin should have a clear name with `unplugin-` prefix.
  183. - Include `unplugin` keyword in `package.json`.
  184. - To provide better DX, packages could export 2 kinds of entry points:
  185. - Default export: the returned value of `createUnplugin` function
  186. ```ts
  187. import UnpluginFeature from 'unplugin-feature'
  188. ```
  189. - Subpath exports: properties of the returned value of `createUnplugin` function for each framework users
  190. ```ts
  191. import VitePlugin from 'unplugin-feature/vite'
  192. ```
  193. - Refer to [unplugin-starter](https://github.com/antfu/unplugin-starter) for more details about this setup.
  194. ## Starter Templates
  195. - [antfu/unplugin-starter](https://github.com/antfu/unplugin-starter)
  196. - [jwr12135/create-unplugin](https://github.com/jwr12135/create-unplugin)
  197. - [sxzz/unplugin-starter](https://github.com/sxzz/unplugin-starter)
  198. ## Community Showcases
  199. - [unplugin-auto-import](https://github.com/antfu/unplugin-auto-import)
  200. - [unplugin-vue2-script-setup](https://github.com/antfu/unplugin-vue2-script-setup)
  201. - [unplugin-icons](https://github.com/antfu/unplugin-icons)
  202. - [unplugin-vue-components](https://github.com/antfu/unplugin-vue-components)
  203. - [unplugin-upload-cdn](https://github.com/zenotsai/unplugin-upload-cdn)
  204. - [unplugin-web-ext](https://github.com/jwr12135/unplugin-web-ext)
  205. - [unplugin-properties](https://github.com/pd4d10/unplugin-properties)
  206. - [unplugin-moment-to-dayjs](https://github.com/1247748612/unplugin-moment-to-dayjs)
  207. - [unplugin-object-3d](https://github.com/m0ksem/unplugin-object-3d)
  208. - [unplugin-parcel-css](https://github.com/ssssota/unplugin-parcel-css)
  209. - [unplugin-vue](https://github.com/sxzz/unplugin-vue)
  210. - [unplugin-vue-macros](https://github.com/sxzz/unplugin-vue-macros)
  211. - [unplugin-vue-define-options](https://github.com/sxzz/unplugin-vue-macros/tree/main/packages/define-options)
  212. - [unplugin-jsx-string](https://github.com/sxzz/unplugin-jsx-string)
  213. - [unplugin-ast](https://github.com/sxzz/unplugin-ast)
  214. - [unplugin-element-plus](https://github.com/element-plus/unplugin-element-plus)
  215. - [unplugin-glob](https://github.com/sxzz/unplugin-glob)
  216. - [unplugin-sentry](https://github.com/kricsleo/unplugin-sentry)
  217. - [unplugin-imagemin](https://github.com/ErKeLost/unplugin-imagemin)
  218. - [unplugin-typedotenv](https://github.com/ssssota/typedotenv)
  219. ## License
  220. [MIT](./LICENSE) License © 2021-PRESENT Nuxt Contrib
  221. <!-- Badges -->
  222. [npm-version-src]: https://img.shields.io/npm/v/unplugin?style=flat&colorA=18181B&colorB=F0DB4F
  223. [npm-version-href]: https://npmjs.com/package/unplugin
  224. [npm-downloads-src]: https://img.shields.io/npm/dm/unplugin?style=flat&colorA=18181B&colorB=F0DB4F
  225. [npm-downloads-href]: https://npmjs.com/package/unplugin
  226. [license-src]: https://img.shields.io/github/license/unjs/unplugin.svg?style=flat&colorA=18181B&colorB=F0DB4F
  227. [license-href]: https://github.com/unjs/unplugin/blob/main/LICENSE