| 1 |
--- |
| 2 |
title: js.Babel |
| 3 |
description: Transpile JavaScript resources using Babel. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [babel] |
| 9 |
returnType: resource.Resource |
| 10 |
signatures: ['js.Babel [OPTIONS] RESOURCE'] |
| 11 |
aliases: [/functions/resources/babel/] |
| 12 |
--- |
| 13 |
|
| 14 |
The `js.Babel` function transforms JavaScript using [Babel][]. |
| 15 |
|
| 16 |
## Setup |
| 17 |
|
| 18 |
Step 1 |
| 19 |
: Install [Node.js][]. |
| 20 |
|
| 21 |
Step 2 |
| 22 |
: Install the required Node packages in the root of your project. For example, to install Babel's core compiler, its command-line interface, and the preset for transpiling modern JavaScript based on your target environments: |
| 23 |
|
| 24 |
```sh |
| 25 |
npm install --save-dev @babel/core @babel/cli @babel/preset-env |
| 26 |
``` |
| 27 |
|
| 28 |
Step 3 |
| 29 |
: Create a Babel configuration file in the root of your project. For example, to use the environment preset to target Google Chrome version 79 or later: |
| 30 |
|
| 31 |
```js {file="babel.config.mjs" copy=true} |
| 32 |
export default { |
| 33 |
presets: [ |
| 34 |
[ |
| 35 |
'@babel/preset-env', |
| 36 |
{ |
| 37 |
targets: { |
| 38 |
chrome: "79" |
| 39 |
} |
| 40 |
} |
| 41 |
] |
| 42 |
] |
| 43 |
}; |
| 44 |
``` |
| 45 |
|
| 46 |
Step 4 |
| 47 |
: Place your JS file within the `assets/js` directory. |
| 48 |
|
| 49 |
Step 5 |
| 50 |
: Add the Babel executable to Hugo's `security.exec.allow` list in your project configuration: |
| 51 |
|
| 52 |
{{< code-toggle file=hugo >}} |
| 53 |
[security.exec] |
| 54 |
allow = ['^(dart-)?sass$', '^go$', '^git$', '^node$', '^postcss$', '^babel$'] |
| 55 |
{{< /code-toggle >}} |
| 56 |
|
| 57 |
Step 6 |
| 58 |
: Create a _partial_ template to process the JavaScript: |
| 59 |
|
| 60 |
```go-html-template {file="layouts/_partials/js.html" copy=true} |
| 61 |
{{ with resources.Get "js/main.js" }} |
| 62 |
{{ $opts := dict |
| 63 |
"minified" (cond hugo.IsDevelopment false true) |
| 64 |
"noComments" (cond hugo.IsDevelopment false true) |
| 65 |
"sourceMap" (cond hugo.IsDevelopment "inline" "none") |
| 66 |
}} |
| 67 |
{{ with . | js.Babel $opts }} |
| 68 |
{{ if hugo.IsDevelopment }} |
| 69 |
<script src="{{ .RelPermalink }}"></script> |
| 70 |
{{ else }} |
| 71 |
{{ with . | fingerprint }} |
| 72 |
<script src="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}" crossorigin="anonymous"></script> |
| 73 |
{{ end }} |
| 74 |
{{ end }} |
| 75 |
{{ end }} |
| 76 |
{{ end }} |
| 77 |
``` |
| 78 |
|
| 79 |
Step 7 |
| 80 |
: Call the _partial_ template from your _base_ template: |
| 81 |
|
| 82 |
```go-html-template {file="layouts/baseof.html" copy=true} |
| 83 |
<head> |
| 84 |
{{ partial "js.html" . }} |
| 85 |
</head> |
| 86 |
``` |
| 87 |
|
| 88 |
## Options |
| 89 |
|
| 90 |
The `js.Babel` function accepts an options map. |
| 91 |
|
| 92 |
`compact` |
| 93 |
: (`bool`) Whether to remove optional newlines and whitespace. Enabled when `minified` is `true`. Default is `false`. |
| 94 |
|
| 95 |
`config` |
| 96 |
: (`string`) The path to the Babel configuration file. By default, Hugo searches the root of the project directory and any modules for `babel.config.js`, `babel.config.mjs`, and `babel.config.cjs` in that order. Use this option only to point to a configuration file with a custom name or one located in a custom subdirectory. |
| 97 |
|
| 98 |
`minified` |
| 99 |
: (`bool`) Whether to minify transpiled code. Enables the `compact` option. Default is `false`. |
| 100 |
|
| 101 |
`noBabelrc` |
| 102 |
: (`bool`) Whether to ignore `.babelrc` and `.babelignore` files. Default is `false`. |
| 103 |
|
| 104 |
`noComments` |
| 105 |
: (`bool`) Whether to remove comments. Default is `false`. |
| 106 |
|
| 107 |
`sourceMap` |
| 108 |
: (`string`) Whether to generate source maps, one of `external`, `inline`, or `none`. Default is `none`. |
| 109 |
|
| 110 |
`verbose` |
| 111 |
: (`bool`) Whether to enable verbose logging. Default is `false`. |
| 112 |
|
| 113 |
<!-- |
| 114 |
In the above, technically "none" is not one of the enumerated sourceMap |
| 115 |
values but it has the same effect and is easier to document than an empty string. |
| 116 |
--> |
| 117 |
|
| 118 |
[Babel]: https://babeljs.io/ |
| 119 |
[Node.js]: https://nodejs.org/en/download |