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

@nrg-ui/code-snippets

v0.3.2

Published

Extract code snippets from your source files for runtime rendering in Vite-powered apps.

Downloads

286

Readme

@nrg-ui/code-snippets

Extract code snippets from your source files for runtime rendering in Vite-powered apps.

Features

  • Multiple snippets per file
  • Merge snippets from multiple files
  • Deterministic ordering
  • Hot reload in dev mode
  • TypeScript-friendly virtual module

Installation

npm install @nrg-ui/code-snippets

Usage

Configuration

// vite.config.ts
import { defineConfig } from 'vite';
import { codeSnippetsPlugin } from '@nrg-ui/code-snippets';

export default defineConfig({
  plugins: [codeSnippetsPlugin()],
});

Plugin Options

The plugin can be used out-of-the-box, but can also be configured to suit your needs.

| Option | Type | Default | Description | | --------------- | ---------- | -------------------------------- | --------------------------------------------------- | | include | string[] | ['src/**/*', 'app/**/*'] | Files to include for snippet extraction. | | exclude | string[] | ['node_modules/**', 'dist/**'] | Files to exclude from snippet extraction. | | rootDir | string | process.cwd() | Root directory to scan for snippets. | | markers.start | RegExp | /\bBEGIN-SNIPPET\s+(\S+)\b/ | Regular expression to match the start of a snippet. | | markers.end | RegExp | /\bEND-SNIPPET\b/ | Regular expression to match the end of a snippet. |

Example

Assume the following src/my-example.ts:

import { readFileSync } from 'fs';

// BEGIN-SNIPPET example-snippet
try {
  const content = readFileSync('my-file.json', 'utf-8');
  JSON.parse(content);
} catch (e) {
  console.error('There was an error loading `my-file.json`', e);
}
// END-SNIPPET
import snippets from '@nrg-ui/code-snippets/virtual';

console.log(snippets['example-snippet']);

This would log the following object:

{
  "name": "example-snippet",
  "code": "try {\n  const content = readFileSync('my-file.json', 'utf-8');\n  JSON.parse(content);\n} catch (e) {\n  console.error('There was an error loading `my-file.json`', e);\n}",
  "sources": [
    {
      "code": "try {\n  const content = readFileSync('my-file.json', 'utf-8');\n  JSON.parse(content);\n} catch (e) {\n  console.error('There was an error loading `my-file.json`', e);\n}",
      "location": {
        "file": "src/my-example.ts",
        "lines": {
          "start": 4,
          "end": 9
        }
      }
    }
  ]
}

Here's an example of using a code snippet in an Ember application:

import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import snippets from '@nrg-ui/code-snippets/virtual';
import { getHighlighter } from 'shiki';

interface SnippetViewerSignature {
  Args: {
    snippetName: string;
  };
}

export default class SnippetViewer extends Component<SnippetViewerSignature> {
  @tracked
  highlightedCode: string = '';

  async setup() {
    const highlighter = await getHighlighter({ theme: 'nord' });

    const snippet = snippets[this.args.snippetName]?.code ?? '';
    this.highlightedCode = highlighter.codeToHtml(snippet, { lang: 'ts' });
  }

  <template>
    <div class='code-snippet-container'>
      {{{this.highlightedCode}}}
    </div>
  </template>
}