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 🙏

© 2026 – Pkg Stats / Ryan Hefner

vite-plugin-singlefile-offline

v0.1.0

Published

Vite plugin that inlines a Vite build into a single self-contained HTML file that runs offline under file:// by transforming ES modules to CommonJS with a shared __require loader.

Readme

vite-plugin-singlefile-offline

Inline a Vite build into one self-contained HTML file that runs offline under file:// by transforming every ES module chunk to CommonJS and evaluating it through a shared __require loader.

MIT Vite

Why

Vite's build output is ES modules loaded via <script type="module">. Under the file:// protocol (and minimal HTTP servers with no SPA fallback), native ESM is blocked and the app won't load. This plugin post-processes the build into a single HTML file with no external dependencies, so it opens by double-clicking — no server, no npx serve.

Unlike vite-plugin-singlefile, which keeps ESM and avoids splitting by configuring Vite's inlining limits, this plugin converts ESM → CommonJS with an AST-based transform and a shared module registry. That's the difference that makes the result work under file:// regardless of how Vite split the chunks (e.g. lazy-loaded routes, dynamic imports, __vitePreload).

Install

npm i -D vite-plugin-singlefile-offline
# peer: vite ^5 || ^6 || ^7 || ^8

Usage

// vite.config.ts
import { defineConfig } from 'vite'
import { singleFileOffline } from 'vite-plugin-singlefile-offline'

export default defineConfig({
  plugins: [singleFileOffline()],
  build: { /* your normal config */ },
})
vite build
# → dist/index.html with everything inlined, runs under file://

Options

| Option | Default | Description | | --- | --- | --- | | deleteOriginalAssets | true | Remove orphaned JS/CSS/font asset files after inlining. | | maxImageSize | 10_000_000 | Max bytes for a remote image to inline as a data URL. | | hashRouterPolyfill | true | Rewrite absolute-path pushState/replaceState URLs to hash fragments (SPAs under file://). | | inlineCSS | true | Inline all CSS into <style> tags. | | inlineImages | true | Download remote images and inline them as data URLs. | | storagePolyfill | true | In-memory localStorage/sessionStorage fallback for file://. | | urlPolyfill | true | new URL(u, base) resolves a missing base against location.href. | | vueRouterHashPatch | false | Patch vue-router's createWebHistory to hash routing. Opt-in — relies on build-time detection of vue-router's minified internals. |

How it works

Pipeline runs in writeBundle (after Vite's HTML post-processing):

  1. AST-transform each JS chunk to CommonJS using acorn + acorn-walk + magic-string. No eval/new Function for module loading; only surgical source rewrites of import/export/import()__require/module.exports.
  2. Register every chunk in a shared __modules registry keyed by its emitted fileName, so cross-chunk imports resolve.
  3. Inline all CSS into <style> tags (CSS code splitting is disabled at config time).
  4. Inject file:// runtime polyfills before the loader script.
  5. Replace the entry <script type="module" src="…"> with one inline loader <script>. Entry requires run on DOMContentLoaded (the inline script lives in <head> and blocks the parser before <div id="app"> exists).
  6. Inline remote images (data URLs) and local fonts.
  7. Delete the now-orphaned asset files.

The module-cache correctness fix

The shared loader caches the module object, not its exports:

function __require(p){
  if(__moduleCache[p]) return __moduleCache[p].exports;  // return cached exports
  var m = { exports: {} };
  __moduleCache[p] = m;                                  // cache the OBJECT
  __modules[p](m, m.exports, __require);
  return m.exports;
}

Caching m.exports before the module body runs captures the original empty {}. When a module reassigns module.exports = y, the cache still points at the empty object and later __require() callers get {} — silently dropping Vue component default exports and rendering slides/components as <!---->. Caching m (and returning __moduleCache[p].exports) fixes this; it's covered by a regression test (__require loader semantics).

Stability & maintenance

The transform rewrites Vite's emitted output, including minified helpers like __vitePreload and __vite__mapDeps. These are Vite-internal and can change between Vite/Rolldown versions. When they do, the plugin may need adjustment. Supported Vite versions are declared in peerDependencies; bump them deliberately.

The vueRouterHashPatch option is the most fragile (it detects minified vue-router internals) and is therefore off by default.

Testing

npm test        # vitest unit tests for the AST transform + loader semantics
npm run typecheck

License

MIT © Jacobinwwey