jaredpalmer / tsdx

Zero-config CLI for TypeScript package development
https://tsdx.io
MIT License
11.2k stars 505 forks source link

Polyfills are not generated from .browserslistrc queries #1132

Closed ekb1zh closed 2 years ago

ekb1zh commented 2 years ago

My algorithm:

npx tsdx create mylib # run create new project and select "basic" option
cd mylib # go inside project
npm i # install packages
touch .browserslistrc # create file .browserslistrc
echo "dead" >> .browserslistrc # append query "dead" into .browserslistrc
> src/index.ts # clear src/index.ts
echo "export default () => { console.log(Object.fromEntries([[\"a\", 1]]), Promise.resolve(0)); }" >> src/index.ts # append code into src/index.ts
rm -r test # remove test folder with old code
npm run build # build project
cat dist/mylib.cjs.production.min.js # see result

Result - polyfills are absent. But they are need for execute code in dead browsers ))

These questions have been discussed in these threads .browserslistrc https://github.com/jaredpalmer/tsdx/issues/951, Object.fromEntries https://github.com/jaredpalmer/tsdx/issues/968, tslib https://github.com/jaredpalmer/tsdx/issues/412, but I didn't find an answer to the question - if targeting .browserslist file to the old browsers, should TSDX add polyfills?

Note: If an incorrect request is entered into the .browserslist file, then an error will appear during the build, which means this file is included in the build, but why are polyfills not generated.

agilgur5 commented 2 years ago
  • If not, so how would such code work in older browsers?

Per https://github.com/jaredpalmer/tsdx/issues/968#issuecomment-791817207 , polyfills are currently out-of-scope for TSDX. They were planned per that answer but were never a part of TSDX outside of regenerator/async/await. That answer also mentions how polyfills in libraries are still a topic of discussion in the JS community. Adding the .browserslistrc as such will effectively tell @babel/preset-env to transform your code to as old as possible, but it won't insert polyfills for behavior that can't be transpiled down to an earlier environment.

The "suggested solution" in https://github.com/jaredpalmer/tsdx/issues/968#issuecomment-791817207 is still the suggested solution in this case, that would make corejs polyfills added. usage-pure means it won't pollute the consumer environment and many consumers already have corejs installed anyway, so that is my suggestion when it comes to the topic of polyfills in libraries. (Side note: babel-polyfills is also now a very stable collection of packages and is actually used under-the-hood of preset-env nowadays. I've also contributed a good bit to it and recently migrated it to TS)