diff options
author | 2023-05-09 00:55:21 -0400 | |
---|---|---|
committer | 2023-05-08 21:55:21 -0700 | |
commit | 5e366872f659abf116b903e5cece999a04cd018b (patch) | |
tree | d06b5ccd28ea49a7a5e050868ff27e676e0d56f7 /docs | |
parent | 1a411e201b71374f515d1f6cdbb1b36186ee48b0 (diff) | |
download | bun-5e366872f659abf116b903e5cece999a04cd018b.tar.gz bun-5e366872f659abf116b903e5cece999a04cd018b.tar.zst bun-5e366872f659abf116b903e5cece999a04cd018b.zip |
implement build api `define` and `loaders` (#2805)
* parse error logs
* clean up types
* remove --jsx-production. use NODE_ENV instead
* add define to js api
* add loaders to js api
* fixups
* sourcemap
* typo fix
* remove label, comment dir just for now
* test tweaks
* test work
* make optional enums actually optional.
allows `sourcemap: undefined`
* overload host ram test
* string tests
* tests
* test for 2815
* requested changes
* sort this list
* remove this test file now that it passes
* oops
* add --format
* finish ts tests
* doc typos related to define and loader
Diffstat (limited to 'docs')
-rw-r--r-- | docs/bundler/migration.md | 30 | ||||
-rw-r--r-- | docs/cli/build.md | 74 |
2 files changed, 87 insertions, 17 deletions
diff --git a/docs/bundler/migration.md b/docs/bundler/migration.md index 260acf1ba..aac37479c 100644 --- a/docs/bundler/migration.md +++ b/docs/bundler/migration.md @@ -63,8 +63,8 @@ In Bun's CLI, simple boolean flags like `--minify` do not accept an argument. Ot --- -- `--loader` -- `--loader` +- `--loader:.ext=loader` +- `--loader .ext:loader` - Bun supports a different set of built-in loaders than esbuild; see [Bundler > Loaders](/docs/bundler/loaders) for a complete reference. The esbuild loaders `dataurl`, `binary`, `base64`, `copy`, and `empty` are not yet implemented. The syntax for `--loader` is slightly different. @@ -131,7 +131,7 @@ In Bun's CLI, simple boolean flags like `--minify` do not accept an argument. Ot - `--watch` - n/a -- Not applicable. +- Not applicable --- @@ -143,7 +143,7 @@ In Bun's CLI, simple boolean flags like `--minify` do not accept an argument. Ot - `--analyze` - n/a -- Not supported. Use `--manifest` to generate a manifest file. +- Not supported --- @@ -161,7 +161,7 @@ In Bun's CLI, simple boolean flags like `--minify` do not accept an argument. Ot - `--certfile` - n/a -- Not applicable, Bun's bundler does +- Not applicable --- @@ -203,7 +203,7 @@ In Bun's CLI, simple boolean flags like `--minify` do not accept an argument. Ot - `--global-name` - n/a -- Not applicable, Bun does not support `iife` output at this time. +- Not applicable, Bun does not support `iife` output at this time --- @@ -248,7 +248,7 @@ In Bun's CLI, simple boolean flags like `--minify` do not accept an argument. Ot - `--jsx-side-effects` - n/a -- JSX is always assumed to be side-effect-free. +- JSX is always assumed to be side-effect-free --- @@ -313,7 +313,8 @@ In Bun's CLI, simple boolean flags like `--minify` do not accept an argument. Ot --- - `--metafile` -- `--manifest` +- n/a +- Not supported --- @@ -340,7 +341,6 @@ In Bun's CLI, simple boolean flags like `--minify` do not accept an argument. Ot - `--outbase` - `--root` -- Not supported --- @@ -515,7 +515,6 @@ In Bun's CLI, simple boolean flags like `--minify` do not accept an argument. Ot - `define` - `define` -- Not supported in JS API --- @@ -637,8 +636,8 @@ In Bun's CLI, simple boolean flags like `--minify` do not accept an argument. Ot --- - `loader` -- n/a -- Not supported in JS API +- `loader` +- Bun supports a different set of built-in loaders than esbuild; see [Bundler > Loaders](/docs/bundler/loaders) for a complete reference. The esbuild loaders `dataurl`, `binary`, `base64`, `copy`, and `empty` are not yet implemented. --- @@ -685,8 +684,11 @@ In Bun's CLI, simple boolean flags like `--minify` do not accept an argument. Ot --- - `metafile` -- `manifest` -- When `manifest` is `true`, the result of `Bun.build()` will contain a `manifest` property. The manifest is compatible with esbuild's metafile format. +- n/a +- Not supported + +<!-- - `manifest` +- When `manifest` is `true`, the result of `Bun.build()` will contain a `manifest` property. The manifest is compatible with esbuild's metafile format. --> --- diff --git a/docs/cli/build.md b/docs/cli/build.md index f205969d5..01262944d 100644 --- a/docs/cli/build.md +++ b/docs/cli/build.md @@ -324,7 +324,7 @@ Depending on the target, Bun will apply different module resolution rules and op {% /table %} -<!-- ### `module` +### `format` Specifies the module format to be used in the generated bundles. @@ -714,6 +714,24 @@ var value = z.string().parse("Hello world!") console.log(_.upperCase(value)); ``` +To mark all imports as external, use the wildcard `*`: + +{% codetabs %} + +```ts#JavaScript +await Bun.build({ + entrypoints: ['./index.tsx'], + outdir: './out', + external: ['*'], +}) +``` + +```bash#CLI +$ bun build ./index.tsx --outdir ./out --external '*' +``` + +{% /codetabs %} + ### `naming` Customizes the generated file names. Defaults to `./[dir]/[name].[ext]`. @@ -988,6 +1006,56 @@ The output file would now look something like this. + var logo = 'https://cdn.example.com/logo-a7305bdef.svg'; ``` +### `define` + +A map of global identifiers to be replaced at build time. Keys of this object are identifier names, and values are JSON strings that will be inlined. + +{% callout } +This is not needed to inline `process.env.NODE_ENV`, as Bun does this automatically. +{% /callout %} + +{% codetabs %} + +```ts#JavaScript +await Bun.build({ + entrypoints: ['./index.tsx'], + outdir: './out', + define: { + STRING: JSON.stringify("value"), + "nested.boolean": "true", + }, +}) +``` + +```bash#CLI +$ bun build ./index.tsx --outdir ./out --define 'STRING="value"' --define "nested.boolean=true" +``` + +{% /codetabs %} + +### `loader` + +A map of file extensions to [built-in loader names](https://bun.sh/docs/bundler/loaders#built-in-loaders). This can be used to quickly customize how certain file files are loaded. + +{% codetabs %} + +```ts#JavaScript +await Bun.build({ + entrypoints: ['./index.tsx'], + outdir: './out', + loader: { + ".png": "dataurl", + ".txt": "file", + }, +}) +``` + +```bash#CLI +$ bun build ./index.tsx --outdir ./out --loader .png:dataurl --loader .txt:file +``` + +{% /codetabs %} + ## Reference ```ts @@ -1003,8 +1071,8 @@ interface BuildOptions { outdir?: string; // default: no write (in-memory only) target?: "browser" | "bun" | "node"; // "browser" splitting?: boolean; // true - plugins?: BunPlugin[]; // [] - loader?: { [k in string]: Loader }; + plugins?: BunPlugin[]; // [] // See https://bun.sh/docs/bundler/plugins + loader?: { [k in string]: Loader }; // See https://bun.sh/docs/bundler/loaders manifest?: boolean; // false external?: string[]; // [] sourcemap?: "none" | "inline" | "external"; // "none" |