How to make universal JavaScript / TypeScript library

I like to write small and simple TypeScript libraries which I can share across my projects and I want to share them with the wider opensource community. But one problem is holding me back from it:

Often happens to me that I publish an NPM module that is working in one context but makes strange errors in others. For example, it works in create-react-app but it does not in ts-node. Or it works everywhere but it makes problems in build. Despite it is just a pure function that does not use something context-dependent like window.

How to prevent these types of problems?

Is there some „magic“ method to make it work (and test) it in every possible context and combination like browser, old browser without ES6, typescript project, create-react-app project, node, ts-node, ES module, imported with require, imported with import, partially imported…?

I am searching for some unified and simple way how to write code not spend most of the time with configuration and updates of (ts, webpack) configs without a ton of boilerplate code. For example, some library that makes builds for every platform for me and autoconfigure it into package.json? Some create-react-app but for libraries.


Pavol Hejný

Milovník technologií, Startuper ve vzdělávání, autor virtuální tabule, full stack vývojář a spoluorganizátor mnoha akcí např. CzechFutureTech.

3 komentáře u „How to make universal JavaScript / TypeScript library“

Napsat komentář

Vaše e-mailová adresa nebude zveřejněna. Vyžadované informace jsou označeny *