# Build Configuration & Dependency Rules

> Auto-generated by pr-review-rules-generator skill on 2026-02-24
> Source: git history mining + domain expertise interview

## Overview

Cross-cutting rules for build configuration (`rollup.config.cjs`, `package.json`, `tsconfig.json`),
dependency management, export map stability, and test requirements for new plugins. These rules
apply repo-wide and are based on patterns identified through both git history and team interview.

## Rules

---

## Rule ID: dependency_version_pinning

**Summary:** All dependencies must use exact version pinning — no ranges (`^`, `~`, or `*`)

**Rationale:** ServiceNow maintains an approved dependency list for `@servicenow` scoped packages.
Using version ranges can allow unapproved or untested transitive versions to enter the build,
creating compliance and reproducibility issues. All dependencies (both `dependencies` and
`devDependencies`) must use exact version strings. This practice is already established in the
repo (see `package.json`) and must be maintained for all new additions and upgrades. (Source:
interview; also see MAINT commit `96fa338`: "Pinning rollup plugin dependency".)

**Scope:**
- Paths: `package.json`

**Detection Guidance:** For any `package.json` change, check every new or modified dependency
entry. Flag any that use `^`, `~`, `>=`, `*`, or other range specifiers. The only acceptable
format is an exact semver string (e.g., `"1.2.3"`, not `"^1.2.3"`). Also verify that version
upgrades are intentional — a PR that bumps a minor version for a reason unrelated to the PR's
stated purpose should be questioned.

**Severity:** `error`

**Source:**
- Type: interview
- Refs: interview, 96fa338
- Confidence: high

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

**Examples:**

### Violation Example
```json
// ❌ Bad: range specifiers allow uncontrolled version resolution
{
  "@rollup/plugin-replace": "^6.0.2",
  "@rollup/plugin-commonjs": "~26.0.1"
}
```

### Correct Example
```json
// ✅ Good: exact versions only
{
  "@rollup/plugin-replace": "6.0.2",
  "@rollup/plugin-commonjs": "26.0.1"
}
```


---

## Rule ID: export_map_stability

**Summary:** Changes to the `exports` field in `package.json` must be backward-compatible

**Rationale:** This package is consumed by multiple ServiceNow teams via its `@servicenow/isomorphic-rollup`
npm package. The `exports` map in `package.json` defines the public API of the package. Breaking
changes (removing export paths, changing conditions like `browser`/`default`, or renaming
sub-paths) will silently fail in consumer packages that haven't updated. New exports can be added
freely; existing exports must not be removed or renamed without a major version bump.

**Scope:**
- Paths: `package.json`
- Change Types: `modify`

**Detection Guidance:** When the `exports` field in `package.json` is modified, diff the old and
new values. Flag any removed export path (e.g., removing `"./resolver"` or `"./browser"`). Flag
any change to an existing path's resolution (e.g., changing `"./dist/resolver.js"` to a different
file). Adding new export paths is safe and does not need to be flagged. Also check that the
`browser` field at the top level is consistent with `exports["."].browser`.

**Severity:** `warning`

**Source:**
- Type: interview
- Refs: interview
- Confidence: high

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


---

## Rule ID: new_plugin_test_requirement

**Summary:** New rollup plugins must include Jest test coverage in the `test/` directory

**Rationale:** Several plugins in this repo were initially shipped without tests, leading to
defects being discovered in production (e.g., DEF0744564 added 417 lines of tests as part of
its fix — tests that should have existed from the start). The test suite uses Jest with the
config in `jest.config.js` and `rollup.config.test.cjs`. Every new plugin directory under `src/`
must have a corresponding test file in `test/` that covers the core happy path and key edge cases.

**Scope:**
- Paths: `src/**`
- Change Types: `add`

