publicDir

  • Type:
type PublicDir =
  | false
  | {
      /**
       * The name of the public directory, can be set as a relative path or an absolute path.
       * @default 'public'
       */
      name?: string;
      /**
       * Whether to copy files from the publicDir to the distDir on production build
       * @default true
       */
      copyOnBuild?: boolean;
    };
  • Default:
const defaultValue = {
  name: 'public',
  copyOnBuild: true,
};

By default, Rsbuild will use the public directory as the directory for serving public assets, files in this directory will be served at /.

Note that:

  • You should always reference public assets using root absolute path. For example, public/icon.png should be referenced in source code as /icon.png.
  • During the production build, the files in public directory will be copied to the output directory (default is dist). Please be careful to avoid name conflicts with the output files. When files in the public directory have the same name as outputs, the outputs have higher priority and will overwrite the files with the same name in the public directory. This feature can be disabled by setting copyOnBuild to false.

Boolean Type

The ability to serve public assets can be disabled by setting publicDir to false:

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

Object Type

When the value of publicDir is of object type, Rsbuild will merge the current configuration with the default configuration.

For example, to set the public folder name as static and disable copyOnBuild:

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

Note that setting the value of copyOnBuild to false means that when you run rsbuild preview for a production preview, you will not be able to access the corresponding static resources.

Path Types

The value of name can be set to a relative path or an absolute path. Relative path will be resolved relative to the project root directory.

  • Relative path example:
export default {
  server: {
    publicDir: {
      name: '../some-public',
    },
  },
};
  • Absolute path example:
import path from 'node:path';

export default {
  server: {
    publicDir: {
      name: path.join(__dirname, '../some-public'),
    },
  },
};