ESLint plugin that flags tree-shaking hazards in your code

@wolfcola/eslint-plugin-treeshake

An ESLint plugin that statically analyzes your source code for patterns known to break tree-shaking in JavaScript bundlers.

Installation

npm install -D @wolfcola/eslint-plugin-treeshake

Configuration

import treeshake from '@wolfcola/eslint-plugin-treeshake';

// Warns on all hazard patterns
export default [treeshake.configs.recommended];

Strict Config

import treeshake from '@wolfcola/eslint-plugin-treeshake';

// Errors on all hazard patterns + enables bundle check
export default [treeshake.configs.strict];

The strict config sets the rule to error level and enables bundleCheck: true, which runs treeshake-check --json as part of linting to cross-reference static findings with actual bundle analysis.

Rule: no-treeshake-hazard

A single rule that detects multiple categories of tree-shaking hazards. Each category can be individually toggled.

Detected Patterns

PatternOptionDefaultDescription
Enum declarationscheckEnumstrueTypeScript enum compiles to an IIFE that bundlers cannot remove
Unannotated callscheckUnannotatedCallstrueTop-level function calls without /*#__PURE__*/ annotation
Prototype mutationscheckPrototypeMutationtrueObject.defineProperty, Object.defineProperties, Object.setPrototypeOf, X.prototype.y = ...
Global assignmentscheckGlobalAssignmenttrueAssignments to window, globalThis, self, global
CommonJS patternscheckCjsPatternstruerequire(), module.exports, exports.x
Missing sideEffectscheckSideEffectsFieldtrueWarns when nearest package.json lacks a sideEffects field
Bundle check (opt-in)bundleCheckfalseRuns treeshake-check --json and maps results to source locations

Rule Options

'wolfcola/no-treeshake-hazard': ['warn', {
  checkEnums: true,
  checkUnannotatedCalls: true,
  checkPrototypeMutation: true,
  checkGlobalAssignment: true,
  checkCjsPatterns: true,
  checkSideEffectsField: true,
  additionalPureFunctions: ['myPureHelper'],
  bundleCheck: false,
  bundleCheckCwd: undefined,
}]

additionalPureFunctions

An array of function names that should be treated as pure (side-effect-free). Calls to these functions at module scope will not trigger the unannotatedCall warning.

Auto-fix and Suggestions

  • Enum declarations: Provides a suggestion to replace enum with an as const object and type alias
  • Unannotated calls: Auto-fixes by inserting /*#__PURE__*/ before the call

Bundle Check Mode

When bundleCheck: true, the rule runs npx treeshake-check --json (with a 60-second timeout) and maps the bundle analysis results back to source file locations. Static findings are deduplicated against bundle check results to avoid double-reporting.

Configs

ConfigRule LevelbundleCheck
recommendedwarnfalse
stricterrortrue

Programmatic API

import treeshake from '@wolfcola/eslint-plugin-treeshake';

// Access the rule directly
treeshake.rules['no-treeshake-hazard'];

// Access the named export
import { noTreeshakeHazard } from '@wolfcola/eslint-plugin-treeshake';