Comments (5)
FWIW, I got my own application to work after I did modifications summarized by this gist. The rabbithole went pretty deep.
I hope this will be useful for anyone stumbling upon this until ESM mode starts working properly for -ts
packages.
Notice that my usecase might be different from yours and there might be additional setup for those exports
fields mentioned in the gist to make things work.
from io-ts.
Here are the scripts that I use to produce the file layout described in suggested solution part (entrypoint is build:ci
script):
{
"scripts": {
"build:ci": "yarn run clear-build-artifacts && yarn run compile-d-ts-files && yarn run tsc --outDir ./dist-esm && yarn run tsc --module CommonJS --outDir ./dist-cjs && yarn run format-output-files && yarn run fix-subpath-exports",
"clear-build-artifacts": "rm -rf dist dist-ts dist-cjs dist-esm build",
"compile-d-ts-files": "yarn run tsc --removeComments false --emitDeclarationOnly --declaration --declarationDir ./dist-ts && yarn run copy-d-ts-files && yarn run tsc:plain --project tsconfig.out.json",
"copy-d-ts-files": "find ./src -mindepth 1 -maxdepth 1 -name '*.d.ts' -exec cp {} ./dist-ts +",
"fix-subpath-exports": "mkdir <SUBPATH> && cp package.json.<SUBPATH> <SUBPATH>/package.json",
"format-output-files": "find dist-ts -name '*.ts' -type f -exec sh -c \"echo '/* eslint-disable */\n/* eslint-enable prettier/prettier */'\"' | cat - $1 > $1.tmp && mv $1.tmp $1' -- {} \\; && eslint --no-eslintrc --config '.eslintrc.output.ts.cjs' --fix './dist-ts/**/*.ts' && eslint --no-eslintrc --config '.eslintrc.output.cjs' --fix 'dist-cjs/*js' 'dist-esm/*js'",
"lint": "eslint ./src --ext .ts,.tsx",
"tsc": "tsc --project tsconfig.build.json",
"tsc:plain": "tsc",
}
}
Notice that when dealing with large number of supported subpath exports, like all -ts
packages seem to do, one probably wants fix-subpath-exports
script to be more generic. One option would be to read subpaths from file, and then iterate each line with for loop, and create directory + copy/generate the stub package.json file.
Here are the TSConfig files used by script:
// tsconfig.out.json
{
// TS config file for formatting the resulting .d.ts files (<project name>/dist-ts/**/*.d.ts) that end up in NPM package for typing information.
"extends": "./tsconfig.json",
"compilerOptions": {
"rootDir": "./dist-ts",
"noEmit": true
},
"include": [
"./dist-ts/**/*"
]
}
// tsconfig.build.json
{
// TS config file to use to compile <project name>/src/**/*.ts files in CI.
"extends": "./tsconfig.json",
"compilerOptions": {
// We don't want dangling // eslint-disable-xyz comments, as that will cause errors during formatting output .[m]js files.
"removeComments": true
},
}
And then the normal tsconfig.json
file as your usual business. The motivation for these is that we would still have nicely auto-formatted (by ESLint, I personally use the Prettier plugin) .d.ts
and .js
files, instead of barely readable garbage produced by TS compiler and such.
P.S. No need to take this whole process into use. Just pick the parts which you think suits best to whatever approach you are using to build & publish packages. To be fair, I am happy with anything that makes subpath imports work in ESM mode. :)
from io-ts.
@gcanti I took a look at the package.json of this project, and I think I could contribute a PR related to this issue. Would you like me to do that, or are you handling this via some internal ideas/pipelines?
from io-ts.
I'm working on a new iteration of io-ts
https://github.com/fp-ts/schema that will be soon included in the @effect
org.
As soon as the new packages will be stable I will backport what's possible to the fp-ts
ecosystem.
After that I will consider to release major versions of many packages, io-ts
included, which will support esm.
from io-ts.
Oh, I see, really interesting to hear about the new iteration, as well as it being included to @effect
umbrella! Will check it out for sure. 👍
from io-ts.
Related Issues (20)
- Behavior changes and types are incorrect based on `intersection` array order
- Intersection with record whose keys are a custom type HOT 1
- io-ts recursion use issue HOT 4
- Clarification question - how to work with the type of codecs themselves? HOT 1
- Surprising acceptance of various inputs HOT 1
- ReadonlyNonEmptyArray can't be used in a mapped type HOT 1
- How to generate documentation for types generated with `t.TypeOf` HOT 1
- Difficulties with generic serialisable type HOT 2
- [Question] Typing a generic mapped union HOT 1
- Inference error for `toString` property in intersection types with TypeScript 4.9.5 HOT 1
- `t.TypeOf<keyof<o>>` should return a string union type, not a numeric union type.
- PSA: TS 5.1 can break using this library HOT 1
- Intersection of Function with Object doesn't validate correctly
- t.Int: use Number.isSafeInteger instead of Number.isInteger
- non-enumerable records with extra keys do not pass `io-ts.record.is`, contrary to TypeScript types HOT 12
- partially enumerable record missing enumerable keys passes `record.is`, contrary to TypeScript types
- Records are stripped when encoding with t.record(NumberFromString, xxx) HOT 2
- Question: Convert a form to a type with different structure
- Incorrect behaviour when used with browser geolocation coordinates object
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from io-ts.