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-entry-shaking-plus

v0.2.3

Published

Mimic tree-shaking behaviour when importing code from an entry file in development mode.

Downloads

3

Readme

Warning This plugin is experimental, bugs might be expected and some edge cases might not be covered.

Note The main execution logic of this plugin only applies to development mode because it addresses an issue which is specific to development mode.

Install

Open in Codeflow

# Using npm
npm i -D vite vite-plugin-entry-shaking
# Using Yarn
yarn add -D vite vite-plugin-entry-shaking
# Using pnpm
pnpm add -D vite vite-plugin-entry-shaking

Usage

Setup the plugin in your Vite configuration file.

import { resolve } from 'path';
import { defineConfig } from 'vite';
import EntryShakingPlugin from 'vite-plugin-entry-shaking';

export default defineConfig({
  plugins: [
    EntryShakingPlugin({
      targets: [resolve(__dirname, 'src/entry-a')],
    }),
  ],
});

Plugin options

Examples

Examples illustrating usage and benefits can be found here. Feel free to fork and play around. For instance, you can toggle the plugin on and off their respective vite config file while serving in development mode and see how it affects the amount of requests made by your browser.

Motivation

The problem this plugin tries to address is well described by this Vite's github issue, so we'll stick to its author's example. Suppose your codebase contains an index file which is basically used as an entry point to dispatch code imported from other files. This is a rather common pattern which may be handy and avoid writing a lot of individual import statements:

// shared/index.ts
export { a } from './a';
export { b } from './b';
export { c } from './c';

Let's pretend you have a module which imports c from that entry point:

// module.ts
import { c } from './shared';

In development mode, when Vite serves the module.ts file, the browser loads the shared/index.ts file which initiates requests for all of the three a, b and c modules it relies on. The thing is, we actually only needed the c module, this results in both a and b requests being unnecessary!

As your projet and entry points grow, you end up paying the price for the ever-increasing amount of unnecessary requests and HMR updates, which consumes time and resources. Well, that escalated quickly. Let's try to work around this…

The idea

The main idea is to rewrite imports from a target entry point and replace them by individual imports of the actual module. Back with the above example:

// module.ts
import { c } from './shared';

// gets rewritten as…
import { c } from './shared/c';

This way, the shared/index.ts file is not loaded by the browser anymore and no additional requests are initiated.

How it works

First of all, the plugin reads all of the target entry files listed in the plugin's targets option. For each entry file :

  • It uses es-module-lexer to get a list of imports and exports.
  • It stores named exports that are re-exports of code imported from another module and the path they resolve to. It also stores whether this re-exported code is the default or a named export of its origin. This lets us correctly rewrite the import using the adequate statement.
  • It also tracks a mutated version of the entry file where these stored named exports are removed. This is required because we might still import code which is actually defined within the entry file, rather than exported from another module. To make these work, we'll still need to serve this mutated version of the entry file so that this kind of code can be reached.

Whenever Vite serves a file which includes an import which resolved to one of the targets, this file is transformed to rewrite the relevant import statements. It extracts the list of entities imported from that entry file, and for each imported entity :

  • it removes the import statement of that entry file.
  • if it has a matching stored named export, it adds a direct import to the relevant module's absolute path, taking into consideration whether it imports a named export or a default export.
  • If it has no matching stored named export, it is some code which is actually defined within the entry file. These are batched and eventually add a recomposed import of the target.

When encountering the above latest case, we have the browser still loading the shared/index.ts, which could therefore trigger unnecessary requests, as described earlier. To prevent this kind of scenario, any import of an entry point is caught and is forced to serve its mutated version we stored while parsing the entry point file. This ensures the entry point only imports what it needs to make the code it explicitly defines work.

Limitations

  • See es-module-lexer's own limitations.
  • The following syntaxes are not handled:
    • dynamic imports
    • import json from './json.json' assert { type: 'json' }
    • import * as xxx from '…'
    • export * from '…'

Note This does not mean you should expect errors using these. Instead, it just means the content they intend to import won't be tree-shaken by the plugin.