全局变量替换
🌐 Global Variable Replacement
Oxc 转换器支持替换全局变量。
🌐 Oxc transformer supports replacing global variables.
定义
🌐 Define
Define 功能提供了一种用常量表达式替换全局变量的方法。此功能类似于 Terser 的 global_defs 选项,以及 esbuild 的 define 选项。
// input
const foo = __DEV__ ? 1 : 2;
// output
const foo = 1;// Example
import { transform } from "oxc-transform";
const result = await transform("lib.js", sourceCode, {
define: {
__DEV__: "true",
},
});每个 define 条目将一个表达式映射到包含该表达式的代码字符串。其键必须是一个标识符(例如 __DEV__),或者是一系列以点连接的标识符(例如 process.env.NODE_ENV、import.meta.env.MODE)。其值必须是一个有效的表达式。
🌐 Each define entry maps an expression to a string of code containing an expression. The keys of it must be an identifier (e.g. __DEV__), or a dotted sequence of identifiers (e.g. process.env.NODE_ENV, import.meta.env.MODE). The values of it must be a valid expression.
始终引用数值
define 的值是表达式的字符串。这意味着该值应始终为字符串。如果你的意思是字符串字面量,你应当用引号括起来(例如 __MODE__: '"development"'、__MODE__: JSON.stringify("development"))。
🌐 The values of define are the string of expressions. This means the value should always a string. If you mean a string literal, you should quote it (e.g. __MODE__: '"development"', __MODE__: JSON.stringify("development")).
对象引用未共享
与 esbuild 不同,当将对象传递给 define 选项的值时,对象引用不会被共享。这意味着如果你更改对象,其他地方不会反映这些更改。
🌐 Differently from esbuild, when passing an object to the value of the define option, the object reference is not shared. This means that if you change the object, the changes will not be reflected in the other places.
const foo = __OBJECT__;
foo.bar = 1;
console.log(foo.bar); // 1
const bar = __OBJECT__;
console.log(foo.bar); // undefined// Example
import { transform } from "oxc-transform";
const result = await transform("lib.js", sourceCode, {
define: {
__OBJECT__: "{}",
},
});注入
🌐 Inject
“注入”功能提供了一种用模块导入替换全局变量的方法。该功能类似于 [esbuild 的 “inject”选项](https://esbuild.github.io/api/#inject)和 ['@rollup/plugin-inject'](https://github.com/rollup/plugins/tree/master/packages/inject)。
// input
const foo = new Promise((resolve) => resolve(1));
// output
import { Promise as P } from "es6-promise";
const foo = new P((resolve) => resolve(1));// Example
import { transform } from "oxc-transform";
const result = await transform("lib.js", sourceCode, {
inject: {
P: ["es6-promise", "Promise"],
},
});每个 inject 条目将一个表达式映射到一个导入标识符。它的键必须是一个标识符(例如 __DEV__),或者是点分隔的标识符序列(例如 process.env.NODE_ENV)。它的值必须是导入源的字符串,或者是包含导入源和导入名称的字符串元组(* 是命名空间导入)。
🌐 Each inject entry maps an expression to an imported identifier. The keys of it must be an identifier (e.g. __DEV__), or a dotted sequence of identifiers (e.g. process.env.NODE_ENV). The values of it must be a string of the import source, or a tuple of strings of the import source and the import name (* is namespace import).
const examples = {
// import { Promise } from 'es6-promise'
Promise: ["es6-promise", "Promise"],
// import { Promise as P } from 'es6-promise'
P: ["es6-promise", "Promise"],
// import $ from 'jquery'
$: "jquery",
// import * as fs from 'fs'
fs: ["fs", "*"],
// use a local module instead of a third-party one
"Object.assign": path.resolve("src/helpers/object-assign.js"),
};