I like how React can be imported as a default object like import React from 'react'
and imported in a destructured way, like import { useEffect } from 'react'
or even import * as Whatever from 'react'
if you're feeling cheeky. I'm trying to build my own typescript library for npm that can be imported in all 3 of these ways (in addition to the usual require
syntax, and I've spent about 15 hours on it so far without being able to get it to work (frustrating).
Right now I'm using rollup to build lib/cjs and lib/esm, to support both CommonJS and Modules. I'm also having typescript-compiler (tsc) build type files and put them inside /lib/types.
But what I'm REALLY stuck on is how to export my files like how React does it. So far I'm using an index.ts "barrel file" at the root of my library, which is something like:
export * from './filename1'
export * from './filename2'
...
But this obviously doesn't provide a default export for my library. Is there some sort of index.d.ts module declaration magic I need to do? Any tips / tutorials on building a typescript library would be helpful. I've been able to get both default imports and named imports to work for my library, but not both at the same time.