Current status: experimental
This repository contains a collection of codemod scripts for use with JSCodeshift that help update Vue.js APIs.
Inspired by react-codemod.
npx vue-codemod <path> -t <transformation> --params [transformation params] [...additional options]
- transformation(required) - name of transformation, see available transformations below; or you can provide a path to a custom transformation module.
- path(required) - files or directory to transform.
- --params(optional) - additional transformation specific args.
- runTransformation(fileInfo, transformation, params)
- Basic testing setup and a dummy CLI
-  Support applying jscodeshiftcodemods to.vuefiles
-  Provide a programmatic interface for usage in vue-cli-plugin-vue-next
- Set up tests
- (WIP) Implement the transformations described below for migration usage
- Built-in transformations need to support TypeScript
- Built-in transformations need to support module systems other than ES module, and those without modules
-  Define an interface for transformation of template blocks (may use vue-eslint-parserfor this)
-  A playground for writing transformations - yarn playgroundand visit http://localhost:3000
The migration path (to be integrated in a new version of vue-migration-helper):
- Install eslint-plugin-vue@7, turn on the vue3-essentialcategory (maybe a few exceptions likevue/no-deprecated-dollar-scopedslots-api)
- Run eslint --fixto fix all auto-fixable issues; if there are any remaining errors, fix them manually
- Run the codemods below
- Install vue@3, vue-loader@16, etc.
- Make sure to use the compat build of vue@3
- Serve the app in development mode, fix the runtime deprecation warnings
Note: even though most of the migration process can be automated, please be aware there might still be subtle differences between Vue 3 and Vue 2 runtime, and the codemods may have uncovered edge cases. Please double check before deploying your Vue 3 app into production.
Legend of annotations:
| Mark | Description | 
|---|---|
| π΄ | work not started | 
| π΅ | needs to or can be implemented in the compat runtime | 
- RFC05: Replace v-bind's.syncwith av-modelargument- Can be detected and fixed by the vue/no-deprecated-v-bind-syncESLint rule
 
- Can be detected and fixed by the 
- RFC14: Remove keyCodesupport inv-on- Can be detected and fixed by the vue/no-deprecated-v-on-number-modifiersESLint rule
- config.keyCodecan be supported in the compat build. It is also detectable with the- vue/no-deprecated-vue-config-keycodesESLint rule
 
- Can be detected and fixed by the 
- RFC19: Remove dataobject declaration- Can be detected and fixed by the vue/no-shared-component-dataand thevue/no-deprecated-data-object-declarationESLint rules
 
- Can be detected and fixed by the 
- RFC01: New slot syntax and RFC06: Slots unification
- Can be detected and partially fixed by the vue/no-deprecated-slot-attributeandvue/no-deprecated-slot-scope-attribute
- During the transition period, with the 2 ESLint rules enabled, it will warn users when they use this.$slots, recommendingthis.$scopedSlotsas a replacement
- When upgrading to Vue 3, replace all .$scopedSlotsoccurrences with.$slots(should pass the abovementioned ESLint checks before running this codemod) (implemented asscoped-slots-to-slots)
 
- Can be detected and partially fixed by the 
- RFC04: Global API treeshaking & RFC09: Global mounting/configuration API change
- implemented as new-global-api
- import Vue from 'vue'->- import * as Vue from 'vue'(implemented as- vue-as-namespace-import)
- Vue.extend->- defineComponent(implemented as- define-component)
- new Vue()->- Vue.createApp()(implemented as- new-vue-to-create-app)- new Vue({ el }),- new Vue().$mount->- Vue.createApp().mount
- new HelloWorld({ el }),- new HelloWorld().$mount->- createApp(HelloWorld).mount
 
- render(h)->- render()and- import { h } from 'vue'(implemented as- remove-contextual-h-from-render)
- Vue.config.productionTip-> removed (implemented as- remove-production-tip)
- π΄ Some global APIs now can only be used on the app instances, while it's possible to support the legacy usage in a compat build, we will provide a codemod to help migration. (global-to-per-app-api)- Vue.config,- Vue.use,- Vue.mixin,- Vue.component,- Vue.directive, etc ->- app.**(It's possible to provide a runtime compatibility layer for single-root apps)
- Vue.prototype.customProperty->- app.config.globalProperties.customProperty
- Vue.config.ignoredElements->- app.config.isCustomElement
- The migration path would be a two-pass approach:
- Scan the entire project to collect all the usages of the abovementioned global properties / methods
- Depending on the result of the first scan:
- If there's only one entry file using these global APIs, then transform it;
- If there's exactly one entry file and one root instance, but several other files are also using Vue.*, then transform the entry file to export the root instance, import it in other files and transform them with the imported root instance;
- If there are more than one entry file or root instances, then the user needs to manually export the root instances, re-apply this codemod to those non-entry files with an argument designating the root instance.
 
 
 
