Vue jscodeshift Adapter
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
-
This may cause things to work incorrectly. Make sure to use the same version for both. (vue-template-compiler@X.Y.Z)
cause A version mismatch between the installed `vue` package and `vue-template-compiler` (often a transitive dependency) causes conflicts in parsing Vue Single File Components.fixExplicitly install `vue-template-compiler` as a dev dependency with the exact same version as your `vue` package: `npm install vue-template-compiler@<YOUR_VUE_VERSION> --save-dev`. -
Error: Your transform did not return a value. Please return the root.toSource().
cause The jscodeshift transform function, after using `vue-jscodeshift-adapter`, must explicitly return the modified source code using `root.toSource()`.fixEnsure your `myCodemod` function (passed to `adapt`) ends with `return root.toSource();`. -
jscodeshift: unknown parser: 'vue'
cause This error typically indicates that jscodeshift itself does not recognize 'vue' as a valid parser type, or that the `--extensions` flag is misunderstood as a parser instruction. The `vue-jscodeshift-adapter` handles the Vue SFC parsing internally.fixYou do not need to specify a 'vue' parser. Ensure you are using the `--extensions vue,js` flag correctly with jscodeshift to tell it which file types to process. If you need a specific JavaScript/TypeScript parser for the *script content*, specify it via `--parser=tsx` or `--parser=babel` etc.
Warnings
- gotcha This adapter only processes the `<script>` block within Vue Single File Components. If a `.vue` file lacks a `<script>` block, your jscodeshift transform will not be invoked for that file, and no changes will be applied, even if other blocks (like `<template>` or `<style>`) exist.
- gotcha The package has not been updated in over two years (last release 3.0.0 was March 2022). This means it may not be fully compatible with the latest Vue.js versions (e.g., Vue 3.3+, Vue 3.4+) or recent versions of jscodeshift and its underlying parsers, potentially leading to parsing errors or unexpected behavior with modern JavaScript/TypeScript syntax or newer Vue SFC features.
- gotcha Version mismatches with `vue-template-compiler` (a common dependency in the Vue ecosystem) can occur, leading to build errors or incorrect parsing of Vue SFCs. This can happen if `vue-jscodeshift-adapter` or another tool in your dependency tree relies on a specific `vue-template-compiler` version that conflicts with your project's Vue version.
Install
-
npm install vue-jscodeshift-adapter -
yarn add vue-jscodeshift-adapter -
pnpm add vue-jscodeshift-adapter
Imports
- adapt
const adapt = require('vue-jscodeshift-adapter');import adapt from 'vue-jscodeshift-adapter';
Quickstart
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.
*/