• Stars
    star
    250
  • Rank 156,385 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Vue.js codemod scripts

vue-codemod

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.

Command Line Usage

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.

Programmatic API

  • runTransformation(fileInfo, transformation, params)

Roadmap

  • Basic testing setup and a dummy CLI
  • Support applying jscodeshift codemods to .vue files
  • 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-parser for this)
  • A playground for writing transformations - yarn playground and visit http://localhost:3000

Included Transformations

Migrating from Vue 2 to Vue 3

The migration path (to be integrated in a new version of vue-migration-helper):

  1. Install eslint-plugin-vue@7, turn on the vue3-essential category (maybe a few exceptions like vue/no-deprecated-dollar-scopedslots-api)
  2. Run eslint --fix to fix all auto-fixable issues; if there are any remaining errors, fix them manually
  3. Run the codemods below
  4. Install vue@3, vue-loader@16, etc.
  5. Make sure to use the compat build of vue@3
  6. 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

Fixable in ESLint

Codemods

  • RFC01: New slot syntax and RFC06: Slots unification
    • Can be detected and partially fixed by the vue/no-deprecated-slot-attribute and vue/no-deprecated-slot-scope-attribute
    • During the transition period, with the 2 ESLint rules enabled, it will warn users when they use this.$slots, recommending this.$scopedSlots as a replacement
    • When upgrading to Vue 3, replace all .$scopedSlots occurrences with .$slots (should pass the abovementioned ESLint checks before running this codemod) (implemented as scoped-slots-to-slots)
  • 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:
        1. Scan the entire project to collect all the usages of the abovementioned global properties / methods
        2. Depending on the result of the first scan:
          1. If there's only one entry file using these global APIs, then transform it;
          2. 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;
          3. 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 optionMergeStrategies behavior change
  • 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-template ESLint 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 h calls 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 update hook 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
  • RFC25: Built-in <Teleport> component
    • Can be detected by the vue/no-reserved-component-names ESLint rule
    • ๐Ÿ”ด A codemod can be implemented to rename all <Teleport> components to some other name like <TeleportComp>.
  • ๐Ÿ”ด 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 import call to .vue files
      • Arrow functions that returns an object with the component property being a dynamic import call.
    • The only case that cannot be easily detected is 2.x async components using manual resolve/reject instead of returning promises. Manual upgrade will be required for such cases but they should be relatively rare.
  • ๐Ÿ”ด RFC30: Add emits component option
    • There could be potential naming conflicts with existing component-level emits options, so we need to scan and warn on such usages
    • To better utilize the new emits option, we can provide a codemod that automatically scans all instances of $emit calls in a component and generate the emits option
  • Vuex 3.x to 4
    • implemented as in combination of new-global-api and vuex-v4
    • Vue.use(Vuex) & new Vue({ store }) -> app.use(store)
    • new Store() -> createStore()
  • Vue Router 3.x to 4
  • vue-class-component 7.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 to Vue besides extends Vue)
    • ๐Ÿ”ด Component.registerHooks -> Vue.registerHooks

Breaking Changes that Can Only Be Manually Migrated

RFCs that May Need Amendments to Simplify the Migration

Note: there are just rough ideas. Amendments may or may not be proposed, depending on the implementation progress of this repo.

Other Opt-In Changes

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.

Generic Transformations

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 use App as the direct root
  • define-component
    • --param.useCompositionAPI: false by default. When set to true, it will import the defineComponent helper from @vue/composition-api instead of vue
    • this transformation adds defineComponent() wrapper to .vue file exports, and replaces Vue.extend calls to defineComponent

Custom Transformation

See https://github.com/facebook/jscodeshift#transform-module

Post Transformation

  • 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

More Repositories

1

vue

This is the repo for Vue 2. For Vue 3, go to https://github.com/vuejs/core
TypeScript
206,615
star
2

awesome-vue

๐ŸŽ‰ A curated list of awesome things related to Vue.js
70,860
star
3

core

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

vue-cli

๐Ÿ› ๏ธ webpack-based tooling for Vue.js Development
JavaScript
29,747
star
5

vuex

๐Ÿ—ƒ๏ธ Centralized State Management for Vue.js.
JavaScript
28,336
star
6

devtools

โš™๏ธ Browser devtools extension for debugging Vue.js applications.
TypeScript
24,243
star
7

vuepress

๐Ÿ“ Minimalistic Vue-powered static site generator
JavaScript
22,293
star
8

vue-router

๐Ÿšฆ The official router for Vue 2
JavaScript
19,008
star
9

pinia

๐Ÿ Intuitive, type safe, light and flexible Store for Vue using the composition api with DevTools support
TypeScript
12,284
star
10

