Skip to content

Dead Code Elimination

Oxc minifier supports eliminating dead code. For example, it removes the statements inside a if (false) block and the unused private class fields.

This feature is always enabled, but you can remove more code by enabling some options.

Useful features in the transformer

Other than the options below, you can also use the define feature in the transformer to replace global identifiers with constant expressions to remove more dead code.

Drop Console

You can remove all console.* calls by enabling the dropConsole option. This option behaves similar to Terser's drop_console option and esbuild's drop: ['console'] option.

js
// input
const bar = window.bar();
console.log("foo", bar, baz());

// output
const bar = window.bar();
``````js
// Example
import { minify } from "oxc-minify";

const result = await minify("lib.js", code, {
  compress: {
    dropConsole: true,
  },
});
``````js
// input
debugger;

// output
``````js
// Example
import { minify } from "oxc-minify";

const result = await minify("lib.js", code, {
  compress: {
    dropDebugger: true,
  },
});
``````js
// input
DEV: console.log("foo");
console.log("bar");

// output
console.log("bar");
``````js
// Example
import { minify } from "oxc-minify";

const result = await minify("lib.js", code, {
  compress: {
    dropLabels: ["DEV"],
  },
});
``````js
// input
{
  function foo() {}
}

// output
``````js
// Example
import { minify } from "oxc-minify";

const result = await minify("lib.js", code, {
  compress: {
    unused: true, // or "keep_assign"
  },
});
``````js
// input
var bar = function foo() {};

// output
var bar = function foo() {};
``````js
// Example
import { minify } from "oxc-minify";

const result = await minify("lib.js", code, {
  compress: {
    keepNames: true, // shorthand of { function: true, class: true }
  },
});
``````js
// input
/* #__PURE__ */ foo();
/* #__PURE__ */ new Foo();
/* #__PURE__ */ foo(bar());
/* #__PURE__ */ (() => {
  foo(bar());
})();
console.log(/* #__PURE__ */ foo());
console.log(/* #__PURE__ */ new Foo());

// output
bar();
console.log(foo());
console.log(new Foo());
``````js
// input
/* #__NO_SIDE_EFFECTS__ */
export function foo() {}
/* #__NO_SIDE_EFFECTS__ */
export const bar = () => {};
foo();
bar();

// output
export function foo() {}
export const bar = () => {};
``````js
// input
foo();
foo.bar();
bar();
bar.baz();
new foo();
foo``;

// output
bar();
``````js
// Example
import { minify } from "oxc-minify";

const result = await minify("lib.js", code, {
  compress: {
    treeshake: {
      manualPureFunctions: ["foo", "bar.baz"],
    },
  },
});
``````js
// input
const foo = {
  get bar() {
    console.log("effect");
    return "bar";
  },
};
foo.bar;

// output (with `compress.treeshake.propertyReadSideEffects: false`)
``````js
// input
const jQuery = $;

// output (with `compress.treeshake.propertyReadSideEffects: false`)
``````js
// input
import { existing } from "cannot-be-resolved";
import { missing } from "somewhere";

// output (with `compress.treeshake.invalidImportSideEffects: true`)
import { existing } from "cannot-be-resolved";
import { missing } from "somewhere";

// output (with `compress.treeshake.invalidImportSideEffects: false`)