Skip to content

Commit

Permalink
MOBILE-3947 ng: Update polyfills
Browse files Browse the repository at this point in the history
The check-es-compat patch can be removed if the following PR is resolved: robatwilliams/es-compat#86
  • Loading branch information
NoelDeMartin committed Nov 30, 2023
1 parent ffed38e commit 6bec9cc
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 24 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,8 @@ jobs:
npm run build:prod
npm run prod --prefix cordova-plugin-moodleapp
- name: JavaScript code compatibility
run: result=$(npx check-es-compat www/*.js 2> /dev/null | grep -v -E "Array\.prototype\.includes|Promise\.prototype\.finally|String\.prototype\.(matchAll|trimRight)|globalThis" | grep -Po "(?<=error).*?(?=\s+ecmascript)" | wc -l); test $result -eq 1
# 6 BigInt usage errors are expected, they are fine without polyfill because they are only used if available.
# See https://github.com/videojs/mpd-parser/blob/v0.22.1/src/segment/urlType.js
run: |
result=$(npx check-es-compat www/*.js --polyfills="{Array,String,TypedArray}.prototype.at,Array.prototype.flatMap,Array.prototype.flat,Array.prototype.includes,globalThis,Object.fromEntries,Object.hasOwn,Promise.prototype.finally,String.prototype.matchAll,String.prototype.trimRight" | grep "6 problems (6 errors, 0 warnings)" | wc -l); test $result -eq 1
npx check-es-compat cordova-plugin-moodleapp/www/*.js
5 changes: 4 additions & 1 deletion angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
"schematics": {},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"builder": "@angular-builders/custom-webpack:browser",
"options": {
"customWebpackConfig": {
"path": "./webpack.config.js"
},
"allowedCommonJsDependencies":[
"chart.js"
],
Expand Down
103 changes: 81 additions & 22 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
"zone.js": "~0.13.0"
},
"devDependencies": {
"@angular-builders/custom-webpack": "^16.0.1",
"@angular-devkit/build-angular": "^16.2.10",
"@angular-eslint/builder": "^16.2.0",
"@angular-eslint/eslint-plugin": "^16.2.0",
Expand Down Expand Up @@ -179,6 +180,7 @@
"minimatch": "^9.0.3",
"native-run": "^2.0.0",
"patch-package": "^6.5.0",
"terser-webpack-plugin": "^5.3.9",
"ts-jest": "^29.1.1",
"ts-node": "^8.3.0",
"typescript": "~5.1.3"
Expand Down
65 changes: 65 additions & 0 deletions patches/check-es-compat+3.1.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
diff --git a/node_modules/check-es-compat/bin/cli.mjs b/node_modules/check-es-compat/bin/cli.mjs
index 25c53f5..26ce475 100755
--- a/node_modules/check-es-compat/bin/cli.mjs
+++ b/node_modules/check-es-compat/bin/cli.mjs
@@ -17,7 +17,8 @@ if (args.length === 0) {
}
}

-async function execute(files) {
+async function execute(args) {
+ const { files, polyfills } = parseArguments(args);
const eslint = new ESLint({
// Ignore any config files
useEslintrc: false,
@@ -34,7 +35,7 @@ async function execute(files) {
es2021: true,
},
rules: {
- 'ecmascript-compat/compat': 'error',
+ 'ecmascript-compat/compat': ['error', { polyfills }],
},
},
});
@@ -46,3 +47,41 @@ async function execute(files) {

return { hasErrors: results.some((result) => result.errorCount > 0) };
}
+
+function parseArguments(args) {
+ const files = [];
+ const polyfills = [];
+ let nextArgIsPolyfills = false;
+
+ for (const arg of args) {
+ if (nextArgIsPolyfills) {
+ nextArgIsPolyfills = false;
+ polyfills.push(...splitPolyfillsArgument(arg));
+ continue;
+ }
+
+ if (arg.startsWith('--polyfills')) {
+ if (arg.startsWith('--polyfills=')) {
+ polyfills.push(...splitPolyfillsArgument(arg.slice(12)));
+ } else {
+ nextArgIsPolyfills = true;
+ }
+
+ continue;
+ }
+
+ files.push(arg);
+ }
+
+ return { files, polyfills };
+}
+
+function splitPolyfillsArgument(polyfills) {
+ const prototypeAtPolyfill = '{Array,String,TypedArray}.prototype.at';
+ const prototypeAtPlaceholder = '{{PROTOTYPEAT}}';
+
+ return polyfills
+ .replace(prototypeAtPolyfill, prototypeAtPlaceholder)
+ .split(',')
+ .map(polyfill => polyfill === prototypeAtPlaceholder ? prototypeAtPolyfill : polyfill);
+}
7 changes: 7 additions & 0 deletions src/polyfills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@ window.__Zone_disable_customElements = true;
import 'zone.js';

// Platform polyfills
import 'core-js/es/array/at';
import 'core-js/es/array/flat-map';
import 'core-js/es/array/flat';
import 'core-js/es/array/includes';
import 'core-js/es/global-this';
import 'core-js/es/object/from-entries';
import 'core-js/es/object/has-own';
import 'core-js/es/promise/finally';
import 'core-js/es/string/at';
import 'core-js/es/string/match-all';
import 'core-js/es/string/trim-right';
import 'core-js/es/typed-array/at';

polyfillEventComposedPath();

Expand Down

0 comments on commit 6bec9cc

Please sign in to comment.