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 🙏

© 2025 – Pkg Stats / Ryan Hefner

vscode-page

v0.0.1

Published

A light-weight page micro framework for vscode webview.

Readme

vscode-page

vscode-page is a light-weight page micro framework for vscode webview. Vs Code Extension developers can use it to finish pages for webviews in their extensions quickly.

Features

  • abstract the communication between htmls and WebviewPanels, developers can focus on business logics.
  • built-in template engine, with handlebars.js.
  • message mapping, a simple way to organize your page logic and view.
  • baseUrl support, then you can use relative paths in your html page.

Installing

npm install vscode-page

Usage

You can build a vscode webview quickly with the following steps:

  1. define messages between your html page and vscode.WebviewPanel.
  2. write your html page and js functions used by that page.
  3. define MessageMappings.
  4. register a command to create (or show) your WebviewPanel with the MessageMappings your defined above.

The whole architecture is shown below:

vscode-page architecture

Also, there is a demo project to show its usage. Before check it, please read the following first.

Messaging

As described in vscode extension guide, html and WebviewPanel can pass messages each other. To standardize this communication, vscode-page defines two message formats.

js -> WebviewPanel

The Json format of messages from js to WebviewPanel:

  • command, a request will be processed by WebviewPanel.
  • parameters, a json argument of a handler to process thecommand. It is optional.

Here is an example:

{
  "command": "showExtension",
  "parameters": {
    "repo": repo,
    "extId": extId,
    "installed": installed
  }
}

js <- WebviewPanel

The json format of messages from WebviewPanel to js:

  • command, if it ends with "Response", then it is a response of the prefix. Such as "showExtensionResponse" is the response of "showExtension".
  • contents, a content array, it is optional. Each item has following keys:
    • id, element id in a html page.
    • body, innerHtml (generated by the template defined in MessageMappings) of the element pointed by id.
  • result, result of the "${command}" handler, it is optional.

Here is an example:

{
  "command": "showExtensionResponse",
  "contents": [
    {
      "id": "xxx",
      "body": "...."
    },
    {
      "id": "yyy",
      "body": "...."
    }
  ]
}

HTML

In order to initialize correctly, for each html, please insert the following two lines in <head>:

<base href="{{base}}" />
<script type="text/javascript">
  "{{init}}"
</script>

Here is the explaination:

  • {{base}} will be replaced by "path.join(context.extensionPath, root)", then you can use relative paths in your html.
  • {{init}} will be replaced by a js defining an EventListener to receive the messages from WebviewPanel.

Then you can invoke the initEventListener functions defined in {{init}} to initialize the EventListener receiving messages from WebviewPanel as below:

initEventListener();

The default implementation of EventListener will only process messages ending with "Response" and use the contents as the innerHtml of the elements selected.

If you want to process other messages, you can pass your function to initEventListener as below:

initEventListener(message => {...});

The message has following key:

  • result, the result of a command handler.

MessageMappings

A MessageMapping defines:

  • command, a command from the message posted by a js function.
  • handler, a handler for the command above, its signature is below and the parameters are from the message posted by a js function.
(parameters?: any) => Promise<any>;
  • templates, a template array for the views of response of the command, it is optional. Each item has the following keys:
    • id, an element id in your html.
    • content, a inline handlebars template, it is optional.
    • contentUrl, a url to a external handlebars template, it is optional.
  • forward, a command the request flow will go, it is optional.

NOTE:

  • "content" and "contentUrl" should not be used both, and one of them must be shown in the definitions.
  • "templates" and "forward" should not be used both.

Here is an example: home.ts

createOrShowPage

To create or show a WebviewPanel, you can invoke function createOrShowPage like beblow (from extension.ts in example):

createOrShowPage(
  "name",
  "ext.home",
  "Sample Page",
  "pages",
  "home.html",
  context,
  messageMappings
);

The signature of createOrShowPage:

export function createOrShowPage(
  name: string,
  viewType: string,
  title: string,
  base: string,
  page: string,
  context: vscode.ExtensionContext,
  messageMappings: MesssageMaping[]
);

Each argument:

  • name, named index of the WebViewPanel you want to create.
  • viewType and title, both of them will be passed to vscode.window.createWebviewPanel.
  • base, local resource root of html.
  • page, html file name.
  • context, vscode.ExtensionContext.
  • messageMappings, MesssageMaping array you defined, find examples in home.ts.

Requirements

vscode-page is a tool for vscode extension development and uses handlebars.js as its template engine.

So, please install vscode and handlebars first.

Development

For local debugging, please run the following command under the directory of vscode-page project:

npm link

Then, in the project using vscode-page, please run the following command:

npm link vscode-page

NOTE:

The linked package in application will use its own node_modules, so if you defined your own handlebars helper in your application, vscode-page can not find it.

app
 |- node_modules
     |- vscode-page  <-- project linked by "npm link"
     |   ├─ node_modules
     |   |    |- handlebars.js <-- used by vscode-page
     |
     |- handlebars.js <-- you defined helper in it

I can not find an easy way to fix it, only find the following steps. If you have any good idea, please let me know.

  • "npm pack" in vscode-page project.
  • "npm install local-package" in your application.

Known Issues

Please visit issues page to submit new issues or check known issues.

Release Notes

0.0.1

First release.

License

vscode-page is released under the Apache License 2.0 license.