npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

typescript-plugin-polyfill

v0.0.0

Published

A TypeScript plugin for precisely inserting polyfill imports based on actual usage

Readme

TypeScript Plugin Polyfill

npm version Build Status License

A TypeScript transformer plugin that intelligently injects polyfill imports based on actual type usage, avoiding redundant polyfill imports that increase bundle size.

Problem Background

Traditional polyfill solutions have the following issues:

  • Lack of type information leads to importing redundant polyfills
  • For example, when using the .at() method, both Array and String .at() polyfills would be imported
  • This increases the final bundle size unnecessarily

This plugin analyzes the actual usage in the code and accurately determines which polyfills are needed based on TypeScript's type system.

Installation

npm install typescript-plugin-polyfill

Usage

As a TypeScript Transformer

import polyfillPlugin from 'typescript-plugin-polyfill';

// Using programmatically with TypeScript API
const program = ts.createProgram(files, {
  // ...other options
});

const transformers: ts.CustomTransformers = {
	before: [
		polyfillPlugin(program, {
			polluting: {
				at: {
					Array: '@example/polyfills/array-at',
					String: '@example/polyfills/string-at'
				},
				includes: {
					Array: '@example/polyfills/array-includes',
					String: '@example/polyfills/string-includes'
				},
				name: {
					Function: '@example/polyfills/function-name'
				},
				finally: {
					Promise: '@example/polyfills/promise-finally'
				}
			}
		})
	]
};

program.emit(undefined, undefined, undefined, undefined, transformers);

With Rollup

// rollup.config.js
import typescript from '@rollup/plugin-typescript';
import polyfillPlugin from 'typescript-plugin-polyfill';

export default {
	// ...other options
	plugins: [
		typescript({
			transformers: (program) => ({
				before: [
					polyfillPlugin(program, {
						polluting: {
							at: {
								Array: 'sky-core/polyfill/Array/prototype/at',
								String: 'sky-core/polyfill/String/prototype/at'
							},
							includes: {
								Array: 'sky-core/polyfill/Array/prototype/includes',
								String: 'sky-core/polyfill/String/prototype/includes'
							},
							name: {
								Function: 'sky-core/polyfill/Function/prototype/name'
							},
							finally: {
								Promise: 'sky-core/polyfill/Promise/prototype/finally'
							}
						}
					})
				]
			})
		})
	]
};

With Webpack

// webpack.config.js
const polyfillPlugin = require('typescript-plugin-polyfill');

module.exports = {
	// ...other options
	module: {
		rules: [
			{
				test: /\.tsx?$/,
				loader: 'ts-loader',
				options: {
					getCustomTransformers: (program) => ({
						before: [
							polyfillPlugin(program, {
								polluting: {
									at: {
										Array: 'sky-core/polyfill/Array/prototype/at',
										String: 'sky-core/polyfill/String/prototype/at'
									}
								// ...more polyfill mappings
								}
							})
						]
					})
				}
			}
		]
	}
};

With ttypescript

{
	"compilerOptions": {
		"plugins": [
			{
				"transform": "typescript-plugin-polyfill",
				"polluting": {
					"at": {
						"Array": "sky-core/polyfill/Array/prototype/at",
						"String": "sky-core/polyfill/String/prototype/at"
					}
					// ...more polyfill mappings
				}
			}
		]
	}
}

How It Works

  1. Uses TypeScript's Transformer API to analyze source code at compile time
  2. Leverages TypeScript's type system to determine the exact types of variables and expressions
  3. Based on the type information, precisely identifies which polyfills are needed
  4. Injects import statements for only the required polyfills during the transformation phase

Features

  • Precise Analysis: Only imports polyfills actually used in the code
  • Type-Aware: Uses TypeScript's type system for improved accuracy
  • Configurable: Customizable mapping of methods to polyfill packages
  • Lightweight: Only adds necessary code imports

License

MIT