Home > purgecss > ContentCache
ContentCache class
Cache of the selectors extracted from content files, shared across PurgeCSS instances.
Integrations that run PurgeCSS many times with the same content, e.g. a PostCSS plugin called once per CSS file or a bundler plugin in watch mode, can set the same cache on every PurgeCSS instance they create with PurgeCSS.contentCache. A content file is then only read and extracted again when its modification time, change time, size, inode or the extractor used for it changed. Runs at the same time share the files being read and extracted: each file is extracted once.
A file modified or changed (e.g. its modification time set back) less than 2 seconds before it was read is extracted again on the next run, as a modification within the precision of the file system timestamps would keep the same modification and change times.
The paths given to the methods are resolved from the current working directory.
Globs are still resolved on every run, so new files are picked up and deleted files no longer contribute selectors. The entries of the files that are no longer used stay in the cache until ContentCache.prune(), ContentCache.delete() or ContentCache.clear() is called.
The extractors are called concurrently and in no particular order: they must not depend on the order of the files or on a state kept between files.
Signature:
export declare class ContentCache
Example
const contentCache = new ContentCache()
for (const css of cssFiles) {
const purgeCSS = new PurgeCSS()
purgeCSS.contentCache = contentCache
await purgeCSS.purge({ content: ['src/pages/*.html'], css: [css] })
}
Properties
Property | Modifiers | Type | Description |
|---|---|---|---|
| number | Number of files in the cache |
Methods
Method | Modifiers | Description |
|---|---|---|
Remove all the files from the cache | ||
Remove a file from the cache | ||
Get the selectors of a file if they are still valid. The selectors may still be being extracted by another run. | ||
Check if a file is in the cache, whether its entry is still valid or not | ||
Absolute paths of the files in the cache | ||
Remove the files that are not in the given list, e.g. the files of a previous build that the content globs no longer match | ||
Store the selectors of a file. The entry is removed when its selectors reject, or once they resolve when the file was modified or changed less than 2 seconds before ContentCacheEntry.checkedAt. |
