mirror of
https://github.com/less/less.js.git
synced 2026-02-10 06:55:09 -05:00
First step in implementing syntax for @import options, proposed in https://github.com/cloudhead/less.js/issues/1185#issuecomment-13710620 (steps (1) and (2)). I've implemented the 'multiple' and 'less' options. One could trivially add 'once' and 'css' options as well, if there was need. Proposed "silent" and "inline" options are deferred for future work. I left the existing "@import-multiple" and "@import-once" syntax in place, although the proposal is for this to be deprecated once the new option syntax is in place.
101 lines
3.7 KiB
JavaScript
101 lines
3.7 KiB
JavaScript
(function (tree) {
|
|
tree.importVisitor = function(importer, finish, evalEnv) {
|
|
this._visitor = new tree.visitor(this);
|
|
this._importer = importer;
|
|
this._finish = finish;
|
|
this.env = evalEnv || new tree.evalEnv();
|
|
this.importCount = 0;
|
|
};
|
|
|
|
tree.importVisitor.prototype = {
|
|
isReplacing: true,
|
|
run: function (root) {
|
|
// process the contents
|
|
this._visitor.visit(root);
|
|
|
|
this.isFinished = true;
|
|
|
|
if (this.importCount === 0) {
|
|
this._finish();
|
|
}
|
|
},
|
|
visitImport: function (importNode, visitArgs) {
|
|
var importVisitor = this,
|
|
evaldImportNode;
|
|
|
|
if (!importNode.css) {
|
|
|
|
try {
|
|
evaldImportNode = importNode.evalForImport(this.env);
|
|
} catch(e){
|
|
if (!e.filename) { e.index = importNode.index; e.filename = importNode.currentFileInfo.filename; }
|
|
// attempt to eval properly and treat as css
|
|
importNode.css = true;
|
|
// if that fails, this error will be thrown
|
|
importNode.error = e;
|
|
}
|
|
|
|
if (evaldImportNode && !evaldImportNode.css) {
|
|
importNode = evaldImportNode;
|
|
this.importCount++;
|
|
var env = new tree.evalEnv(this.env, this.env.frames.slice(0));
|
|
this._importer.push(importNode.getPath(), importNode.currentFileInfo, function (e, root, imported) {
|
|
if (e && !e.filename) { e.index = importNode.index; e.filename = importNode.currentFileInfo.filename; }
|
|
if (imported && !importNode.options.multiple) { importNode.skip = imported; }
|
|
|
|
var subFinish = function() {
|
|
importVisitor.importCount--;
|
|
|
|
if (importVisitor.importCount === 0 && importVisitor.isFinished) {
|
|
importVisitor._finish();
|
|
}
|
|
};
|
|
|
|
if (root) {
|
|
importNode.root = root;
|
|
new(tree.importVisitor)(importVisitor._importer, subFinish, env)
|
|
.run(root);
|
|
} else {
|
|
subFinish();
|
|
}
|
|
});
|
|
}
|
|
}
|
|
visitArgs.visitDeeper = false;
|
|
return importNode;
|
|
},
|
|
visitRule: function (ruleNode, visitArgs) {
|
|
visitArgs.visitDeeper = false;
|
|
return ruleNode;
|
|
},
|
|
visitDirective: function (directiveNode, visitArgs) {
|
|
this.env.frames.unshift(directiveNode);
|
|
return directiveNode;
|
|
},
|
|
visitDirectiveOut: function (directiveNode) {
|
|
this.env.frames.shift();
|
|
},
|
|
visitMixinDefinition: function (mixinDefinitionNode, visitArgs) {
|
|
this.env.frames.unshift(mixinDefinitionNode);
|
|
return mixinDefinitionNode;
|
|
},
|
|
visitMixinDefinitionOut: function (mixinDefinitionNode) {
|
|
this.env.frames.shift();
|
|
},
|
|
visitRuleset: function (rulesetNode, visitArgs) {
|
|
this.env.frames.unshift(rulesetNode);
|
|
return rulesetNode;
|
|
},
|
|
visitRulesetOut: function (rulesetNode) {
|
|
this.env.frames.shift();
|
|
},
|
|
visitMedia: function (mediaNode, visitArgs) {
|
|
this.env.frames.unshift(mediaNode.ruleset);
|
|
return mediaNode;
|
|
},
|
|
visitMediaOut: function (mediaNode) {
|
|
this.env.frames.shift();
|
|
}
|
|
};
|
|
|
|
})(require('./tree')); |