Bring
<script setup>
to Vue 2. Works for Vite, Nuxt, Vue CLI, Webpack, esbuild and more, powered by
unplugin
.
<script setup>
built-in,
you no longer need this plugin
. Thereby this plugin has entered maintenance mode and will only support Vue 2.6 or earlier. This project will reach End of Life by the end of 2022.
npm i -D unplugin-vue2-script-setup
npm i @vue/composition-api
Install
@vue/composition-api
in your App's entry (it enables the
setup()
hook):
import Vue from 'vue'
import VueCompositionAPI from '@vue/composition-api'
Vue.use(VueCompositionAPI)
// vite.config.ts
import { defineConfig } from 'vite'
import { createVuePlugin as Vue2 } from 'vite-plugin-vue2'
import ScriptSetup from 'unplugin-vue2-script-setup/vite'
export default defineConfig({
plugins: [
Vue2(),
ScriptSetup({ /* options */ }),
Example: playground/
It's built-in in Nuxt Bridge.
Vue CLI
// vue.config.js
const ScriptSetup = require('unplugin-vue2-script-setup/webpack').default
module.exports = {
parallel: false, // disable thread-loader, which is not compactible with this plugin
configureWebpack: {
plugins: [
ScriptSetup({ /* options */ }),
Example: examples/vue-cli
To use TypeScript with Vue CLI, install @vue/cli-plugin-typescript
but disable the type check:
npm i -D @vue/cli-plugin-typescript vue-tsc
const ScriptSetup = require('unplugin-vue2-script-setup/webpack').default
module.exports = {
parallel: false,
configureWebpack: {
plugins: [
ScriptSetup({ /* options */ }),
chainWebpack(config) {
// disable type check and let `vue-tsc` handles it
config.plugins.delete('fork-ts-checker')
And then use vue-tsc
to do the type check at build time: