2.2.2 • Published 5 months ago

vite-plugin-module-list v2.2.2

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

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";
2.2.1

5 months ago

2.0.3

6 months ago

2.2.0

5 months ago

2.0.2

6 months ago

2.2.2

5 months ago

2.1.0

6 months ago

2.0.1

6 months ago

2.0.0

6 months ago

1.5.0

7 months ago

1.4.1

8 months ago

1.4.0

8 months ago

1.3.1

8 months ago

1.3.0

8 months ago

1.2.0

8 months ago

1.1.0

8 months ago

1.0.3

8 months ago

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago