Skip to content

无用代码消除

🌐 Dead Code Elimination

Oxc 压缩器支持消除死代码。例如,它可以移除 if (false) 块中的语句以及未使用的私有类字段。

🌐 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.

转换器中的有用功能

除了下面的选项之外,你还可以使用转换器中的 define 功能 将全局标识符替换为常量表达式,从而移除更多无用代码。

🌐 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

通过启用 dropConsole 选项,你可以删除所有 console.* 调用。此选项的行为类似于 Terserdrop_console 选项和 esbuild 的 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,
  },
});

整个调用表达式被移除

注意,这个选项会移除整个调用表达式,包括参数。这是有意为之,因为移除调用参数的求值有助于提升运行时性能,尤其是当调用参数计算成本高时。然而,如果这些参数中的任何一个有副作用,这种转换会改变你代码的行为。如果你想保留参数,可以使用 ['compress.treeshake.manualPureFunctions: ['console']'](#define-pure-functions) 选项。

🌐 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. If you want to keep the arguments, you can use compress.treeshake.manualPureFunctions: ['console'] option.

删除调试器

🌐 Drop Debugger

通过启用 dropDebugger 选项,你可以移除所有 debugger 语句。此选项默认启用。此选项的行为类似于 Terserdrop_debugger 选项和 esbuild 的 drop: ['debugger'] 选项

🌐 You can remove all debugger statements by enabling the dropDebugger option. This option is enabled by default. This option behaves similar to Terser's drop_debugger option and esbuild's drop: ['debugger'] option.

js
// input
debugger;

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

const result = await minify("lib.js", code, {
  compress: {
    dropDebugger: true,
  },
});

取消标签

🌐 Drop Labels

你可以通过启用 dropLabels 选项来移除所有具有指定标签的语句。此选项的行为类似于 esbuild 的 dropLabels 选项

🌐 You can remove all labeled statements with specified labels by enabling the dropLabels option. This option behaves similar to esbuild's dropLabels option.

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"],
  },
});

未使用的声明

🌐 Unused Declarations

默认情况下,所有未使用的函数/类/变量声明都会被移除。你可以使用 unused 选项来保留它们。

🌐 All unused function / class / variable declarations are removed by default. You can keep them by using the unused option.

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"
  },
});

保留 name 属性值

🌐 Keep name Property Values

默认情况下,Oxc 压缩器假设你的代码不依赖于函数或类的 name 属性。这是因为 name 属性是从函数/类名或变量名推断的,而保留原始名称会阻碍输出大小的减少。

🌐 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.

要保留 name 属性值,可以使用 keepNames 选项。

🌐 To keep the name property values, you can use the keepNames option.

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 }
  },
});

mangle.keepNames 选项

如果你正在使用“变形”功能,你可能还想启用[“mangle.keepNames”选项](./mangling#keep-name-property-values)。

🌐 If you are using the mangling feature, you may also want to enable the mangle.keepNames option.

控制副作用检测

🌐 Controlling Side Effect Detection

有多种选项可以控制副作用检测。

🌐 There are multiple options to control the side effect detection.

纯注释

🌐 Pure Annotations

默认情况下,Oxc 压缩器会遵守纯注释。纯注释是用来标记那些在返回值未被使用时可以安全移除的表达式的注释。更多信息请参见 草案规范提案

🌐 By default, Oxc minifier respects pure annotations. Pure annotations are annotation comments that marks expressions that can be safely removed if their return values are not used. See the draft specification proposal for more information.

通过将“compress.treeshake.annotations”选项设置为“false”来禁用。

🌐 This can be disabled by setting compress.treeshake.annotations option to false.

#__PURE__ / @__PURE__

#__PURE__ 注解用于标记那些如果其返回值未被使用则可以安全移除的函数调用。注意,它仅标记函数调用本身,不包括其参数。

🌐 The #__PURE__ annotation is used to mark function calls that can be safely removed if their return values are not used. Note that it only marks the function call itself and does not cover the arguments of it.

如果你想标记其他表达式或涵盖这些参数,你可以使用 IIFE 将它们封装起来,然后在上面添加 #__PURE__ 注解。

🌐 If you want to mark other expressions or the cover the arguments, you can wrap them with a IIFE and put the #__PURE__ annotation on it.

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());

这个函数不必是纯函数

尽管名字如此,该函数不必是纯函数(纯函数 - 维基百科)。它并不表示调用可以被缓存。换句话说,该函数不必是引用透明的(引用透明性 - 维基百科)。

