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

vite-plugin-generate-typed-assets

v1.2.0

Published

A Vite plugin to generate static-typed asset files automatically.

Downloads

6

Readme

Vite Plugin: Generate Assets

npm version license

A Vite plugin that automatically generates TypeScript asset index files (index.ts and index.d.ts) for your assets directory, complete with type definitions. It watches for changes in your assets folder and updates the index files accordingly, providing a seamless way to manage and import your assets with full type safety and autocomplete support.

Table of Contents

Features

  • Automatic Index Generation: Generates index.ts and index.d.ts files for your assets directory.
  • Type Definitions: Provides TypeScript type definitions for strong typing and autocomplete.
  • Asset Watching: Watches for changes in the assets directory and regenerates index files automatically.
  • Customizable: Configurable options for assets directory, output file, and supported asset extensions.
  • Seamless Integration: Works with Vite's build and development processes.

Installation

Install the plugin via npm:

npm install vite-plugin-generate-typed-assets --save-dev

Usage

Add the plugin to your vite.config.ts file:

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react-swc";
import generateAssetsPlugin from "vite-plugin-generate-typed-assets";
import path from "path";

export default defineConfig({
  plugins: [
    react(),
    generateAssetsPlugin({
      assetsDir: path.resolve(__dirname, "src/assets"),
      outputFile: path.resolve(__dirname, "src/assets/index.ts"),
      assetExtensions: [
        ".png",
        ".jpg",
        ".jpeg",
        ".svg",
        ".gif",
        ".webp",
        ".mp3",
        ".json",
      ],
    }),
  ],
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "src"),
    },
  },
});

Options

The generateAssetsPlugin function accepts an options object with the following properties:

[".png", ".jpg", ".jpeg", ".svg", ".gif", ".webp", ".mp3", ".json"];

Example Options

generateAssetsPlugin({
  assetsDir: path.resolve(__dirname, "src/assets"),
  outputFile: path.resolve(__dirname, "src/assets/index.ts"),
  assetExtensions: [".png", ".jpg", ".jpeg", ".svg"],
});

Example

Assuming your assets directory has the following structure:

src/assets/
├── Images/
│   ├── Header/
│   │   ├── close.svg
│   │   ├── home.svg
│   │   └── more.svg
│   └── Sidebar/
│       ├── logo.png
│       └── menu.svg
└── react.svg

After running the Vite dev server with the plugin configured, the plugin will generate index.ts and index.d.ts files in src/assets/.

Generated index.ts

export const Images = {
  Header: {
    close: new URL("./Images/Header/close.svg", import.meta.url).href,
    home: new URL("./Images/Header/home.svg", import.meta.url).href,
    more: new URL("./Images/Header/more.svg", import.meta.url).href,
  },
  Sidebar: {
    logo: new URL("./Images/Sidebar/logo.png", import.meta.url).href,
    menu: new URL("./Images/Sidebar/menu.svg", import.meta.url).href,
  },
};

export const react = new URL("./react.svg", import.meta.url).href;

Generated index.d.ts:

export const Images: {
  Header: {
    close: string;
    home: string;
    more: string;
  };

  Sidebar: {
    logo: string;
    menu: string;
  };
};

export const react: string;

Integration with React and Vite

Here's how you can use the generated assets in your React components:

Step 1: Import the Assets

import { Images, react } from "@/assets";

Step 2: Use in Components

function Header() {
  return (
    <header>
      <img src={Images.Header.close} alt="Close" />
      <img src={Images.Header.home} alt="Home" />
      <img src={Images.Header.more} alt="More" />
    </header>
  );
}

function Sidebar() {
  return (
    <aside>
      <img src={Images.Sidebar.logo} alt="Logo" />
      <img src={Images.Sidebar.menu} alt="Menu" />
    </aside>
  );
}

function App() {
  return (
    <div>
      <Header />
      <Sidebar />
      <img src={react} alt="React Logo" />
    </div>
  );
}

export default App;

Notes:

Contributing

Development Setup

  1. Clone the Repository:

    git clone https://github.com/glockx/vite-plugin-generate-typed-assets.git
    cd vite-plugin-generate-typed-assets
  2. Install Dependencies:

    npm install
  3. Build the Plugin:

    npm run build

License

This project is licensed under the MIT License - see the LICENSE file for details