publicDir

  • 类型: false | object
  • 默认值:
{
  /** Directory to serve as static assets */
  name: 'public',
  /** Whether copy files from the publicDir into the distDir on build */
  copyOnBuild: true,
}

默认情况下,Rsbuild 会将 public 目录作为静态资源服务的文件夹,该目录中的文件可在 / 路径下访问。

需要注意的是:

  • 引入 public 中的资源永远应该使用根绝对路径。例如,public/icon.png 应该在源码中被引用为 /icon.png
  • 在生产环境构建过程中,该目录中的文件将会被拷贝到 dist 目录下,应注意不要和其他产物文件名冲突。(此功能可通过将 copyOnBuild 设置为 false 禁用)

Boolean 类型

通过将 publicDir 设置成 false 可禁用静态资源服务能力:

export default {
  server: {
    publicDir: false,
  },
};

Object 类型

publicDir 的值为 object 类型时,Rsbuild 会根据当前配置与默认配置进行合并。

{
  /** Directory to serve as static assets
   * @default 'public'
   */
  name?: string,
  /** Whether copy files from the publicDir into the distDir on build
   * @default true
   */
  copyOnBuild?: boolean,
}

需要注意的是,设置 copyOnBuild 为 false 会导致生产环境预览时无法访问对应的静态资源文件。

export default {
  server: {
    publicDir: {
      name: 'static',
      copyOnBuild: false,
    },
  },
};