npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-sfc-parser

v0.1.2

Published

Vue.js single file component parser for static analysis

Downloads

80,672

Readme

Vue SFC Parser

Vue.js single file component parser for static analysis.

Usage

Vue SFC Parser is similar to vue-template-compiler's parseComponent but has some useful helpers for code analysis.

const { parseComponent } = require('vue-sfc-parser')

const code = `
<template>
  <p>Hi</p>
</template>

<script lang="ts">
export default {}
</script>
`

const res = parseComponent(code)
console.log(res.template.calcGlobalOffset(7))
console.log(res.script.calcGlobalOffset(5))

References

parseComponent(code: string): SFCDescriptor

This is almost same as vue-template-compiler's parseComponent. SFCDescriptor is looks like following:

interface SFCDescriptor {
  template: SFCBlock | null
  script: SFCBlock | null
  styles: SFCBlock[]
  customBlocks: SFCBlock[]
}

The SFCBlcok is similar to vue-template-compiler one too, but having additional helper methods.

Additional Helpers of SFCBlock

  • calcGlobalOffset(offset: number): number

  • calcGlobalRange(range: [number, number]): [number, number]

    These methods are for calcurating global position from block position. For example:

    <docs>Test Docs</docs>
    <template>
      <p>Hi</p>
    </template>

    On the above SFC, if you provide 5 to template.calcGlobalOffset which indicates the position from the beggining of template block, it will return 38 which is the position from the beggining of the file.

createDiffWatcher(): SFCDiffWatcher

Create a watcher object which will detect each SFC block's diff. SFCDiffWatcher has following methods:

  • add(filename: string, content: string): void
  • remove(filename: string): void
  • diff(filename: string, content: string): SFCDiff

You can add/remove SFC file to the watcher by using add/remove methods. Then you obtain each SFC block's diff by using diff method. It returns an object having some methods which you can register callbacks that will called when the corresponding blocks are changed.

Example:

const { createDiffWatcher } = require('vue-sfc-parser')
const fs = require('fs')
const chokidar = require('chokidar')

const watcher = createDiffWatcher()

chokidar
  .watch('**/*.vue')
  .on('add', filename => {
    watcher.add(filename, fs.readFileSync(filename, 'utf8'))
  })
  .on('unlink', filename => {
    watcher.add(filename)
  })
  .on('change', filename => {
    watcher
      .diff(filename, fs.readFileSync(filename, 'utf8'))
      .template(template => {
        console.log(template.content)
      })
      .script(script => {
        console.log(script.content)
      })
      .styles(styles => {
        styles.forEach(s => {
          console.log(s.content)
        })
      })
      .customBlocks('block-name', blocks => {
        blocks.forEach(b => {
          console.log(b.content)
        })
      })
  })

License

MIT