This package has been deprecated

Author message:

renamed to `unplugin-vue2-script-setup`, see https://github.com/antfu/unplugin-vue2-script-setup/releases/tag/v0.4.0

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

0.3.5 • Public • Published

vue2-script-setup-transform

NPM version

Bring <script setup> to Vue 2.

Install

npm i -D vue2-script-setup-transform
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
// vite.config.ts
import { defineConfig } from 'vite'
import { createVuePlugin as Vue2 } from 'vite-plugin-vue2'
import ScriptSetup from 'vue2-script-setup-transform/vite-plugin'

export default defineConfig({
  plugins: [
    Vue2(),
    ScriptSetup(),
  ],
})

Example: playground/


Nuxt
npm i @nuxtjs/composition-api
// nuxt.config.js
export default {
  buildModules: [
    '@nuxtjs/composition-api/module',
    'vue2-script-setup-transform/nuxt',
  ],
}

This module works for both Nuxt 2 and Nuxt Vite

Example: examples/nuxt


Vue CLI
// vue.config.js

module.exports = {
  configureWebpack: {
    plugins: [
      require('vue2-script-setup-transform/webpack-plugin').default(),
    ],
  },
}

Example: examples/vue-cli


Webpack
// webpack.config.js
module.exports = {
  /* ... */
  plugins: [
    require('vue2-script-setup-transform/webpack-plugin').default()
  ]
}


JavaScript API
import { transform } from 'vue2-script-setup-transform'

const Vue2SFC = transform(`
<template>
  <!-- ... -->
</template>

<script setup>
  // ...
</script>
`)


IDE

We recommend using VS Code with Volar to get the best experience (You might want to disable Vetur if you have it).

When using Volar, you will 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 your tsconfig.json with:

{
  "compilerOptions": {
    "types": [
      "vue2-script-setup-transform/types"
    ]
  }
}
ESLint

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

Recommendations

If you enjoy using <script setup>, you might also want to try vue-global-api to improve the DX even further.

Progress

  • [x] PoC
  • [x] Components registration
  • [x] Compile time macros defineProps defineEmits withDefaults
  • [x] Global types
  • [x] Merge with normal scripts
  • [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 © 2021 Anthony Fu

Readme

Keywords

none

Package Sidebar

Install

npm i vue2-script-setup-transform

Weekly Downloads

2

Version

0.3.5

License

MIT

Unpacked Size

80.7 kB

Total Files

38

Last publish

Collaborators

  • antfu