Blame view

node_modules/less/lib/less-browser/cache.js 1.69 KB
ce4c83ff   wxy   初始提交
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  // 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 || {};
              vars = vars || "{}"; // if not set, treat as the JSON representation of an empty object
  
              if (timestamp && webInfo.lastModified &&
                  (new Date(webInfo.lastModified).valueOf() ===
                      new Date(timestamp).valueOf()) &&
                  JSON.stringify(modifyVars) === vars) {
                  // Use local copy
                  return css;
              }
          }
      };
  };