vue2-migration-helper
Transforms Vue.js SFCs to composition api syntax.
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
vue2MigrationHelper
Features
- add
setup
method- with
props
andcontext
arguments
- with
- add required vue
imports
- only adds required imports after traversing code
- update
data
properties- Uses
data
variable usingreactive
- Return reactive references using
ref
- updates usage of these varaiables
- Uses
- update
computed
syntax- defines variable for each property using new syntax
computed
- defines variable for each property using new syntax
- update
watch
syntax- updates watch syntax
- integrate
methods
directly into setup- defines
methods
into the setup body - updates method calls
- defines
- update template
ref
usage- adds a new variable for each
templateRef
usingref(null)
- add new defined templateRefs to return statement
- adds a new variable for each
- 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 thesetup
method
- removes depracted life cycle hooks, injects deprecated hooks code into
-
component
registration - replace
this
usage with newcontext
parameter for $events etc- replaces
this
keyword usage as this no longer refers to vue component itself.
- replaces
missing something?
Example
For a Vue.js SFC (single file component) like this:
const zero = {} props: title: String likes: Number callback: Function components: SomeComponent { return one: true two: 2 three: 'three' } watch: { console } { console } { console } computed: { return !thisone } { return !thisone } { return !thisone } { console } { console } methods: ... { console } { console } { const html = this$refstemplateRefinnerHTML console console } { this$refstemplateRefinnerHTML = 'html' console console this console } { console console this console }
this script generates Vue SFC using composition API:
const zero = {} components: SomeComponent props: title: String likes: Number callback: Function { const data = const templateRef = const oneComputed = const twoComputed = const threeComputed = ; { console } { console } { console } { const html = templateRefinnerHTML console console console } { templateRefinnerHTML = 'html' console console console } { console console console console } return ... oneComputed twoComputed threeComputed fourMethod fiveMethod oneMethod twoMethod threeMethod templateRef }