Skip to content

Commit

Permalink
migrate to @mrhenry/polyfill-library (#1772)
Browse files Browse the repository at this point in the history
* migrate to mrhenry/polyfill-library

* upgrade to polyfill-library 5.0.0
  • Loading branch information
romainmenke committed Feb 28, 2024
1 parent 6630007 commit fada6c7
Show file tree
Hide file tree
Showing 54 changed files with 482 additions and 316 deletions.
387 changes: 168 additions & 219 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"prettier": {
"useTabs": true
},
"version": "1.2.1",
"version": "1.2.2",
"volta": {
"node": "20.2.0",
"npm": "9.6.7"
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-plugin-core-web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

> Babel plugin that polyfills browser features.
>
> Bringing [polyfill.io](https://polyfill.io/v3/) to your babel flow.
> Bringing [@mrhenry/polyfill-library](https://github.com/mrhenry/polyfill-library) to your babel flow.
[![version](https://img.shields.io/npm/v/@mrhenry/babel-plugin-core-web.svg)](https://www.npmjs.com/package/@mrhenry/babel-plugin-core-web) [![Build](https://github.com/mrhenry/core-web/workflows/Build/badge.svg)](https://github.com/mrhenry/core-web/actions/workflows/build-and-test.yml) [![BrowserStack Status](https://automate.browserstack.com/badge.svg?badge_key=aG8ra0x5L2xhbEhIOHh2U003NWI1T3pYVTVUZDFJNEU1cHJLU0JPSFhWZz0tLVJwMm13ekViMGs3VFJQeTR6V3lpeXc9PQ==--cdb365a51e633a467329a4b6a475c736b7578a09)](https://automate.browserstack.com/public-build/aG8ra0x5L2xhbEhIOHh2U003NWI1T3pYVTVUZDFJNEU1cHJLU0JPSFhWZz0tLVJwMm13ekViMGs3VFJQeTR6V3lpeXc9PQ==--cdb365a51e633a467329a4b6a475c736b7578a09) [![CodeQL](https://github.com/mrhenry/core-web/workflows/CodeQL/badge.svg)](https://github.com/mrhenry/core-web/actions/workflows/codeql-analysis.yml)


## why

Maintaining a correct but minimal polyfill bundle from [polyfill.io](https://polyfill.io/v3/) is a manual process and prone to errors. Babel however knows which features you actually used and the environment you are targeting. Bringing the two together works for us, we hope it does for you too.
Maintaining a correct but minimal polyfill bundle from [@mrhenry/polyfill-library](https://github.com/mrhenry/polyfill-library) is a manual process and prone to errors. Babel however knows which features you actually used and the environment you are targeting. Bringing the two together works for us, we hope it does for you too.

## install

Expand Down
4 changes: 2 additions & 2 deletions packages/babel-plugin-core-web/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mrhenry/babel-plugin-core-web",
"version": "1.2.1",
"version": "1.2.2",
"description": "browser feature polyfills as a babel plugin",
"main": "lib/index.js",
"author": "Simon Menke <[email protected]>",
Expand Down Expand Up @@ -33,7 +33,7 @@
"dependencies": {
"@babel/helper-module-imports": "^7.15.4",
"@babel/types": "^7.23.5",
"@mrhenry/core-web": "^1.2.1",
"@mrhenry/core-web": "^1.2.2",
"fast-deep-equal": "^3.1.3"
},
"bugs": {
Expand Down
2 changes: 1 addition & 1 deletion packages/core-web-example/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mrhenry/core-web-example",
"version": "1.2.1",
"version": "1.2.2",
"private": true,
"scripts": {
"build": "babel src -d lib"
Expand Down
4 changes: 2 additions & 2 deletions packages/core-web-generator/lib/generate.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/core-web-generator/lib/generate.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions packages/core-web-generator/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mrhenry/core-web-generator",
"version": "1.2.1",
"version": "1.2.2",
"private": true,
"scripts": {
"clean": "rm -rf ./lib/",
Expand All @@ -13,11 +13,11 @@
},
"dependencies": {
"@mdn/browser-compat-data": "^5.5.10",
"@mrhenry/polyfill-library": "^5.0.0",
"@webcomponents/custom-elements": "^1.5.0",
"@webcomponents/shadycss": "^1.11.0",
"@webcomponents/shadydom": "^1.9.0",
"@webcomponents/template": "^1.5.0",
"polyfill-library": "^4.0.0"
"@webcomponents/template": "^1.5.0"
},
"devDependencies": {
"@babel/parser": "*",
Expand Down
4 changes: 2 additions & 2 deletions packages/core-web-generator/src/generate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from 'fs';
import * as polyfillLibrary from "polyfill-library";
import * as sources from "polyfill-library/lib/sources.js";
import * as polyfillLibrary from "@mrhenry/polyfill-library";
import * as sources from "@mrhenry/polyfill-library/lib/sources.js";
import * as path from "path";

const coreWebDir = path.resolve(__dirname, "../../core-web");
Expand Down
4 changes: 2 additions & 2 deletions packages/core-web-generator/src/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
declare module 'polyfill-library';
declare module 'polyfill-library/lib/sources.js';
declare module '@mrhenry/polyfill-library';
declare module '@mrhenry/polyfill-library/lib/sources.js';
2 changes: 1 addition & 1 deletion packages/core-web-tests/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mrhenry/core-web-tests",
"version": "1.2.1",
"version": "1.2.2",
"private": true,
"scripts": {
"build": "webpack",
Expand Down
2 changes: 1 addition & 1 deletion packages/core-web/__mapping.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var AddEntriesFromIterable = (function() {
IteratorClose(iteratorRecord, error);
throw error;
}
// Polyfill.io fallback for non-array-like strings which exist in some ES3 user-agents
// fallback for non-array-like strings which exist in some ES3 user-agents
nextItem =
(Type(nextItem) === "string" || nextItem instanceof String) &&
toString.call(nextItem) == "[object String]"
Expand Down
32 changes: 32 additions & 0 deletions packages/core-web/helpers/_ESAbstract.AddValueToKeyedGroup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import SameValue from "@mrhenry/core-web/helpers/_ESAbstract.SameValue";
import SameValueNonNumber from "@mrhenry/core-web/helpers/_ESAbstract.SameValueNonNumber";
import Type from "@mrhenry/core-web/helpers/_ESAbstract.Type";

// _ESAbstract.AddValueToKeyedGroup
/* global SameValue */

// 7.3.35 AddValueToKeyedGroup ( groups, key, value )
// eslint-disable-next-line no-unused-vars
function AddValueToKeyedGroup(groups, key, value) {
// 1. For each Record { [[Key]], [[Elements]] } g of groups, do
for (var i = 0; i < groups.length; i++) {
var g = groups[i];
// a. If SameValue(g.[[Key]], key) is true, then
if (SameValue(g["[[Key]]"], key)) {
// i. Assert: Exactly one element of groups meets this criterion.
// ii. Append value to g.[[Elements]].
g["[[Elements]]"].push(value);
// iii. Return unused.
return;
}
}
// 2. Let group be the Record { [[Key]]: key, [[Elements]]: « value » }.
var group = {
"[[Key]]": key,
"[[Elements]]": [value]
};
// 3. Append group to groups.
groups.push(group);
// 4. Return unused.
}
export default AddValueToKeyedGroup;
18 changes: 9 additions & 9 deletions packages/core-web/helpers/_ESAbstract.AdvanceStringIndex.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import CodePointAt from "@mrhenry/core-web/helpers/_ESAbstract.CodePointAt";
import UTF16SurrogatePairToCodePoint from "@mrhenry/core-web/helpers/_ESAbstract.UTF16SurrogatePairToCodePoint";

// _ESAbstract.AdvanceStringIndex
/* global */
/* global CodePointAt */

// 22.2.5.2.3 AdvanceStringIndex ( S, index, unicode )
function AdvanceStringIndex(S, index, unicode) { // eslint-disable-line no-unused-vars
// eslint-disable-next-line no-unused-vars
function AdvanceStringIndex(S, index, unicode) {
// 1. Assert: index ≤ 253 - 1.
if (index > Number.MAX_SAFE_INTEGER) {
throw new TypeError('Assertion failed: `index` must be <= 2**53');
}
// 2. If unicode is false, return index + 1.
if (unicode === false) {
return index + 1;
}
// 3. Let length be the number of code units in S.
// 3. Let length be the length of S.
var length = S.length;
// 4. If index + 1 ≥ length, return index + 1.
if (index + 1 >= length) {
return index + 1;
}
// 5. Let cp be ! CodePointAt(S, index).
var cp = S.codePointAt(index);
// 5. Let cp be CodePointAt(S, index).
var cp = CodePointAt(S, index);
// 6. Return index + cp.[[CodeUnitCount]].
return index + cp.length;
return index + cp["[[CodeUnitCount]]"];
}
export default AdvanceStringIndex;
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function ArraySpeciesCreate(originalArray, length) { // eslint-disable-line no-u
// 5. Let C be ? Get(originalArray, "constructor").
var C = Get(originalArray, 'constructor');

// Polyfill.io - We skip this section as not sure how to make a cross-realm normal Array, a same-realm Array.
// We skip this section as not sure how to make a cross-realm normal Array, a same-realm Array.
// 6. If IsConstructor(C) is true, then
// if (IsConstructor(C)) {
// a. Let thisRealm be the current Realm Record.
Expand Down
60 changes: 60 additions & 0 deletions packages/core-web/helpers/_ESAbstract.CodePointAt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import UTF16SurrogatePairToCodePoint from "@mrhenry/core-web/helpers/_ESAbstract.UTF16SurrogatePairToCodePoint";

// _ESAbstract.CodePointAt
/* global UTF16SurrogatePairToCodePoint */
// 11.1.4 Static Semantics: CodePointAt ( string, position )
// eslint-disable-next-line no-unused-vars
function CodePointAt(string, position) {
// 1. Let size be the length of string.
var size = string.length;
// 2. Assert: position ≥ 0 and position < size.
// 3. Let first be the code unit at index position within string.
var first = String.prototype.charCodeAt.call(string, position);
// 4. Let cp be the code point whose numeric value is the numeric value of first.
var cp = first;

var firstIsLeading = first >= 0xd800 && first <= 0xdbff;
var firstIsTrailing = first >= 0xdc00 && first <= 0xdfff;

// 5. If first is neither a leading surrogate nor a trailing surrogate, then
if (!firstIsLeading && !firstIsTrailing) {
// a. Return the Record { [[CodePoint]]: cp, [[CodeUnitCount]]: 1, [[IsUnpairedSurrogate]]: false }.
return {
"[[CodePoint]]": cp,
"[[CodeUnitCount]]": 1,
"[[IsUnpairedSurrogate]]": false
};
}
// 6. If first is a trailing surrogate or position + 1 = size, then
if (firstIsTrailing || position + 1 === size) {
// a. Return the Record { [[CodePoint]]: cp, [[CodeUnitCount]]: 1, [[IsUnpairedSurrogate]]: true }.
return {
"[[CodePoint]]": cp,
"[[CodeUnitCount]]": 1,
"[[IsUnpairedSurrogate]]": true
};
}
// 7. Let second be the code unit at index position + 1 within string.
var second = String.prototype.charCodeAt.call(string, position + 1);

var secondIsTrailing = second >= 0xdc00 && second <= 0xdfff;

// 8. If second is not a trailing surrogate, then
if (!secondIsTrailing) {
// a. Return the Record { [[CodePoint]]: cp, [[CodeUnitCount]]: 1, [[IsUnpairedSurrogate]]: true }.
return {
"[[CodePoint]]": cp,
"[[CodeUnitCount]]": 1,
"[[IsUnpairedSurrogate]]": true
};
}
// 9. Set cp to UTF16SurrogatePairToCodePoint(first, second).
cp = UTF16SurrogatePairToCodePoint(first, second);
// 10. Return the Record { [[CodePoint]]: cp, [[CodeUnitCount]]: 2, [[IsUnpairedSurrogate]]: false }.
return {
"[[CodePoint]]": cp,
"[[CodeUnitCount]]": 2,
"[[IsUnpairedSurrogate]]": false
};
}
export default CodePointAt;
4 changes: 2 additions & 2 deletions packages/core-web/helpers/_ESAbstract.Construct.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ function Construct(F /* [ , argumentsList [ , newTarget ]] */) { // eslint-disab
}

// 5. Return ? F.[[Construct]](argumentsList, newTarget).
// Polyfill.io - If newTarget is the same as F, it is equivalent to new F(...argumentsList).
// If newTarget is the same as F, it is equivalent to new F(...argumentsList).
if (newTarget === F) {
return new (Function.prototype.bind.apply(F, [null].concat(argumentsList)))();
} else {
// Polyfill.io - This is mimicking section 9.2.2 step 5.a.
// This is mimicking section 9.2.2 step 5.a.
var obj = OrdinaryCreateFromConstructor(newTarget, Object.prototype);
return Call(F, obj, argumentsList);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import AdvanceStringIndex from "@mrhenry/core-web/helpers/_ESAbstract.AdvanceStringIndex";
import CodePointAt from "@mrhenry/core-web/helpers/_ESAbstract.CodePointAt";
import UTF16SurrogatePairToCodePoint from "@mrhenry/core-web/helpers/_ESAbstract.UTF16SurrogatePairToCodePoint";
import CreateIterResultObject from "@mrhenry/core-web/helpers/_ESAbstract.CreateIterResultObject";
import Type from "@mrhenry/core-web/helpers/_ESAbstract.Type";
import CreateDataProperty from "@mrhenry/core-web/helpers/_ESAbstract.CreateDataProperty";
Expand Down
92 changes: 92 additions & 0 deletions packages/core-web/helpers/_ESAbstract.GroupBy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import AddValueToKeyedGroup from "@mrhenry/core-web/helpers/_ESAbstract.AddValueToKeyedGroup";
import SameValue from "@mrhenry/core-web/helpers/_ESAbstract.SameValue";
import SameValueNonNumber from "@mrhenry/core-web/helpers/_ESAbstract.SameValueNonNumber";
import Type from "@mrhenry/core-web/helpers/_ESAbstract.Type";
import Call from "@mrhenry/core-web/helpers/_ESAbstract.Call";
import GetIterator from "@mrhenry/core-web/helpers/_ESAbstract.GetIterator";
import GetMethod from "@mrhenry/core-web/helpers/_ESAbstract.GetMethod";
import GetV from "@mrhenry/core-web/helpers/_ESAbstract.GetV";
import ToObject from "@mrhenry/core-web/helpers/_ESAbstract.ToObject";
import IsCallable from "@mrhenry/core-web/helpers/_ESAbstract.IsCallable";
import IteratorClose from "@mrhenry/core-web/helpers/_ESAbstract.IteratorClose";
import IteratorStep from "@mrhenry/core-web/helpers/_ESAbstract.IteratorStep";
import IteratorNext from "@mrhenry/core-web/helpers/_ESAbstract.IteratorNext";
import IteratorComplete from "@mrhenry/core-web/helpers/_ESAbstract.IteratorComplete";
import ToBoolean from "@mrhenry/core-web/helpers/_ESAbstract.ToBoolean";
import Get from "@mrhenry/core-web/helpers/_ESAbstract.Get";
import IteratorValue from "@mrhenry/core-web/helpers/_ESAbstract.IteratorValue";
import RequireObjectCoercible from "@mrhenry/core-web/helpers/_ESAbstract.RequireObjectCoercible";
import ToPropertyKey from "@mrhenry/core-web/helpers/_ESAbstract.ToPropertyKey";
import ToPrimitive from "@mrhenry/core-web/helpers/_ESAbstract.ToPrimitive";
import OrdinaryToPrimitive from "@mrhenry/core-web/helpers/_ESAbstract.OrdinaryToPrimitive";
import ToString from "@mrhenry/core-web/helpers/_ESAbstract.ToString";

// _ESAbstract.GroupBy
/* global AddValueToKeyedGroup, Call, GetIterator, IsCallable, IteratorClose, IteratorStep, IteratorValue, RequireObjectCoercible, ToPropertyKey */

// 7.3.36 GroupBy ( items, callbackfn, keyCoercion )
// eslint-disable-next-line no-unused-vars
function GroupBy(items, callbackfn, keyCoercion) {
// 1. Perform ? RequireObjectCoercible(items).
RequireObjectCoercible(items);
// 2. If IsCallable(callbackfn) is false, throw a TypeError exception.
if (IsCallable(callbackfn) === false) {
throw new TypeError("callbackfn is not callable.");
}
// 3. Let groups be a new empty List.
var groups = [];
// 4. Let iteratorRecord be ? GetIterator(items, sync).
var iteratorRecord = GetIterator(items);
// 5. Let k be 0.
var k = 0;
// 6. Repeat,
// eslint-disable-next-line no-constant-condition
while (true) {
// a. If k ≥ 253 - 1, then
if (k >= Number.MAX_SAFE_INTEGER) {
// i. Let error be ThrowCompletion(a newly created TypeError object).
var error = new TypeError("k greater than or equal to MAX_SAFE_INTEGER");
// ii. Return ? IteratorClose(iteratorRecord, error).
return IteratorClose(iteratorRecord, error);
}
// b. Let next be ? IteratorStep(iteratorRecord).
var next = IteratorStep(iteratorRecord);
// c. If next is false, then
if (next === false) {
// i. Return groups.
return groups;
}
// d. Let value be ? IteratorValue(next).
var value = IteratorValue(next);
// e. Let key be Completion(Call(callbackfn, undefined, « value, 𝔽(k) »)).
var key;
try {
key = Call(callbackfn, undefined, [value, k]);
} catch (err) {
// f. IfAbruptCloseIterator(key, iteratorRecord).
return IteratorClose(iteratorRecord, err);
}
// g. If keyCoercion is property, then
if (keyCoercion === "property") {
// i. Set key to Completion(ToPropertyKey(key)).
try {
key = ToPropertyKey(key);
} catch (err) {
// ii. IfAbruptCloseIterator(key, iteratorRecord).
return IteratorClose(iteratorRecord, err);
}
}
// h. Else,
else {
// i. Assert: keyCoercion is zero.
// ii. If key is -0𝔽, set key to +0𝔽.
// eslint-disable-next-line no-compare-neg-zero
if (key === -0) key = 0;
}
// i. Perform AddValueToKeyedGroup(groups, key, value).
AddValueToKeyedGroup(groups, key, value);
// j. Set k to k + 1.
k = k + 1;
}
}
export default GroupBy;
2 changes: 1 addition & 1 deletion packages/core-web/helpers/_ESAbstract.HasOwnProperty.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function HasOwnProperty(o, p) { // eslint-disable-line no-unused-vars
// 3. Let desc be ? O.[[GetOwnProperty]](P).
// 4. If desc is undefined, return false.
// 5. Return true.
// Polyfill.io - As we expect user agents to support ES3 fully we can skip the above steps and use Object.prototype.hasOwnProperty to do them for us.
// As we expect user agents to support ES3 fully we can skip the above steps and use Object.prototype.hasOwnProperty to do them for us.
return Object.prototype.hasOwnProperty.call(o, p);
}
export default HasOwnProperty;
2 changes: 1 addition & 1 deletion packages/core-web/helpers/_ESAbstract.IsArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function IsArray(argument) { // eslint-disable-line no-unused-vars
// c. Return ? IsArray(target).
// 4. Return false.

// Polyfill.io - We can skip all the above steps and check the string returned from Object.prototype.toString().
// We can skip all the above steps and check the string returned from Object.prototype.toString().
return Object.prototype.toString.call(argument) === '[object Array]';
}
export default IsArray;
2 changes: 1 addition & 1 deletion packages/core-web/helpers/_ESAbstract.IsCallable.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function IsCallable(argument) { // eslint-disable-line no-unused-vars
// 2. If argument has a [[Call]] internal method, return true.
// 3. Return false.

// Polyfill.io - Only function objects have a [[Call]] internal method. This means we can simplify this function to check that the argument has a type of function.
// Only function objects have a [[Call]] internal method. This means we can simplify this function to check that the argument has a type of function.
return typeof argument === 'function';
}
export default IsCallable;

0 comments on commit fada6c7

Please sign in to comment.