- π΅ Detect and warn on optionMergeStrategiesbehavior change
 
- implemented as 
- RFC07: Functional and async components API change
- π΅ a compatibility mode can be provided for functional components for one-at-a-time migration
- Can be detected by the vue/no-deprecated-functional-templateESLint rule
- π΄ SFCs using <template functional>should be converted to normal SFCs
 
- RFC08: Render function API change
- Template users won't be affected
- JSX plugin will be rewritten to cover most use cases (work-in-progress, available at https://github.com/vueComponent/jsx/)
- π΄ For Users who manually write render functions using h- π΅ It's possible to provide a compat plugin that patches render functions and make them expose a 2.x compatible arguments, and can be turned off in each component for a one-at-a-time migration process.
- π΄ It's also possible to provide a codemod that auto-converts hcalls to use the new VNode data format, since the mapping is pretty mechanical.
 
- π΄ Functional components using context will likely have to be manually migrated, but a similar adaptor can be provided.
 
- RFC12: Custom directive API change
- bind->- beforeMount
- inserted->- mounted
- remove updatehook and insert a comment to note the user about the change
- componentUpdated->- updated
- unbind->- unmounted
- π΄ VNode interface change (a runtime compat plugin is also possible, see the notes for RFC08)
 
- RFC13: Composition API
- import ... from '@vue/composition-api'->- import ... from 'vue'(implemented as- import-composition-api-from-vue)
- There are some subtle differences between the plugin and Vue 3 implementation, see https://github.com/vuejs/composition-api#limitations for more details.
 
- RFC16: Remove inline-template- Can be detected by the vue/no-deprecated-inline-templateESLint rule
- π΄ Possible alternatives are addressed in the RFC
 
- Can be detected by the 
- RFC25: Built-in <Teleport>component- Can be detected by the vue/no-reserved-component-namesESLint rule
- π΄ A codemod can be implemented to rename all <Teleport>components to some other name like<TeleportComp>.
 
- Can be detected by the 
- π΄ RFC26: New async component API
- π΅ In the compat build, it is possible to check the return value of functional components and warn legacy async components usage. This should cover all Promise-based use cases.
- π΄ The syntax conversion is mechanical and can be performed via a codemod. The challenge is in determining which plain functions should be considered async components. Some basic heuristics can be used (note this may not cover 100% of the existing usage):
- Arrow functions that returns dynamic importcall to.vuefiles
- Arrow functions that returns an object with the componentproperty being a dynamicimportcall.
 
- Arrow functions that returns dynamic 
- The only case that cannot be easily detected is 2.x async components using manual resolve/rejectinstead of returning promises. Manual upgrade will be required for such cases but they should be relatively rare.
 
- π΄ RFC30: Add emitscomponent option- There could be potential naming conflicts with existing component-level emitsoptions, so we need to scan and warn on such usages
- To better utilize the new emitsoption, we can provide a codemod that automatically scans all instances of$emitcalls in a component and generate theemitsoption
 
- There could be potential naming conflicts with existing component-level 
- Vuex 3.x to 4
- implemented as in combination of new-global-apiandvuex-v4
- Vue.use(Vuex)&- new Vue({ store })->- app.use(store)
- new Store()->- createStore()
 
- implemented as in combination of 
- Vue Router 3.x to 4
- implemented as in combination of new-global-apiandvue-router-v4
- Vue.use(VueRouter)&- new Vue({ router })->- app.use(router)
- new VueRouter()->- createRouter()
- mode: 'history', base: BASE_URLetc. ->- history: createWebHistory(BASE_URL)etc.
- π΄ RFC21: Scoped slot API for router-link
- π΄ RFC28: Change active and exact-active behavior for router-link
 
- implemented as in combination of 
- vue-class-component7.x to 8- import { Component } from 'vue-class-component'->- import { Options as Component } from 'vue-class-component'
- π΄ import Vue from 'vue'->import { Vue } from 'vue-class-component'(Need to avoid name collision if there's any reference toVuebesidesextends Vue)
- π΄ Component.registerHooks->Vue.registerHooks
 
- RFC17: Changed behavior when using transitionas root- Can be detected by the vue/require-toggle-inside-transitionESLint rule
 
- Can be detected by the 
- RFC22: Merge metafields from parent to child inRouteLocation- Seems no codemod or ESLint rule is applicable to this breaking change
 
- RFC24: Attribute coercion behavior change
- Codemod is not likely to help in this case
 
- RFC31: Attribute Fallthrough + Functional Component Updates
- Warn of every $listenersand.nativeusage
 
- Warn of every 
- Subtle differences between @vue/composition-apiand the Vue 3 implementation are listed in the@vue/composition-apiREADME
- The precedence of v-ifandv-forhave been flipped when using both on the same element.
- Warn for mixing v-forandref.
- Warn about deprecated instance methods and properties: $destroy,$children
Note: there are just rough ideas. Amendments may or may not be proposed, depending on the implementation progress of this repo.
- π΅ RFC04: Global API treeshaking & RFC09: Global mounting/configuration API change
- Vue.extendcan be supported in a compat runtime as an alias to- defineComponent
 
- π΅ RFC11: Component v-modelAPI change- I don't have a clear idea on how to progressively migrate the v-modelAPI because both the author and consumer of the components need to change their ways to use this API, according to the current RFC. So we might need a compatibility layer in the runtime.
 
- I don't have a clear idea on how to progressively migrate the 
These features are only deprecated but still supported in the compatiblity builds. There will be runtime warnings and ESLint rules to detect their usages. Some of them can be automatically migrated with the help of codemods.
- RFC15: Remove filters
- Can be detected by the vue/no-deprecated-filterESLint rule
 
- Can be detected by the 
- π΄ RFC18: Transition class name adjustments
- For <transition>components with customenter-classorleave-class:- enter-class->- enter-from-class
- leave-class->- leave-from-class
 
- Otherwise, there are two possible solutions:
- Change every .v-enterand.v-leaveselector in the stylesheets to.v-enter-fromand.v-leave-from
- Or, attach enter-from-class="v-enter v-enter-from" leave-from-class="v-leave v-leave-from"to these<transition>components. Users can delete these attributes after they updated the corresponding stylesheets
 
- Change every 
 
- For 
- RFC20: Events API Change
- Can be detected by the vue/no-deprecated-events-apiESLint rule
- π΄ A codemod can be implemented to use other libraries like tiny-emitter for the events API
 
- Can be detected by the 
- RFC23-scoped-styles-changes
- The new behavior should be opt-in
 
- RFC27: Custom Elements Interop Improvements
- (Covered by the Global API RFCs): Vue.config.ignoredElements->app.config.isCustomElement
- π΄ Vue 2 non-<component>tags withisusage ->- <component is>(for SFC templates).
- v-is(for in-DOM templates).
 
- The vue/no-deprecated-html-element-isESLint rule can be used to detect usage forisusage on built-in HTML tags.
 
- (Covered by the Global API RFCs): 
- setand- deleteinstance or global methods will be supported only in IE compat builds.- implemented as remove-vue-set-and-delete
 
- implemented as 
Aside from migrating Vue 2 apps to Vue 3, this repository also includes some generic transformations that can help clean up your codebase.
- remove-trivial-root- this transformation removes trivial root components like { render: () => h(App) }and useAppas the direct root
 
- this transformation removes trivial root components like 
- define-component- --param.useCompositionAPI:- falseby default. When set to- true, it will import the- defineComponenthelper from- @vue/composition-apiinstead of- vue
- this transformation adds defineComponent()wrapper to.vuefile exports, and replacesVue.extendcalls todefineComponent
 
See https://github.com/facebook/jscodeshift#transform-module
- Running transformations will generally ruin the formatting of your files. A recommended way to solve that problem is by using Prettier or eslint --fix.
- Even after running prettier its possible to have unnecessary new lines added/removed. This can be solved by ignoring white spaces while staging the changes in git.
git diff --ignore-blank-lines | git apply --cached