• Stars
    star
    539
  • Rank 82,402 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 2 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Vite plugin for Vue 2.7

@vitejs/plugin-vue2 npm

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

More Repositories

1

vite

Next generation frontend tooling. It's fast!
TypeScript
67,211
star
2

awesome-vite

⚡️ A curated list of awesome things related to Vite.js
JavaScript
14,108
star
3

docs-cn

Chinese translation of vite.dev
Vue
838
star
4

vite-plugin-react-swc

Speed up your Vite dev server with SWC
TypeScript
822
star
5

vite-plugin-react

The all-in-one Vite plugin for React projects.
TypeScript
574
star
6

create-vite-app

Create a Vite-powered app in seconds!
JavaScript
573
star
7

vite-plugin-vue

Vite Vue Plugins
TypeScript
456
star
8

vite-plugin-react-pages

A vite framework for building react app. Especially suitable for document site and demos/playgrounds of react components.
TypeScript
452
star
9

vite-plugin-react-refresh

React Fast Refresh plugin for Vite
TypeScript
304
star
10

vite-plugin-basic-ssl

Basic https setup using an automatically generated self-signed certificate
TypeScript
299
star
11

vite-ecosystem-ci

Vite Ecosystem CI
TypeScript
228
star
12

rfcs

RFCs for substantial changes to Vite core
109
star
13

docs-ko

ViteJS 한글 공식 문서 (Korean translation of vitejs.dev)
TypeScript
103
star
14

vite-ts-monorepo-rfc

An RFC for better support for TS monorepos in Vite
TypeScript
98
star
15

vite-benchmark

Benchmark tool for vitejs/vite
JavaScript
79
star
16

docs-ja

Japanese translation of vitejs.dev
Vue
76
star
17

vite-plugin-vue2-jsx

Vite plugin for Vue 2.7 JSX support
TypeScript
58
star
18

release-scripts

@vitejs release scripts
TypeScript
44
star
19

docs-pt

Portuguese translation of vitejs.dev | Documentação da Vite em Português. 🇦🇴 🇧🇷 🇵🇹
31
star
20

docs-es

Traducción al español de la documentación de vitejs.dev
JavaScript
21
star
21

rfcs-bot

Automation of RFCs creation for vitejs/rfcs
TypeScript
13
star
22

.github

10
star
23

docs-de

🇩🇪 German translation of vitejs.dev | Die deutsche Übersetzung von vitejs.dev.
6
star