vitepress

Vite & Vue powered static site generator.
TypeScript
10,982
star
11

vue-hackernews-2.0

HackerNews clone built with Vue 2.0, vue-router & vuex, with server-side rendering
JavaScript
10,964
star
12

petite-vue

6kb subset of Vue optimized for progressive enhancement
TypeScript
8,689
star
13

apollo

๐Ÿš€ Apollo/GraphQL integration for VueJS
TypeScript
5,980
star
14

vue-class-component

ES / TypeScript decorator for class-style Vue components.
TypeScript
5,812
star
15

vetur

Vue tooling for VS Code.
TypeScript
5,721
star
16

language-tools

โšก High-performance Vue language tooling based-on Volar.js
TypeScript
5,403
star
17

v2.vuejs.org

๐Ÿ“„ Documentation for Vue 2
JavaScript
5,051
star
18

vue-loader

๐Ÿ“ฆ Webpack loader for Vue.js components
TypeScript
4,970
star
19

rfcs

RFCs for substantial changes / feature additions to Vue core
4,822
star
20

eslint-plugin-vue

Official ESLint plugin for Vue.js
JavaScript
4,365
star
21

composition-api

Composition API plugin for Vue 2
TypeScript
4,176
star
22

vuefire

๐Ÿ”ฅ Firebase bindings for Vue.js
TypeScript
3,784
star
23

router

๐Ÿšฆ The official router for Vue.js
TypeScript
3,588
star
24

vue-test-utils

Component Test Utils for Vue 2
JavaScript
3,569
star
25

vue-rx

๐Ÿ‘๏ธ RxJS integration for Vue.js.
JavaScript
3,343
star
26

create-vue

๐Ÿ› ๏ธ The recommended way to start a Vite-powered Vue project
Vue
3,297
star
27

docs

๐Ÿ“„ Documentation for Vue 3
Vue
2,788
star
28

vue-touch

Hammer.js wrapper for Vue.js
JavaScript
2,728
star
29

vuex-router-sync

Effortlessly keep vue-router and vuex store in sync.
JavaScript
2,523
star
30

vue-hackernews

HackerNews clone with Vue.js
Vue
2,514
star
31

v2.cn.vuejs.org

๐Ÿ‡จ๐Ÿ‡ณ Chinese translation for v2.vuejs.org
JavaScript
1,865
star
32

babel-plugin-transform-vue-jsx

babel plugin for vue 2.0 jsx
JavaScript
1,844
star
33

babel-plugin-jsx

JSX for Vue 3
TypeScript
1,668
star
34

vue-syntax-highlight

๐Ÿ’ก Sublime Text syntax highlighting for single-file Vue components
1,481
star
35

jsx-vue2

monorepo for Babel / Vue JSX related packages
JavaScript
1,450
star
36

ui

๐Ÿ’ป UI components for official Vue organization apps
Vue
1,331
star
37

vue-docs-zh-cn

่ฏฅ้กน็›ฎๅทฒไธๅ†็ปดๆŠค๏ผŒๆœ‰ๅŠณ้€š่ฟ‡ Vue ๅฎ˜็ฝ‘ๆŸฅ้˜…ๆœ€ๆ–ฐ็š„ๆ–‡ๆกฃ
1,330
star
38

core-vapor

Vue Vapor is a variant of Vue that offers rendering without the Virtual DOM.
TypeScript
1,205
star
39

vueify

Browserify transform for single-file Vue components
JavaScript
1,172
star
40

vue-web-component-wrapper

(Vue 2 only) Wrap a Vue component as a web component / custom element.
JavaScript
1,036
star
41

docs-next-zh-cn

๐Ÿ‡จ๐Ÿ‡ณ Chinese translation for v3.vuejs.org
Vue
959
star
42

test-utils

Vue Test Utils for Vue 3
TypeScript
957
star
43

devtools-next

The next iteration of Vue DevTools
TypeScript
882
star
44

roadmap

๐Ÿ—บ๏ธ Roadmap for the Vue.js project (archive)
851
star
45

rollup-plugin-vue

Roll .vue files
TypeScript
846
star
46

repl

Vue SFC REPL as a Vue 3 component
TypeScript
799
star
47

vue-jest

Jest Vue transformer
JavaScript
741
star
48

vue-migration-helper

CLI tool to aid in migration from Vue 1.x to 2.0
JavaScript
595
star
49

vue-dev-server

A POC dev server that allows you to import `*.vue` files via native ES modules imports.
TypeScript
572
star
50

vue2-ssr-docs

Vue.js Server-Side Rendering Guide (for Vue 2)
559
star
51

vue-hot-reload-api