**Detection Guidance:** If the PR adds a new directory under `src/` that constitutes a new plugin
(i.e., contains an `index.ts` or `index.js` that exports a rollup plugin factory), check for a
corresponding file in `test/{plugin-name}/`. If no test file is present, flag this as a required
addition. At minimum, tests should cover: (1) the plugin is invoked by rollup, (2) the happy path
output is correct, and (3) at least one edge case documented in the plugin's rationale.

**Severity:** `warning`

**Source:**
- Type: interview
- Refs: interview, DEF0744564
- Confidence: high

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


---

## Rule ID: isomorphic_build_correctness

**Summary:** New plugins must export browser-safe variants and must not import Node-only modules at the top level

**Rationale:** The entire purpose of this package is isomorphic execution — the same plugins must
work in both Node.js and browser (WASM/WebWorker) environments. The dual build is configured in
`rollup.config.cjs` with separate browser and Node output bundles. Multiple MAINT commits
(e.g., `b2e2117`, `1d0bb46`, `a41ed46`) fixed cases where Node-only imports (`fs`, `process`) leaked into browser builds, causing runtime failures in the browser build.

**Scope:**
- Paths: `src/**`, `rollup.config.cjs`

**Detection Guidance:** For any new plugin or change to an existing plugin: (1) check that Node-
only imports (`import fs from 'fs'`, `require('os')`) are either
guarded by environment checks or only used in the Node-specific adapter, not in the shared plugin
code, (2) verify that the plugin is exported in both `dist/index.js` (node) and `dist/browser/`
paths, (3) check `rollup.config.cjs` to confirm the new plugin is included in both build targets,
(4) look for `typeof window` or similar environment guards where needed. Using `import path from 'path'` is allowed because we have path polyfill import resolution logic added by the build.

**Severity:** `error`

**Source:**
- Type: interview
- Refs: interview, b2e2117, 1d0bb46, a41ed46
- Confidence: high

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

**Examples:**

### Violation Example
```typescript
// ❌ Bad: top-level Node-only import will break browser build
import * as fs from 'fs';

export function myPlugin(): Plugin {
  return {
    name: 'my-plugin',
    load(id) { return fs.readFileSync(id, 'utf-8'); }
  };
}
```

### Correct Example
```typescript
// ✅ Good: accept fs adapter as dependency injection, not top-level import
export function myPlugin(fsAdapter: FsInterface): Plugin {
  return {
    name: 'my-plugin',
    load(id) { return fsAdapter.readFile(id); }
  };
}
```


---

## Rule ID: plugin_export_requirement

**Summary:** All plugins must be individually exported and composable by consumers

**Rationale:** This is a public npm package. Customers must be able to import and use any plugin
independently for their own build configurations. There should be no internal-only or hidden
plugins — every plugin that exists in the package must be exported from the appropriate entry
point (`@servicenow/isomorphic-rollup` for Rollup plugins, `@servicenow/isomorphic-rollup/vite`
for Vite plugins). Composed plugin sets like `servicenowFrontEndPlugins` and
`servicenowVitePlugins` are conveniences, not gatekeepers. A customer should always be able to
pick individual plugins and compose them differently.

**Scope:**
- Paths: `src/**/index.ts`, `package.json`
- Change Types: `add`, `modify`

**Detection Guidance:** When a new plugin is added under `src/`, verify it is exported from the
appropriate entry point (`src/index.ts` or `src/vite/index.ts`). If a plugin is only referenced
internally by a composed set (e.g., used inside `servicenowVitePlugins` but not exported), flag
it as a violation. Also verify the plugin's factory function and options type are both exported so
consumers can configure it independently.

**Severity:** `error`

**Source:**
- Type: interview
- Refs: interview
- Confidence: high

**Override:**
- Allowed: false


---

## Attribution

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

## Override Mechanism

To override a rule in a PR:

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

2. **In repo config (`.pr-review-config.yaml`):**
   ```yaml
   overrides:
     - rule_id: export_map_stability
       paths: ["package.json"]
       reason: "Major version bump — breaking export changes are intentional"
   ```

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