Debian Codegraph @

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

raw
1 ---
2 _comment: Do not remove front matter.
3 ---
4
5 `params`
6 : (`map` or `slice`) Params that can be imported as JSON in your JS files, e.g.
7
8 ```go-html-template
9 {{ $js := resources.Get "js/main.js" | js.Build (dict "params" (dict "api" "https://example.org/api")) }}
10 ```
11
12 And then in your JS file:
13
14 ```js
15 import * as params from '@params';
16 ```
17
18 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.
19
20 `minify`
21 : (`bool`) Whether to minify the generated JS code. Default is `false`.
22
23 `loaders`
24 : {{< new-in 0.140.0 />}}
25 : (`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>.
26
27 `inject`
28 : (`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>.
29
30 `shims`
31 : (`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:
32
33 ```go-html-template
34 {{ $shims := dict "react" "js/shims/react.js" "react-dom" "js/shims/react-dom.js" }}
35 {{ $js = $js | js.Build dict "shims" $shims }}
36 ```
37
38 The _shim_ files may look like these:
39
40 ```js
41 // js/shims/react.js
42 module.exports = window.React;
43 ```
44
45 ```js
46 // js/shims/react-dom.js
47 module.exports = window.ReactDOM;
48 ```
49
50 With the above, these imports should work in both scenarios:
51
52 ```js
53 import * as React from 'react';
54 import * as ReactDOM from 'react-dom/client';
55 ```
56
57 `target`
58 : (`string`) The language target. One of: `es5`, `es2015`, `es2016`, `es2017`, `es2018`, `es2019`, `es2020`, `es2021`, `es2022`, `es2023`, `es2024`, or `esnext`. Default is `esnext`.
59
60 `platform`
61 : {{< new-in 0.140.0 />}}
62 : (`string`) One of `browser`, `node`, `neutral`. Default is `browser`. See <https://esbuild.github.io/api/#platform>.
63
64 `externals`
65 : (`slice`) External dependencies. Use this to trim dependencies you know will never be executed. See <https://esbuild.github.io/api/#external>.
66
67 `defines`
68 : (`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.
69
70 ```go-html-template
71 {{ $defines := dict "process.env.NODE_ENV" `"development"` }}
72 ```
73
74 `drop`
75 : {{< new-in 0.144.0 />}}
76 : (`string`) Edit your source code before building to drop certain constructs: One of `debugger` or `console`.
77 : See <https://esbuild.github.io/api/#drop>
78
79 `sourceMap`
80 : (`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.
81
82 `sourcesContent`
83 : {{< new-in 0.140.0 />}}
84 : (`bool`) Whether to include the content of the source files in the source map. Default is `true`.
85
86 `JSX`
87 : (`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>.
88
89 `JSXImportSource`
90 : (`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>.
91
92 The combination of `JSX` and `JSXImportSource` is helpful if you want to use a non-React JSX library like Preact, e.g.:
93
94 ```go-html-template
95 {{ $js := resources.Get "js/main.jsx" | js.Build (dict "JSX" "automatic" "JSXImportSource" "preact") }}
96 ```
97
98 With the above, you can use Preact components and JSX without having to manually import `h` and `Fragment` every time:
99
100 ```jsx
101 import { render } from 'preact';
102
103 const App = () => <>Hello world!</>;
104
105 const container = document.getElementById('app');
106 if (container) render(<App />, container);
107 ```