Skip to content
This repository has been archived by the owner on Oct 27, 2020. It is now read-only.

feat: add overrideDependencies option (options.overrideDependencies) #15

Open
wants to merge 2 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 1 addition & 14 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
{
"presets": [
[
"env",
{
"useBuiltIns": true,
"targets": {
"node": 4.3
},
"exclude": [
"transform-async-to-generator",
"transform-regenerator"
]
}
],
[
"env",
{
Expand Down Expand Up @@ -45,4 +32,4 @@
]
}
}
}
}
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<h2 align="center">Options</h2>

|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<String>}`|none|Provide different dependencies for the modules. This override the default dependencies.|

<h2 align="center">Examples</h2>

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

<h2 align="center">Hints</h2>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

L55+

overrideDependencies

...


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.

<h2 align="center">Maintainers</h2>

<table>
Expand Down
4 changes: 2 additions & 2 deletions example/.babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{
"useBuiltIns": true,
"targets": {
"node": 4.3
"node": "4.3"
},
"exclude": [
"transform-async-to-generator",
Expand All @@ -32,4 +32,4 @@
]
}
}
}
}
21 changes: 12 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand All @@ -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;
Expand All @@ -69,14 +70,15 @@ 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}`);
const cacheFile = path.join(cacheDirectory, `${hash}.json`);
data.remainingRequest = remainingRequest;
data.cacheIdentifier = cacheIdentifier;
data.cacheFile = cacheFile;
data.overrideDependencies = overrideDependencies;
fs.readFile(cacheFile, 'utf-8', (readFileErr, content) => {
if (readFileErr) {
callback();
Expand All @@ -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);
Expand Down