Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@vitejs/plugin-vue2

vitejs1.2mMIT2.3.3TypeScript support: included

[!CAUTION] > Vue 2 has reached EOL, and this project is no longer actively maintained.

readme

@vitejs/plugin-vue2 npm

[!CAUTION] Vue 2 has reached EOL, and this project is no longer actively maintained.


Note: this plugin only works with Vue@^2.7.0.

// vite.config.js
import vue from '@vitejs/plugin-vue2'

export default {
  plugins: [vue()]
}

Options

export interface Options {
  include?: string | RegExp | (string | RegExp)[]
  exclude?: string | RegExp | (string | RegExp)[]

  isProduction?: boolean

  // options to pass on to vue/compiler-sfc
  script?: Partial<Pick<SFCScriptCompileOptions, 'babelParserPlugins'>>
  template?: Partial<
    Pick<
      SFCTemplateCompileOptions,
      | 'compiler'
      | 'compilerOptions'
      | 'preprocessOptions'
      | 'transpileOptions'
      | 'transformAssetUrls'
      | 'transformAssetUrlsOptions'
    >
  >
  style?: Partial<Pick<SFCStyleCompileOptions, 'trim'>>
}

Asset URL handling

When @vitejs/plugin-vue2 compiles the <template> blocks in SFCs, it also converts any encountered asset URLs into ESM imports.

For example, the following template snippet:

<img src="../image.png" />

Is the same as:

<script setup>
import _imports_0 from '../image.png'
</script>
<img :src="_imports_0" />

By default the following tag/attribute combinations are transformed, and can be configured using the template.transformAssetUrls option.

{
  video: ['src', 'poster'],
  source: ['src'],
  img: ['src'],
  image: ['xlink:href', 'href'],
  use: ['xlink:href', 'href']
}

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'.

Example for passing options to vue/compiler-sfc:

import vue from '@vitejs/plugin-vue2'

export default {
  plugins: [
    vue({
      template: {
        compilerOptions: {
          // ...
        },
        transformAssetUrls: {
          // ...
        }
      }
    })
  ]
}

Example for transforming custom blocks

import vue from '@vitejs/plugin-vue2'

const vueI18nPlugin = {
  name: 'vue-i18n',
  transform(code, id) {
    if (!/vue&type=i18n/.test(id)) {
      return
    }
    if (/\.ya?ml$/.test(id)) {
      code = JSON.stringify(require('js-yaml').load(code.trim()))
    }
    return `export default Comp => {
      Comp.i18n = ${code}
    }`
  }
}

export default {
  plugins: [vue(), vueI18nPlugin]
}

License

MIT

changelog

2.3.3 (2024-11-26)

2.3.2 (2024-11-26)

Features

2.3.1 (2023-11-16)

Bug Fixes

2.3.0 (2023-11-16)

Features

2.2.0 (2022-12-10)

Features

2.1.0 (2022-11-30)

Bug Fixes

  • esbuild: transpile with esnext in dev (#60) (bd87898)
  • invalidate script module cache when it changed in hot update (#67) (b8e6133)

Features

  • resolve complier from peer dep when unable to resolve from the root (#68) (0ea62d2)

2.0.1 (2022-11-09)

Bug Fixes

  • allow overwriting template.transformAssetUrls.includeAbsolute (#48) (7db0767)

2.0.0 (2022-09-13)

Breaking Changes

  • only support Vite 3 (#28)

Bug Fixes

  • handle undefined on import.meta.hot.accept (b668430)

Features

1.1.2 (2022-07-01)

Bug Fixes

  • force resolution of vue to deal with deps requiring vue (9a78726), closes #16
  • handle decorators in ts rewriteDefault fallback (2d24d2a), closes #17

1.1.1 (2022-06-28)

Bug Fixes

  • handle no template code in .vue file (#11) (42b0851), closes #15

1.1.0 (2022-06-20)

Features

1.0.1 (2022-06-17)

Bug Fixes

  • disable prettify by default (cd80f72)

Features

1.0.0 (2022-06-17)

Features