Hello. I work on a team dedicated to improving user experience
when working with money. We deliver the front-end in npm packages.
At some point, I ran into problems that led me to use a
field exports
inpackage.json
Problem # 1
Packages can export functions with the same name but do different things. Let's take 2 state managers as an example: Reatom and Effector.
They export the function createStore
. If you try to export them from one package (let's call it vendors
), you get the following picture:
// @some/vendors/index.ts
export { createStore } from '@reatom/core';
export { createStore } from 'effector';
There is a conflict of names. This kind of code just won't work.
This can be avoided by as
:
// @some/vendors/index.ts
export { createStore as reatomCreateStore } from '@reatom/core';
export { createStore as effectorCreateStore } from 'effector';
Looks, frankly, lousy. In an amicable way, each re-export must be written through as
to maintain consistency. This makes DX worse for me.
I started looking for a solution that would avoid both the name conflict and the need to write as
. How could it look ... For example, like this:
// @some/vendors/reatom.ts
export { createStore } from 'reatom';
// @some/vendors/effector.ts
export { createStore } from 'effector';
In two different files, we write regular exports and import the required implementation createStore
:
// someFile.ts
import { createStore } from 'vendors/effector';
#2
vendors
, , -, - . , Runtypes.
exports
, :
// someFile.ts
import { createStore, Dictionary, createEvent, Record } from 'vendors';
- . , - :
// someFile.ts
import { createStore, createEvent } from 'vendors/effector';
import { Dictionary, Record } from 'vendors/runtypes';
. .
// someFile.ts
import { createStore, createEvent } from 'vendors/state';
import { Dictionary, Record } from 'vendors/contract';
, exports
package.json
// package.json
"exports": {
"./contract": "./build/contract.js",
"./state": "./build/state.js",
"./package.json": "./package.json"
}
, . TypeScript, .
package.json types
, , . . contract
, state
. ?
typesVersions
package.json
// package.json
"typesVersions": {
"*": {
"contract": ["build/contract.d.ts"],
"state": ["build/state.d.ts"]
}
}
, exports
, d.ts
, .
exports
vendors
. DX.
, Effector :
import { createEvent } from 'effector';
:
import { createEvent } from 'effector/compat';
!