Skip to content

Obfuscate TypeScript source ​

Requires Codefend 3.3.0 or later for the recommended m_ preset.

This example applies Codefend to TypeScript before compiling it to JavaScript. The program calculates a discounted order total in integer cents.

Install Node.js, then start in a new directory:

sh
npm init -y
npm install --save-dev codefend typescript

1. Add typed application code ​

Create source/main.cts:

ts
function m_total(m_price: number, m_count: number, m_percent: number): number {
  const m_subtotal = m_price * m_count;
  const m_discount = Math.trunc((m_subtotal * m_percent) / 100);
  return m_subtotal - m_discount;
}

console.log(m_total(1000, 2, 10));

Expected obfuscated source ​

With the configuration in the next step, Codefend produces:

ts
function Ox0(Ox1: number, Ox2: number, Ox3: number): number {
  const Ox4 = Ox1 * Ox2;
  const Ox5 = Math.trunc((Ox4 * Ox3) / 100);
  return Ox4 - Ox5;
}

console.log(Ox0(1000, 2, 10));

The program's expected output remains 1800. Generated identifier numbers can change when the source or Codefend version changes.

The .cts extension makes the resulting module CommonJS under TypeScript's NodeNext mode, producing main.cjs. This keeps the example explicit even if the surrounding package uses ES modules. The number types, Math.trunc, and console.log are retained.

2. Configure the source transformation ​

Create .codefendrc.json in the project root:

json
{
  "generation": {
    "inputDir": "source",
    "outputDir": "release"
  },
  "parser": {
    "name": "startWithMAndUnderscore"
  }
}

The startWithMAndUnderscore preset selects this example's m_ names, leaving unrelated underscore names alone. See parser presets for the exact rule and why we recommend this convention.

It leaves this example's plain filename unchanged. The default empty pool uses generated prefix-and-sequence names.

Reserve release for generated source, separate from source. Codefend removes an existing output directory. Keep compiled JavaScript, source maps, declaration files, dependencies, and binary assets outside the processed input.

3. Compile and compare ​

Run these commands from the project root:

sh
npx tsc source/main.cts --target ES2022 --module NodeNext --outDir build/original
node build/original/main.cjs
npx codefend --obfuscate
npx tsc release/main.cts --target ES2022 --module NodeNext --outDir build/generated
node build/generated/main.cjs

Both executions should print:

text
1800

The original and generated TypeScript compile into separate build directories. TypeScript documents compiling explicit input files and NodeNext module behavior.

Project integration ​

Keep exported APIs, property keys, framework hooks, decorator metadata, and runtime data formats stable. TypeScript types do not protect a renamed JSON field from breaking a server contract. If you emit declarations for consumers, verify that the published declarations and JavaScript expose the same intended API.

Codefend uses text matching, not the TypeScript symbol table, and it does not provide a source map for its replacements. Compile and run representative generated code; assess debugging/source-map needs separately. For bundler-based projects, see Vite, esbuild, Rollup, or Webpack, and read current limitations.

Add Codefend to your workflow ​

Add generation before TypeScript compilation in your existing npm build script. In CI, install the pinned project dependencies with npm ci, compile the generated source, and run tests against that build before packaging JavaScript or declarations.

sh
npx codefend --obfuscate
npx tsc release/main.cts --target ES2022 --module NodeNext --outDir build/generated
node build/generated/main.cjs

In CI, stop on a failed step and package only validated generated output. See build and deployment for the shared workflow.