Build ToolsNormalizenormalizeJsBuildNewlines

normalizeJsBuildNewlines

Normalize excessive blank lines in compiled JavaScript files by collapsing multiple consecutive newlines into a consistent format.

Overview

normalizeJsBuildNewlines is a utility for normalizing compiled JavaScript files by removing excessive blank lines.

It processes files based on the provided pattern(s) and ensures that:

  • Excessive newlines are collapsed

    • Any occurrence of three or more consecutive line breaks (\n or \r\n) will be normalized into a consistent newline format.
    • This helps prevent unnecessarily large gaps in compiled output.

    Example

    *Before:

    const a = 1;
    
    
    
    const b = 2;

    *After:

    const a = 1;
    
    const b = 2;
  • Only affected files are rewritten

    • Files are only modified when excessive newlines are detected.
    • Safe to run multiple times without introducing changes.
  • Supports common JavaScript build outputs

    • Includes .js, .cjs, .mjs, .esm.js, .module.js.

Why use this?

  • Cleans up messy build output formatting.
  • Improves readability of compiled files.
  • Keeps distribution artifacts consistent.
  • Safe for repeated post-build execution (idempotent).

Importing

import { normalizeJsBuildNewlines } from "@rzl-zone/build-tools";
const { normalizeJsBuildNewlines } = require("@rzl-zone/build-tools");

Syntax

normalizeJsBuildNewlines(
  pattern: StringCollection,
  options?: NormalizeJsArtifactsOptions
): Promise<void>
normalizeJsBuildNewlines(pattern, options?): Promise<void>

Parameters

pattern

Glob pattern or list of patterns that target JavaScript output files.

Accepted values:

  • string ➔ single glob pattern.
  • string[] ➔ multiple glob patterns.
  • Set<string> ➔ unique collection of patterns.

Supported glob features

Patterns support common glob syntax, including:

  • * ➔ match any characters except /.
  • ** ➔ match nested directories.
  • {} ➔ brace expansion (match multiple extensions or patterns).
  • ! ➔ exclude patterns (can be combined with options.patternOptions.ignore).

Info

Pattern resolution behavior can be customized via options.patternOptions (e.g. hidden files, case sensitivity, concurrency, and ignore rules).

File Matching Behavior

  • This utility only processes files that match recognized JavaScript build output formats.
  • This filtering is applied after pattern matching.
  • Supported file types include:
    • .js.
    • .mjs.
    • .cjs.
    • .esm.js.
    • .module.js.

Files that do not match these formats will be automatically ignored, even if they are included in the provided pattern.

Examples:
  • dist/index.js ➔ ✅ processed.
  • dist/index.ts ➔ ❌ ignored.
  • dist/styles.css ➔ ❌ ignored.
  • dist/index.js.map ➔ ❌ ignored.

Examples

TypeScript/JavaScript
// Single pattern
normalizeJsBuildNewlines("dist/**/*.js");

// Multiple patterns
normalizeJsBuildNewlines(["dist/**/*.js", "build/**/*.js"]);

// Using Set
normalizeJsBuildNewlines(new Set(["dist/**/*.js"]));

// With options
normalizeJsBuildNewlines("dist/**/*.{js,cjs,mjs}", {
  patternOptions: {
    ignore: ["**/*.test.js"]
  }
});

// Exclude pattern
normalizeJsBuildNewlines([
  "dist/**/*.{js,cjs,mjs}",
  "!dist/**/*.test.js"
]);

options

Optional configuration object that defines how the utility function should behave in various scenarios, allowing customization and greater control at runtime.


logLevel

  • Description:
    Controls how detailed the logger output should be during execution.

    Supported values

    • silent ➔ no output.
    • error ➔ errors only.
    • info ➔ standard logs (default).
    • debug ➔ verbose logs.

    Notes

    • Invalid values are automatically reset to default.
    • All levels include error output except silent.
  • Required:

  • Type:
    "error" | "silent" | "info" | "debug"

  • Default:
    "info"

  • Example:

    TypeScript/JavaScript
    // Minimal output (errors only)
    await normalizeJsBuildNewlines("dist/**/*.js", {
      logLevel: "error"
    });
    
    // Debug mode (verbose logs)
    await normalizeJsBuildNewlines("dist/**/*.js", {
      logLevel: "debug"
    });
    
    // Silent mode (no logs)
    await normalizeJsBuildNewlines("dist/**/*.js", {
      logLevel: "silent"
    });

patternOptions

  • Description:
    Controls how input file patterns are matched.

    Used to filter or adjust which files are included during processing.
    Useful for ignoring files, handling hidden files, or tweaking matching behavior.

    Behavior

    • Affects which files are selected.
    • Can exclude unwanted files (e.g. test files).
    • Falls back to default configuration when invalid type or not provided.
  • Required:

  • Type:
    object

  • Default:

    {
      absolute: false,
      baseNameMatch: false,
      caseSensitiveMatch: true,
      concurrency: os.cpus().length,
      dot: true,
      followSymbolicLinks: true,
      globstar: true,
      ignore: [],
      markDirectories: false,
      objectMode: false,
      onlyDirectories: false,
      onlyFiles: true,
      unique: true,
      throwErrorOnBrokenSymbolicLink: false
    }
  • Example:

    TypeScript/JavaScript
    // Ignore source maps
    await normalizeJsBuildNewlines("dist/**/*.{js,cjs}", {
      patternOptions: {
        ignore: ["**/*.map"]
      }
    });
    
    // Case-insensitive matching
    await normalizeJsBuildNewlines("dist/**/*.{js,cjs}", {
      patternOptions: {
        caseSensitiveMatch: false
      }
    });
    
    // Disallowing patterns to match entries that begin with a period (.).
    await normalizeJsBuildNewlines("dist/**/*.{js,cjs}", {
      patternOptions: {
        dot: false
      }
    });

Returns

Returns a Promise<void>.
The function performs asynchronous file processing and does not return any value.

Version information

  • Since: v0.0.7.
  • Category: normalize.
  • Stability: Stable (Production-ready).
Made with ❤️ by @rzl-app