remove the old importer interface and move the node code into the common importer

This commit is contained in:
Luke Page
2014-02-22 16:47:59 +00:00
parent 35d58f2392
commit e989b01f51
2 changed files with 44 additions and 59 deletions

View File

@@ -87,6 +87,8 @@ var less = {
}
};
less.Parser.environment = require("./environments/node");
require('./tree/color');
require('./tree/directive');
require('./tree/detached-ruleset');
@@ -117,44 +119,6 @@ require('./tree/unicode-descriptor');
require('./tree/negative');
require('./tree/extend');
require('./tree/ruleset-call');
less.Parser.environment = require("./environments/node");
less.Parser.fileLoader = function (filename, currentFileInfo, callback, env) {
var newFileInfo = {
relativeUrls: env.relativeUrls,
entryPath: currentFileInfo.entryPath,
rootpath: currentFileInfo.rootpath,
rootFilename: currentFileInfo.rootFilename
};
function handleDataAndCallCallback(e, data, resolvedFilename) {
if (e) {
callback(e);
return;
}
// Pass on an updated rootpath if path of imported file is relative and file
// is in a (sub|sup) directory
//
// Examples:
// - If path of imported file is 'module/nav/nav.less' and rootpath is 'less/',
// then rootpath should become 'less/module/nav/'
// - If path of imported file is '../mixins.less' and rootpath is 'less/',
// then rootpath should become 'less/../'
var originalRelativePath = less.Parser.environment.getPath(env, filename);
if(newFileInfo.relativeUrls && !less.Parser.environment.isPathAbsolute(env, filename) && originalRelativePath) {
newFileInfo.rootpath = newFileInfo.rootpath + originalRelativePath; // append (sub|sup) directory path of imported file
}
newFileInfo.currentDirectory = less.Parser.environment.getPath(env, resolvedFilename);
newFileInfo.filename = resolvedFilename;
callback(null, data, resolvedFilename, newFileInfo); //TODO adjust callback
}
less.Parser.environment.loadFile(env, filename, currentFileInfo.currentDirectory, handleDataAndCallCallback);
};
require('./env');
require('./functions');
require('./colors');

View File

@@ -82,31 +82,52 @@ less.Parser = function Parser(env) {
callback(e, root, importedPreviously, fullPath);
};
if (less.Parser.importer) {
less.Parser.importer(path, currentFileInfo, fileParsedFunc, env);
} else {
less.Parser.fileLoader(path, currentFileInfo, function(e, contents, fullPath, newFileInfo) {
if (e) {fileParsedFunc(e); return;}
var newFileInfo = {
relativeUrls: env.relativeUrls,
entryPath: currentFileInfo.entryPath,
rootpath: currentFileInfo.rootpath,
rootFilename: currentFileInfo.rootFilename
};
less.Parser.environment.loadFile(env, path, currentFileInfo.currentDirectory, function (e, contents, resolvedFilename) {
if (e) {
fileParsedFunc(e);
return;
}
var newEnv = new tree.parseEnv(env);
// Pass on an updated rootpath if path of imported file is relative and file
// is in a (sub|sup) directory
//
// Examples:
// - If path of imported file is 'module/nav/nav.less' and rootpath is 'less/',
// then rootpath should become 'less/module/nav/'
// - If path of imported file is '../mixins.less' and rootpath is 'less/',
// then rootpath should become 'less/../'
var originalRelativePath = less.Parser.environment.getPath(env, path);
if(newFileInfo.relativeUrls && !less.Parser.environment.isPathAbsolute(env, path) && originalRelativePath) {
newFileInfo.rootpath = newFileInfo.rootpath + originalRelativePath; // append (sub|sup) directory path of imported file
}
newFileInfo.currentDirectory = less.Parser.environment.getPath(env, resolvedFilename);
newFileInfo.filename = resolvedFilename;
newEnv.currentFileInfo = newFileInfo;
newEnv.processImports = false;
newEnv.contents[fullPath] = contents;
var newEnv = new tree.parseEnv(env);
if (currentFileInfo.reference || importOptions.reference) {
newFileInfo.reference = true;
}
newEnv.currentFileInfo = newFileInfo;
newEnv.processImports = false;
newEnv.contents[resolvedFilename] = contents;
if (importOptions.inline) {
fileParsedFunc(null, contents, fullPath);
} else {
new(less.Parser)(newEnv).parse(contents, function (e, root) {
fileParsedFunc(e, root, fullPath);
});
}
}, env);
}
if (currentFileInfo.reference || importOptions.reference) {
newFileInfo.reference = true;
}
if (importOptions.inline) {
fileParsedFunc(null, contents, resolvedFilename);
} else {
new(less.Parser)(newEnv).parse(contents, function (e, root) {
fileParsedFunc(e, root, resolvedFilename);
});
}
});
}
};