output.cleanDistPath

  • Type:
type CleanDistPath = boolean | 'auto' | CleanDistPathObject;
  • Default: 'auto'

Whether to clean up all files under the output directory before the build starts (the output directory defaults to dist).

Default behavior

The default value of output.cleanDistPath is 'auto'. If the output directory is a subdir of the project root path, Rsbuild will automatically clean all files under the output directory.

When output.distPath.root is an external directory, or equals to the project root directory, cleanDistPath is not enabled by default, to avoid accidentally deleting files from other directories.

export default {
  output: {
    distPath: {
      root: '../../some-dir',
    },
  },
};

Forced switch

You can set cleanDistPath to true to force it to be enabled, or set it to false to force it to be disabled.

export default {
  output: {
    cleanDistPath: true,
  },
};

Conditional

If you only need to clean files in production mode, but not in development mode, you can configure it as:

export default {
  output: {
    cleanDistPath: process.env.NODE_ENV === 'production',
  },
};

Options

output.cleanDistPath supports configuration as an object to achieve more granular control.

enable

  • Type: boolean | 'auto'
  • Default: 'auto'

Whether to clean up all files under the output directory before the build starts.

export default {
  output: {
    // Equivalent to `cleanDistPath: true`
    cleanDistPath: {
      enable: true,
    },
  },
};

keep

  • Type: RegExp[]
  • Default: undefined

Specify the files to keep in the output directory. If the file's absolute path matches the regular expression in keep, the file will not be removed.

For example, to keep the dist/foo.json file:

export default {
  output: {
    cleanDistPath: {
      keep: [/dist\/foo.json/],
    },
  },
};