robatwilliams / es-compat

Check JavaScript code compatibility with target runtime environments
MIT License
64 stars 13 forks source link

Support polyfilling/ignoring syntax features #105

Closed tantian1498 closed 2 months ago

tantian1498 commented 3 months ago

Does this plugin only support the following polyfills?

polyfills: { type: 'array', items: { type: 'string', enum: [ // This list is hard-coded so it can serve as documentation 'globalThis', '{Array,String,TypedArray}.prototype.at', '{Array,TypedArray}.prototype.findLast', '{Array,TypedArray}.prototype.toReversed', '{Array,TypedArray}.prototype.toSorted', '{Array,TypedArray}.prototype.with', 'Array.prototype.flat', 'Array.prototype.flatMap', 'Array.prototype.includes', 'Array.prototype.toSpliced', 'Error.cause', 'Object.entries', 'Object.fromEntries', 'Object.getOwnPropertyDescriptors', 'Object.hasOwn', 'Object.values', 'Promise.prototype.allSettled', 'Promise.prototype.any', 'Promise.prototype.finally', 'String.prototype.matchAll', 'String.prototype.padEnd', 'String.prototype.padStart', 'String.prototype.replaceAll', 'String.prototype.trimEnd', 'String.prototype.trimLeft', 'String.prototype.trimRight', 'String.prototype.trimStart', ], }, },

What should I do if I want to add the async feature without modifying the browserslist?

robatwilliams commented 3 months ago

Async/await is a syntax feature so cannot be polyfilled.

You will have to transpile your code which will turn async/await into promises.

The discussion on #95 may be of interest.

tantian1498 commented 3 months ago

Async/await is a syntax feature so cannot be polyfilled.

You will have to transpile your code which will turn async/await into promises.

The discussion on #95 may be of interest.

thx!yes i have transpile my code,so I don't need a real polyfill,this means I need to ignore some errors, such as async.

I've looked at https://github.com/robatwilliams/es-compat/issues/95 and it seems like there is no ignore functionality in your plugin? So I can only check code compatibility after packaging all the code?

robatwilliams commented 3 months ago

No, no ignore feature.

So yes, in your use case you would check compatibility of the final bundle rather than your source code. If that finds some unsupported syntax, then your transpiler config isn't appropriate. If it finds some unsupported APIs, then you need to add polyfills.

I hadn't thought that it would be much of a problem that you can't check until after build, as the resolutions to unsupported syntax and APIs are quite straightforward. And I see this plugin as a guardrail that saves you maybe once a month, rather than something you use to help guide your coding around lots of compatibility issues every day.