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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@namchee/henshin-strip-debug

v1.0.1

Published

TypeScript transformers that strips debugger statements, including console statements

Downloads

8

Readme

@namchee/henshin-strip-debug

A TypeScript transfomer that replaces debugging statements such as debugger and console methods with void expressions. Supports plain JavaScript and syntax extensions like JSX and TSX.

Example

Given the following code:

console.log('Hello World!');

function add(a: number, b: number): number {
    debugger;
    return a + b;
}

console['table']({ foo: 'bar' });

This transformer will transform your code to the following code:

void 0;

function add(a: number, b: number): number {
    void 0; // debugger statements are replaced
    return a + b;
}

void 0; // support computed accessor too!

Installation

# Using npm
npm install -D @namchee/henshin-strip-debug

# Using yarn
yarn add -D @namchee/henshin-strip-debug

# Using pnpm
pnpm install -D @namchee/henshin-strip-debug

# Using bun
bun install -D @namchee/henshin-strip-debug

API

createStripDebugTransformer(config: Config = defaultConfig)

A factory function that creates a TypeScript transformer instance that strips debugger statements. Designed to be consumed by transpilers and compilers alike.

import ts from 'typescript';

import { createStripDebugTransformer } from '@namchee/henshin-strip-debug';

const sourceFile = `const add = (a: number, b: number) => a + b;

add(1, 2);
console.log('Hello World!');
`;
const transformer = createStripDebugTransformer(config);

/*
 * const add = (a: number, b: number) => a + b;
 *
 * add(1, 2);
 * void 0;
 */
const result = ts.transpileModule(sourceFile, {
    transformers: {
        before: [transformer]
    }
}).outputText;

stripDebug(source: string, config: Config = defaultConfig, path: string = 'index.ts')

A helper function that strips debugger statement source using compilation options provided in config.

import { stripDebug } from '@namchee/henshin-strip-debug';

const sourceFile = `const add = (a: number, b: number) => a + b;

add(1, 2);
console.log('Hello World!');
`;

/*
 * const add = (a: number, b: number) => a + b;
 *
 * add(1, 2);
 * void 0;
 */
const result = stripDebug(sourceFile); 

Configuration

| Name | Type | Default | Description | | ----------------- | ----------------- | ------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | compilerOptions | CompilerOptions | { "target": "ES2015" } | TypeScript compiler options to be used on transformation process. Will be ignored by createStripDebugTransformer. Please refer to the official documentation for more information regarding supported values. | | debugger | boolean | true | Strips debugger statements from the source. | | exclude | string[] | [] | List of console methods that should not be stripped by the transformer. For example, filling this options with ['table'] will not strip console.table calls. |

If omitted, the transformer will use the following configuration:

const config = {
    compilerOptions: {
        target: ts.ScriptTarget.ES2015,
    },
    exclude: [],
    debugger: true,
};

FAQ

  1. What does void 0 do?

    void is an operator that evaluates the given expression and returns undefined. TL;DR, it's a code that doesn't do anything.

  2. Why not removing the debugging statements altogether?

    Completely stripping the debugging statements breaks some code. For example, given the following code:

    if (process.env.NODE_ENV === 'development')
      console.log('im debugging');
    
    const a = 3;
    
    a > 3 ? console.log('Greater than') : console.log('Lesser equal than');

    Completely stripping the debugging statements yield the following code:

    if (process.env.NODE_ENV === 'development')
         
    const a = 3;
    
    a > 3 ? : ;

    which is an invalid code. However, replacing it with a code that doesn't do anything like void 0 will not break the code!

Acknowledgements

This transformer is heavily inspired by @rollup/plugin-strip. This transformer is the generic version of it by utilizing Compiler API