HUGO 中文文档

  • 新闻
  • 文档
  • 主题
  • 社区
  • GitHub
gohugoio Star
  • 关于
    • 本节内容
    • 简介
    • 特性
    • 隐私
    • 安全
    • 许可证
  • 安装
    • 本节内容
    • macOS
    • Linux
    • Windows
    • BSD
  • 快速上手
    • 本节内容
    • 快速入门
    • 基本用法
    • 目录结构
    • 配置
    • 配置标记
    • 术语表
    • 配置构建
    • 外部学习资源
  • 快速参考
    • 本节内容
    • 表情符号
    • 函数
    • 方法
    • 页面集合
  • 内容管理
    • 本节内容
    • 组织
    • 页面包
    • 内容格式
    • Front matter (前置 matter)
    • 构建选项
    • 页面资源
    • 图片处理
    • 短代码
    • 相关内容
    • 章节
    • 内容类型
    • 原型
    • 分类法
    • 摘要
    • 链接和交叉引用
    • URL 管理
    • 菜单
    • 评论
    • 多语言
    • Markdown 属性
    • 语法高亮
    • 图表
    • 数学公式
    • 数据源
    • 内容适配器
  • 模板
    • 本节内容
    • 简介
    • 模板类型
    • 查找顺序
    • 基模板
    • 首页模板
    • 单个模板
    • 章节模板
    • 分类模板
    • 术语模板
    • 局部模板
    • 内容视图模板
    • 短代码模板
    • 站点地图模板
    • RSS 模板
    • 404 模板
    • robots.txt 模板
    • 菜单
    • 分页
    • 内嵌模板
    • 自定义输出格式
  • 函数
    • 本节内容
    • css
    • fmt
    • go 模板
    • hugo
    • js
    • lang
    • openapi3
    • os
    • urls
    • 全局
    • 加密
    • 反射
    • 变形
    • 变换
    • 哈希
    • 图像
    • 图表函数
    • 字符串
    • 安全函数
    • 局部模板函数
    • 数学
    • 数据
    • 时间
    • 模板
    • 比较
    • 类型转换
    • 编码
    • 调试
    • 资源
    • 路径
    • 集合
  • 方法
    • 本节内容
    • Duration
    • Menu
    • Page
    • Pager
    • Resource
    • Shortcode
    • Site
    • Taxonomy
    • Time
    • 菜单项
    • 页面
  • 渲染钩子
    • 本节内容
    • 简介
    • 块引用
    • 代码块
    • 标题
    • Images
    • 链接
    • Passthrough
    • 表格
  • Hugo 模块
    • 本节内容
    • 配置 Hugo 模块
    • 使用 Hugo 模块
    • 主题组件
  • Hugo 管道
    • 本节内容
    • 简介
    • 将 Sass 编译为 CSS
    • PostCSS
    • PostProcess
    • JavaScript 构建
    • 资源压缩
    • 连接资产
    • 指纹和 SRI 哈希
    • 从字符串创建资源
    • 从模板创建资源
  • 命令行界面
  • 故障排除
    • 本节内容
    • Audit
    • 日志记录
    • 检查
    • 弃用
    • 性能
    • FAQs
  • 开发者工具
    • 本节内容
    • 编辑器插件
    • 前端
    • Search
    • 迁移
    • 其他项目
  • 托管和部署
    • 本节内容
    • Hugo 部署
    • 使用 Rclone 部署
    • 使用 Rsync 部署
    • 在 21YunBox 上托管
    • 在 AWS Amplify 上托管
    • 在 Cloudflare Pages 上托管
    • 在 Firebase 上托管
    • 在 GitLab Pages 上托管
    • 在 Netlify 上托管
    • 在 Render 上托管
    • 托管在 Azure 静态 Web 应用上
    • 托管在 GitHub Pages 上
    • 托管在 KeyCDN 上
  • 贡献
    • 本节内容
    • 开发
    • 文档
    • 主题
  • 维护
HUGO MODULES

配置 Hugo 模块

此页面描述了模块的配置选项。

模块配置:顶级

hugo.
     
module:
  noProxy: none
  noVendor: ""
  private: _._
  proxy: direct
  replacements: ""
  vendorClosest: false
  workspace: "off"
