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-module-list

v2.2.2

Published

Vite plugin that writes a module that dynamically imports modules found in a folder.

Downloads

437

Readme

Vite plugin: Module list

🧶 Vite plugin that generates a module that automatically imports modules of a given folder.

Key features

  • Writes a simple module that imports all modules of a specified folder.
  • Outputs different exports styles (dynamic, static, types, with or without file extension).
  • Handles JavaScript/TypeScript and CSS modules.
  • Automatically updates when files are added, removed, or renamed to the specified folder.
  • Optionally formats the written module using Prettier.
  • Works with Vite 2.x and onward.

Installation

npm install vite-plugin-module-list

Import

import moduleList from "vite-plugin-module-list";

API

Please checkout the API documentation for a full list of available options.

The default exported function returns a regular Vite plugin object. It adds hook that sets a file change listener on the Vite development server.

import { resolve } from "path";
import moduleList from "vite-plugin-module-list";

export default defineConfig({
  plugins: [
    moduleList({
      rootPath: resolve("src/pages"),
      outputPath: resolve("src/pages.ts"),
      includeExtensions: ["tsx"],
      formatOptions: {
        trailingComma: "all",
      },
      mode: {
        language: "ts",
        dynamic: true,
      },
    }),
  ],
});

Example

Dynamic import

With the example configuration above, the pages.ts module will be generated:

src/
+ pages.ts
  pages/
    A.css
    A.tsx
    A.test.tsx
    B.css
    B.tsx
    C.tsx
    README.md

It will contain:

// File automatically generated by `vite-plugin-module-list`
export default [
  { path: "A.tsx", module: () => import("./pages/A") },
  { path: "B.tsx", module: () => import("./pages/B") },
  { path: "C.tsx", module: () => import("./pages/C") },
];

Note that the CSS and test files are excluded. This behavior can be overridden with the include, exclude, and includeExtensions options.

It can then be imported in another module that wraps the dynamic imports with a lazy decorator:

import MODULE_LIST from "./pages";

const PAGE_LIST = MODULE_LIST.map(({ path, module }) => {
  const name = path.slice(0, -4);
  return {
    path: `/${name.toLowerCase()}`,
    name,
    Page: lazy(module),
  };
});

Static import

This plugin can also be used to automatically group TypeScript types or name-exported functions.

For example, a plugin instance with this setup:

moduleList({
  rootPath: resolve("src/types"),
  outputPath: resolve("src/types.ts"),
  formatOptions: {
    trailingComma: "all",
  },
  mode: {
    language: "ts",
    type: true,
  },
});

Would yield this module on this project structure:

src/
+ types.ts
  types/
    A.ts
    B.ts

The types.ts module would contain:

// File automatically generated by `vite-plugin-module-list`
export type { A } from "./types/A";
export type { B } from "./types/B";