-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Description
Feature Request: Polyfill Checker for Library Projects
Feature Request ...
I would like to propose a new babel feature that would greatly simplify polyfill verification for library projects. This enhancement would emit a lightweight JavaScript executable that verifies the running JS engine supports the language constructs "used" in a specific library package.
Background ...
Library developers that use es2015+ constructs, face an issue as to whether they should polyfill their library or not (please refer to the W3C Polyfill Findings specifically Advice for library authors).
Personally, I am in the alternative camp that project libraries should NOT polyfill. Rather, that is the responsibility of the client app. My reasoning for this is:
-
As a library author you have absolutely NO idea what your target env is going to be. One can attempt to apply polyfills to a library (with "non polluting" due diligence), however that is from a perspective of potential problematic targets. If an antiquated target is run, there will still be issues.
-
In reality, we want babel to free us from this burden, and it is becoming better and better at doing this!
-
From an app perspective, applying the needed polyfills has become a fairly simple and straightforward process, with the advent of the "babel-polyfill" import, in conjunction with babel's "babel-preset-env" configured with useBuiltins:"usage".
This alternative tactic requires the library to:
- document needed polyfills, and
- abort early when run in an environment that is feature deficient.
Both of these tasks are tedious when done manually. It occurred to me that babel could assist in both of these tasks.
Feature Requirements ...
The requirements for this enhancement are:
-
Provide a run-time hook that verifies the language constructs found in the library project are available in the executing JavaScript engine. If not, return a summation (nicely formatted for human consumption) which can be used either in warning logs or an error processor.
In essence this requires some persistence as to what language constructs are "in use" for the library in question.
-
Accomplish this in a very lightweight way (both in deployment size and execution speed) so as to have minimal impact on the library package.
Proposed Solution ...
Due to both the "run-time hook" and "persistence" requirements, one solution would be for babel to support an option to emit a machine-generated "checker" file (ultimately included in the library project) that would perform these checks at run-time.
The contents of this "checker" would presumably be similar to what logically is accomplished by "babel-polyfill", except:
-
instead of resolving issues by applying needed polyfills, it merely summarizes and reports violations back to the invoker
-
it would only check the constructs that are in-use by the project library code (similar to how the useBuiltins:"usage" option operates)
As a result, this solution is very light-weight, and can be included in the library project without penalty. In essence it doesn't have to include the needed polyfills - it merely performs the conditional checks.
The babel option could be something like genPolyfillChecker where two items are specified:
-
libName: identifying the project's library name - for human consumption (in the the formatted messages), and -
outputFile: the emitted machine-generated "checker" file
This generated PolyfillChecker outputFile would be included at the start of the library project's expansion, and could also be used as the genesis of any library documentation highlighting the required environment.
Your consideration is greatly appreciated.
Thoughts?