Misc scribbles

Vite library mode bundles your library's dependencies (which I don't think is good)

2025-02-23

my cats

Vite is a very popular "build tool" for making all sorts of javascript apps. It is most commonly used for making single page apps, but it also has a feature called "library mode" which helps you publish things that you have written on NPM. I imagine it is much less commonly used than vite itself, but I wanted to comment on it because it has a behavior I'm not a big fan of...

#Vite library mode completely minifies+bundles all your dependencies

For example, if your library that is using vite library mode has

import leftPad from 'left-pad'

const str = leftPad('foobar', 6)

then it is turned into this in the dist folder by vite library mode

function d(t) {
  return t && t.__esModule && Object.prototype.hasOwnProperty.call(t, 'default')
    ? t.default
    : t
}
var f, o
function n() {
  if (o) return f
  ;(o = 1), (f = i)
  var t = [
    '',
    ' ',
    '  ',
    '   ',
    '    ',
    '     ',
    '      ',
    '       ',
    '        ',
    '         ',
  ]
  function i(a, r, e) {
    if (((a = a + ''), (r = r - a.length), r <= 0)) return a
    if ((!e && e !== 0 && (e = ' '), (e = e + ''), e === ' ' && r < 10))
      return t[r] + a
    for (var u = ''; r & 1 && (u += e), (r >>= 1), r; ) e += e
    return u + a
  }
  return f
}
var P = n()
const l = /* @__PURE__ */ d(P)
l('foobar', 6)

yes, that's the left-pad source code, inlined into your library's distribution

I personally believe this is bad. It could have just put this into the dist folder:

import leftPad from 'left-pad'

const str = leftPad('foobar', 6)

then, any consumer of your library can trace the import from your library to left-pad, which is responsibly declared in your package.json "dependencies", and everything is fine and normal.

But this is not how vite library mode does it, so I must construct some strawmen to wonder why anyone would do this

#Why someone might bundle their dependencies they upload to NPM

#Why someone might minify the things they upload to NPM

(you can likely toggle minification in vite library mode settings, but it minifies by default)

#Why I think it is bad to bundle your dependencies for artifacts that you upload to NPM

#Why I think it is bad to minify the libraries that you upload to NPM

#Conclusions

I am a dependencies apologist. I like libraries, they do good things for me, and I enjoy getting regular bugfixes and improvements. I like good library hygiene

I have previously written about why you may NOT need a bundler for your library here https://cmdcolin.github.io/posts/2022-05-27-youmaynotneedabundler.

I also wrote a small article about a bundler-less way to make a pure-ESM typescript packages here https://cmdcolin.github.io/posts/2025-01-12-pureesm

#Footnote 1

I am not trying to pick on vite here. They do great work for the community. I bet many other quickstart NPM library creation kits have this same trouble (e.g. from my list https://cmdcolin.github.io/posts/2022-05-27-youmaynotneedabundler)

For example, the first one on that list, microbundle, has a full wiki page to justify doing things this way, but has even more complex subtle behavior https://github.com/developit/microbundle/wiki/How-Microbundle-decides-which-dependencies-to-bundle