js.md (6432B)
1 --- 2 title: JavaScript Building 3 description: Hugo Pipes can process JavaScript files with [ESBuild](https://github.com/evanw/esbuild). 4 date: 2020-07-20 5 publishdate: 2020-07-20 6 lastmod: 2020-07-20 7 categories: [asset management] 8 keywords: [] 9 menu: 10 docs: 11 parent: "pipes" 12 weight: 45 13 weight: 45 14 sections_weight: 45 15 draft: false 16 --- 17 18 Any JavaScript resource file can be transpiled and "tree shaken" using `js.Build` which takes for argument either a string for the filepath or a dict of options listed below. 19 20 ### Options 21 22 targetPath [string] 23 : If not set, the source path will be used as the base target path. 24 Note that the target path's extension may change if the target MIME type is different, e.g. when the source is TypeScript. 25 26 params [map or slice] {{< new-in "0.78.0" >}} 27 : Params that can be imported as JSON in your JS files, e.g.: 28 29 ```go-html-template 30 {{ $js := resources.Get "js/main.js" | js.Build (dict "params" (dict "api" "https://example.org/api")) }} 31 ``` 32 And then in your JS file: 33 34 ```js 35 import * as params from '@params'; 36 ``` 37 38 Note that this is meant for small data sets, e.g. config settings. For larger data, please put/mount the files into `/assets` and import them directly. 39 40 minify [bool] 41 : Let `js.Build` handle the minification. 42 43 inject [slice] {{< new-in "0.81.0" >}} 44 : 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 45 46 shims {{< new-in "0.81.0" >}} 47 : 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: 48 49 ``` 50 {{ $shims := dict "react" "js/shims/react.js" "react-dom" "js/shims/react-dom.js" }} 51 {{ $js = $js | js.Build dict "shims" $shims }} 52 ``` 53 54 The _shim_ files may look like these: 55 56 ```js 57 // js/shims/react.js 58 module.exports = window.React; 59 ``` 60 61 ```js 62 // js/shims/react-dom.js 63 module.exports = window.ReactDOM; 64 ``` 65 66 67 With the above, these imports should work in both scenarios: 68 69 ```js 70 import * as React from 'react' 71 import * as ReactDOM from 'react-dom'; 72 ``` 73 74 target [string] 75 : The language target. 76 One of: `es5`, `es2015`, `es2016`, `es2017`, `es2018`, `es2019`, `es2020` or `esnext`. 77 Default is `esnext`. 78 79 externals [slice] 80 : External dependencies. Use this to trim dependencies you know will never be executed. See https://esbuild.github.io/api/#external 81 82 83 defines [map] 84 : Allow to define a set of string replacement to be performed when building. Should be a map where each key is to be replaced by its value. 85 86 ```go-html-template 87 {{ $defines := dict "process.env.NODE_ENV" `"development"` }} 88 ``` 89 90 format [string] {{< new-in "0.74.3" >}} 91 : The output format. 92 One of: `iife`, `cjs`, `esm`. 93 Default is `iife`, a self-executing function, suitable for inclusion as a <script> tag. 94 95 sourceMap 96 : Whether to generate source maps. Enum, currently only `inline` (we will improve that). 97 98 ### Import JS code from /assets 99 100 {{< new-in "0.78.0" >}} 101 102 Since Hugo `v0.78.0` `js.Build` has full support for the virtual union file system in [Hugo Modules](/hugo-modules/). You can see some simple examples in this [test project](https://github.com/gohugoio/hugoTestProjectJSModImports), but in short this means that you can do this: 103 104 ```js 105 import { hello } from 'my/module'; 106 ``` 107 108 And it will resolve to the top-most `index.{js,ts,tsx,jsx}` inside `assets/my/module` in the layered file system. 109 110 ```js 111 import { hello3 } from 'my/module/hello3'; 112 ``` 113 114 Will resolve to `hello3.{js,ts,tsx,jsx}` inside `assets/my/module`. 115 116 Any imports starting with `.` is resolved relative to the current file: 117 118 ```js 119 import { hello4 } from './lib'; 120 ``` 121 122 For other files (e.g. `JSON`, `CSS`) you need to use the relative path including any extension, e.g: 123 124 ```js 125 import * as data from 'my/module/data.json'; 126 ``` 127 128 Any imports in a file outside `/assets` or that does not resolve to a component inside `/assets` will be resolved by [ESBuild](https://esbuild.github.io/) with the **project directory** as the resolve directory (used as the starting point when looking for `node_modules` etc.). Also see [hugo mod npm pack](/commands/hugo_mod_npm_pack/). If you have any imported NPM dependencies in your project, you need to make sure to run `npm install` before you run `hugo`. 129 130 Also note the new `params` option that can be passed from template to your JS files, e.g.: 131 132 ```go-html-template 133 {{ $js := resources.Get "js/main.js" | js.Build (dict "params" (dict "api" "https://example.org/api")) }} 134 ``` 135 And then in your JS file: 136 137 ```js 138 import * as params from '@params'; 139 ``` 140 141 Hugo will, by default, generate a `assets/jsconfig.json` file that maps the imports. This is useful for navigation/intellisense help inside code editors, but if you don't need/want it, you can [turn it off](/getting-started/configuration/#configure-build). 142 143 144 145 ### Include Dependencies In package.json / node_modules 146 147 Any imports in a file outside `/assets` or that does not resolve to a component inside `/assets` will be resolved by [ESBuild](https://esbuild.github.io/) with the **project directory** as the resolve directory (used as the starting point when looking for `node_modules` etc.). Also see [hugo mod npm pack](/commands/hugo_mod_npm_pack/). If you have any imported NPM dependencies in your project, you need to make sure to run `npm install` before you run `hugo`. 148 149 {{< new-in "0.78.1" >}} From Hugo `0.78.1` the start directory for resolving NPM packages (aka. packages that live inside a `node_modules` folder) is always the main project folder. 150 151 **Note:** If you're developing a theme/component that is supposed to be imported and depends on dependencies inside `package.json`, we recommend reading about [hugo mod npm pack](/commands/hugo_mod_npm_pack/), a tool to consolidate all the NPM dependencies in a project. 152 153 154 ### Examples 155 156 ```go-html-template 157 {{ $built := resources.Get "js/index.js" | js.Build "main.js" }} 158 ``` 159 160 Or with options: 161 162 ```go-html-template 163 {{ $externals := slice "react" "react-dom" }} 164 {{ $defines := dict "process.env.NODE_ENV" `"development"` }} 165 166 {{ $opts := dict "targetPath" "main.js" "externals" $externals "defines" $defines }} 167 {{ $built := resources.Get "scripts/main.js" | js.Build $opts }} 168 <script type="text/javascript" src="{{ $built.RelPermalink }}" defer></script> 169 ``` 170 171