HUGO
Menu
GitHub 90640 stars Mastodon

js.Babel

Returns a resource created by transpiling the given JavaScript resource using Babel.
Syntax
js.Babel [OPTIONS] RESOURCE
Returns
resource.Resource
Alias
babel

Usage

The js.Babel function returns a resource created by transpiling the given JavaScript resource using Babel. Babel converts modern JavaScript syntax to syntax supported by the target environments set in your Babel configuration file.

By default, Hugo searches the root of the project directory and of each module for babel.config.js, babel.config.mjs, and babel.config.cjs, in that order. To use a configuration file with a different name or location, set the config option.

Requirements

To use this function, install Node.js, then install Babel’s core compiler and its command-line interface in the root of your project:

npm install --save-dev @babel/core @babel/cli

Install any Babel presets and plugins that you use the same way.

Then add the babel executable to the security.exec.allow list in your project configuration:

security:
  exec:
    allow:
    - ^(dart-)?sass$
    - ^go$
    - ^git$
    - ^node$
    - ^postcss$
    - ^babel$
[security]
  [security.exec]
    allow = ['^(dart-)?sass$', '^go$', '^git$', '^node$', '^postcss$', '^babel$']
{
   "security": {
      "exec": {
         "allow": [
            "^(dart-)?sass$",
            "^go$",
            "^git$",
            "^node$",
            "^postcss$",
            "^babel$"
         ]
      }
   }
}

File system access

Hugo runs the Node.js tool used by this function with a permission model that limits the files the tool can read to the paths in security.node.permissions.allowRead. By default, this is the project directory. If the tool needs to read files from other locations, such as configuration files, plugins, or imported files in the node_modules directory of a parent directory in a monorepo, add those paths to the list.

Options

The js.Babel function accepts an options map.

compact
(bool) Whether to remove optional newlines and whitespace. Enabled when minified is true. Default is false.
config
(string) The path to the Babel configuration file. Hugo resolves a relative path against the root of the project directory and of each module. The build fails if Hugo cannot find the file. Use this option only for a configuration file with a custom name, or one in a custom subdirectory.
minified
(bool) Whether to minify the transpiled code. Enables the compact option. Default is false.
noBabelrc
(bool) Whether to ignore .babelrc and .babelignore files. Default is false.
noComments
(bool) Whether to remove comments. Default is false.
sourceMap
(string) The type of source map to generate, one of external, inline, or none. Default is none. For external source maps, Hugo publishes the source map next to the generated JavaScript file, adding .map to the file name.
verbose
(bool) Whether to enable verbose logging. Default is false.

Examples

This example uses Babel’s environment preset to transpile JavaScript for Google Chrome version 79 or later.

Step 1
Install the @babel/preset-env preset, which transpiles modern JavaScript based on your target environments:
npm install --save-dev @babel/preset-env
Step 2
Create a Babel configuration file in the root of your project:
babel.config.mjs
export default {
  presets: [
    [
      '@babel/preset-env',
      {
        targets: {
          chrome: '79'
        }
      }
    ]
  ]
};
Step 3
Create a partial template to process the assets/js/main.js file:
layouts/_partials/js.html
{{ with resources.Get "js/main.js" }}
  {{ $opts := dict
    "minified" (not hugo.IsDevelopment)
    "noComments" (not hugo.IsDevelopment)
    "sourceMap" (cond hugo.IsDevelopment "inline" "none")
  }}
  {{ with . | js.Babel $opts }}
    {{ if hugo.IsDevelopment }}
      <script src="{{ .RelPermalink }}"></script>
    {{ else }}
      {{ with . | fingerprint }}
        <script src="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}" crossorigin="anonymous"></script>
      {{ end }}
    {{ end }}
  {{ end }}
{{ end }}
Step 4
Call the partial template from your base template:
layouts/baseof.html
<head>
  {{ partial "js.html" . }}
</head>

When you run the hugo server command, Babel adds an inline source map and keeps the comments. When you run the hugo build command, Babel minifies the code and removes the comments.