Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions lib/less/import-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,21 @@ module.exports = function(environment) {
} else if (importOptions.inline) {
fileParsedFunc(null, contents, resolvedFilename);
} else {
new Parser(newEnv, importManager, newFileInfo).parse(contents, function (e, root) {
fileParsedFunc(e, root, resolvedFilename);
});
var importsCache = require('./imports-cache');
var entry = importsCache.get(resolvedFilename);
if (entry && entry.root) {
fileParsedFunc(null, entry.root, resolvedFilename);
}
else {
new Parser(newEnv, importManager, newFileInfo).parse(contents, function (e, root) {

importsCache.set(resolvedFilename, {
contents: contents,
root: root
});
fileParsedFunc(e, root, resolvedFilename);
});
}
}
};

Expand Down
36 changes: 36 additions & 0 deletions lib/less/imports-cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@


// When the cache is disabled, it will stay empty

var cache = {};
var enabled = false;

module.exports = {

isEnabled: function() {
return enabled;
},
enable: function() {
enabled = true;
},
disable: function() {
enabled = false;
this.clean();
},

set: function (key, value) {
if ( enabled ) {
cache[key] = value;
}
},
get: function (key) {
return cache[key];
},
remove: function(key) {
delete cache[key];
},
clean: function(key) {
cache = {};
}
};

1 change: 1 addition & 0 deletions lib/less/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = function(environment, fileManagers) {
ParseTree: (ParseTree = require('./parse-tree')(SourceMapBuilder)),
ImportManager: (ImportManager = require('./import-manager')(environment)),
render: require("./render")(environment, ParseTree, ImportManager),
invalidateImportsCacheFile: require("./imports-cache").remove,
parse: require("./parse")(environment, ParseTree, ImportManager),
LessError: require('./less-error'),
transformTree: require('./transform-tree'),
Expand Down
18 changes: 18 additions & 0 deletions lib/less/render.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
var PromiseConstructor;

var importsCache = require('./imports-cache');



module.exports = function(environment, ParseTree, ImportManager) {
var render = function (input, options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}

// importsCache can be enabled or disabled per render phase
if ( options.withImportsCaching ) {
importsCache.enable();
// If you invalidate cache yourself, you will have to call
// less.invalidateImportsCacheFile(filePath) everytime a less file is modified
if ( !options.withManualImportsCachingInvalidation ) {
importsCache.clean();
}
}
else {
importsCache.disable();
}


if (!callback) {
if (!PromiseConstructor) {
PromiseConstructor = typeof Promise === 'undefined' ? require('promise') : Promise;
Expand Down