|  | 
|  | 1 | +# Dead Code Elimination | 
|  | 2 | + | 
|  | 3 | +Oxc minifier supports eliminating dead code. For example, it removes the statements inside a `if (false)` block and the unused private class fields. | 
|  | 4 | + | 
|  | 5 | +This feature is always enabled, but you can remove more code by enabling some options. | 
|  | 6 | + | 
|  | 7 | +::: tip Useful features in the transformer | 
|  | 8 | + | 
|  | 9 | +Other than the options below, you can also use [the `define` feature in the transformer](/docs/guide/usage/transformer/global-variable-replacement#define) to replace global identifiers with constant expressions to remove more dead code. | 
|  | 10 | + | 
|  | 11 | +::: | 
|  | 12 | + | 
|  | 13 | +## Drop Console | 
|  | 14 | + | 
|  | 15 | +You can remove all `console.*` calls by enabling the `dropConsole` option. This option behaves similar to [Terser](https://terser.org/)'s `drop_console` option and [esbuild's `drop: ['console']` option](https://esbuild.github.io/api/#drop). | 
|  | 16 | + | 
|  | 17 | +```js | 
|  | 18 | +// input | 
|  | 19 | +const bar = window.bar(); | 
|  | 20 | +console.log("foo", bar, baz()); | 
|  | 21 | + | 
|  | 22 | +// output | 
|  | 23 | +const bar = window.bar(); | 
|  | 24 | +``` | 
|  | 25 | + | 
|  | 26 | +```js | 
|  | 27 | +// Example | 
|  | 28 | +import { minify } from "oxc-minify"; | 
|  | 29 | + | 
|  | 30 | +const result = minify("lib.js", code, { | 
|  | 31 | +  compress: { | 
|  | 32 | +    dropConsole: true, | 
|  | 33 | +  }, | 
|  | 34 | +}); | 
|  | 35 | +``` | 
|  | 36 | + | 
|  | 37 | +::: warning The whole call expression is removed | 
|  | 38 | + | 
|  | 39 | +Note that this option removes the whole call expression including the arguments. This is intentional because removing the evaluation of call arguments is useful for improving the runtime performance if those are expensive to compute. However, if any of those arguments have side effects, this transformation will change the behavior of your code. | 
|  | 40 | + | 
|  | 41 | +<!-- TODO: suggest pure functions option when exposed --> | 
|  | 42 | + | 
|  | 43 | +::: | 
|  | 44 | + | 
|  | 45 | +## Drop Debugger | 
|  | 46 | + | 
|  | 47 | +You can remove all `debugger` statements by enabling the `dropDebugger` option. This option is enabled by default. This option behaves similar to [Terser](https://terser.org/)'s `drop_debugger` option and [esbuild's `drop: ['debugger']` option](https://esbuild.github.io/api/#drop). | 
|  | 48 | + | 
|  | 49 | +```js | 
|  | 50 | +// input | 
|  | 51 | +debugger; | 
|  | 52 | + | 
|  | 53 | +// output | 
|  | 54 | +``` | 
|  | 55 | + | 
|  | 56 | +```js | 
|  | 57 | +// Example | 
|  | 58 | +import { minify } from "oxc-minify"; | 
|  | 59 | + | 
|  | 60 | +const result = minify("lib.js", code, { | 
|  | 61 | +  compress: { | 
|  | 62 | +    dropDebugger: true, | 
|  | 63 | +  }, | 
|  | 64 | +}); | 
|  | 65 | +``` | 
|  | 66 | + | 
|  | 67 | +## Drop Labels | 
|  | 68 | + | 
|  | 69 | +You can remove all labeled statements with specified labels by enabling the `dropLabels` option. This option behaves similar to [esbuild's `dropLabels` option](https://esbuild.github.io/api/#drop-labels). | 
|  | 70 | + | 
|  | 71 | +```js | 
|  | 72 | +// input | 
|  | 73 | +DEV: console.log("foo"); | 
|  | 74 | +console.log("bar"); | 
|  | 75 | + | 
|  | 76 | +// output | 
|  | 77 | +console.log("bar"); | 
|  | 78 | +``` | 
|  | 79 | + | 
|  | 80 | +```js | 
|  | 81 | +// Example | 
|  | 82 | +import { minify } from "oxc-minify"; | 
|  | 83 | + | 
|  | 84 | +const result = minify("lib.js", code, { | 
|  | 85 | +  compress: { | 
|  | 86 | +    dropLabels: ["DEV"], | 
|  | 87 | +  }, | 
|  | 88 | +}); | 
|  | 89 | +``` | 
|  | 90 | + | 
|  | 91 | +## Unused Declarations | 
|  | 92 | + | 
|  | 93 | +All unused function / class / variable declarations are removed by default. You can keep them by using the `unused` option. | 
|  | 94 | + | 
|  | 95 | +```js | 
|  | 96 | +// input | 
|  | 97 | +{ | 
|  | 98 | +  function foo() {} | 
|  | 99 | +} | 
|  | 100 | + | 
|  | 101 | +// output | 
|  | 102 | +``` | 
|  | 103 | + | 
|  | 104 | +```js | 
|  | 105 | +// Example | 
|  | 106 | +import { minify } from "oxc-minify"; | 
|  | 107 | + | 
|  | 108 | +const result = minify("lib.js", code, { | 
|  | 109 | +  compress: { | 
|  | 110 | +    unused: true, // or "keep_assign" | 
|  | 111 | +  }, | 
|  | 112 | +}); | 
|  | 113 | +``` | 
|  | 114 | + | 
|  | 115 | +## Keep `name` Property Values | 
|  | 116 | + | 
|  | 117 | +By default, Oxc minifier assumes that your code does not rely on the `name` property of functions / classes. This is because the `name` property is inferred from the function / class name or the variable name and keeping the original name would prevent reducing the output size. | 
|  | 118 | + | 
|  | 119 | +To keep the `name` property values, you can use the `keepNames` option. | 
|  | 120 | + | 
|  | 121 | +```js | 
|  | 122 | +// input | 
|  | 123 | +var bar = function foo() {}; | 
|  | 124 | + | 
|  | 125 | +// output | 
|  | 126 | +var bar = function foo() {}; | 
|  | 127 | +``` | 
|  | 128 | + | 
|  | 129 | +```js | 
|  | 130 | +// Example | 
|  | 131 | +import { minify } from "oxc-minify"; | 
|  | 132 | + | 
|  | 133 | +const result = minify("lib.js", code, { | 
|  | 134 | +  compress: { | 
|  | 135 | +    keepNames: true, // shorthand of { function: true, class: true } | 
|  | 136 | +  }, | 
|  | 137 | +}); | 
|  | 138 | +``` | 
|  | 139 | + | 
|  | 140 | +::: tip `mangle.keepNames` option | 
|  | 141 | + | 
|  | 142 | +If you are using the mangling feature, you may also want to enable [the `mangle.keepNames` option](./mangling#keep-name-property-values). | 
|  | 143 | + | 
|  | 144 | +::: | 
0 commit comments