UNPKG

react-inline-for-rix-loader

Version:

for developing webpack rix-loader, folk from https://github.com/martinandert/react-inline

80 lines (52 loc) 1.83 kB
'use strict'; var _classCallCheck = require('babel-runtime/helpers/class-call-check')['default']; var _Object$keys = require('babel-runtime/core-js/object/keys')['default']; var _interopRequireDefault = require('babel-runtime/helpers/interop-require-default')['default']; exports.__esModule = true; var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs); var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _mkdirp = require('mkdirp'); var _mkdirp2 = _interopRequireDefault(_mkdirp); var hasOwnProperty = Object.prototype.hasOwnProperty; function remove(filePath) { if (_fs2['default'].existsSync(filePath)) { _fs2['default'].unlinkSync(filePath); } } function load(filePath) { if (!_fs2['default'].existsSync(filePath)) { _mkdirp2['default'].sync(_path2['default'].dirname(filePath)); store({}, filePath); return {}; } var data = _fs2['default'].readFileSync(filePath, { encoding: 'utf8' }); return JSON.parse(data); } function store(data, filePath) { _fs2['default'].writeFileSync(filePath, JSON.stringify(data)); } var DiskCache = (function () { function DiskCache(name, options) { _classCallCheck(this, DiskCache); this.filePath = _path2['default'].resolve(_path2['default'].join(options.cacheDir, name + '.json')); this.fetch.bind(this); this.clear.bind(this); } DiskCache.prototype.fetch = function fetch(key, miss) { var cache = load(this.filePath); if (hasOwnProperty.call(cache, key)) { return cache[key]; } cache[key] = miss(_Object$keys(cache)); store(cache, this.filePath); return cache[key]; }; DiskCache.prototype.clear = function clear() { remove(this.filePath); }; return DiskCache; })(); exports['default'] = DiskCache; module.exports = exports['default'];