Skip to content

Commit e2eabae

Browse files
gregorymdevelopit
andauthored
Document @ for the --define option (#692)
* Document @ for the --define option I had to dig through the code in order to figure out how to replace an expression. I was initially trying to do `microbundle --define global=window` which should actually be `microbundle --define @global=window` * Add --define section to readme Co-authored-by: Jason Miller <developit@users.noreply.github.com>
1 parent 8b60fc8 commit e2eabae

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,16 @@ To achieve the smallest possible bundle size, libraries often wish to rename int
220220

221221
It's also possible to configure repeatable short names for each mangled property, so that every build of your library has the same output. **See the wiki for a [complete guide to property mangling in Microbundle](https://github.com/developit/microbundle/wiki/mangle.json).**
222222

223+
### Defining build-time constants
224+
225+
The `--define` option can be used to inject or replace build-time constants when bundling. In addition to injecting string or number constants, prefixing the define name with `@` allows injecting JavaScript expressions.
226+
227+
| Build command | Source code | Output |
228+
|---------------|-------------|--------|
229+
`microbundle --define VERSION=2` | `console.log(VERSION)` | `console.log(2)`
230+
`microbundle --define API_KEY='abc123'` | `console.log(API_KEY)` | `console.log("abc123")`
231+
`microbundle --define @assign=Object.assign` | `assign(a, b)` | `Object.assign(a, b)`
232+
223233
### All CLI Options <a name="options"></a>
224234

225235
```
@@ -244,7 +254,7 @@ Options
244254
--target Specify your target environment (node or web) (default web)
245255
--external Specify external dependencies, or 'none' (default peerDependencies and dependencies in package.json)
246256
--globals Specify globals dependencies, or 'none'
247-
--define Replace constants with hard-coded values
257+
--define Replace constants with hard-coded values (use @key=exp to replace an expression)
248258
--alias Map imports to different modules
249259
--compress Compress output using Terser
250260
--no-compress Disable output compressing

0 commit comments

Comments
 (0)