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-entrypoints

v1.0.2

Published

Vite plugin to generate additional entry points for files matching glob patterns

Downloads

6

Readme

vite-plugin-entrypoints

GitHub Release Workflow Status npm Version npm Downloads License GitHub Repo Stars

Vite plugin to generate additional entry points for files matching glob patterns.

Purpose

Modern JavaScript libraries should be built with tree-shaking and code splitting in mind. It allows applications to eliminate unused code, thus reducing the amount of JavaScript code sent to clients.

For a library which exposes a single main module it is important to split code exported from that module into separate chunks.

An example for tree-shakable library looks like this:

// main.js (assuming this is the main module in package.json)
export { a } from './module-a';
export { b } from './module-b';

// module-a.js
export const a = 'a';

// module-b.js
export const b = 'b';

When building a library using Vite library mode the built distribution is usually one single JavaScript file. In order to create a tree-shakable output we have to apply one of the code splitting techniques supported by rollup.js.

One common practice is to define multiple entry points to force rollup.js to split code into chunks. In the example above we would not only define main.js as our single entry point but also module-a.js and module-b.js, even if their bundle output is never meant to be used directly by library consumers.

In a Vite configuration file it would look like this:

// vite.config.js
import { resolve } from 'path';
import { defineConfig } from 'vite';

export default defineConfig({
  build: {
    lib: {
      entry: {
        main: resolve(__dirname, 'main.js'),
        'module-a': resolve(__dirname, 'module-a.js'),
        'module-b': resolve(__dirname, 'module-b.js'),
      },
      name: 'MyLib',
      fileName: 'my-lib',
    },
  },
});

But what if we have a huge library with lots of components or modules exported from the main entry point? It becomes annoying to list every relevant entry file into our Vite configuration file.

This plugin solves our problem by generating entry points for all files matching given glob patterns.

Installation

# NPM
npm install vite-plugin-entrypoints --save-dev

# Yarn
yarn add vite-plugin-entrypoints --dev

# PNPM
pnpm add vite-plugin-entrypoints --save-dev

Usage

Import the plugin in your Vite configuration file and add it to the plugins array.

import { resolve } from 'node:path';
import { defineConfig } from 'vite';
import entrypoints from 'vite-plugin-entrypoints';

export default defineConfig({
  plugins: [
    entrypoints({
      entryFilePatterns: ['modules/**/*.js'],
      // more optional settings
    }),
  ],
  build: {
    lib: {
      entry: { main: resolve(__dirname, 'main.js') },
    },
  },
});

Important: The value of the entry property in your build.lib options must be an object! Usage of just a string or an array is not supported yet.

Configuration

| Option | Type | Required | Default | Description | | ------------------- | ---------- | -------- | ---------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | entryFilePatterns | string[] | yes | - | Glob patterns to match entry files in base directory. A separate entry chunk is created for each matching file.Example: ['modules/**/*.js'] | | baseDir | string | no | process.cwd() | Base directory to resolve entry file patterns from. This path is stripped from entry file names.Example: 'src' | | entryNameMapper | function | no | Matched file name with extension removed | Mapping function to generate entry names for entry files. Receives matched file name as argument and must return new file name.Example: name => 'prefix/' + name |

Contributing

  1. Fork this Git repository.
  2. Create a new feature/bugfix branch and make your changes.
  3. Add a changeset to your branch by running pnpm changeset and pick a suitable version increment for your change according to semantic versioning. Enter a brief description of your change.
  4. Create a pull request targeting our main branch.

License

MIT