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

vway

v0.0.3

Published

一个描述 name, attribute, children 结构 的内部DSL

Downloads

3

Readme

一个描述 name, attribute, children 结构 的内部DSL

示例

import { init } from './vway'

let t = init()
let { div, a, img, h1, p, button } = t

let dom = div(
  a`href="https://vitejs.dev" target="_blank"`(img`src=${src1} class="logo" alt="Vite logo"`()),
  a`href="https://www.typescriptlang.org/" target="_blank"`(
    img`src=${src2} class="logo vanilla" alt="TypeScript logo"`()
  ),
  h1('Vite + TypeScript'),
  div`class="card"`(button`id="counter" type="button"`()),
  p`class="read-the-docs"`('Click on the Vite and TypeScript logos to learn more')
)

console.log(dom)

函数名为name字段,模板字符串为attribute字段,最后括号里的参数则为 children 字段,注意,一般情况下,哪怕没有 children 字 段, 也不能省略最后的小括号,没有调用之前是一个 Vway对象,调用之后才拿到 数据,但是在没有 attribute 字段时模板字符串是可 以省略的,参考示例里的 h1 标签


可以在 init 时 配置 transform 选项,可以自定义 Vway 对象调用之后的返回值,示例如下

import { init, Vway } from './vway'

const id = Symbol('html')
let t = init({
  id,
  transform({ name, attribute, children }) {
    let dom = document.createElement(name)
    attribute?.forEach((val, key) => {
      dom.setAttribute(key, val[0]) // attribute 的结构是 Map<string, any[]> 这里假设所有字段都只定义一次,只用作演示,不考虑更多复杂的情况
    })
    children?.forEach(item => {
      if (item instanceof Node || typeof item === 'string') dom.append(item)
      else if (item instanceof Vway && item?.id === id) dom.append(item()) // 如果children中有Vway对象,在这里手动调用,可以帮助没有 children 的Vway在书写时省略小括号
    })
    return dom
  }
})
let { div, a, img, h1, p, button } = t

let dom = div(
  a`href="https://vitejs.dev" target="_blank"`(img`src=${src1} class="logo" alt="Vite logo"`),
  a`href="https://www.typescriptlang.org/" target="_blank"`(
    img`src=${src2} class="logo vanilla" alt="TypeScript logo"` // 因为在 transform 当中进行了调用, 在这里就可以省略最后的小括号
  ),
  h1('Vite + TypeScript'),
  div`class="card"`(button`id="counter" type="button"`),
  p`class="read-the-docs"`('Click on the Vite and TypeScript logos to learn more')
)

document.querySelector('body')!.append(dom)