Vue Codemod Scripts

0.0.5 · active · verified Sun Apr 19

vue-codemod is an experimental utility providing a collection of codemod scripts to assist developers in updating Vue.js APIs, primarily for migrating applications from Vue 2 to Vue 3. It leverages Facebook's JSCodeshift for abstract syntax tree (AST) transformations and offers specific support for `.vue` single-file components. As of version `0.0.5`, it includes transformations like `remove-vue-set-and-delete` and outlines a roadmap for further features, including TypeScript support and broader module system compatibility. Maintained by the Vue.js team, its "experimental" status and low version number indicate a development cadence focused on feature completion rather than rapid, frequent releases, positioning it as a tool for early adopters or those undertaking significant migrations. Its key differentiators include official Vue team backing and direct support for `.vue` file transformations, which are critical for comprehensive Vue application upgrades.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates the programmatic usage of `vue-codemod` by applying a simple, custom transformation to a temporary file. This illustrates how to interface with `runTransformation` directly, though CLI usage is more common.

import { runTransformation } from 'vue-codemod';
import path from 'path';
import fs from 'fs';
import os from 'os';

// Create a temporary directory and file for the quickstart to operate on
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'vue-codemod-quickstart-'));
const filePath = path.join(tempDir, 'main.js');
const originalCode = 'import Vue from "vue";\nnew Vue().mount("#app");\nconsole.log("Hello Vue");';
fs.writeFileSync(filePath, originalCode, 'utf8');

const fileInfo = {
  path: filePath,
  source: fs.readFileSync(filePath, 'utf8')
};

// This dummy transformation replaces 'Vue' with 'App' for demonstration.
// In a real scenario, the 'transformation' argument for 'runTransformation'
// would typically be a loaded module from 'vue-codemod/dist/transformations/<name>'.
// The 'api' object passed to the transformation function contains 'jscodeshift'.
const dummyTransformation = (file: { path: string; source: string }, api: { jscodeshift: any }) => {
  const j = api.jscodeshift;
  const root = j(file.source);
  return root.find(j.Identifier, { name: 'Vue' })
    .replaceWith(j.identifier('App'))
    .toSource();
};

async function applyCodemod() {
  console.log(`Created temporary file at: ${filePath}`);
  console.log(`Applying codemod to ${filePath}...`);
  try {
    const transformedCode = await runTransformation(
      fileInfo,
      dummyTransformation, // Pass the transformation function
      {} // Optional parameters for the transformation
    );
    console.log('Transformation complete.');
    console.log('Original code (first 50 chars):\n' + fileInfo.source.substring(0, 50) + '...');
    console.log('Transformed code (first 50 chars):\n' + transformedCode.substring(0, 50) + '...');
    // In a real application, you'd write `transformedCode` back to the file
    // fs.writeFileSync(filePath, transformedCode, 'utf8');
  } catch (error) {
    console.error('Error during transformation:', error);
  } finally {
    // Clean up the temporary directory and file
    fs.rmSync(tempDir, { recursive: true, force: true });
    console.log(`Cleaned up temporary directory: ${tempDir}`);
  }
}

applyCodemod();

view raw JSON →