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-ast-editor

v0.1.1

Published

A tool for programmatically manipulating Vue single-file components

Downloads

5

Readme

Vue Ast Editor

A tool for programmatically manipulating Vue single-file components.

Usage

import VueAstEditor from 'vue-ast-editor';

const ast = new VueAstEditor(src);
await ast.ready();

// imports the component and adds it to the component option,
// initializing the option if necessary
ast.importComponent('@/components/MyComponent.vue');
// initializes the data option or adds to it
ast.addData('foo', '"this string will be parsed into a JS value"');
ast.addProp('myProp');
// automatically restructures between array and object syntax
ast.updateProp('myProp', { type: 'String', required: 'false', default: '"default value"' });
ast.addMethod('myMethod');
// updates references in the component definition, HTML attributes, and HTML template expressions
// renaming is implemented heuristically and not promised to be 100% accurate
ast.renameMethod('myMethod', 'newName');

console.log(ast.toString());

Methods

General

  • ready()
    • returns a promise that resolves when parsing is done
  • toString()
    • prints the source code of the component
    • options unconfigurable and set to personal preferences; recommend piping the output through a formatter
  • filterHAST(filter)
    • a convenience wrapper around posthtml.tree.match
  • findOption(name)
    • returns a jscodeshift collection
  • renameAttribute(name, newName)
    • called internally by the various renaming methods

Option management

  • importComponent(path: string)
    • path expects a file type ending
  • deportComponent(name)
    • note the difference in API. To import a component, we need its path, but to deport it, we need only to know its name; e.g., "@/components/MyBtn.vue" vs. "MyBtn"
  • components()
    • returns an object of { [component_name]: true }
  • addProp(name)
  • renameProp(name, newName)
  • updateProp(name, attrs)
    • where attrs is an object of { [attr_name]: string | null }
    • string will be parsed, null removes the attribute
  • removeProp(name)
  • props()
    • returns an object of { [prop_name]: [ast_node] }
  • addData(name, val: string) // string will be parsed
  • renameData(name, newName)
  • setData(name, newVal: string) // string will be parsed
  • data()
    • returns an object of { [data_name]: [ast_node] }
  • addWatcher(name, node=null)
    • the optional second argument is for refactoring purposes
  • renameWatcher(name, newName)
  • updateWatcher(name, attrs)
    • where attrs is an object of { [deep | immediate]: true | null }
  • removeWatcher(name)
  • watchers()
    • returns an object of { [watcher_name]: [ast_node] }
  • addComputed(name, node=null)
    • optional second argument is for refactoring purposes
  • renameComputed(name, newName)
  • addComputerSetter(name)
  • removeComputedSetter(name)
  • removeComputed(name)
  • computed()
    • returns an object of { [computed_name]: [ast_node] }
  • addMethod(name, node=null)
    • optional second argument is for refactoring purposes
  • renameMethod(name, newName)
  • removeMethod(name)
  • methods()
    • returns an object of { [method_name]: [ast_node] }
  • addHook(name)
  • removeHook(name)

Refactoring (experimental!)

  • async refactorIntoComponent(htmlNode, cmpPath, attrs=[])
    • pushes the given HTML node into a new component
    • attrs is a list of HTML attributes that you want moved into the new component. attrs not in the list will remain in the source component.
  • pushAboveSlot(htmlNode, cmp)
    • given a node in a slot
  • pushBelowSlot(htmlNode, cmp)
  • pushAroundSlot(htmlNode, cmp)
  • pushIntoSlot(htmlNode, cmp)
  • pushIntoNewSlot(htmlNode, slotName, hostCmp)
  • pushComponent(componentName, cmp)
  • pushData(dataName | array of names, cmp)
  • pushComputed(computedName | array of names, cmp)
  • pushWatcher(watcherName | array of names, cmp)
  • pushMethod(methodName | array of name, cmp)

Lower-level utilities used in refactoring

  • findContainingSlot(htmlNode)
  • findParentComponent(htmlNode)
  • findHostSlot(htmlNode, hostCmp)
  • removeNode(htmlNode)
  • copyNode(htmlNode)
  • replaceNode(target, replacement)
  • insertBefore(newNode, target)
  • insertAfter(newNode, target)
  • append(node, parent)