[module]
  noProxy = 'none'
  noVendor = ''
  private = '_._'
  proxy = 'direct'
  replacements = ''
  vendorClosest = false
  workspace = 'off'
{
   "module": {
      "noProxy": "none",
      "noVendor": "",
      "private": "_._",
      "proxy": "direct",
      "replacements": "",
      "vendorClosest": false,
      "workspace": "off"
   }
}
noProxy
(string) 逗号分隔的 glob 列表,匹配不应使用上面配置的代理的路径。
noVendor
(string) 可选的 Glob 模式,匹配在进行依赖包管理时要跳过的模块路径,例如 “github.com/**”
private
(string) 逗号分隔的 glob 列表,匹配应被视为私有的路径。
proxy
(string) 定义用于下载远程模块的代理服务器。默认为 direct ,表示使用 “git clone” 等命令。
vendorClosest
(bool) 启用后,我们将选择最接近使用它的模块的依赖包版本。默认行为是选择第一个。请注意,给定模块路径仍然只能有一个依赖项,因此一旦它被使用,就不能重新定义。默认为 false 。
workspace
(string) 要使用的 workspace 文件。这将启用 Go 工作区模式。请注意,这也可以通过操作系统环境变量来设置,例如 export HUGO_MODULE_WORKSPACE=/my/hugo.work 。这仅适用于 Go 1.18+。在 Hugo v0.109.0 中,我们将默认值更改为 off ,现在我们根据工作目录解析任何相对的工作文件名称。
replacements
(string) 逗号分隔的模块路径到目录的映射列表,例如 github.com/bep/my-theme -> ../..,github.com/bep/shortcodes -> /some/path 。这主要用于模块的临时本地开发,在这种情况下,您可能希望将其保存为环境变量,例如: env HUGO_MODULE_REPLACEMENTS="github.com/bep/my-theme -> ../.." 。相对路径相对于 themesDir 。允许使用绝对路径。

请注意,上述术语直接映射到 Go Modules 中的对应项。其中一些设置可能更适合设置为操作系统环境变量。例如,要设置要使用的代理服务器:

env HUGO_MODULE_PROXY=https://proxy.example.org hugo

Most of the commands for Hugo Modules require a newer version (>= 1.18) of Go installed (see https://golang.org/dl/) and the relevant VCS client (e.g. Git, see https://git-scm.com/downloads/ ). If you have an “older” site running on Netlify, you may have to set GO_VERSION to 1.19 or newer in your Environment settings.

For more information about Go Modules, see:

  • https://go.dev/wiki/Modules
  • https://blog.golang.org/using-go-modules

模块配置:hugoVersion

如果您的模块需要特定版本的 Hugo 才能工作,您可以在 module 部分中指示这一点,如果使用过旧/过新的版本,用户将收到警告。

hugo.
     
module:
  hugoVersion:
    extended: false
    max: ""
    min: ""
[module]
  [module.hugoVersion]
    extended = false
    max = ''
    min = ''
{
   "module": {
      "hugoVersion": {
         "extended": false,
         "max": "",
         "min": ""
      }
   }
}

以上任何一项都可以省略。

min
(string) 支持的最低 Hugo 版本,例如 0.55.0
max
(string) 支持的最高 Hugo 版本,例如 0.55.0
extended
(bool) 是否需要 Hugo 的扩展版本。

模块配置:导入

hugo.
     
module:
  imports:
  - disable: false
    ignoreConfig: false
    ignoreImports: false
    path: github.com/gohugoio/hugoTestModules1_linux/modh1_2_1v
  - path: my-shortcodes
[module]
  [[module.imports]]
    disable = false
    ignoreConfig = false
    ignoreImports = false
    path = 'github.com/gohugoio/hugoTestModules1_linux/modh1_2_1v'
  [[module.imports]]
    path = 'my-shortcodes'
{
   "module": {
      "imports": [
         {
            "disable": false,
            "ignoreConfig": false,
            "ignoreImports": false,
            "path": "github.com/gohugoio/hugoTestModules1_linux/modh1_2_1v"
         },
         {
            "path": "my-shortcodes"
         }
      ]
   }
}
path
可以是有效的 Go Module 模块路径,例如 github.com/gohugoio/myShortcodes ,也可以是存储在主题文件夹中的模块的目录名称。
ignoreConfig
如果启用,则不会加载任何模块配置文件,例如 hugo.toml 。请注意,这也会停止加载任何传递依赖的模块。
ignoreImports
如果启用,则不会跟踪模块导入。
disable
设置为 true 以禁用模块,同时保留 go.* 文件中的任何版本信息。
noMounts
不挂载此导入中的任何文件夹。
noVendor
从不为该导入创建依赖包(仅允许在主项目中)。

Most of the commands for Hugo Modules require a newer version (>= 1.18) of Go installed (see https://golang.org/dl/) and the relevant VCS client (e.g. Git, see https://git-scm.com/downloads/ ). If you have an “older” site running on Netlify, you may have to set GO_VERSION to 1.19 or newer in your Environment settings.

For more information about Go Modules, see:

  • https://go.dev/wiki/Modules
  • https://blog.golang.org/using-go-modules

模块配置:挂载

Hugo 0.56.0 引入 mounts 配置时,我们小心地保留了现有的 contentDir 、 staticDir 等配置,以确保所有现有站点都能继续工作。但是你不应该同时使用两者:如果你添加了 mounts 部分,你应该移除旧的 contentDir 、 staticDir 等设置。

添加挂载点时,相关目标根目录的默认挂载点将被忽略:请务必显式添加它。

默认挂载点

hugo.
     
module:
  mounts:
  - source: content
    target: content
  - source: static
    target: static
  - source: layouts
    target: layouts
  - source: data
    target: data
  - source: assets
    target: assets
  - source: i18n
    target: i18n
  - source: archetypes
    target: archetypes
[module]
  [[module.mounts]]
    source = 'content'
    target = 'content'
  [[module.mounts]]
    source = 'static'
    target = 'static'
  [[module.mounts]]
    source = 'layouts'
    target = 'layouts'
  [[module.mounts]]
    source = 'data'
    target = 'data'
  [[module.mounts]]
    source = 'assets'
    target = 'assets'
  [[module.mounts]]
    source = 'i18n'
    target = 'i18n'
  [[module.mounts]]
    source = 'archetypes'
    target = 'archetypes'
{
   "module": {
      "mounts": [
         {
            "source": "content",
            "target": "content"
         },
         {
            "source": "static",
            "target": "static"
         },
         {
            "source": "layouts",
            "target": "layouts"
         },
         {
            "source": "data",
            "target": "data"
         },
         {
            "source": "assets",
            "target": "assets"
         },
         {
            "source": "i18n",
            "target": "i18n"
         },
         {
            "source": "archetypes",
            "target": "archetypes"
         }
      ]
   }
}
source
(string) 挂载点的源目录。对于主项目,这可以是项目相关的或绝对的。对于其他模块,它必须是项目相关的。
target
(string) 它应该被挂载到 Hugo 的虚拟文件系统中的位置。它必须以 Hugo 的组件文件夹之一开头: static 、 content 、 layouts 、 data 、 assets 、 i18n 或 archetypes 。例如 content/blog 。
disableWatch
New in v0.128.0
(bool) 是否在此挂载点上禁用监视模式下的监视功能。默认为 false 。
lang
(string) 语言代码,例如 “en”。仅与 content 挂载点相关,以及在多主机模式下与 static 挂载点相关。
includeFiles
(string 或 string slice) 一个或多个 glob 模式,匹配要包含的文件或目录。如果未设置 excludeFiles ,则匹配 includeFiles 的文件将是已挂载的文件。

glob 模式与从 source 根目录开始的文件名匹配,即使在 Windows 上也应该使用 Unix 风格的斜杠, / 匹配挂载根目录, ** 可以用作超级星号来递归匹配所有子目录下的文件,例如 /posts/** .jpg 。

搜索不区分大小写。

excludeFiles
(string 或 string slice) 一个或多个 glob 模式,匹配要排除的文件。

示例

hugo.
     
module:
  mounts:
  - excludeFiles: docs/*
    source: content
    target: content
  - source: node_modules
    target: assets
  - source: assets
    target: assets
[module]
  [[module.mounts]]
    excludeFiles = 'docs/*'
    source = 'content'
    target = 'content'
  [[module.mounts]]
    source = 'node_modules'
    target = 'assets'
  [[module.mounts]]
    source = 'assets'
    target = 'assets'
{
   "module": {
      "mounts": [
         {
            "excludeFiles": "docs/*",
            "source": "content",
            "target": "content"
         },
         {
            "source": "node_modules",
            "target": "assets"
         },
         {
            "source": "assets",
            "target": "assets"
         }
      ]
   }
}

See also

  • 使用 Hugo 模块
  • ByCount
  • Front matter (前置 matter)
  • NextInSection
  • Passthrough

On this page

  • 模块配置:顶级
  • 模块配置:hugoVersion
  • 模块配置:导入
  • 模块配置:挂载
Last updated: January 10, 2025: 添加 gtm 谷歌代码管理 (6220bf5)
Improve this page
By the Hugo Authors
Hugo Logo
  • File an Issue
  • Get Help
  • @GoHugoIO
  • @spf13
  • @bepsays
 

Hugo Sponsors

Your Company?
 

The Hugo logos are copyright © Steve Francia 2013–2025.

The Hugo Gopher is based on an original work by Renée French.

  • 新闻
  • 文档
  • 主题
  • 社区
  • GitHub
  • 关于
  • 安装
  • 快速上手
  • 快速参考
  • 内容管理
  • 模板
  • 函数
  • 方法
  • 渲染钩子
  • Hugo 模块
  • Hugo 管道
  • 命令行界面
  • 故障排除
  • 开发者工具
  • 托管和部署
  • 贡献
  • 维护