mirror of
https://github.com/less/less.js.git
synced 2026-01-22 05:37:56 -05:00
- Basically, compares assigned vars to vars in localStorage. If they match (and all the other cache criteria pass), then loads from cache
43 lines
1.6 KiB
JavaScript
43 lines
1.6 KiB
JavaScript
// Cache system is a bit outdated and could do with work
|
|
|
|
module.exports = function(window, options, logger) {
|
|
var cache = null;
|
|
if (options.env !== 'development') {
|
|
try {
|
|
cache = (typeof window.localStorage === 'undefined') ? null : window.localStorage;
|
|
} catch (_) {}
|
|
}
|
|
return {
|
|
setCSS: function(path, lastModified, modifyVars, styles) {
|
|
if (cache) {
|
|
logger.info('saving ' + path + ' to cache.');
|
|
try {
|
|
cache.setItem(path, styles);
|
|
cache.setItem(path + ':timestamp', lastModified);
|
|
if (modifyVars) {
|
|
cache.setItem(path + ':vars', JSON.stringify(modifyVars));
|
|
}
|
|
} catch(e) {
|
|
//TODO - could do with adding more robust error handling
|
|
logger.error('failed to save "' + path + '" to local storage for caching.');
|
|
}
|
|
}
|
|
},
|
|
getCSS: function(path, webInfo, modifyVars) {
|
|
var css = cache && cache.getItem(path),
|
|
timestamp = cache && cache.getItem(path + ':timestamp'),
|
|
vars = cache && cache.getItem(path + ':vars');
|
|
|
|
modifyVars = modifyVars || {};
|
|
|
|
if (timestamp && webInfo.lastModified &&
|
|
(new Date(webInfo.lastModified).valueOf() ===
|
|
new Date(timestamp).valueOf()) &&
|
|
(!modifyVars && !vars || JSON.stringify(modifyVars) === vars)) {
|
|
// Use local copy
|
|
return css;
|
|
}
|
|
}
|
|
};
|
|
};
|