🌐 Despite the name, the function does not have to be pure (Pure function - Wikipedia). It does not indicate that the calls can be cached. In other words, the function does not have to be referentially transparent (Referential transparency - Wikipedia).

#__NO_SIDE_EFFECTS__ / @__NO_SIDE_EFFECTS__

#__NO_SIDE_EFFECTS__ 注解用于标记一个函数声明,如果其返回值未被使用,所有对该函数的调用都可以安全地删除。如果你在多个地方调用了该函数,这将非常有用。

🌐 The #__NO_SIDE_EFFECTS__ annotation is used to mark a function declaration that all calls of it can be safely removed if their return values are not used. This is useful if you have a function call of that function in multiple places.

js
// input
/* #__NO_SIDE_EFFECTS__ */
export function foo() {}
/* #__NO_SIDE_EFFECTS__ */
export const bar = () => {};
foo();
bar();

// output
export function foo() {}
export const bar = () => {};

定义纯函数

🌐 Define Pure Functions

你也可以用“compress.treeshake.manualPureFunctions”选项标记函数,而不是用纯注释标记函数。这个选项是一个函数名称数组。该功能类似于[Rollup的“treeshake.manualPureFunctions”选项](https://rollup.nodejs.cn/configuration-options/#treeshake-manualpurefunctions)和[Terser](https://terser.nodejs.cn/)的“pure_funcs”选项。

🌐 Instead of marking functions with pure annotations, you can also mark functions via the compress.treeshake.manualPureFunctions option. This option is an array of function names. This feature is similar to Rollup's treeshake.manualPureFunctions option and Terser's pure_funcs option.

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"],
    },
  },
});

忽略属性读取的副作用

🌐 Ignoring Property Read Side Effects

默认情况下,Oxc 压缩器假定属性读取会产生副作用。这是因为访问 null 的属性会抛出错误。此外,还有一种情况是属性为 getter,而 getter 可能有副作用。你可以通过将 compress.treeshake.propertyReadSideEffects 选项设置为 false 来告诉 Oxc 压缩器忽略这些可能性。此功能类似于 Rollup 的 treeshake.propertyReadSideEffects 选项Terser 的 pure_getters 选项

🌐 By default, Oxc minifier assumes that property reads have side effects. This is because the accessing a property of null throws and error. Also there's a case where a property is a getter and the getter might has a side effect. You can tell Oxc minifier to ignore those possibilities by setting compress.treeshake.propertyReadSideEffects option to false. This feature is similar to Rollup's treeshake.propertyReadSideEffects option and Terser's pure_getters option.

js
// input
const foo = {
  get bar() {
    console.log("effect");
    return "bar";
  },
};
foo.bar;

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

忽略全局变量访问的副作用

🌐 Ignoring Global Variable Access Side Effects

默认情况下,Oxc 压缩器假设全局变量的访问会产生副作用。这是因为访问不存在的全局变量会抛出错误。此外,还有一种情况是全局变量是一个 getter,而这个 getter 可能会产生副作用。你可以通过将 compress.treeshake.unknownGlobalSideEffects 选项设置为 false 来告诉 Oxc 压缩器忽略这些可能性。这个功能类似于 Rollup 的 treeshake.unknownGlobalSideEffects 选项

🌐 By default, Oxc minifier assumes that global variable accesses have side effects. This is because accessing a non-existent global variable throws an error. Also there's a case where a global variable is a getter and the getter might has a side effect. You can tell Oxc minifier to ignore those possibilities by setting compress.treeshake.unknownGlobalSideEffects option to false. This feature is similar to Rollup's treeshake.unknownGlobalSideEffects option.

js
// input
const jQuery = $;

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

忽略无效导入语句的副作用

🌐 Ignoring Invalid Import Statement Side Effects

默认情况下,Oxc 压缩器假设导入语句具有副作用。这是因为在以下情况下,导入语句会产生副作用:

🌐 By default, Oxc minifier assumes that import statements have side effects. This is because import statements have side effects in the following cases:

  • 无法解析导入
  • 导入名称未从导入的模块中导出

你可以通过将 compress.treeshake.invalidImportSideEffects 选项设置为 false 来告诉 Oxc 压缩器忽略这些可能性。

🌐 You can tell Oxc minifier to ignore those possibilities by setting compress.treeshake.invalidImportSideEffects option to false.

js
// input
import { existing } from "cannot-be-resolved";
import { missing } from "somewhere";

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