Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Vue.js codemod scripts

License

NotificationsYou must be signed in to change notification settings

webdevike/vue-codemods

 
 

Repository files navigation

Current status: experimental

This repository contains a collection of codemod scripts for use withJSCodeshift that help update Vue.js APIs.

Inspired byreact-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 applyingjscodeshift codemods to.vue files
  • Provide a programmatic interface for usage invue-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 usevue-eslint-parser for this)
  • A playground for writing transformations -yarn playground and visithttp://localhost:3000

Included Transformations

Migrating from Vue 2 to Vue 3

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

  1. Install eslint-plugin-vue@7, turn on thevue3-essential category (maybe a few exceptions likevue/no-deprecated-dollar-scopedslots-api)
  2. Runeslint --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:

MarkDescription
🔴work not started
🔵needs to or can be implemented in the compat runtime

Fixable in ESLint

Codemods

  • RFC01: New slot syntax andRFC06: Slots unification
    • Can be detected and partially fixed by thevue/no-deprecated-slot-attribute andvue/no-deprecated-slot-scope-attribute
    • During the transition period, with the 2 ESLint rules enabled, it will warn users when they usethis.$slots, recommendingthis.$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 asscoped-slots-to-slots)
  • RFC04: Global API treeshaking &RFC09: Global mounting/configuration API change
    • implemented asnew-global-api
    • import Vue from 'vue' ->import * as Vue from 'vue' (implemented asvue-as-namespace-import)
    • Vue.extend ->defineComponent (implemented asdefine-component)
    • new Vue() ->Vue.createApp() (implemented asnew-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() andimport { h } from 'vue' (implemented asremove-contextual-h-from-render)
    • Vue.config.productionTip -> removed (implemented asremove-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 usingVue.*, 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 onoptionMergeStrategies 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 thevue/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 athttps://github.com/vueComponent/jsx/)
    • 🔴 For Users who manually write render functions usingh
      • 🔵 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-convertsh 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
    • removeupdate 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 asimport-composition-api-from-vue)
    • There are some subtle differences between the plugin and Vue 3 implementation, seehttps://github.com/vuejs/composition-api#limitations for more details.
  • RFC16: Removeinline-template
  • RFC25: Built-in<Teleport> component
    • Can be detected by thevue/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 dynamicimport call to.vue files
      • Arrow functions that returns an object with thecomponent property being a dynamicimport call.
    • The only case that cannot be easily detected is 2.x async components using manualresolve/reject instead of returning promises. Manual upgrade will be required for such cases but they should be relatively rare.
  • 🔴RFC30: Addemits component option
    • There could be potential naming conflicts with existing component-levelemits options, so we need to scan and warn on such usages
    • To better utilize the newemits option, we can provide a codemod that automatically scans all instances of$emit calls in a component and generate theemits option
  • Vuex 3.x to 4
    • implemented as in combination ofnew-global-api andvuex-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 toVue besidesextends 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 useApp as the direct root
  • define-component
    • --param.useCompositionAPI:false by default. When set totrue, it will import thedefineComponent helper from@vue/composition-api instead ofvue
    • this transformation addsdefineComponent() wrapper to.vue file exports, and replacesVue.extend calls todefineComponent

Custom Transformation

Seehttps://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 usingPrettier oreslint --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

About

Vue.js codemod scripts

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript79.9%
  • Vue13.9%
  • JavaScript4.5%
  • HTML1.1%
  • CSS0.6%

[8]ページ先頭

©2009-2025 Movatter.jp