Giter VIP home page Giter VIP logo

vue-sfc-parser's Introduction

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 SFCBlock 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): SFCDescriptor
  • 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

vue-sfc-parser's People

Contributors

getflourish avatar ktsn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.