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-config-lib

v0.2.0

Published

Vite plugin that configures sane defaults for building libraries.

Downloads

19

Readme

Vite Plugin Config Lib

Vite plugin that configures sane defaults for building libraries.

Getting Started

Install the plugin:

npm install vite-plugin-config-lib --save-dev

Add the plugin to your vite.config.js:

import { defineConfig } from 'vite';
import { lib } from 'vite-plugin-config-lib';

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

As long as you have a common entrypoint (eg. src/index.ts), this should be a working configuration for most libraries. Only defaults are provided, so any configuration by earlier plugins or by the user will be left as-is. For example, if you need to use an entry that is not auto-detected, you can just add it to your configuration normally.

export default defineConfig({
  plugins: [lib()],
  // Override the auto-detected lib entry.
  build: {
    lib: {
      entry: 'src/foo.ts',
    }
  }
});

Common Entrypoints

The plugin will auto detect entrypoints with the following prefixes and extensions:

  • Prefixes: index, bin, main, src/index, src/bin, src/main
  • Extensions: .ts, .tsx, .mts, .cts, .js, .jsx, .mjs, .cjs

Multiple entrypoints can be detected. However, if the same prefix exists with multiple extensions, only one will be detected as an entrypoint. The extensions are listed in order of priority, with the highest priority first. Therefore, if index.ts and index.tsx both exist, then index.ts will be detected.

Config Defaults

  • build.sourcemap: true
  • build.minify: false
  • build.lib.formats: Auto-detected from the type field in the nearest package.json file.
  • build.lib.fileName: '[name]'
  • build.lib.entry: Auto-detected at the Vite root from a list of common entry points.
  • build.rollupOptions.treeshake: false
  • build.rollupOptions.external: Externalizes NodeJS built-ins, native modules, and production dependencies.
  • build.rollupOptions.output.preserveModules: true

Some of the above defaults are generally the only options that make sense for a library. Others are more opinionated, but should probably be correct for most projects.

Source maps are enabled by default. This doesn't hurt anything, and is almost always the correct choice so that consumers can reference them in-place, or use them to generate correct bundle source maps of their own.

Minification is disabled by default. Disabling it avoids some potential issues and provides a better debugging experience for consumers. Minification can be a performance optimization, but that's generally also paired with bundling, which can be done by consumers if necessary.

The lib fileName and Rollup preserveModules defaults cause the build output files to map 1:1 with build inputs. Bundling is not necessary in a library, and can even be harmful if consumers are treeshaking. This also allows for sub-path exports and generating type definition files adjacent to their corresponding source files. Consumers can always choose to bundle if that's the correct choice for their environment or performance needs.

Externalization is generally necessary with preserveModules enabled. A basic externalization default configuration is provided that handles NodeJS built-ins, native modules, and production dependencies. If more advanced externalization support is needed, use the rollup-plugin-node-externals plugin which will override the default solution.

Tree shaking is disabled by default. It's safe to assume that consumers will do their own treeshaking if necessary. Disabling it also avoids issues related to side effects.

Recommended Supplemental Plugins