js.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/cliInstall 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 whenminifiedistrue. Default isfalse. 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 thecompactoption. Default isfalse. noBabelrc- (
bool) Whether to ignore.babelrcand.babelignorefiles. Default isfalse. noComments- (
bool) Whether to remove comments. Default isfalse. sourceMap- (
string) The type of source map to generate, one ofexternal,inline, ornone. Default isnone. Forexternalsource maps, Hugo publishes the source map next to the generated JavaScript file, adding.mapto the file name. verbose- (
bool) Whether to enable verbose logging. Default isfalse.
Examples
This example uses Babel’s environment preset to transpile JavaScript for Google Chrome version 79 or later.
- Step 1
- Install the
@babel/preset-envpreset, 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.jsfile: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.
