版博士V2.0程序
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # @vitejs/plugin-vue [![npm](https://img.shields.io/npm/v/@vitejs/plugin-vue.svg)](https://npmjs.com/package/@vitejs/plugin-vue)
  2. > Note: as of `vue` 3.2.13+ and `@vitejs/plugin-vue` 1.9.0+, `@vue/compiler-sfc` is no longer required as a peer dependency.
  3. ```js
  4. // vite.config.js
  5. import vue from '@vitejs/plugin-vue'
  6. export default {
  7. plugins: [vue()],
  8. }
  9. ```
  10. For JSX / TSX support, [`@vitejs/plugin-vue-jsx`](https://github.com/vitejs/vite-plugin-vue/tree/main/packages/plugin-vue-jsx) is also needed.
  11. ## Options
  12. ```ts
  13. export interface Options {
  14. include?: string | RegExp | (string | RegExp)[]
  15. exclude?: string | RegExp | (string | RegExp)[]
  16. isProduction?: boolean
  17. // options to pass on to vue/compiler-sfc
  18. script?: Partial<Pick<SFCScriptCompileOptions, 'babelParserPlugins'>>
  19. template?: Partial<
  20. Pick<
  21. SFCTemplateCompileOptions,
  22. | 'compiler'
  23. | 'compilerOptions'
  24. | 'preprocessOptions'
  25. | 'preprocessCustomRequire'
  26. | 'transformAssetUrls'
  27. >
  28. >
  29. style?: Partial<Pick<SFCStyleCompileOptions, 'trim'>>
  30. /**
  31. * Transform Vue SFCs into custom elements.
  32. * - `true`: all `*.vue` imports are converted into custom elements
  33. * - `string | RegExp`: matched files are converted into custom elements
  34. *
  35. * @default /\.ce\.vue$/
  36. */
  37. customElement?: boolean | string | RegExp | (string | RegExp)[]
  38. /**
  39. * Enable Vue reactivity transform (experimental).
  40. * https://vuejs.org/guide/extras/reactivity-transform.html
  41. * - `true`: transform will be enabled for all vue,js(x),ts(x) files except
  42. * those inside node_modules
  43. * - `string | RegExp`: apply to vue + only matched files (will include
  44. * node_modules, so specify directories if necessary)
  45. * - `false`: disable in all cases
  46. *
  47. * @default false
  48. */
  49. reactivityTransform?: boolean | string | RegExp | (string | RegExp)[]
  50. /**
  51. * Use custom compiler-sfc instance. Can be used to force a specific version.
  52. */
  53. compiler?: typeof _compiler
  54. }
  55. ```
  56. ## Asset URL handling
  57. When `@vitejs/plugin-vue` compiles the `<template>` blocks in SFCs, it also converts any encountered asset URLs into ESM imports.
  58. For example, the following template snippet:
  59. ```vue
  60. <img src="../image.png" />
  61. ```
  62. Is the same as:
  63. ```vue
  64. <script setup>
  65. import _imports_0 from '../image.png'
  66. </script>
  67. <img :src="_imports_0" />
  68. ```
  69. By default the following tag/attribute combinations are transformed, and can be configured using the `template.transformAssetUrls` option.
  70. ```js
  71. {
  72. video: ['src', 'poster'],
  73. source: ['src'],
  74. img: ['src'],
  75. image: ['xlink:href', 'href'],
  76. use: ['xlink:href', 'href']
  77. }
  78. ```
  79. Note that only attribute values that are static strings are transformed. Otherwise, you'd need to import the asset manually, e.g. `import imgUrl from '../image.png'`.
  80. ## Example for passing options to `vue/compiler-sfc`:
  81. ```ts
  82. import vue from '@vitejs/plugin-vue'
  83. export default {
  84. plugins: [
  85. vue({
  86. template: {
  87. compilerOptions: {
  88. // ...
  89. },
  90. transformAssetUrls: {
  91. // ...
  92. },
  93. },
  94. }),
  95. ],
  96. }
  97. ```
  98. ## Example for transforming custom blocks
  99. ```ts
  100. import vue from '@vitejs/plugin-vue'
  101. import yaml from 'js-yaml'
  102. const vueI18nPlugin = {
  103. name: 'vue-i18n',
  104. transform(code, id) {
  105. if (!/vue&type=i18n/.test(id)) {
  106. return
  107. }
  108. if (/\.ya?ml$/.test(id)) {
  109. code = JSON.stringify(yaml.load(code.trim()))
  110. }
  111. return `export default Comp => {
  112. Comp.i18n = ${code}
  113. }`
  114. },
  115. }
  116. export default {
  117. plugins: [vue(), vueI18nPlugin],
  118. }
  119. ```
  120. ## Using Vue SFCs as Custom Elements
  121. > Requires `vue@^3.2.0` & `@vitejs/plugin-vue@^1.4.0`
  122. Vue 3.2 introduces the `defineCustomElement` method, which works with SFCs. By default, `<style>` tags inside SFCs are extracted and merged into CSS files during build. However when shipping a library of custom elements, it may be desirable to inline the styles as JavaScript strings and inject them into the custom elements' shadow root instead.
  123. Starting in 1.4.0, files ending with `*.ce.vue` will be compiled in "custom elements" mode: its `<style>` tags are compiled into inlined CSS strings and attached to the component as its `styles` property:
  124. ```js
  125. import { defineCustomElement } from 'vue'
  126. import Example from './Example.ce.vue'
  127. console.log(Example.styles) // ['/* css content */']
  128. // register
  129. customElements.define('my-example', defineCustomElement(Example))
  130. ```
  131. Note in custom elements mode there is no need to use `<style scoped>` since the CSS is already scoped inside the shadow DOM.
  132. The `customElement` plugin option can be used to configure the behavior:
  133. - `{ customElement: true }` will import all `*.vue` files in custom element mode.
  134. - Use a string or regex pattern to change how files should be loaded as Custom Elements (this check is applied after `include` and `exclude` matches).
  135. ## License
  136. MIT