Giter Club home page Giter Club logo

vue2-migration-helper's Introduction

vue2-migration-helper

Transforms Vue.js SFCs to composition api syntax.

Build Status Codacy Badge codecov Netlify Status All Contributors

NPM

Install

npm i vue2-migration-helper

CLI

# convert all .vue files in source directory and outputs in target directory
vue2-migration-helper --source="source" --target="target"

# displays help
vue2-migration-helper --help

Programatically use

import { vue2MigrationHelper } from 'vue2-migration-helper'

vue2MigrationHelper({
  source: 'source/',
  target: 'target/',
})

Features

  • add setup method
    • with props and context arguments
  • add required vue imports
    • only adds required imports after traversing code
  • update data properties
    • Uses data variable using reactive
    • Return reactive references using ref
    • updates usage of these varaiables
  • update computed syntax
    • defines variable for each property using new syntax computed
  • update watch syntax
    • updates watch syntax
  • integrate methods directly into setup
    • defines methods into the setup body
    • updates method calls
  • update template ref usage
    • adds a new variable for each templateRef using ref(null)
    • add new defined templateRefs to return statement
  • convert props syntax
  • update lifecycle hooks and remove deperecated lifecycle hooks
    • removes depracted life cycle hooks, injects deprecated hooks code into setup method.
    • copies other hooks into the setup method
  • component registration
  • replace this usage with new context parameter for $events etc
    • replaces this keyword usage as this no longer refers to vue component itself.

missing something?

Example

For a Vue.js SFC (single file component) like this:

import SomeComponent from './SomeComponent'
const zero = {}

export default {
  props: {
    title: String,
    likes: Number,
    callback: Function,
  },

  components: {
    SomeComponent,
  },

  data() {
    return {
      one: true,
      two: 2,
      three: 'three',
    }
  },

  watch: {
    one(val) {
      console.log(val)
    },
    two: (val) => {
      console.log(val)
    },
    three: function (a, b) {
      console.log(a, b)
    },
  },

  computed: {
    oneComputed() {
      return !this.one
    },
    twoComputed: () => {
      return !this.one
    },
    threeComputed: function () {
      return !this.one
    },
  },

  created() {
    console.log('created')
  },

  mounted() {
    console.log('mounted')
  },

  methods: {
    ...[
      function fourMethod() {
        console.log('fourMethod')
      },
      function fiveMethod() {
        console.log('fiveMethod')
      },
    ],

    oneMethod() {
      const html = this.$refs.templateRef.innerHTML
      console.log('oneMethod')
      console.log(this.oneComputed)
    },

    twoMethod: function () {
      this.$refs.templateRef.innerHTML = 'html'
      console.log('twoMethod')
      console.log(this.twoComputed)
      this.oneMethod()
      console.log(this.$router)
    },

    threeMethod: () => {
      console.log('threeMethod')
      console.log(this.threeComputed)
      this.twoMethod()

      console.log(this.$store)
    },
  },
}

this script generates Vue SFC using composition API:

import {
  ref,
  reacted,
  toRefs,
  watch,
  computed,
  onCreated,
  onMounted,
} from 'vue'

import SomeComponent from './SomeComponent'
const zero = {}

export default {
  components: {
    SomeComponent,
  },
  props: {
    title: String,
    likes: Number,
    callback: Function,
  },

  setup(props, context) {
    const data = reactive({
      one: true,
      two: 2,
      three: 'three',
    })
    const templateRef = ref(null)
    watch(three, (a, b) => {
      console.log(a, b)
    })
    watch(two, (val) => {
      console.log(val)
    })
    watch(one, (val) => {
      console.log(val)
    })
    const oneComputed = computed(() => {
      return !data.one
    })
    const twoComputed = computed(() => {
      return data.two + 5
    })
    const threeComputed = computed(() => {
      return data.three.toUpperCase()
    })

    ;(() => {
      console.log('created')
    })()

    onMounted(() => {
      console.log('mounted')
    })

    function fourMethod() {
      console.log('fourMethod')
    }

    function fiveMethod() {
      console.log('fiveMethod')
    }

    function oneMethod() {
      const html = templateRef.innerHTML
      console.log('oneMethod')
      console.log(oneComputed)
      console.log(context.$data)
    }

    function twoMethod() {
      templateRef.innerHTML = 'html'
      console.log('twoMethod')
      console.log(twoComputed)
      oneMethod()
      console.log(context.$router)
    }

    function threeMethod() {
      console.log('threeMethod')
      console.log(threeComputed)
      twoMethod()
      console.log(fourMethod)
      console.log(context.$store)
    }

    return {
      ...ref(data),
      oneComputed,
      twoComputed,
      threeComputed,
      fourMethod,
      fiveMethod,
      oneMethod,
      twoMethod,
      threeMethod,
      templateRef,
    }
  },
}

vue2-migration-helper's People

Contributors

dependabot[bot] avatar mubaidr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

vue2-migration-helper's Issues

Unclear how to use

Remote: accessing the sandbox results in Request failed with status code 404.

Local: after installing using npm, the vue CLI does not recognize vue2-migration-helper as a runnable script.

What is the complete CLI statement necessary to invoke the script?

Thanks


Environment Info:
...
Binaries:
Node: 12.18.3 - C:\Program Files\Tools\nodejs\node.EXE
npm: 6.14.8 - C:\Program Files\Tools\nodejs\npm.CMD
npmPackages:
...
vue: 2.6.11 => 2.6.11 (2.5.17, 2.6.12)
...
vue2-migration-helper: ^0.3.8 => 0.3.8
...
npmGlobalPackages:
@vue/cli: Not Found

replacing ast breaks setupMethod, returnStatement references

// TODO: replacing ast breaks setupMethod, returnStatement references
// update ast instad of replace
this.ast = replaceReferences(this.ast, dataPropsList, 'this.', '', 'data.')
this.ast = replaceReferences(this.ast, computedPropsList, 'this.')
this.ast = replaceReferences(this.ast, methodsList, 'this.')
}


This issue was generated by todo based on a TODO comment in a2ad1fc. It's been assigned to @mubaidr because they committed the code.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.