loader-fs-cache
raw JSON → 1.0.3 verified Sat Apr 25 auth: no javascript deprecated
A filesystem cache helper extracted from babel-loader's fs-cache module. Version 1.0.3 is the latest and only release. This package provides a simple promisified file system cache that stores compiled results on disk, checking timestamps and content hashes to invalidate stale entries. It is a standalone extraction from the babel-loader source. Not recommended for direct use; prefer babel-loader or other caching solutions like webpack's persistent cache.
Common errors
error Cannot find module 'find-cache-dir' ↓
cause Missing peer dependency `find-cache-dir`.
fix
npm install find-cache-dir
error TypeError: cache(...) is not a function ↓
cause Using named import instead of default import.
fix
Use
import cache from 'loader-fs-cache' instead of import { cache } from 'loader-fs-cache'. Warnings
deprecated This package is no longer maintained. It was extracted from babel-loader and has been superseded by babel-loader's own caching or other solutions. ↓
fix Use babel-loader's built-in cache or implement your own caching with find-cache-dir and fs.
gotcha The cache key is generated from `identifier` and `config` but not from source hash. If source changes without different identifier/config, old cache may be returned. ↓
fix Ensure you include a hash of the source in the identifier or config to guarantee invalidation.
gotcha The package relies on `find-cache-dir` which may return `undefined` if no package root is found, causing errors. ↓
fix Provide a fallback directory when using `findCacheDir`.
Install
npm install loader-fs-cache yarn add loader-fs-cache pnpm add loader-fs-cache Imports
- default wrong
const cache = require('loader-fs-cache');correctimport cache from 'loader-fs-cache' - cache wrong
import { cache } from 'loader-fs-cache';correctimport cache from 'loader-fs-cache' - type wrong
import type Cache from 'loader-fs-cache';correcttype Cache = typeof import('loader-fs-cache')
Quickstart
import cache from 'loader-fs-cache';
import findCacheDir from 'find-cache-dir';
import { join } from 'path';
const cacheDir = findCacheDir({ name: 'my-repo' }) ?? '.';
const src = 'const x = 1;';
const options = {
directory: cacheDir,
identifier: 'babel',
config: { presets: ['@babel/preset-env'] },
};
cache(options).then((result) => {
if (result && result.src === src) {
console.log('Cache hit');
} else {
console.log('Cache miss');
}
});