mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 10:28:47 +00:00
Update define-constant.md
This commit is contained in:
@@ -51,12 +51,12 @@ if (true) {
|
||||
|
||||
And finally, Bun detects the `else` branch is not reachable, and eliminates it.
|
||||
|
||||
---
|
||||
|
||||
```ts
|
||||
console.log("Production mode");
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## What types of values are supported?
|
||||
|
||||
Values can be strings, identifiers, properties, or JSON.
|
||||
@@ -65,8 +65,6 @@ Values can be strings, identifiers, properties, or JSON.
|
||||
|
||||
To make all usages of `window` be `undefined`, you can use the following command.
|
||||
|
||||
---
|
||||
|
||||
```sh
|
||||
bun --define window="undefined" src/index.ts
|
||||
```
|
||||
@@ -81,9 +79,7 @@ if (typeof window !== "undefined") {
|
||||
}
|
||||
```
|
||||
|
||||
You can also set the value to be another identifier.
|
||||
|
||||
For example, to make all usages of `global` be `globalThis`, you can use the following command.
|
||||
You can also set the value to be another identifier. For example, to make all usages of `global` be `globalThis`, you can use the following command.
|
||||
|
||||
```sh
|
||||
bun --define global="globalThis" src/index.ts
|
||||
@@ -161,20 +157,3 @@ if (process.env.NODE_ENV === "production") {
|
||||
The `--define` flag operates on the AST (Abstract Syntax Tree) level, not on the text level. It happens during the transpilation process, which means it can be used in optimizations like dead code elimination.
|
||||
|
||||
String replacement tools tend to have escaping issues and replace unintended parts of the code.
|
||||
|
||||
```ts
|
||||
export default `${MY_DEFINE_VALUE}`;
|
||||
```
|
||||
|
||||
Using `--define=MY_DEFINE_VALUE="}ABC"`,
|
||||
|
||||
```ts
|
||||
export default "}ABC";
|
||||
```
|
||||
|
||||
Using a string replacement tool:
|
||||
|
||||
```ts
|
||||
// Note the missing "}" at the beginning:
|
||||
export default `${}ABC`;
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user