๐ŸŒถ๏ธ Hot reload API for Vue components
JavaScript
465
star
52

vue-animated-list

A Vue.js plugin for easily animating `v-for` rendered lists.
JavaScript
462
star
53

vue-next-webpack-preview

JavaScript
427
star
54

vue-eslint-parser

The ESLint custom parser for `.vue` files.
TypeScript
422
star
55

vue-async-data

Async data loading plugin
JavaScript
417
star
56

vue-component-compiler

Compile a single file Vue component into a CommonJS module.
TypeScript
341
star
57

vue-cli-plugin-vue-next

A Vue CLI plugin for trying out vue-next (experimental)
JavaScript
339
star
58

eslint-config-vue

JavaScript
326
star
59

component-compiler-utils

Lower level utilities for compiling Vue single file components
TypeScript
318
star
60

blog

๐Ÿ“ The official Vue.js blog
Vue
296
star
61

vue-test-utils-jest-example

Example project using Jest + vue-test-utils together
JavaScript
296
star
62

tsconfig

Base tsconfig for Vue 3 projects.
280
star
63

vue-template-explorer

Vue template compilation explorer
Vue
263
star
64

events

Source code for the new Vue.js Events page
Vue
252
star
65

jp.vuejs.org

๐Ÿ‡ฏ๐Ÿ‡ต Japanese translation for vuejs.org
JavaScript
246
star
66

theme

VitePress theme for vuejs.org.
Vue
217
star
67

v3-migration-guide

Vue 2 -> Vue 3 migration guide
JavaScript
211
star
68

vue-element

register a custom element with Vue.js.
JavaScript
208
star
69

vue-curated

๐Ÿ–ผ๏ธ The curated Vue packages list
177
star
70

Discussion

Vue.js discussion
166
star
71

vuex-observable

Consume Vuex actions as Observables using RxJS 5
JavaScript
155
star
72

vue-issue-helper

Vue
145
star
73

composition-api-converter

Automatically migrate components to the Function API
JavaScript
145
star
74

babel-preset-vue-app

Babel preset for Vue app.
JavaScript
125
star
75

art

๐ŸŽจ Artworks
121
star
76

laravel-elixir-vue-2

Laravel Elixir Vue 2.0 support plugin
JavaScript
106
star
77

composition-api-rfc

Vuepress render for the Composition API RFC
JavaScript
106
star
78

vue-router-demos

Live demos for vue-router
Vue
106
star
79

eslint-plugin-vue-libs

Eslint plugin for Vue internal development
JavaScript
105
star
80

vue-test-utils-mocha-webpack-example

Example project using mocha-webpack and vue-test-utils
JavaScript
104
star
81

eslint-config-typescript

eslint-config-typescript for vue projects
JavaScript
103
star
82

babel-preset-vue

Babel preset for transforming Vue JSX.
JavaScript
87
star
83

ecosystem-ci

Vue Ecosystem CI
TypeScript
81
star
84

vue-test-utils-getting-started

Demo project for `vue-test-utils`
JavaScript
81
star
85

vue-webpack-meteor-example

Example using Vue with Meteor, while leveraging the normal Webpack + NPM workflow for your front-end.
Vue
78
star
86

vue-requests

Need a Vue.js module or looking for ideas?
70
star
87

eslint-config-airbnb

ESLint Shareable Configs for Airbnb JavaScript Style Guide in Vue.js Projects
JavaScript
67
star
88

news.vuejs.org

Vue.js News Portal
Vue
66
star
89

vue-curated-client

Official curation list client
Vue
65
star
90

eslint-config-prettier

eslint-config-prettier for vue-cli
JavaScript
61
star
91

vue-template-es2015-compiler

Support a subset of handy ES2015 features in Vue 2.0 templates.
JavaScript
60
star
92

vue-test-utils-typescript-example

Example project using TypeScript, Jest + vue-test-utils together
Vue
59
star
93

create-eslint-config

Utility to setup ESLint in Vue.js projects.
JavaScript
41
star
94

vue-ssr-html-stream

Transform stream to simplify Vue SSR streaming
HTML
40
star
95

eslint-config-standard

ESLint Shareable Configs for JavaScript Standard Style in Vue.js Projects
JavaScript
39
star
96

create-vue-templates

Snapshots of the generated templates of `npm create vue@latest`
Vue
39
star
97

it.vuejs.org

Italian translation for vuejs.org ๐Ÿ‡ฎ๐Ÿ‡น
JavaScript
35
star
98

systemjs-plugin-vue

SystemJS plugin for Vue single file components
JavaScript
34
star
99

test-utils-docs

Docs for vue-test-utils-next
JavaScript
32
star
100

vue-curated-server

JavaScript
31
star