Movatterモバイル変換


[0]ホーム

URL:


unplugin-vue2-script-setup
TypeScript icon, indicating that this package has built-in type declarations

0.11.4 • Public • Published

unplugin-vue2-script-setup

NPM version

Bring<script setup> to Vue 2. Works for Vite, Nuxt, Vue CLI, Webpack, esbuild and more, powered byunplugin.

⚠️ With the release ofVue 2.7, which has Composition API and<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.

Install

npm i -D unplugin-vue2-script-setupnpm i @vue/composition-api

Install@vue/composition-api in your App's entry (it enables thesetup() hook):

importVuefrom'vue'importVueCompositionAPIfrom'@vue/composition-api'Vue.use(VueCompositionAPI)
Vite
// vite.config.tsimport{defineConfig}from'vite'import{createVuePluginasVue2}from'vite-plugin-vue2'importScriptSetupfrom'unplugin-vue2-script-setup/vite'exportdefaultdefineConfig({plugins:[Vue2(),ScriptSetup({/* options */}),],})

Example:playground/


Nuxt

It's built-in inNuxt Bridge.

Vue CLI
// vue.config.jsconstScriptSetup=require('unplugin-vue2-script-setup/webpack').defaultmodule.exports={parallel:false,// disable thread-loader, which is not compactible with this pluginconfigureWebpack:{plugins:[ScriptSetup({/* options */}),],},}

Example:examples/vue-cli

TypeScript

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
constScriptSetup=require('unplugin-vue2-script-setup/webpack').defaultmodule.exports={parallel:false,configureWebpack:{plugins:[ScriptSetup({/* options */}),],},chainWebpack(config){// disable type check and let `vue-tsc` handles itconfig.plugins.delete('fork-ts-checker')},}

And then usevue-tsc to do the type check at build time:

// package.json{"scripts":{"dev":"vue-cli-service serve","build":"vue-tsc --noEmit && vue-cli-service build"}}


Webpack
// webpack.config.jsconstScriptSetup=require('unplugin-vue2-script-setup/webpack').defaultmodule.exports={/* ... */plugins:[ScriptSetup({/* options */}),]}


Rollup
// rollup.config.jsimportVuefrom'rollup-plugin-vue'importScriptSetupfrom'unplugin-vue2-script-setup/rollup'exportdefault{plugins:[Vue(),ScriptSetup({/* options */}),]}


esbuild
// esbuild.config.jsimport{build}from'esbuild'importScriptSetupfrom'unplugin-vue2-script-setup/esbuild'build({/* ... */plugins:[ScriptSetup({/* options */}),],})


Jest
npm i -D vue-jest
// jest.config.jsmodule.exports={transform:{'.*\\.(vue)$':'unplugin-vue2-script-setup/jest',},}


JavaScript API
import{transform}from'unplugin-vue2-script-setup'constVue2SFC=awaittransform(`<template>  <!-- ... --></template><script setup>  // ...</script>`)


IDE

We recommend usingVS Code withVolar to get the best experience (You might want to disable Vetur if you have it).

When using Volar, you need to install@vue/runtime-dom as devDependencies to make it work on Vue 2.

npm i -D @vue/runtime-dom

Learn more

Global Types

If the global types are missing for your IDE, update yourtsconfig.json with:

{"compilerOptions":{"types":["unplugin-vue2-script-setup/types"]}}
Support Vue 2 template

Volar preferentially supports Vue 3. Vue 3 and Vue 2 template has some different. You need to set theexperimentalCompatMode option to support Vue 2 template.

{"compilerOptions":{    ...},"vueCompilerOptions":{"experimentalCompatMode":2},}
ESLint

If you are using ESLint, you might get@typescript-eslint/no-unused-vars warning with<script setup>. You can disable it and addnoUnusedLocals: true in yourtsconfig.json, Volar will infer the real missing locals correctly for you.

Configurations

Ref Sugar (take 2)

In v0.5.x, we shipped theexperimentalRef Sugar (take 2) implementation based on Vue 3's@vue/reactivity-transform package. Notice the syntax is not settled yet and might be changed in the future updates.Use at your own risk!

To enabled it, pass the option:

ScriptSetup({reactivityTransform:true})

To get TypeScript support, update yourtsconfig.json with:

{"compilerOptions":{"types":["unplugin-vue2-script-setup/types","unplugin-vue2-script-setup/ref-macros"]}}

Recommendations

If you enjoy using<script setup>, you might also want to tryunplugin-auto-import to improve the DX even further.

Progress

  • [x] PoC
  • [x] Components registration
  • [x] Compile time macrosdefinePropsdefineEmitswithDefaultsdefineExpose
  • [x] Global types
  • [x] Merge with normal scripts
  • [x]Ref Sugar (take 2)
  • [x]<template lang="pug"> support
  • [x] Vite plugin
  • [x] Webpack plugin
  • [x] Nuxt module
  • [ ]Top-level await (not supported)

How?

👀

image

It's made possible by transforming the<script setup> syntax back to normal<script> and let the Vue 2 SFC compiler handle the rest.


Sponsors

License

MIT License © 2021Anthony Fu

Readme

Keywords

none

Package Sidebar

Install

npm i unplugin-vue2-script-setup

Weekly Downloads

27,261

Version

0.11.4

License

MIT

Unpacked Size

1.67 MB

Total Files

32

Last publish

Collaborators

  • userquin
  • hannoeru
  • sxzz
  • antfu
  • xiaoxiangmoe

[8]ページ先頭

©2009-2025 Movatter.jp