diff --git a/.babelrc b/.babelrc index c1e910c..e7e4dbd 100644 --- a/.babelrc +++ b/.babelrc @@ -1,18 +1,5 @@ { "presets": [ - [ - "env", - { - "useBuiltIns": true, - "targets": { - "node": 4.3 - }, - "exclude": [ - "transform-async-to-generator", - "transform-regenerator" - ] - } - ], [ "env", { @@ -45,4 +32,4 @@ ] } } -} \ No newline at end of file +} diff --git a/README.md b/README.md index 00528bf..e134d92 100644 --- a/README.md +++ b/README.md @@ -43,12 +43,15 @@ module.exports = { > ⚠️ Note that there is an overhead for saving the reading and saving the cache file, so only use this loader to cache expensive loaders. +The loader checks timestamp values of all dependencies of the cached modules. Only if modification timestamp hasn't changed the cached result is used. +

Options

|Name|Type|Default|Description| |:--:|:--:|:-----:|:----------| |**`cacheDirectory`**|`{String}`|`path.resolve('.cache-loader')`|Provide a cache directory where cache items should be stored| |**`cacheIdentifier`**|`{String}`|`cache-loader:{version} {process.env.NODE_ENV}`|Provide an invalidation identifier which is used to generate the hashes. You can use it for extra dependencies of loaders.| +|**`overrideDependencies`**|`{Array}`|none|Provide different dependencies for the modules. This override the default dependencies.|

Examples

@@ -95,6 +98,33 @@ module.exports = { } ``` +

Hints

+ +For extra performance you could override the dependencies of modules for rarly changing modules: + +```js +module.exports = { + module: { + rules: [ + { + test: /\.js$/, + use: [ + { + loader: 'cache-loader', + options: { + overrideDependencies: [path.resolve('yarn.lock')] + } + } + ], + include: path.resolve('node_modules') + } + ] + } +} +``` + +With this config timestamps from files are no longer checked. Instead only the timestamp of the yarn lockfile is checked, which should be ok for normal yarn usage. +

Maintainers

diff --git a/example/.babelrc b/example/.babelrc index fa5c8e6..e7e4dbd 100644 --- a/example/.babelrc +++ b/example/.babelrc @@ -5,7 +5,7 @@ { "useBuiltIns": true, "targets": { - "node": 4.3 + "node": "4.3" }, "exclude": [ "transform-async-to-generator", @@ -32,4 +32,4 @@ ] } } -} \ No newline at end of file +} diff --git a/src/index.js b/src/index.js index 061f61e..9fd600e 100644 --- a/src/index.js +++ b/src/index.js @@ -12,8 +12,9 @@ const ENV = process.env.NODE_ENV || 'development'; function loader(...args) { const callback = this.async(); const { data } = this; - const dependencies = this.getDependencies().concat(this.loaders.map(l => l.path)); - const contextDependencies = this.getContextDependencies(); + const { fileExists, cacheFile, remainingRequest, cacheIdentifier, overrideDependencies } = data; + const dependencies = overrideDependencies || this.getDependencies().concat(this.loaders.map(l => l.path)); + const contextDependencies = overrideDependencies ? [] : this.getContextDependencies(); const toDepDetails = (dep, mapCallback) => { fs.stat(dep, (err, stats) => { if (err) { @@ -36,9 +37,9 @@ function loader(...args) { } const [deps, contextDeps] = taskResults; const writeCacheFile = () => { - fs.writeFile(data.cacheFile, JSON.stringify({ - remainingRequest: data.remainingRequest, - cacheIdentifier: data.cacheIdentifier, + fs.writeFile(cacheFile, JSON.stringify({ + remainingRequest, + cacheIdentifier, dependencies: deps, contextDependencies: contextDeps, result: args, @@ -47,11 +48,11 @@ function loader(...args) { callback(null, ...args); }); }; - if (data.fileExists) { + if (fileExists) { // for performance skip creating directory writeCacheFile(); } else { - mkdirp(path.dirname(data.cacheFile), (mkdirErr) => { + mkdirp(path.dirname(cacheFile), (mkdirErr) => { if (mkdirErr) { callback(null, ...args); return; @@ -69,7 +70,7 @@ function pitch(remainingRequest, prevRequest, dataInput) { cacheIdentifier: `cache-loader:${pkgVersion} ${ENV}`, }; const options = Object.assign({}, defaultOptions, loaderOptions); - const { cacheIdentifier, cacheDirectory } = options; + const { cacheIdentifier, cacheDirectory, overrideDependencies } = options; const data = dataInput; const callback = this.async(); const hash = digest(`${cacheIdentifier}\n${remainingRequest}`); @@ -77,6 +78,7 @@ function pitch(remainingRequest, prevRequest, dataInput) { data.remainingRequest = remainingRequest; data.cacheIdentifier = cacheIdentifier; data.cacheFile = cacheFile; + data.overrideDependencies = overrideDependencies; fs.readFile(cacheFile, 'utf-8', (readFileErr, content) => { if (readFileErr) { callback(); @@ -95,7 +97,8 @@ function pitch(remainingRequest, prevRequest, dataInput) { callback(); return; } - async.each(cacheData.dependencies.concat(cacheData.contextDependencies), (dep, eachCallback) => { + const dependencies = cacheData.dependencies.concat(cacheData.contextDependencies); + async.each(dependencies, (dep, eachCallback) => { fs.stat(dep.path, (statErr, stats) => { if (statErr) { eachCallback(statErr);