Vue Diff Viewer
Vue-diff is a Vue 3 component designed for rendering code differences in a user-friendly interface. It offers both split (side-by-side) and unified (inline) viewing modes, similar to those found in popular version control interfaces like GitHub Desktop. The component leverages `diff-match-patch` for its core diffing algorithm and `highlight.js` for robust syntax highlighting across various programming languages. Key features include support for customizable light and dark themes, virtual scrolling to efficiently handle large text comparisons, and a folding view for improved readability. It ships with TypeScript types, ensuring a good developer experience in TypeScript projects. The current stable version is 1.2.4, released in June 2022. While functional, its release cadence appears to be slow, suggesting a maintenance-focused development status rather than active feature expansion.
Common errors
-
Error: Failed to resolve component: Diff
cause The `vue-diff` component has not been correctly registered with your Vue application instance.fixEnsure you have called `app.use(VueDiff);` (or `app.use(VueDiff, { componentName: 'YourComponentName' });`) in your main application file (e.g., `main.ts` or `main.js`) before mounting the app. -
Cannot find module 'vue-diff/dist/index.css' or its corresponding type declarations.
cause The CSS styles for `vue-diff` were not imported, leading to missing styling for the diff viewer.fixAdd `import 'vue-diff/dist/index.css';` to your main application entry file (e.g., `main.ts` or `main.js`) or within a global style sheet. -
Syntax highlighting is not working for specific languages.
cause The language specified in the `language` prop is not supported by default, or `highlight.js` needs to be extended with the desired language.fixCheck the list of default supported languages in the `vue-diff` documentation. If your language is not listed, refer to the 'Extend languages' section to register additional `highlight.js` languages.
Warnings
- breaking `vue-diff` is exclusively built for Vue 3. It will not function with Vue 2 applications, requiring a migration if coming from an older Vue version.
- gotcha The library has not received a major update since June 2022, suggesting it is in maintenance mode. While functional, expect slower bug fixes, feature additions, or updates to newer dependencies.
- gotcha When customizing themes, ensure that the CSS variables or custom theme classes are correctly defined and provided. The `theme` prop accepts `'dark'`, `'light'`, or `custom${string}`. Incorrectly named custom themes will default to the fallback theme.
Install
-
npm install vue-diff -
yarn add vue-diff -
pnpm add vue-diff
Imports
- VueDiff
const VueDiff = require('vue-diff');import VueDiff from 'vue-diff';
- CSS Styles
import 'vue-diff/index.css';
import 'vue-diff/dist/index.css';
- Vue Application Plugin
new Vue().use(VueDiff);
app.use(VueDiff);
Quickstart
import { createApp } from 'vue';
import VueDiff from 'vue-diff';
import 'vue-diff/dist/index.css';
const app = createApp({
template: `
<div style="padding: 20px;">
<h1>Code Diff Example</h1>
<Diff
mode="split"
theme="dark"
language="typescript"
:prev="prevCode"
:current="currentCode"
:virtual-scroll="{ height: 300, lineMinHeight: 24, delay: 100 }"
/>
</div>
`,
setup() {
const prevCode = `
function hello(name: string) {
console.log('Hello, ' + name + '!');
const age = 30;
return name + age;
}
`;
const currentCode = `
function greeting(name: string) {
console.log('Hi, ' + name + '!');
const city = 'New York';
return `Welcome, ${name} from ${city}`;
}
`;
return { prevCode, currentCode };
}
});
app.use(VueDiff, { componentName: 'Diff' });
app.mount('#app');