Socket
Socket
Sign inDemoInstall

cache-loader

Package Overview
Dependencies
291
Maintainers
9
Versions
17
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.2.5 to 2.0.0

22

CHANGELOG.md

@@ -5,2 +5,24 @@ # Change Log

<a name="2.0.0"></a>
# [2.0.0](https://github.com/webpack-contrib/cache-loader/compare/v1.2.5...v2.0.0) (2018-12-21)
### Chores
* drop support for `webpack` < 4 ([#51](https://github.com/webpack-contrib/cache-loader/issues/51)) ([2e76d3f](https://github.com/webpack-contrib/cache-loader/commit/2e76d3f))
* drop support for node < 6 ([#50](https://github.com/webpack-contrib/cache-loader/issues/50)) ([b8225cd](https://github.com/webpack-contrib/cache-loader/commit/b8225cd))
### Features
* add `cacheContext` option ([#49](https://github.com/webpack-contrib/cache-loader/issues/49)) ([22d0173](https://github.com/webpack-contrib/cache-loader/commit/22d0173))
### BREAKING CHANGES
* drop support for `webpack` < 4
* drop support for node < 6
<a name="1.2.5"></a>

@@ -7,0 +29,0 @@ ## [1.2.5](https://github.com/webpack-contrib/cache-loader/compare/v1.2.4...v1.2.5) (2018-10-31)

2

dist/cjs.js

@@ -1,3 +0,3 @@

'use strict';
"use strict";
module.exports = require('./index');

@@ -1,2 +0,2 @@

'use strict';
"use strict";

@@ -6,28 +6,32 @@ Object.defineProperty(exports, "__esModule", {

});
exports.default = loader;
exports.pitch = pitch;
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
/* eslint-disable
import/order
*/
var fs = require('fs');
var path = require('path');
var async = require('neo-async');
var crypto = require('crypto');
var mkdirp = require('mkdirp');
const fs = require('fs');
var _require = require('loader-utils'),
getOptions = _require.getOptions;
const path = require('path');
var validateOptions = require('schema-utils');
const async = require('neo-async');
var pkg = require('../package.json');
const crypto = require('crypto');
var env = process.env.NODE_ENV || 'development';
const mkdirp = require('mkdirp');
var schema = require('./options.json');
const {
getOptions
} = require('loader-utils');
var defaults = {
const validateOptions = require('schema-utils');
const pkg = require('../package.json');
const env = process.env.NODE_ENV || 'development';
const schema = require('./options.json');
const defaults = {
cacheContext: '',
cacheDirectory: path.resolve('.cache-loader'),

@@ -40,30 +44,35 @@ cacheIdentifier: `cache-loader:${pkg.version} ${env}`,

function loader() {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
function pathWithCacheContext(cacheContext, originalPath) {
if (!cacheContext) {
return originalPath;
}
var options = Object.assign({}, defaults, getOptions(this));
if (originalPath.includes(cacheContext)) {
return originalPath.split('!').map(subPath => path.relative(cacheContext, subPath)).join('!');
}
return originalPath.split('!').map(subPath => path.resolve(cacheContext, subPath)).join('!');
}
function loader(...args) {
const options = Object.assign({}, defaults, getOptions(this));
validateOptions(schema, options, 'Cache Loader');
const {
write: writeFn
} = options;
const callback = this.async();
const {
data
} = this;
const dependencies = this.getDependencies().concat(this.loaders.map(l => l.path));
const contextDependencies = this.getContextDependencies(); // Should the file get cached?
var writeFn = options.write;
let cache = true; // this.fs can be undefined
// e.g when using the thread-loader
// fallback to the fs module
const FS = this.fs || fs;
var callback = this.async();
var data = this.data;
var dependencies = this.getDependencies().concat(this.loaders.map(function (l) {
return l.path;
}));
var contextDependencies = this.getContextDependencies();
// Should the file get cached?
var cache = true;
// this.fs can be undefined
// e.g when using the thread-loader
// fallback to the fs module
var FS = this.fs || fs;
var toDepDetails = function toDepDetails(dep, mapCallback) {
FS.stat(dep, function (err, stats) {
const toDepDetails = (dep, mapCallback) => {
FS.stat(dep, (err, stats) => {
if (err) {

@@ -74,3 +83,3 @@ mapCallback(err);

var mtime = stats.mtime.getTime();
const mtime = stats.mtime.getTime();

@@ -85,3 +94,3 @@ if (mtime / 1000 >= Math.floor(data.startTime / 1000)) {

mapCallback(null, {
path: dep,
path: pathWithCacheContext(options.cacheContext, dep),
mtime

@@ -92,20 +101,14 @@ });

async.parallel([function (cb) {
return async.mapLimit(dependencies, 20, toDepDetails, cb);
}, function (cb) {
return async.mapLimit(contextDependencies, 20, toDepDetails, cb);
}], function (err, taskResults) {
async.parallel([cb => async.mapLimit(dependencies, 20, toDepDetails, cb), cb => async.mapLimit(contextDependencies, 20, toDepDetails, cb)], (err, taskResults) => {
if (err) {
callback.apply(undefined, [null].concat(args));
callback(null, ...args);
return;
}
if (!cache) {
callback.apply(undefined, [null].concat(args));
callback(null, ...args);
return;
}
var _taskResults = _slicedToArray(taskResults, 2),
deps = _taskResults[0],
contextDeps = _taskResults[1];
const [deps, contextDeps] = taskResults;
writeFn(data.cacheKey, {

@@ -116,5 +119,5 @@ remainingRequest: data.remainingRequest,

result: args
}, function () {
}, () => {
// ignore errors here
callback.apply(undefined, [null].concat(args));
callback(null, ...args);
});

@@ -125,18 +128,14 @@ });

function pitch(remainingRequest, prevRequest, dataInput) {
var _this = this;
var options = Object.assign({}, defaults, getOptions(this));
const options = Object.assign({}, defaults, getOptions(this));
validateOptions(schema, options, 'Cache Loader (Pitch)');
var readFn = options.read,
cacheKeyFn = options.cacheKey;
var callback = this.async();
var data = dataInput;
data.remainingRequest = remainingRequest;
data.cacheKey = cacheKeyFn(options, remainingRequest);
readFn(data.cacheKey, function (readErr, cacheData) {
const {
read: readFn,
cacheContext,
cacheKey: cacheKeyFn
} = options;
const callback = this.async();
const data = dataInput;
data.remainingRequest = pathWithCacheContext(cacheContext, remainingRequest);
data.cacheKey = cacheKeyFn(options, data.remainingRequest);
readFn(data.cacheKey, (readErr, cacheData) => {
if (readErr) {

@@ -146,3 +145,4 @@ callback();

}
if (cacheData.remainingRequest !== remainingRequest) {
if (cacheData.remainingRequest !== data.remainingRequest) {
// in case of a hash conflict

@@ -152,5 +152,6 @@ callback();

}
var FS = _this.fs || fs;
async.each(cacheData.dependencies.concat(cacheData.contextDependencies), function (dep, eachCallback) {
FS.stat(dep.path, function (statErr, stats) {
const FS = this.fs || fs;
async.each(cacheData.dependencies.concat(cacheData.contextDependencies), (dep, eachCallback) => {
FS.stat(dep.path, (statErr, stats) => {
if (statErr) {

@@ -160,2 +161,3 @@ eachCallback(statErr);

}
if (stats.mtime.getTime() !== dep.mtime) {

@@ -165,5 +167,6 @@ eachCallback(true);

}
eachCallback();
});
}, function (err) {
}, err => {
if (err) {

@@ -174,9 +177,6 @@ data.startTime = Date.now();

}
cacheData.dependencies.forEach(function (dep) {
return _this.addDependency(dep.path);
});
cacheData.contextDependencies.forEach(function (dep) {
return _this.addContextDependency(dep.path);
});
callback.apply(undefined, [null].concat(_toConsumableArray(cacheData.result)));
cacheData.dependencies.forEach(dep => this.addDependency(pathWithCacheContext(cacheContext, dep.path)));
cacheData.contextDependencies.forEach(dep => this.addContextDependency(pathWithCacheContext(cacheContext, dep.path)));
callback(null, ...cacheData.result);
});

@@ -190,7 +190,7 @@ });

var directories = new Set();
const directories = new Set();
function write(key, data, callback) {
var dirname = path.dirname(key);
var content = JSON.stringify(data);
const dirname = path.dirname(key);
const content = JSON.stringify(data);

@@ -201,3 +201,3 @@ if (directories.has(dirname)) {

} else {
mkdirp(dirname, function (mkdirErr) {
mkdirp(dirname, mkdirErr => {
if (mkdirErr) {

@@ -209,3 +209,2 @@ callback(mkdirErr);

directories.add(dirname);
fs.writeFile(key, content, 'utf-8', callback);

@@ -217,3 +216,3 @@ });

function read(key, callback) {
fs.readFile(key, 'utf-8', function (err, content) {
fs.readFile(key, 'utf-8', (err, content) => {
if (err) {

@@ -225,3 +224,3 @@ callback(err);

try {
var data = JSON.parse(content);
const data = JSON.parse(content);
callback(null, data);

@@ -235,11 +234,8 @@ } catch (e) {

function cacheKey(options, request) {
var cacheIdentifier = options.cacheIdentifier,
cacheDirectory = options.cacheDirectory;
var hash = digest(`${cacheIdentifier}\n${request}`);
const {
cacheIdentifier,
cacheDirectory
} = options;
const hash = digest(`${cacheIdentifier}\n${request}`);
return path.join(cacheDirectory, `${hash}.json`);
}
exports.default = loader;
exports.pitch = pitch;
}
{
"type": "object",
"properties": {
"cacheContext": {
"type": "string"
},
"cacheKey": {

@@ -5,0 +8,0 @@ "instanceof": "Function"

{
"name": "cache-loader",
"version": "1.2.5",
"version": "2.0.0",
"description": "Caches the result of following loaders on disk.",
"license": "MIT",
"repository": "webpack-contrib/cache-loader",
"author": "Tobias Koppers @sokra",
"license": "MIT",
"homepage": "https://github.com/webpack-contrib/cache-loader",
"bugs": "https://github.com/webpack-contrib/cache-loader/issues",
"main": "dist/cjs.js",
"files": [
"dist"
],
"engines": {
"node": ">= 6.9.0"
},
"scripts": {

@@ -15,6 +18,7 @@ "start": "npm run build -- -w",

"clean": "del-cli dist",
"commitlint": "commitlint",
"commitmsg": "commitlint -e $GIT_PARAMS",
"lint": "eslint --cache src test",
"lint-staged": "lint-staged",
"prebuild": "npm run clean",
"prepare": "npm run build",
"prepublish": "npm run build",
"release": "standard-version",

@@ -25,9 +29,14 @@ "security": "npm audit",

"test:coverage": "jest --collectCoverageFrom='src/**/*.js' --coverage",
"travis:coverage": "npm run test:coverage -- --runInBand",
"travis:lint": "npm run lint && npm run security",
"travis:test": "npm run test -- --runInBand",
"appveyor:test": "npm run test",
"defaults": "webpack-defaults",
"webpack-defaults": "webpack-defaults"
"ci:lint": "npm run lint && npm run security",
"ci:test": "npm run test -- --runInBand",
"ci:coverage": "npm run test:coverage -- --runInBand",
"ci:lint:commits": "commitlint --from=origin/master --to=${CIRCLE_SHA1}",
"defaults": "webpack-defaults"
},
"files": [
"dist"
],
"peerDependencies": {
"webpack": "^4.0.0"
},
"dependencies": {

@@ -37,32 +46,55 @@ "loader-utils": "^1.1.0",

"neo-async": "^2.5.0",
"schema-utils": "^0.4.2"
"schema-utils": "^1.0.0"
},
"devDependencies": {
"babel-cli": "^6.0.0",
"babel-jest": "^21.0.0",
"babel-plugin-transform-object-rest-spread": "^6.0.0",
"babel-polyfill": "^6.0.0",
"babel-preset-env": "^1.6.0",
"@babel/cli": "^7.2.3",
"@babel/core": "^7.2.2",
"@babel/polyfill": "^7.2.3",
"@babel/preset-env": "^7.2.3",
"@commitlint/cli": "^7.2.1",
"@commitlint/config-conventional": "^7.1.2",
"@webpack-contrib/defaults": "^3.0.0",
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^23.6.0",
"babel-loader": "^8.0.4",
"cross-env": "^5.0.0",
"del": "^3.0.0",
"del-cli": "^1.0.0",
"eslint": "^4.0.0",
"eslint": "^5.10.0",
"eslint-config-webpack": "^1.0.0",
"eslint-plugin-import": "^2.0.0",
"jest": "^21.0.0",
"lint-staged": "^5.0.0",
"eslint-plugin-prettier": "^3.0.0",
"husky": "^1.2.1",
"jest": "^23.6.0",
"lint-staged": "^8.1.0",
"memory-fs": "^0.4.1",
"normalize-path": "^3.0.0",
"pre-commit": "^1.0.0",
"prettier": "^1.15.2",
"standard-version": "^4.0.0",
"webpack": "^3.0.0",
"webpack-defaults": "^1.6.0"
"webpack": "^4.27.1",
"webpack-cli": "^3.1.2"
},
"engines": {
"node": ">= 4.8 < 5.0.0 || >= 5.10"
"keywords": [
"webpack"
],
"babel": {
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "6.9.0"
},
"useBuiltIns": "usage"
}
]
]
},
"peerDependencies": {
"webpack": "^2.0.0 || ^3.0.0 || ^4.0.0"
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"repository": "https://github.com/webpack-contrib/cache-loader.git",
"bugs": "https://github.com/webpack-contrib/cache-loader/issues",
"homepage": "https://github.com/webpack-contrib/cache-loader",
"pre-commit": "lint-staged",
"lint-staged": {

@@ -73,3 +105,8 @@ "*.js": [

]
},
"commitlint": {
"extends": [
"@commitlint/config-conventional"
]
}
}

@@ -0,1 +1,7 @@

<div align="center">
<a href="https://webpack.js.org/">
<img width="200" height="200" src="https://cdn.rawgit.com/webpack/media/e7485eb2/logo/icon-square-big.svg">
</a>
</div>
[![npm][npm]][npm-url]

@@ -7,22 +13,20 @@ [![node][node]][node-url]

[![chat][chat]][chat-url]
[![size][size]][size-url]
<div align="center">
<a href="https://webpack.js.org/">
<img width="200" height="200" src="https://cdn.rawgit.com/webpack/media/e7485eb2/logo/icon-square-big.svg">
</a>
<h1>Cache Loader</h1>
<p>Caches the result of following loaders on disk (default) or in the database</p>
</div>
# cache-loader
<h2 align="center">Install</h2>
The `cache-loader` allow to Caches the result of following loaders on disk (default) or in the database.
```bash
## Getting Started
To begin, you'll need to install `cache-loader`:
```console
npm install --save-dev cache-loader
```
<h2 align="center">Usage</h2>
Add this loader in front of other (expensive) loaders to cache the result on disk.
**webpack.config.js**
```js

@@ -34,11 +38,8 @@ module.exports = {

test: /\.ext$/,
use: [
'cache-loader',
...loaders
],
include: path.resolve('src')
}
]
}
}
use: ['cache-loader', ...loaders],
include: path.resolve('src'),
},
],
},
};
```

@@ -48,15 +49,19 @@

<h2 align="center">Options</h2>
## Options
|Name|Type|Default|Description|
|:--:|:--:|:-----:|:----------|
|**`cacheKey`**|`{Function(options, request) -> {String}}`|`undefined`|Allows you to override default cache key generator|
|**`cacheDirectory`**|`{String}`|`path.resolve('.cache-loader')`|Provide a cache directory where cache items should be stored (used for default read/write implementation)|
|**`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 (used for default read/write implementation)|
|**`write`**|`{Function(cacheKey, data, callback) -> {void}}`|`undefined`|Allows you to override default write cache data to file (e.g. Redis, memcached)|
|**`read`**|`{Function(cacheKey, callback) -> {void}}`|`undefined`|Allows you to override default read cache data from file|
| Name | Type | Default | Description |
| :-------------------: | :----------------------------------------------: | :---------------------------------------------: | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **`cacheContext`** | `{String}` | `undefined` | Allows you to override the default cache context in order to generate the cache relatively to a path. By default it will use absolute paths |
| **`cacheKey`** | `{Function(options, request) -> {String}}` | `undefined` | Allows you to override default cache key generator |
| **`cacheDirectory`** | `{String}` | `path.resolve('.cache-loader')` | Provide a cache directory where cache items should be stored (used for default read/write implementation) |
| **`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 (used for default read/write implementation) |
| **`write`** | `{Function(cacheKey, data, callback) -> {void}}` | `undefined` | Allows you to override default write cache data to file (e.g. Redis, memcached) |
| **`read`** | `{Function(cacheKey, callback) -> {void}}` | `undefined` | Allows you to override default read cache data from file |
<h2 align="center">Examples</h2>
## Examples
### Basic
**webpack.config.js**
```js

@@ -68,16 +73,14 @@ module.exports = {

test: /\.js$/,
use: [
'cache-loader',
'babel-loader'
],
include: path.resolve('src')
}
]
}
}
use: ['cache-loader', 'babel-loader'],
include: path.resolve('src'),
},
],
},
};
```
### `Database Integration`
### Database Integration
**webpack.config.js**
```js

@@ -95,3 +98,6 @@ // Or different database client - memcached, mongodb, ...

function digest(str) {
return crypto.createHash('md5').update(str).digest('hex');
return crypto
.createHash('md5')
.update(str)
.digest('hex');
}

@@ -104,3 +110,2 @@

// Read data from database and parse them

@@ -126,3 +131,2 @@ function read(key, callback) {

// Write data to database under cacheKey

@@ -145,74 +149,36 @@ function write(key, data, callback) {

write,
}
},
},
'babel-loader'
'babel-loader',
],
include: path.resolve('src')
}
]
}
}
include: path.resolve('src'),
},
],
},
};
```
<h2 align="center">Maintainers</h2>
## Contributing
<table>
<tbody>
<tr>
<td align="center">
<a href="https://github.com/sokra">
<img width="150" height="150" src="https://github.com/sokra.png?size=150">
</br>
Tobias Koppers
</a>
</td>
<td align="center">
<a href="https://github.com/bebraw">
<img width="150" height="150" src="https://github.com/bebraw.png?v=3&s=150">
</br>
Juho Vepsäläinen
</a>
</td>
<td align="center">
<a href="https://github.com/d3viant0ne">
<img width="150" height="150" src="https://github.com/d3viant0ne.png?v=3&s=150">
</br>
Joshua Wiens
</a>
</td>
<td align="center">
<a href="https://github.com/michael-ciniawsky">
<img width="150" height="150" src="https://github.com/michael-ciniawsky.png?v=3&s=150">
</br>
Michael Ciniawsky
</a>
</td>
<td align="center">
<a href="https://github.com/evilebottnawi">
<img width="150" height="150" src="https://github.com/evilebottnawi.png?v=3&s=150">
</br>
Alexander Krasnoyarov
</a>
</td>
</tr>
<tbody>
</table>
Please take a moment to read our contributing guidelines if you haven't yet done so.
[CONTRIBUTING](./.github/CONTRIBUTING.md)
## License
[MIT](./LICENSE)
[npm]: https://img.shields.io/npm/v/cache-loader.svg
[npm-url]: https://npmjs.com/package/cache-loader
[node]: https://img.shields.io/node/v/cache-loader.svg
[node-url]: https://nodejs.org
[deps]: https://david-dm.org/webpack-contrib/cache-loader.svg
[deps-url]: https://david-dm.org/webpack-contrib/cache-loader
[chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg
[chat-url]: https://gitter.im/webpack/webpack
[test]: https://img.shields.io/travis/webpack-contrib/cache-loader.svg
[test-url]: https://travis-ci.org/webpack-contrib/cache-loader
[tests]: https://img.shields.io/circleci/project/github/webpack-contrib/cache-loader.svg
[tests-url]: https://circleci.com/gh/webpack-contrib/cache-loader
[cover]: https://codecov.io/gh/webpack-contrib/cache-loader/branch/master/graph/badge.svg
[cover-url]: https://codecov.io/gh/webpack-contrib/cache-loader
[chat]: https://badges.gitter.im/webpack/webpack.svg
[chat-url]: https://gitter.im/webpack/webpack
[size]: https://packagephobia.now.sh/badge?p=cache-loader
[size-url]: https://packagephobia.now.sh/result?p=cache-loader
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc