remove env from all environment calls except the ones that actually need it

This commit is contained in:
Luke Page
2014-09-21 16:46:44 +01:00
parent a53be80014
commit fb6c879cc4
9 changed files with 43 additions and 43 deletions

View File

@@ -23,11 +23,11 @@ function getXMLHttpRequest() {
return {
// make generic but overriddable
warn: function warn(env, msg) {
warn: function warn(msg) {
console.warn(msg);
},
// make generic but overriddable
getPath: function getPath(env, filename) {
getPath: function getPath(filename) {
var j = filename.lastIndexOf('/');
if (j < 0) {
j = filename.lastIndexOf('\\');
@@ -38,7 +38,7 @@ return {
return filename.slice(0, j + 1);
},
// make generic but overriddable
isPathAbsolute: function isPathAbsolute(env, filename) {
isPathAbsolute: function isPathAbsolute(filename) {
return /^(?:[a-z-]+:|\/|\\)/i.test(filename);
},
alwaysMakePathsAbsolute: function alwaysMakePathsAbsolute() {
@@ -168,17 +168,19 @@ return {
handleResponse(xhr, callback, errback);
}
},
loadFile: function loadFile(env, filename, currentDirectory, callback) {
if (currentDirectory && !this.isPathAbsolute(env, filename)) {
loadFile: function loadFile(filename, currentDirectory, options, callback) {
if (currentDirectory && !this.isPathAbsolute(filename)) {
filename = currentDirectory + filename;
}
options = options || {};
// sheet may be set to the stylesheet for the initial load or a collection of properties including
// some env variables for imports
var hrefParts = this.extractUrlParts(filename, window.location.href);
var href = hrefParts.url;
if (env.useFileCache && fileCache[href]) {
if (options.useFileCache && fileCache[href]) {
try {
var lessText = fileCache[href];
callback(null, lessText, href, { lastModified: new Date() });
@@ -188,7 +190,7 @@ return {
return;
}
this.doXHR(href, env.mime, function doXHRCallback(data, lastModified) {
this.doXHR(href, options.mime, function doXHRCallback(data, lastModified) {
// per file cache
fileCache[href] = data;

View File

@@ -377,10 +377,10 @@ function loadStyleSheet(sheet, callback, reload, remaining, modifyVars) {
instanceOptions.useFileCache = true;
}
less.environment.loadFile(instanceOptions, sheet.href, null, function loadInitialFileCallback(e, data, path, webInfo) {
less.environment.loadFile(sheet.href, null, instanceOptions, function loadInitialFileCallback(e, data, path, webInfo) {
var newFileInfo = {
currentDirectory: less.environment.getPath(instanceOptions, path),
currentDirectory: less.environment.getPath(path),
filename: path,
rootFilename: path,
relativeUrls: instanceOptions.relativeUrls};

View File

@@ -5,25 +5,25 @@ var path = require('path'),
isUrlRe = /^(?:https?:)?\/\//i;
module.exports = {
warn: function(env, msg) {
warn: function(msg) {
console.warn(msg);
},
encodeBase64: function encodeBase64(env, str) {
encodeBase64: function encodeBase64(str) {
return new Buffer(str).toString('base64');
},
supportsDataURI: function(env) {
supportsDataURI: function() {
return true;
},
mimeLookup: function (env, filename) {
mimeLookup: function (filename) {
return require('mime').lookup(filename);
},
charsetLookup: function (env, mime) {
charsetLookup: function (mime) {
return require('mime').charsets.lookup(mime);
},
readFileSync: function (filename) {
return require("fs").readFileSync(filename);
},
getPath: function (env, filename) {
getPath: function (filename) {
var j = filename.lastIndexOf('/');
if (j < 0) {
j = filename.lastIndexOf('\\');
@@ -33,10 +33,10 @@ module.exports = {
}
return filename.slice(0, j + 1);
},
isPathAbsolute: function(env, filename) {
isPathAbsolute: function(filename) {
return (/^(?:[a-z-]+:|\/|\\)/i).test(filename);
},
getAbsolutePath: function getAbsolutePath(env, filename) {
getAbsolutePath: function getAbsolutePath(filename) {
return require('path').resolve(filename);
},
getSourceMapGenerator: function getSourceMapGenerator() {
@@ -128,11 +128,13 @@ module.exports = {
returner.url = returner.fileUrl + (urlParts[5] || "");
return returner;
},
loadFile: function(env, filename, currentDirectory, callback) {
loadFile: function(filename, currentDirectory, options, callback) {
var fullFilename,
data,
isUrl = isUrlRe.test( filename );
options = options || {};
if (isUrl || isUrlRe.test(currentDirectory)) {
if (request === undefined) {
try { request = require('request'); }
@@ -151,7 +153,7 @@ module.exports = {
urlStr = urlObj.format();
}
request.get({uri: urlStr, strictSSL: !env.insecure }, function (error, res, body) {
request.get({uri: urlStr, strictSSL: !options.insecure }, function (error, res, body) {
if (error) {
callback({ type: 'File', message: "resource '" + urlStr + "' gave this Error:\n "+ error +"\n" });
}
@@ -160,7 +162,7 @@ module.exports = {
return;
}
if (!body) {
this.warn( env, 'Warning: Empty body (HTTP '+ res.statusCode + ') returned by "' + urlStr +'"');
this.warn('Warning: Empty body (HTTP '+ res.statusCode + ') returned by "' + urlStr +'"');
}
fullFilename = urlStr;
callback(null, body, fullFilename);
@@ -168,10 +170,10 @@ module.exports = {
} else {
var paths = [currentDirectory];
if (env.paths) paths.push.apply(paths, env.paths);
if (options.paths) paths.push.apply(paths, options.paths);
if (paths.indexOf('.') === -1) paths.push('.');
if (env.syncImport) {
if (options.syncImport) {
for (var i = 0; i < paths.length; i++) {
try {
fullFilename = path.join(paths[i], filename);

View File

@@ -1,42 +1,39 @@
module.exports = {
/**
* Warns the user about something
* @param {Object} env - the environment or options object
* @param {String} msg - the message about the warning
*/
warn: function(env, msg) {
warn: function(msg) {
},
/**
* gets the path from the filename, e.g. "http://wwe.files.com/ha/ha.less" would return
* "http://wwe.files.com/ha/"
* If the filename is a file e.g. "file.less" it should return the empty string ""
* @param {Object} env - the environment or options object
* @param {String} filename - the filename to extract the path.
* @returns {String}
*/
getPath: function (env, filename) {
getPath: function (filename) {
},
/**
* Returns whether the path is absolute, e.g. "/file.less" = true, "file.less" = false
* @param {Object} env - the environment or options object
* @param {String} filename - the filename
* @returns {Boolean}
*/
isPathAbsolute: function(env, filename) {
isPathAbsolute: function(filename) {
},
/**
* Loads a file for an import aynscronously (or syncronously)
* @param {Object} env - the environment or options object
* @param {String} filename - the filename
* @param {String} currentDirectory - the current directory we are in
* @param {Object} options - the environment or options object
* @param {Function} callback - a function to callback when finished,
* taking the format callback(error, contents, fullfilename, reserved)
* where error is { type: {string}, message: {string} }, contents is {string} and fullfilename is {string}
* for reserved, see less-browser/index.js which uses this argument for cache information
* @returns {Boolean}
*/
loadFile: function(env, filename, currentDirectory, callback) {
loadFile: function(filename, currentDirectory, options, callback) {
},
supportsDataURI: function(env) {
supportsDataURI: function() {
}
};

View File

@@ -5,7 +5,7 @@ module.exports = function(environment) {
functionRegistry.add("data-uri", function(mimetypeNode, filePathNode) {
if (!environment.supportsDataURI(this.env)) {
if (!environment.supportsDataURI()) {
return new URL(filePathNode || mimetypeNode, this.index, this.currentFileInfo).eval(this.env);
}
@@ -36,10 +36,10 @@ module.exports = function(environment) {
// detect the mimetype if not given
if (arguments.length < 2) {
mimetype = environment.mimeLookup(this.env, filePath);
mimetype = environment.mimeLookup(filePath);
// use base 64 unless it's an ASCII or UTF-8 format
var charset = environment.charsetLookup(this.env, mimetype);
var charset = environment.charsetLookup(mimetype);
useBase64 = ['US-ASCII', 'UTF-8'].indexOf(charset) < 0;
if (useBase64) { mimetype += ';base64'; }
}

View File

@@ -71,7 +71,7 @@ module.exports = function(environment) {
if (useBase64) {
try {
returner = environment.encodeBase64(this.env, returner);
returner = environment.encodeBase64(returner);
} catch(e) {
useBase64 = false;
}

View File

@@ -1,7 +1,6 @@
var contexts = require("./contexts"),
Parser = require('./parser/parser');
// TODO - why does environment need env passed everywhere?
// Now we have one import manager per parse, can we move things from env to the import manager
// and then move the import manager onto env (if required there - if not, keep seperate)
@@ -17,10 +16,10 @@ module.exports = function(environment) {
this.error = null;
this.env = env;
};
ImportManager.prototype.getAbsolutePath = function(env, filename) {
ImportManager.prototype.getAbsolutePath = function(filename) {
// proxy needed for "DebugInfo"
// I hope one day we can remove this function
return environment.getAbsolutePath(env, filename);
return environment.getAbsolutePath(filename);
};
ImportManager.prototype.push = function (path, currentFileInfo, importOptions, callback) {
var parserImports = this;
@@ -45,7 +44,7 @@ module.exports = function(environment) {
rootFilename: currentFileInfo.rootFilename
};
environment.loadFile(this.env, path, currentFileInfo.currentDirectory, function loadFileCallback(e, contents, resolvedFilename) {
environment.loadFile(path, currentFileInfo.currentDirectory, this.env, function loadFileCallback(e, contents, resolvedFilename) {
if (e) {
fileParsedFunc(e);
return;
@@ -59,10 +58,10 @@ module.exports = function(environment) {
// then rootpath should become 'less/module/nav/'
// - If path of imported file is '../mixins.less' and rootpath is 'less/',
// then rootpath should become 'less/../'
newFileInfo.currentDirectory = environment.getPath(parserImports.env, resolvedFilename);
newFileInfo.currentDirectory = environment.getPath(resolvedFilename);
if(newFileInfo.relativeUrls) {
newFileInfo.rootpath = environment.join((parserImports.env.rootpath || ""), environment.pathDiff(newFileInfo.currentDirectory, newFileInfo.entryPath));
if (!environment.isPathAbsolute(parserImports.env, newFileInfo.rootpath) && environment.alwaysMakePathsAbsolute()) {
if (!environment.isPathAbsolute(newFileInfo.rootpath) && environment.alwaysMakePathsAbsolute()) {
newFileInfo.rootpath = environment.join(newFileInfo.entryPath, newFileInfo.rootpath);
}
}

View File

@@ -73,7 +73,7 @@ var Parser = function Parser(env, imports) {
function getDebugInfo(index) {
var filename = env.currentFileInfo.filename;
filename = imports.getAbsolutePath(env, filename);
filename = imports.getAbsolutePath(filename);
return {
lineNumber: utils.getLocation(index, parserInput.getInput()).line + 1,

View File

@@ -128,7 +128,7 @@ module.exports = function (environment) {
if (!this._sourceMapFileInline) {
this.sourceMap = sourceMapContent;
} else {
sourceMapURL = "data:application/json;base64," + environment.encodeBase64(null, sourceMapContent);
sourceMapURL = "data:application/json;base64," + environment.encodeBase64(sourceMapContent);
}
if (sourceMapURL) {