mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-21 21:35:28 +02:00
Merge commit 'a024bc7d76fcc5e49e8210f9b0896db9ef21861a'
This commit is contained in:
@@ -1,17 +1,18 @@
|
||||
---
|
||||
title: js.Build
|
||||
description: Bundles, transpiles, tree shakes, and minifies JavaScript resources.
|
||||
weight: 30
|
||||
description: Bundle, transpile, tree shake, and minify JavaScript resources.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: []
|
||||
related:
|
||||
- functions/js/Batch
|
||||
- functions/js/Babel
|
||||
- functions/resources/Fingerprint
|
||||
- functions/resources/Minify
|
||||
returnType: resource.Resource
|
||||
signatures: ['js.Build [OPTIONS] RESOURCE']
|
||||
weight: 10
|
||||
toc: true
|
||||
---
|
||||
|
||||
@@ -27,31 +28,37 @@ The `js.Build` function uses the [evanw/esbuild] package to:
|
||||
|
||||
```go-html-template
|
||||
{{ with resources.Get "js/main.js" }}
|
||||
{{ if hugo.IsDevelopment }}
|
||||
{{ with . | js.Build }}
|
||||
{{ $opts := dict
|
||||
"minify" hugo.IsProduction
|
||||
"sourceMap" (cond hugo.IsProduction "" "external")
|
||||
"targetPath" "js/main.js"
|
||||
}}
|
||||
{{ with . | js.Build $opts }}
|
||||
{{ if hugo.IsProduction }}
|
||||
{{ with . | fingerprint }}
|
||||
<script src="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}" crossorigin="anonymous"></script>
|
||||
{{ end }}
|
||||
{{ else }}
|
||||
<script src="{{ .RelPermalink }}"></script>
|
||||
{{ end }}
|
||||
{{ else }}
|
||||
{{ $opts := dict "minify" true }}
|
||||
{{ with . | js.Build $opts | fingerprint }}
|
||||
<script src="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}" crossorigin="anonymous"></script>
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
## Options
|
||||
|
||||
targetPath
|
||||
: (`string`) If not set, the source path will be used as the base target path.
|
||||
###### targetPath
|
||||
|
||||
(`string`) If not set, the source path will be used as the base target path.
|
||||
Note that the target path's extension may change if the target MIME type is different, e.g. when the source is TypeScript.
|
||||
|
||||
format
|
||||
: (`string`) The output format. One of: `iife`, `cjs`, `esm`. Default is `iife`, a self-executing function, suitable for inclusion as a `<script>` tag.
|
||||
###### format
|
||||
|
||||
(`string`) The output format. One of: `iife`, `cjs`, `esm`. Default is `iife`, a self-executing function, suitable for inclusion as a `<script>` tag.
|
||||
|
||||
{{% include "./_common/options.md" %}}
|
||||
|
||||
### Import JS code from the assets directory
|
||||
## Import JS code from the assets directory
|
||||
|
||||
`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:
|
||||
|
||||
@@ -67,7 +74,7 @@ import { hello3 } from 'my/module/hello3';
|
||||
|
||||
Will resolve to `hello3.{js,ts,tsx,jsx}` inside `assets/my/module`.
|
||||
|
||||
Any imports starting with `.` is resolved relative to the current file:
|
||||
Any imports starting with `.` are resolved relative to the current file:
|
||||
|
||||
```js
|
||||
import { hello4 } from './lib';
|
||||
|
Reference in New Issue
Block a user