Babel.md - hugo - [fork] hugo port for 9front
 (HTM) git clone https://git.drkhsh.at/hugo.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Submodules
 (DIR) README
 (DIR) LICENSE
       ---
       Babel.md (2762B)
       ---
            1 ---
            2 title: js.Babel
            3 description: Compile the given JavaScript resource with 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 ---
           12 
           13 ```go-html-template
           14 {{ with resources.Get "js/main.js" }}
           15   {{ $opts := dict
           16     "minified" hugo.IsProduction
           17     "noComments" hugo.IsProduction
           18     "sourceMap" (cond hugo.IsProduction "none" "external")
           19   }}
           20   {{ with . | js.Babel $opts }}
           21     {{ if hugo.IsProduction }}
           22       {{ with . | fingerprint }}
           23         <script src="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}" crossorigin="anonymous"></script>
           24       {{ end }}
           25     {{ else }}
           26       <script src="{{ .RelPermalink }}"></script>
           27     {{ end }}
           28   {{ end }}
           29 {{ end }}
           30 ```
           31 
           32 ## Setup
           33 
           34 ### Step 1
           35 
           36 Install [Node.js](https://nodejs.org/en/download)
           37 
           38 ### Step 2
           39 
           40 Install the required Node.js packages in the root of your project.
           41 
           42 ```sh
           43 npm install --save-dev @babel/core @babel/cli
           44 ```
           45 
           46 ### Step 3
           47 
           48 Add the babel executable to Hugo's `security.exec.allow` list in your site configuration:
           49 
           50 {{< code-toggle file=hugo >}}
           51 [security.exec]
           52   allow = ['^(dart-)?sass(-embedded)?$', '^go$', '^npx$', '^postcss$', '^babel$']
           53 {{< /code-toggle >}}
           54 
           55 ## Configuration
           56 
           57 We add the main project's `node_modules` to `NODE_PATH` when running Babel and similar tools. There are some known [issues](https://github.com/babel/babel/issues/5618) with Babel in this area, so if you have a `babel.config.js` living in a Hugo Module (and not in the project itself), we recommend using `require` to load the presets/plugins, e.g.:
           58 
           59 ```js
           60 module.exports = {
           61   presets: [
           62     [
           63       require("@babel/preset-env"),
           64       {
           65         useBuiltIns: "entry",
           66         corejs: 3,
           67       },
           68     ],
           69   ],
           70 };
           71 ```
           72 
           73 ## Options
           74 
           75 compact
           76 : (`bool`) Whether to remove optional newlines and whitespace. Enabled when `minified` is `true`. Default is `false`
           77 
           78 config
           79 : (`string`) Path to the Babel configuration file. Hugo will, by default, look for a `babel.config.js` file in the root of your project. See&nbsp;[details](https://babeljs.io/docs/en/configuration).
           80 
           81 minified
           82 : (`bool`) Whether to minify the compiled code. Enables the `compact` option. Default is `false`.
           83 
           84 noBabelrc
           85 : (`string`) Whether to ignore `.babelrc` and `.babelignore` files. Default is `false`.
           86 
           87 noComments
           88 : (`bool`) Whether to remove comments. Default is `false`.
           89 
           90 sourceMap
           91 : (`string`) Whether to generate source maps, one of `external`, `inline`, or `none`. Default is `none`.
           92 
           93 verbose
           94 : (`bool`) Whether to enable verbose logging. Default is `false`
           95 
           96 <!--
           97 In the above, technically "none" is not one of the enumerated sourceMap
           98 values but it has the same effect and is easier to document than an empty string.
           99 -->