{"id":21701434,"url":"https://github.com/ktsn/vue-sfc-parser","last_synced_at":"2025-04-12T13:36:39.619Z","repository":{"id":57396586,"uuid":"123159541","full_name":"ktsn/vue-sfc-parser","owner":"ktsn","description":"Vue.js single file component parser for static analysis","archived":false,"fork":false,"pushed_at":"2018-10-29T10:32:32.000Z","size":65,"stargazers_count":47,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-26T08:11:23.211Z","etag":null,"topics":["parser","single-file-component","static-analysis","vue"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ktsn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-02-27T16:50:45.000Z","updated_at":"2025-02-17T09:12:55.000Z","dependencies_parsed_at":"2022-08-27T14:27:50.106Z","dependency_job_id":null,"html_url":"https://github.com/ktsn/vue-sfc-parser","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ktsn%2Fvue-sfc-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ktsn%2Fvue-sfc-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ktsn%2Fvue-sfc-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ktsn%2Fvue-sfc-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ktsn","download_url":"https://codeload.github.com/ktsn/vue-sfc-parser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248574006,"owners_count":21126939,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["parser","single-file-component","static-analysis","vue"],"created_at":"2024-11-25T20:19:56.912Z","updated_at":"2025-04-12T13:36:39.591Z","avatar_url":"https://github.com/ktsn.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Vue SFC Parser\n\nVue.js single file component parser for static analysis.\n\n## Usage\n\nVue SFC Parser is similar to [vue-template-compiler](https://www.npmjs.com/package/vue-template-compiler)'s `parseComponent` but has some useful helpers for code analysis.\n\n```js\nconst { parseComponent } = require('vue-sfc-parser')\n\nconst code = `\n\u003ctemplate\u003e\n  \u003cp\u003eHi\u003c/p\u003e\n\u003c/template\u003e\n\n\u003cscript lang=\"ts\"\u003e\nexport default {}\n\u003c/script\u003e\n`\n\nconst res = parseComponent(code)\nconsole.log(res.template.calcGlobalOffset(7))\nconsole.log(res.script.calcGlobalOffset(5))\n```\n\n## References\n\n### `parseComponent(code: string): SFCDescriptor`\n\nThis is almost same as `vue-template-compiler`'s `parseComponent`. `SFCDescriptor` is looks like following:\n\n```ts\ninterface SFCDescriptor {\n  template: SFCBlock | null\n  script: SFCBlock | null\n  styles: SFCBlock[]\n  customBlocks: SFCBlock[]\n}\n```\n\nThe `SFCBlock` is similar to `vue-template-compiler` one too, but having additional helper methods.\n\n### Additional Helpers of SFCBlock\n\n* `calcGlobalOffset(offset: number): number`\n* `calcGlobalRange(range: [number, number]): [number, number]`\n\n  These methods are for calcurating global position from block position. For example:\n\n  ```vue\n  \u003cdocs\u003eTest Docs\u003c/docs\u003e\n  \u003ctemplate\u003e\n    \u003cp\u003eHi\u003c/p\u003e\n  \u003c/template\u003e\n  ```\n\n  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.\n\n### `createDiffWatcher(): SFCDiffWatcher`\n\nCreate a watcher object which will detect each SFC block's diff. `SFCDiffWatcher` has following methods:\n\n* `add(filename: string, content: string): SFCDescriptor`\n* `remove(filename: string): void`\n* `diff(filename: string, content: string): SFCDiff`\n\nYou 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.\n\nExample:\n\n```js\nconst { createDiffWatcher } = require('vue-sfc-parser')\nconst fs = require('fs')\nconst chokidar = require('chokidar')\n\nconst watcher = createDiffWatcher()\n\nchokidar\n  .watch('**/*.vue')\n  .on('add', filename =\u003e {\n    watcher.add(filename, fs.readFileSync(filename, 'utf8'))\n  })\n  .on('unlink', filename =\u003e {\n    watcher.add(filename)\n  })\n  .on('change', filename =\u003e {\n    watcher\n      .diff(filename, fs.readFileSync(filename, 'utf8'))\n      .template(template =\u003e {\n        console.log(template.content)\n      })\n      .script(script =\u003e {\n        console.log(script.content)\n      })\n      .styles(styles =\u003e {\n        styles.forEach(s =\u003e {\n          console.log(s.content)\n        })\n      })\n      .customBlocks('block-name', blocks =\u003e {\n        blocks.forEach(b =\u003e {\n          console.log(b.content)\n        })\n      })\n  })\n```\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fktsn%2Fvue-sfc-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fktsn%2Fvue-sfc-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fktsn%2Fvue-sfc-parser/lists"}