Vue SFC Parser

1.1.6 · abandoned · verified Sun Apr 19

Vue Parser is a utility library designed to parse the contents of Vue Single File Components (.vue files) by leveraging an Abstract Syntax Tree (AST), specifically based on the `parse5` library. It enables developers to extract specific content from `<template>`, `<script>`, or `<style>` tags. A key differentiator is its ability to pad the extracted content with a specified string (defaulting to comments for script/style) to preserve original line numbers, which is particularly beneficial for linting and error reporting tools. The current stable version is 1.1.6, but the package has not seen updates for approximately four years as of April 2026. This lack of recent development suggests the project is abandoned, which may lead to compatibility issues with newer versions of Vue.js (especially Vue 3+), Node.js, or related ecosystem tools.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates parsing a Vue SFC to extract content from `<script>`, `<template>`, and `<style>` tags, illustrating how to specify language attributes and showing the resulting padded output for preserving line numbers.

import vueParser from 'vue-parser';

const vueFileContents = `
<template lang="pug">
.hello
  h1 {{ msg }}
</template>

<script lang="js">
export default {
  name: 'Hello',

  data () {
    return {
      msg: 'Hello World!'
    }
  }

}
</script>

<style>
h1 {
  font-weight: normal;
}
</style>
`;

console.log('--- Extracting Script Content ---');
const myScriptContents = vueParser.parse(vueFileContents, 'script', { lang: ['js', 'jsx'] });
console.log(myScriptContents);

console.log('\n--- Extracting Template Content ---');
const myTemplateContents = vueParser.parse(vueFileContents, 'template', { lang: ['pug'] });
console.log(myTemplateContents);

console.log('\n--- Extracting Style Content (default CSS) ---');
const myStyleContents = vueParser.parse(vueFileContents, 'style');
console.log(myStyleContents);

view raw JSON →