npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-comp-to-setup

v1.2.1

Published

Convert Vue files written using the Composition API into Script Setup syntax

Downloads

133

Readme

Vue Composition API to setup (AST)

NPM License NPM Version GitHub Actions Workflow Status

Demo

vue-comp-to-setup is a command-line tool designed to convert Vue files written using the Composition API into the more modern Script Setup syntax. This tool automates the transformation process, making your code cleaner, easier to maintain, and aligned with the latest Vue.js best practices.

Features

  • [x] Convert Composition API-based Vue files to Script Setup syntax
  • [x] Handles both single files and entire directories
  • [x] Automatically updates defineProps, defineEmits, and defineOptions.
  • [x] Cleans up imports and adjusts the structure of your components.

Supported features

  • [x] defineOptions

    • [x] Extract component name
    • [x] Extract custom attributes (...i18n, inheritAttrs, ...customOptions)
  • [x] defineProps

    • [x] Add const if necessary (const props = defineProps({...}))
    • [x] Convert with type declaration (const props = withDefaults(defineProps<{...}>(), {...}))
    • [x] Support props options like (const props = defineProps({...}))
  • [x] defineEmits

    • [x] Add const if necessary (const emit = defineEmits([...]))
    • [x] Find emits if used in a setup function
  • [x] defineExpose

  • [x] useAttrs from setup(_, { attrs })

  • [x] useSlots from setup(_, { slots })

  • [ ] useRuntimeConfig from setup(_, { runtimeConfig })

  • [x] Extract components

<script lang="ts">
  import { defineAsyncComponent } from 'vue';
  export default {
    components: {
      AsyncHelloWorld: defineAsyncComponent(() => import('./hello-world.vue'))
    }
  }
</script>

<script setup lang="ts">
  import { defineAsyncComponent } from 'vue';
	
  const AsyncHelloWorld = defineAsyncComponent(() => import('./hello-world.vue'));
</script>
  • [x] Extract from return statement
<script lang="ts">
  export default {
    setup() {
      return {
        checked: () => {}
      }
    }
  }
</script>

<script setup lang="ts">
  const checked = () => {}
</script>

Requirements

  • Node.js > 18
  • Valid Vue file written in Typescript (.vue extension)

Usage

The vue-comp-to-setup project has CLI

Install locally or global

# npm
npm i vue-comp-to-setup

CLI

Convert a Single File

To convert a single .vue file, run:

# npm
# convert single vue file
npx vue-comp-to-setup single [cliOptions] <vue file path>

Options

-v, --view             Preview changes in the editor.
-h, --help             Help for vue-comp-to-setup

Example:

# npm
# convert single vue file
npx vue-comp-to-setup single "src/components/HelloWorld.vue"

Example output

✔ Successfully converted file: src/components/HelloWorld.vue

Convert All Files in a Directory

To convert all .vue files in a specified directory, run:

# npm
# convert folder
npx vue-comp-to-setup folder <folder dir path>

How It Works

Browser: Vue code -> Parser (SFC) -> @babel/parse AST -> Script Setup transform (@babel/traverse) -> Generate @babel/generator -> Format with Prettier standalone

CLI: Read file -> Vue code -> Parser (SFC) -> @babel/parse AST -> Script Setup transform (@babel/traverse) -> Generate @babel/generator -> Format with Prettier standalone -> Format with Prettier API -> Write file

The tool performs the following steps:

1.	Parse Vue Files: Reads the content of the Vue files and parse to AST with Babel.
2.	Convert to Script Setup: Extracts props, emits, and options (like component name and i18n) and converts them into Script Setup syntax (defineProps, defineEmits, defineOptions).
3.	Clean Up Imports: Removes unnecessary imports such as defineComponent and adjusts the remaining imports as needed.
4.	Rewrite Setup Content: Extracts the content inside the setup function and refactors it according to Script Setup syntax, removing return statements.
5.	Code Formatting: Automatically formats the converted code using Prettier.

Useful Links

  • https://lihautan.com/babel-ast-explorer/

License

This project is licensed under the MIT License. See the LICENSE file for details.

This README provides a comprehensive overview of your project, explaining how to install, use, and understand its functionality. You can customize the repository link and other specifics as needed.