Debian Codegraph @

docs/content/en/_common/functions/js/options.md

raw
1 ---
2 _comment: Do not remove front matter.
3 ---
4
5 `defines`
6 : (`map`) This option allows you to define a set of string replacements to be performed when building. It must be a map where each key will be replaced by its value.
7
8 ```go-html-template
9 {{ $defines := dict "process.env.NODE_ENV" `"development"` }}
10 ```
11
12 `drop`
13 : {{< new-in 0.144.0 />}}
14 : (`string`) Edit your source code before building to drop certain constructs: One of `debugger` or `console`.
15 : See <https://esbuild.github.io/api/#drop>
16
17 `externals`
18 : (`slice`) External dependencies. Use this to trim dependencies you know will never be executed. See <https://esbuild.github.io/api/#external>.
19
20 `inject`
21 : (`slice`) This option allows you to automatically replace a global variable with an import from another file. The path names must be relative to `assets`. See <https://esbuild.github.io/api/#inject>.
22
23 `JSX`
24 : (`string`) How to handle/transform JSX syntax. One of: `transform`, `preserve`, `automatic`. Default is `transform`. Notably, the `automatic` transform was introduced in React 17+ and will cause the necessary JSX helper functions to be imported automatically. See <https://esbuild.github.io/api/#jsx>.
25
26 `JSXImportSource`
27 : (`string`) Which library to use to automatically import its JSX helper functions from. This only works if `JSX` is set to `automatic`. The specified library needs to be installed through npm and expose certain exports. See <https://esbuild.github.io/api/#jsx-import-source>.
28
29 The combination of `JSX` and `JSXImportSource` is helpful if you want to use a non-React JSX library like Preact, e.g.:
30
31 ```go-html-template
32 {{ $js := resources.Get "js/main.jsx" | js.Build (dict "JSX" "automatic" "JSXImportSource" "preact") }}
33 ```
34
35 With the above, you can use Preact components and JSX without having to manually import `h` and `Fragment` every time:
36
37 ```jsx
38 import { render } from 'preact';
39
40 const App = () => <>Hello world!</>;
41
42 const container = document.getElementById('app');
43 if (container) render(<App />, container);
44 ```
45
46 `loaders`
47 : {{< new-in 0.140.0 />}}
48 : (`map`) Configuring a loader for a given file type lets you load that file type with an `import` statement or a `require` call. For example, configuring the `.png` file extension to use the data URL loader means importing a `.png` file gives you a data URL containing the contents of that image. Loaders available are `none`, `base64`, `binary`, `copy`, `css`, `dataurl`, `default`, `empty`, `file`, `global-css`, `js`, `json`, `jsx`, `local-css`, `text`, `ts`, `tsx`. See <https://esbuild.github.io/api/#loader>.
49
50 `minify`
51 : (`bool`) Whether to minify the generated JS code. Default is `false`.
52
53 `params`
54 : (`map` or `slice`) Params that can be imported as JSON in your JS files, e.g.
55
56 ```go-html-template
57 {{ $js := resources.Get "js/main.js" | js.Build (dict "params" (dict "api" "https://example.org/api")) }}
58 ```
59
60 And then in your JS file:
61
62 ```js
63 import * as params from '@params';
64 ```
65
66 Note that this is meant for small data sets, e.g., configuration settings. For larger data sets, please put/mount the files into `assets` and import them directly.
67
68 `platform`
69 : {{< new-in 0.140.0 />}}
70 : (`string`) One of `browser`, `node`, `neutral`. Default is `browser`. See <https://esbuild.github.io/api/#platform>.
71
72 `shims`
73 : (`map`) This option allows swapping out a component with another. A common use case is to load dependencies like React from a CDN (with _shims_) when in production, but running with the full bundled `node_modules` dependency during development:
74
75 ```go-html-template
76 {{ $shims := dict "react" "js/shims/react.js" "react-dom" "js/shims/react-dom.js" }}
77 {{ $js = $js | js.Build dict "shims" $shims }}
78 ```
79
80 The _shim_ files may look like these:
81
82 ```js
83 // js/shims/react.js
84 module.exports = window.React;
85 ```
86
87 ```js
88 // js/shims/react-dom.js
89 module.exports = window.ReactDOM;
90 ```
91
92 With the above, these imports should work in both scenarios:
93
94 ```js
95 import * as React from 'react';
96 import * as ReactDOM from 'react-dom/client';
97 ```
98
99 `sourceMap`
100 : (`string`) The type of source map to generate. One of `external`, `inline`, `linked`, or `none`. Default is `none`. Linked and external source maps will be written to the target with the output file name + ".map". When `linked` a `sourceMappingURL` will also be written to the output file.
101
102 `sourcesContent`
103 : {{< new-in 0.140.0 />}}
104 : (`bool`) Whether to include the content of the source files in the source map. Default is `true`.
105
106 `target`
107 : (`string`) The language target. One of: `es5`, `es2015`, `es2016`, `es2017`, `es2018`, `es2019`, `es2020`, `es2021`, `es2022`, `es2023`, `es2024`, `es2025`, or `esnext`. Default is `esnext`.