Vue jscodeshift Adapter

3.0.0 · maintenance · verified Tue Apr 21

vue-jscodeshift-adapter is a utility library designed to bridge jscodeshift, Facebook's JavaScript codemod toolkit, with Vue.js Single File Components (SFCs). It enables developers to apply programmatic code transformations specifically to the `<script>` sections within `.vue` files, which jscodeshift does not natively support. The current stable version is 3.0.0. This package was an early solution in the Vue codemod space, heavily inspiring the 'jscodeshift-adapters' project. While jscodeshift itself receives regular updates, vue-jscodeshift-adapter appears to be in a maintenance-only state with its last release over two years ago. Its primary differentiator is its targeted extraction and re-insertion of Vue SFC script content, allowing standard jscodeshift transforms to operate on Vue-specific logic, distinguishing it from general jscodeshift usage or other Vue migration tools like 'vue-codemod' that offer pre-built transformations.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a jscodeshift transform, adapt it for Vue SFCs using vue-jscodeshift-adapter, and execute it via the jscodeshift CLI to modify 'var' declarations to 'let' and add comments to script blocks.

import adapt from 'vue-jscodeshift-adapter';

// Define your jscodeshift transform function.
// This example changes 'var' to 'let' and adds a comment.
function myCodemod(file, api) {
  const j = api.jscodeshift;
  const root = j(file.source);

  // Transform 'var' declarations to 'let'
  root.find(j.VariableDeclaration, { kind: 'var' })
    .forEach(path => {
      path.node.kind = 'let';
    });

  // Add a comment at the top of the script block
  const firstNode = root.get().node;
  if (firstNode) {
    firstNode.comments = firstNode.comments || [];
    firstNode.comments.unshift(j.commentBlock(' This script was processed by vue-jscodeshift-adapter '));
  }

  return root.toSource();
}

// Export the adapted transform function
export default adapt(myCodemod);

/*
To run this codemod on your Vue and JavaScript files:

1. Save the above code as `transform.js` in your project root.
2. Ensure you have `jscodeshift` installed as a dev dependency:
   `npm install jscodeshift --save-dev`
3. Run the codemod using the jscodeshift CLI:
   `npx jscodeshift your-project-path -t ./transform.js --extensions vue,js`

   Replace `your-project-path` with the actual path to your source files (e.g., `./src`).
   The `--extensions vue,js` flag ensures both Vue SFCs and regular JS files are processed.
*/

view raw JSON →