Rules Hub

Coding Rules Library

← Back to all rules
frontend ruleStack: general
polyfillscore-jsmaintainabilitydependenciesbrowser-compatibility

Prefer standard polyfill libraries

Avoid manually implementing polyfills in source code; use granular imports from established libraries like core-js to ensure correctness and maintainability.

PR: Feat/FCK-2200 - Polyfill isWellFormed for Safari slugs #341Created: Dec 7, 2025

Bad example

Old codets
1// lib/polyfills/is-well-formed.ts
2// Manually copying complex logic into the codebase
3if (!String.prototype.isWellFormed) {
4 String.prototype.isWellFormed = function () {
5 const str = String(this);
6 // ... 30 lines of complex surrogate pair logic ...
7 return true;
8 };
9}

Explanation (EN)

Copying and pasting polyfill logic into the source code is risky. It creates unmaintained code that may miss edge cases, lacks updates, and clutters the repository.

Objašnjenje (HR)

Kopiranje logike polyfill-a u izvorni kod je rizično. Stvara neodržavan kod koji može propustiti rubne slučajeve, ne prima ažuriranja i nepotrebno opterećuje repozitorij.

Good example

New codets
1// lib/polyfills.ts
2// Import specific feature to keep bundle size low while using a battle-tested implementation
3import 'core-js/actual/string/is-well-formed';

Explanation (EN)

Using a standard library like `core-js` ensures the polyfill is spec-compliant and tested. Granular imports prevent bloating the bundle size by only including what is needed.

Objašnjenje (HR)

Korištenje standardne biblioteke poput `core-js` osigurava da je polyfill usklađen sa specifikacijama i testiran. Granularni uvoz sprječava povećanje paketa uključivanjem samo onoga što je potrebno.

Notes (EN)

If a manual polyfill is strictly necessary (e.g., due to extreme bundle size constraints preventing even granular imports), you must explicitly document the source/origin of the implementation in a comment.

Bilješke (HR)

Ako je ručni polyfill strogo neophodan (npr. zbog ekstremnih ograničenja veličine paketa koja sprječavaju čak i granularni uvoz), morate u komentaru eksplicitno navesti izvor/porijeklo implementacije.