vue-sfc-parser is a utility library for parsing Vue.js Single File Components (SFCs) specifically designed for static analysis workflows. It provides a structured representation of an SFC, similar to `vue-template-compiler`'s `parseComponent` function, but enhances it with additional helper methods on `SFCBlock` objects, such as `calcGlobalOffset` and `calcGlobalRange`, to facilitate mapping block-specific positions back to the overall file position. Currently at version 0.1.2, the library also includes a `createDiffWatcher` API for efficiently detecting changes within individual SFC blocks, enabling sophisticated hot-reloading or analysis tools. Its release cadence is ad-hoc, reflecting its early development stage. A key differentiator is its focus on providing precise positional data and change detection, which is crucial for linters, language servers, and build tools that operate on Vue SFCs.
npm install vue-sfc-parserVerified import paths — ran on the pinned version, not inferred.
Demonstrates parsing a Vue SFC, accessing its blocks, and using `calcGlobalOffset` to map block-relative positions to global file positions.
Pin exact versions (e.g., `"vue-sfc-parser": "0.1.2"`) or use caret (`^0.1.2`) with caution, testing thoroughly after updates.
Implement explicit null checks, e.g., `if (sfcDescriptor.template) { /* ... */ }` or use optional chaining `sfcDescriptor.template?.content`.Use `import { parseComponent } from 'vue-sfc-parser';` instead of `const { parseComponent } = require('vue-sfc-parser');`.Ensure you are using `import { parseComponent } from 'vue-sfc-parser';` for ESM, or if strictly CommonJS, verify the export structure. If using TypeScript, check `tsconfig.json` for module resolution settings.Add a null check before accessing the block: `if (sfcDescriptor.script) { console.log(sfcDescriptor.script.content); }` or use optional chaining: `sfcDescriptor.script?.content`.No dependency data recorded yet.