8

I’m building a TypeScript package to be published on NPM. I’ll be consuming this package in future web development projects likely using Vite. When I build a future website with this module, does it matter if it’s already bundled? Won’t Rollup (used by Vite to build the website) bundle the code regardless of whether the code on NPM is bundled (like in a lib.esm.js file)? Why not just use TSC (TypeScript Compiler) to compile TS to JS for NPM and then let the consuming project (whether Rollup or Webpack or Parcel) bundle it optimizing for the browser?

What am I missing that other NPM authors know?

Note, I’m authoring this package as strictly an ESM Module (type: module) so I’m not worrying about CJS.

Ben Stickley
  • 1,551
  • 1
  • 16
  • 22
  • Socratic: Why even compile to JS if the consumer is using TypeScript? (e.g. Deno) – jsejcksn Dec 31 '21 at 19:26
  • @jsejcksn, great point. For my use case, I'm bundling this code for the browser so JS is required. – Ben Stickley Jan 01 '22 at 14:57
  • @jsejcksn I've been down this path. It was a nightmare. Neither TypeScript nor IDEs cope too well with TypeScript (except for type definitions) in `node_modules`. – Steve Jul 27 '22 at 10:52

1 Answers1

2

You’re right. You don’t need to bundle it. Just transpile each file individually from the source directory to the output directory and point your package.json’s main to the transpiled entry point. Use Babel directly.

An example of this is react-markdown.

Steve
  • 8,066
  • 11
  • 70
  • 112