• Stars
    star
    515
  • Rank 82,753 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 2 years ago
  • Updated 6 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
64,165
star
2

awesome-vite

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

docs-cn

Chinese translation of vitejs.dev
TypeScript
831
star
4

vite-plugin-react-swc

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

create-vite-app

Create a Vite-powered app in seconds!
JavaScript
572
star
6

vite-plugin-react

The all-in-one Vite plugin for React projects.
TypeScript
461
star
7

vite-plugin-react-pages

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

vite-plugin-vue

Vite Vue Plugins
TypeScript
371
star
9

vite-plugin-react-refresh

React Fast Refresh plugin for Vite
TypeScript
303
star
10

vite-plugin-basic-ssl

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

vite-ecosystem-ci

Vite Ecosystem CI
TypeScript
219
star
12

rfcs

RFCs for substantial changes to Vite core
109
star
13

docs-ko

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

vite-benchmark

Benchmark tool for vitejs/vite
JavaScript
79
star
15

docs-ja

Japanese translation of vitejs.dev
TypeScript
72
star
16

vite-plugin-vue2-jsx

Vite plugin for Vue 2.7 JSX support
TypeScript
54
star
17

release-scripts

@vitejs release scripts
TypeScript
35
star
18

docs-pt

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

docs-es

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

rfcs-bot

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

.github

7
star
22

docs-de

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