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-class-to-options

v0.1.2

Published

An utility to convert Vue.js Single File Components from Class API to option API.

Downloads

20

Readme

Vue Component Class API To Options API

Version    License    Issues    Downloads

As I needed it on a project with a lot of Class API components, I made this little utility that tries to converts Vue Single File Components written in Class API (with the help of vue-class-componentand vue-property-decorator) to the same component written with Options API.

This utility could be the base for a future thing able to cross-convert between Class, Options and Composition APIs.

This is for now a first attempt and is not deeply tested, so don't use it in production without checking with attention every generated file. That's why the original files are actually not overwritten by the converted ones, which are written aside originals, with an .optionsAPI.vue extension.

Installation and usage

Install the package globally, then launch it giving as parameter the path to the file to convert, relative to where the command has been launched:

npm --global install vue-class-to-options then classToOptions path/to/file/toConvert.vue

or with yarn :

yarn global add vue-class-to-options then classToOptions path/to/file/toConvert.vue

Errors will be displayed if the file does not exists, if it's not a Vue file and if it's not a Class API Component. In case of success, the original file remains untouched, and a new file is saved alongside it, named with the format sameBaseName.optionsAPI.vue. When you carefully checked what has been done and handled the unprocessed parts, you can rename the original file to something else, then rename the new file as the original one, and then launch your tests.

Output

At this early state, only these options are converted :

  • All the file fragment before the <script>tag. It's commonly the template part of the component. This part is copied without any modification. A guard avoid to cut it if any <script> tag is included in the <template>.

  • The same for the fragment after </script>(commonly <style>), also copied without modification.

  • The imports statements, placed at the beginning of the script tag. It's followed by any static out of component stuff if there is (const declarations, out of component functions, etc.). The import part changes little bit from

  • The component data interface, extracted from the original class component properties declarations, with their types. It's placed between the import and static statements and the component export.

  • The component name, extracted from the original class name.

  • The child components to register, grabbed from the @Component decorator

  • The props, grabbed from the @Prop decorators. If the prop is fully declared in the original source, all is well declared in the destination, with the Vue type, the TS type via Proptype<> and default / required options. If the Vue type is missing from source, the TS type is just added as a comment in the destination, and you'll have to declare things as expected manually. My next step is to try to guess the proper Vue type from the TS type and the need for required / default options, if I can.

  • The component's data, extracted from properties declaration in the original, as properties of function return's, implementing the interface discussed previously.

  • Then the component's methods, quite copied as they are in the original source.

  • All the others unprocessed stuff (e.g. all the lines unprocessed by the parsers) are copied in a block comment at the end of the component marked "UNPROCESSED LINES", and have to be manually handled.

Issues & contributions

As said, this is an early stage of this project, and you'll have to carefully check the converted components before taking it to some production.

You're welcome to report any bug, suggestion or feature request at https://github.com/vertcitron/vue-class-to-options/issues. I promise to handle the bugs, but the features requests will depend on their content and the time left by my other stuffs.

You're also welcome to contribute if you feel it. In this case, after forking and dependances install (npm install or yarn), you can launch via some common commands :

  • npm run build (or yarn build) to build the bundle (in dist/index.js).
  • npm run watch (or yarn watch) to build in watch mode.
  • npm run start (or yarn start) to build and launch after build : add at end of CLI the file path you want to convert.
  • npm run dev (or yarn dev) to start it via ts-node, without bundling. You also have to add a .vue file in the CLI. Permits to restart the utility on each watch trigger.
  • npm run test (or yarn test) to launch unit tests, all located in the testsdirectory.

This node package is coded with Typescript 3.8 and is actually not bundled, maybe this will be done later.

When contributing, please submit your PRs under the master branch of the original repo, and before submitting make sure all tests are passing.