I've been enjoying exploring the different ways React can be imported, from using import React from 'react'
to importing specific components with destructuring like
import { useEffect } from 'react'
, or even cheekily doing import * as Whatever from 'react'
. Now I'm on a mission to create my own TypeScript library for npm that can be imported in all these ways, along with the traditional require
syntax. I've put in around 15 hours so far, but haven't quite cracked it (frustrating).
Currently, I'm utilizing rollup to build lib/cjs and lib/esm to support both CommonJS and Modules. Additionally, typescript-compiler (tsc) is generating type files stored inside /lib/types.
However, my biggest roadblock is figuring out how to export my files similarly to how React does it. At the moment, I'm using an index.ts "barrel file" at the root of my library, which looks something like this:
export * from './filename1'
export * from './filename2'
...
But this setup doesn't include a default export for my library. Do I need to perform some sort of magic with an index.d.ts module declaration? Any advice or tutorials on creating a TypeScript library would be greatly appreciated. While I've managed to make either default imports or named imports work for my library individually, I'm struggling to get them both working together.