# Polyfill Plugin Review Rules

> Auto-generated by pr-review-rules-generator skill on 2026-02-24
> Source: git history mining (DEF commits, reverts) + domain expertise interview

## Overview

Rules for the polyfill plugin (`src/polyfill/`). This plugin has the most revert history in the
repo — it has been reverted and re-applied multiple times, making it the highest-risk area. It
handles browser/node polyfilling for built-in Node modules and must work correctly in both
isomorphic environments.

## Rules

---

## Rule ID: node_prefix_polyfill_invariant

**Summary:** Ensure polyfill plugin handles `node:` prefixed module imports

**Rationale:** DEF0739502 identified that the polyfill plugin did not handle `require('node:path')`
or `import 'node:fs'` style imports, which are valid modern Node.js syntax. When packages use this
prefix, they were silently passed through without polyfilling, causing failures in browser builds.
This fix has been reverted and re-applied (commits 986b0e4, 7b3e5b0), making it critical that any
changes to polyfill resolution logic preserve `node:` prefix handling.

**Scope:**
- Paths: `src/polyfill/**`
- Change Types: `modify`

**Detection Guidance:** When reviewing changes to the polyfill plugin's `resolveId` or `load`
hooks, verify that the `node:` prefix is stripped (or accounted for) before looking up the polyfill
mapping. Check that test coverage includes imports with the `node:` prefix (e.g.,
`require('node:path')`, `import 'node:events'`). If a refactor changes the module ID matching
logic, trace through what happens when the ID starts with `node:`.

**Severity:** `error`

**Source:**
- Type: `incident`
- Refs: DEF0739502, d502112, 986b0e4, 7b3e5b0
- Confidence: high

**Override:**
- Allowed: false
- Requires: n/a

**Examples:**

### Violation Example
```typescript
// ❌ Bad: does not handle 'node:' prefix — 'node:path' won't match 'path' in the polyfill map
resolveId(id: string) {
  if (polyfillMap[id]) {
    return virtualModuleId(id);
  }
}
```

### Correct Example
```typescript
// ✅ Good: strip 'node:' prefix before polyfill lookup
resolveId(id: string) {
  const normalizedId = id.startsWith('node:') ? id.slice(5) : id;
  if (polyfillMap[normalizedId]) {
    return virtualModuleId(normalizedId);
  }
}
```


---

## Rule ID: polyfill_entry_point_resolution

**Summary:** Preserve the `resolveEntryPoints` option and its default value of `true`

**Rationale:** DEF0739502 introduced a `resolveEntryPoints` option to prevent the polyfill plugin
from treating a package named after a Node.js builtin (e.g., a custom `path` package) as a
polyfill target when bundling that package itself. The default must be `true`. This option was
reverted (986b0e4, 7b3e5b0) and later committed as a standalone fix. Removing or changing the
default would break builds for packages whose name collides with a Node builtin.

**Scope:**
- Paths: `src/polyfill/**`

**Detection Guidance:** Check that `PolyfillOptions.resolveEntryPoints` still defaults to `true`
in the options type and in plugin initialization logic. If options handling is refactored, verify
that omitting this field from user config still produces `resolveEntryPoints = true` behavior.
Look for the specific check that skips polyfill resolution when processing entry points and
`resolveEntryPoints` is `false`.

**Severity:** `error`

**Source:**
- Type: `incident`
- Refs: DEF0739502, 2502604, 986b0e4, 7b3e5b0, 3b75691
- Confidence: high

**Override:**
- Allowed: true
- Requires: justification


---

## Rule ID: polyfill_isomorphic_compatibility

**Summary:** Polyfill plugin changes must be verified in both Node and browser build environments

**Rationale:** The polyfill plugin serves different purposes in Node vs browser builds: in Node
builds it should be a no-op (native modules are available), in browser builds it injects polyfills
for built-in modules. Changes that work in one environment often silently break the other. The
repo has multiple instances of polyfill-related fixes that required follow-up corrections because
only one environment was tested.

**Scope:**
- Paths: `src/polyfill/**`

**Detection Guidance:** For any change to the polyfill plugin, check whether the PR tests both the
Node build path and the browser/isomorphic path. Look for tests under `test/` that cover both
environments. If the change conditionally imports Node-only modules (`fs`, `path`), verify there
is a browser-safe fallback. Check that the rollup build config (`rollup.config.cjs`) correctly
handles the plugin in both browser and node output configurations.

**Severity:** `warning`

**Source:**
- Type: mined
- Refs: e59bafd, bce3684
- Confidence: medium

**Override:**
- Allowed: true
- Requires: justification


---

## Attribution

All rules are traceable to specific sources:
- `incident` = Direct from production defect (DEF*)
- `mined` = Derived from git history analysis (reverts, high-churn)
- `interview` = Captured during domain expertise interview

## Override Mechanism

To override a rule in a PR:

1. **In PR description:**
   ```markdown
   <!-- pr-review: skip node_prefix_polyfill_invariant reason="Justification here" -->
   ```

2. **In repo config (`.pr-review-config.yaml`):**
   ```yaml
   overrides:
     - rule_id: polyfill_entry_point_resolution
       paths: ["path/to/exclude/**"]
       reason: "Why this path is exempt"
   ```

Note: Rules marked `override.allowed: false` cannot be skipped without approval.
