mirror of
https://github.com/less/less.js.git
synced 2026-02-15 17:35:39 -05:00
Clean up dependency injection so environment is injected at the top level
This commit is contained in:
@@ -3,9 +3,6 @@
|
||||
//
|
||||
/*global window, document, location */
|
||||
|
||||
var less = require('./non-node-index.js'),
|
||||
options = window.less;
|
||||
|
||||
var logLevel = {
|
||||
debug: 3,
|
||||
info: 2,
|
||||
@@ -13,16 +10,28 @@ var logLevel = {
|
||||
none: 0
|
||||
};
|
||||
|
||||
var less;
|
||||
|
||||
function log(str, level) {
|
||||
if (typeof(console) !== 'undefined' && less.logLevel >= level) {
|
||||
console.log('less: ' + str);
|
||||
}
|
||||
}
|
||||
|
||||
var isFileProtocol = /^(file|chrome(-extension)?|resource|qrc|app):/.test(location.protocol);
|
||||
/*
|
||||
TODO - options is now hidden - we should expose it on the less object, but not have it "as" the less object
|
||||
alternately even have it on environment
|
||||
e.g. less.environment.options.fileAsync = true;
|
||||
is it weird you do
|
||||
less = { fileAsync: true }
|
||||
then access as less.environment.options.fileAsync ?
|
||||
*/
|
||||
|
||||
less.environment = require("./environment/browser.js")(less, isFileProtocol, log, logLevel);
|
||||
window.less = less;
|
||||
var isFileProtocol = /^(file|chrome(-extension)?|resource|qrc|app):/.test(location.protocol),
|
||||
options = window.less,
|
||||
environment = require("./environment/browser.js")(options, isFileProtocol, log, logLevel);
|
||||
|
||||
window.less = less = require('./non-node-index.js')(environment);
|
||||
|
||||
less.env = options.env || (location.hostname == '127.0.0.1' ||
|
||||
location.hostname == '0.0.0.0' ||
|
||||
@@ -46,8 +55,8 @@ less.logLevel = typeof(options.logLevel) != 'undefined' ? options.logLevel : (le
|
||||
// doesn't start loading before the stylesheets are parsed.
|
||||
// Setting this to `true` can result in flickering.
|
||||
//
|
||||
less.async = less.async || false;
|
||||
less.fileAsync = less.fileAsync || false;
|
||||
options.async = options.async || false;
|
||||
options.fileAsync = options.fileAsync || false;
|
||||
|
||||
// Interval between watch polls
|
||||
less.poll = less.poll || (isFileProtocol ? 1000 : 1500);
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
/*global window, XMLHttpRequest */
|
||||
|
||||
module.exports = function(less, isFileProtocol, log, logLevel) {
|
||||
module.exports = function(options, isFileProtocol, log, logLevel) {
|
||||
|
||||
var fileCache = {};
|
||||
|
||||
//TODOS - move log somewhere. pathDiff and doing something similiar in node. use pathDiff in the other browser file for the initial load
|
||||
//TODOS - move log somewhere. pathDiff and doing something similar in node. use pathDiff in the other browser file for the initial load
|
||||
// isFileProtocol is global
|
||||
|
||||
function getXMLHttpRequest() {
|
||||
@@ -135,7 +135,7 @@ return {
|
||||
doXHR: function doXHR(url, type, callback, errback) {
|
||||
|
||||
var xhr = getXMLHttpRequest();
|
||||
var async = isFileProtocol ? less.fileAsync : less.async;
|
||||
var async = isFileProtocol ? options.fileAsync : options.async;
|
||||
|
||||
if (typeof(xhr.overrideMimeType) === 'function') {
|
||||
xhr.overrideMimeType('text/css');
|
||||
@@ -154,7 +154,7 @@ return {
|
||||
}
|
||||
}
|
||||
|
||||
if (isFileProtocol && !less.fileAsync) {
|
||||
if (isFileProtocol && !options.fileAsync) {
|
||||
if (xhr.status === 0 || (xhr.status >= 200 && xhr.status < 300)) {
|
||||
callback(xhr.responseText);
|
||||
} else {
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
module.exports = function(less) {
|
||||
module.exports = function(environment) {
|
||||
var Anonymous = require("../tree/anonymous.js"),
|
||||
URL = require("../tree/url.js"),
|
||||
functionRegistry = require("./function-registry.js");
|
||||
|
||||
functionRegistry.add("data-uri", function(mimetypeNode, filePathNode) {
|
||||
|
||||
if (!less.environment.supportsDataURI(this.env)) {
|
||||
if (!environment.supportsDataURI(this.env)) {
|
||||
return new URL(filePathNode || mimetypeNode, this.currentFileInfo).eval(this.env);
|
||||
}
|
||||
|
||||
@@ -27,19 +27,19 @@ module.exports = function(less) {
|
||||
|
||||
if (this.env.isPathRelative(filePath)) {
|
||||
if (this.currentFileInfo.relativeUrls) {
|
||||
filePath = less.environment.join(this.currentFileInfo.currentDirectory, filePath);
|
||||
filePath = environment.join(this.currentFileInfo.currentDirectory, filePath);
|
||||
} else {
|
||||
filePath = less.environment.join(this.currentFileInfo.entryPath, filePath);
|
||||
filePath = environment.join(this.currentFileInfo.entryPath, filePath);
|
||||
}
|
||||
}
|
||||
|
||||
// detect the mimetype if not given
|
||||
if (arguments.length < 2) {
|
||||
|
||||
mimetype = less.environment.mimeLookup(this.env, filePath);
|
||||
mimetype = environment.mimeLookup(this.env, filePath);
|
||||
|
||||
// use base 64 unless it's an ASCII or UTF-8 format
|
||||
var charset = less.environment.charsetLookup(this.env, mimetype);
|
||||
var charset = environment.charsetLookup(this.env, mimetype);
|
||||
useBase64 = ['US-ASCII', 'UTF-8'].indexOf(charset) < 0;
|
||||
if (useBase64) { mimetype += ';base64'; }
|
||||
}
|
||||
@@ -47,7 +47,7 @@ module.exports = function(less) {
|
||||
useBase64 = /;base64$/.test(mimetype);
|
||||
}
|
||||
|
||||
var buf = less.environment.readFileSync(filePath);
|
||||
var buf = environment.readFileSync(filePath);
|
||||
|
||||
// IE8 cannot handle a data-uri larger than 32KB. If this is exceeded
|
||||
// and the --ieCompat flag is enabled, return a normal url() instead.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
module.exports = function(less) {
|
||||
module.exports = function(environment) {
|
||||
var functions = {
|
||||
functionRegistry: require("./function-registry.js"),
|
||||
functionCaller: require("./function-caller.js")
|
||||
@@ -8,11 +8,11 @@ module.exports = function(less) {
|
||||
require("./default.js");
|
||||
require("./color.js");
|
||||
require("./color-blending.js");
|
||||
require("./data-uri.js")(less);
|
||||
require("./data-uri.js")(environment);
|
||||
require("./math.js");
|
||||
require("./number.js");
|
||||
require("./string.js");
|
||||
require("./svg.js")(less);
|
||||
require("./svg.js")(environment);
|
||||
require("./types.js");
|
||||
|
||||
return functions;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
module.exports = function(less) {
|
||||
module.exports = function(environment) {
|
||||
var Dimension = require("../tree/dimension.js"),
|
||||
Color = require("../tree/color.js"),
|
||||
Anonymous = require("../tree/anonymous.js"),
|
||||
@@ -71,7 +71,7 @@ module.exports = function(less) {
|
||||
|
||||
if (useBase64) {
|
||||
try {
|
||||
returner = less.environment.encodeBase64(this.env, returner);
|
||||
returner = environment.encodeBase64(this.env, returner);
|
||||
} catch(e) {
|
||||
useBase64 = false;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
var PromiseConstructor = typeof Promise === 'undefined' ? require('promise') : Promise;
|
||||
var less = require("./non-node-index.js");
|
||||
var environment = require("./environment/node");
|
||||
var less = require("./non-node-index.js")(environment);
|
||||
|
||||
less.render = function (input, options, callback) {
|
||||
options = options || {};
|
||||
@@ -87,6 +88,4 @@ less.writeError = function (ctx, options) {
|
||||
console.error(less.formatError(ctx, options));
|
||||
};
|
||||
|
||||
less.environment = require("./environment/node");
|
||||
|
||||
module.exports = less;
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
var less = {
|
||||
version: [1, 6, 3],
|
||||
data: require('./data/index.js')
|
||||
module.exports = function(environment) {
|
||||
var less = {
|
||||
version: [2, 0, 0],
|
||||
data: require('./data/index.js'),
|
||||
tree: require('./tree/index.js'),
|
||||
visitor: require('./visitor/index.js'),
|
||||
Parser: require('./parser/parser.js')(environment),
|
||||
functions: require('./functions/index.js')(environment),
|
||||
contexts: require("./contexts.js"),
|
||||
environment: environment
|
||||
};
|
||||
|
||||
return less;
|
||||
};
|
||||
|
||||
less.tree = require('./tree/index.js');
|
||||
less.visitor = require('./visitor/index.js');
|
||||
less.Parser = (require('./parser/parser.js'))(less);
|
||||
less.functions = require('./functions/index.js')(less);
|
||||
less.contexts = require("./contexts.js");
|
||||
|
||||
less.SourceMapOutput = require('./source-map-output.js')(less);
|
||||
|
||||
module.exports = less;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
var contexts = require("../contexts.js");
|
||||
module.exports = function(less, env, Parser) {
|
||||
module.exports = function(environment, env, Parser) {
|
||||
var rootFilename = env && env.filename;
|
||||
return {
|
||||
paths: env.paths || [], // Search paths, when importing
|
||||
@@ -32,7 +32,7 @@ module.exports = function(less, env, Parser) {
|
||||
rootFilename: currentFileInfo.rootFilename
|
||||
};
|
||||
|
||||
less.environment.loadFile(env, path, currentFileInfo.currentDirectory, function loadFileCallback(e, contents, resolvedFilename) {
|
||||
environment.loadFile(env, path, currentFileInfo.currentDirectory, function loadFileCallback(e, contents, resolvedFilename) {
|
||||
if (e) {
|
||||
fileParsedFunc(e);
|
||||
return;
|
||||
@@ -46,11 +46,11 @@ module.exports = function(less, env, Parser) {
|
||||
// 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 = less.environment.getPath(env, resolvedFilename);
|
||||
newFileInfo.currentDirectory = environment.getPath(env, resolvedFilename);
|
||||
if(newFileInfo.relativeUrls) {
|
||||
newFileInfo.rootpath = less.environment.join((env.rootpath || ""), less.environment.pathDiff(newFileInfo.currentDirectory, newFileInfo.entryPath));
|
||||
if (!less.environment.isPathAbsolute(env, newFileInfo.rootpath) && less.environment.alwaysMakePathsAbsolute()) {
|
||||
newFileInfo.rootpath = less.environment.join(newFileInfo.entryPath, newFileInfo.rootpath);
|
||||
newFileInfo.rootpath = environment.join((env.rootpath || ""), environment.pathDiff(newFileInfo.currentDirectory, newFileInfo.entryPath));
|
||||
if (!environment.isPathAbsolute(env, newFileInfo.rootpath) && environment.alwaysMakePathsAbsolute()) {
|
||||
newFileInfo.rootpath = environment.join(newFileInfo.entryPath, newFileInfo.rootpath);
|
||||
}
|
||||
}
|
||||
newFileInfo.filename = resolvedFilename;
|
||||
|
||||
@@ -5,7 +5,8 @@ var LessError = require('../less-error.js'),
|
||||
getImportManager = require("./imports.js"),
|
||||
getParserInput = require("./parser-input.js");
|
||||
|
||||
module.exports = function(less) {
|
||||
module.exports = function(environment) {
|
||||
var SourceMapOutput = require("../source-map-output")(environment);
|
||||
//
|
||||
// less.js - parser
|
||||
//
|
||||
@@ -51,7 +52,7 @@ var Parser = function Parser(env) {
|
||||
}
|
||||
this.env = env;
|
||||
|
||||
var imports = this.imports = getImportManager(less, env, Parser);
|
||||
var imports = this.imports = getImportManager(environment, env, Parser);
|
||||
|
||||
function expect(arg, msg, index) {
|
||||
// some older browsers return typeof 'function' for RegExp
|
||||
@@ -88,7 +89,7 @@ var Parser = function Parser(env) {
|
||||
|
||||
function getDebugInfo(index) {
|
||||
var filename = env.currentFileInfo.filename;
|
||||
filename = less.environment.getAbsolutePath(env, filename);
|
||||
filename = environment.getAbsolutePath(env, filename);
|
||||
|
||||
return {
|
||||
lineNumber: parserInput.getLocation(index).line + 1,
|
||||
@@ -111,8 +112,8 @@ var Parser = function Parser(env) {
|
||||
parse: function (str, callback, additionalData) {
|
||||
var root, error = null, globalVars, modifyVars, preText = "";
|
||||
|
||||
globalVars = (additionalData && additionalData.globalVars) ? less.Parser.serializeVars(additionalData.globalVars) + '\n' : '';
|
||||
modifyVars = (additionalData && additionalData.modifyVars) ? '\n' + less.Parser.serializeVars(additionalData.modifyVars) : '';
|
||||
globalVars = (additionalData && additionalData.globalVars) ? Parser.serializeVars(additionalData.globalVars) + '\n' : '';
|
||||
modifyVars = (additionalData && additionalData.modifyVars) ? '\n' + Parser.serializeVars(additionalData.modifyVars) : '';
|
||||
|
||||
if (globalVars || (additionalData && additionalData.banner)) {
|
||||
preText = ((additionalData && additionalData.banner) ? additionalData.banner : "") + globalVars;
|
||||
@@ -206,7 +207,7 @@ var Parser = function Parser(env) {
|
||||
}
|
||||
|
||||
if (options.sourceMap) {
|
||||
evaldRoot = new less.SourceMapOutput(
|
||||
evaldRoot = new SourceMapOutput(
|
||||
{
|
||||
contentsIgnoredCharsMap: parser.imports.contentsIgnoredChars,
|
||||
writeSourceMap: options.writeSourceMap,
|
||||
@@ -231,7 +232,7 @@ var Parser = function Parser(env) {
|
||||
throw new LessError(parser, e, env);
|
||||
}
|
||||
|
||||
var CleanCSS = less.environment.getCleanCSS();
|
||||
var CleanCSS = environment.getCleanCSS();
|
||||
if (options.cleancss && CleanCSS) {
|
||||
var cleancssOptions = options.cleancssOptions || {};
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
module.exports = function (less) {
|
||||
module.exports = function (environment) {
|
||||
|
||||
var SourceMapOutput = function (options) {
|
||||
this._css = [];
|
||||
@@ -14,7 +14,7 @@ module.exports = function (less) {
|
||||
}
|
||||
this._sourceMapRootpath = options.sourceMapRootpath;
|
||||
this._outputSourceFiles = options.outputSourceFiles;
|
||||
this._sourceMapGeneratorConstructor = less.environment.getSourceMapGenerator();
|
||||
this._sourceMapGeneratorConstructor = environment.getSourceMapGenerator();
|
||||
|
||||
if (this._sourceMapRootpath && this._sourceMapRootpath.charAt(this._sourceMapRootpath.length-1) !== '/') {
|
||||
this._sourceMapRootpath += '/';
|
||||
@@ -127,7 +127,7 @@ module.exports = function (less) {
|
||||
if (this._writeSourceMap) {
|
||||
this._writeSourceMap(sourceMapContent);
|
||||
} else {
|
||||
sourceMapURL = "data:application/json;base64," + less.environment.encodeBase64(sourceMapContent);
|
||||
sourceMapURL = "data:application/json;base64," + environment.encodeBase64(sourceMapContent);
|
||||
}
|
||||
|
||||
if (sourceMapURL) {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
var Node = require("./node.js");
|
||||
var Node = require("./node.js"),
|
||||
FunctionCaller = require("../functions/function-caller.js");
|
||||
//
|
||||
// A function call node.
|
||||
//
|
||||
@@ -30,7 +31,6 @@ Call.prototype.accept = function (visitor) {
|
||||
//
|
||||
Call.prototype.eval = function (env) {
|
||||
var args = this.args.map(function (a) { return a.eval(env); }),
|
||||
FunctionCaller = require("../non-node-index.js").functions.functionCaller, //TODO! Move out
|
||||
result, funcCaller = new FunctionCaller(this.name, env, this.currentFileInfo);
|
||||
|
||||
if (funcCaller.isValid()) { // 1.
|
||||
|
||||
Reference in New Issue
Block a user