Skip to content

TypeScript transformation that inlines calls to small functions. ⚡️

License

Notifications You must be signed in to change notification settings

JoshuaKGoldberg/ts-function-inliner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

66 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TypeScript Function Inliner

TypeScript transformation that inlines calls to small functions. ⚡️

👪 All Contributors: 1 🤝 Code of Conduct: Kept 🧪 Coverage 📝 License: MIT 📦 npm version 💪 TypeScript: Strict

Explanation

Many projects choose to extract common shared logic into small helper functions. TypeScript projects often use small user-defined type guards to inform type narrowing. Unfortunately, the overhead of extracting logic into functions can hurt application performance before JIT optimizers fully kick in.1

This TypeScript transformation plugin detects calls to small one-line functions and inlines them in the output JavaScript. The resultant code will function the same regardless of the transformation.

Example

Given the following function:

export function isNotFalsy(value: unknown) {
	return !!value;
}

Before:

isNotFalsy("Hello!");

After:

!!"Hello!";

Note: this transformer does not remove the original function declarations. Use a separate tool after the transform, such as Terser, if you'd like to configure that.

Usage

npm i ts-function-inliner

Per github.com/Microsoft/TypeScript/issues/14419, TSConfig plugins don't support transformers. However, you can use this in other pipelines.

Usage with Gulp

Specify it as a custom transformer with gulp-typescript:

import gulp from "gulp";
import ts from "gulp-typescript";
import { transformerProgram } from "ts-function-inliner";

gulp.task("typescript", function () {
	gulp
		.src("src/**/*.ts")
		.pipe(
			ts({
				getCustomTransformers: (program) => ({
					before: [transformerProgram(program)],
				}),
			}),
		)
		.pipe(gulp.dest("lib"));
});

Development

See .github/CONTRIBUTING.md. Thanks! 💖

Contributors

Josh Goldberg
Josh Goldberg

💻 🤔 🚇 🚧 🔧

💙 This package was templated with create-typescript-app.

Footnotes

  1. See Microsoft/TypeScript: Added some Type type predicates internally #50010, which caused a 1-2% performance hit in TypeScript.

About

TypeScript transformation that inlines calls to small functions. ⚡️

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published