mirror of
https://github.com/less/less.js.git
synced 2026-04-09 03:00:20 -04:00
@@ -38,35 +38,37 @@ if (dumpLineNumbers) {
|
||||
//
|
||||
// Watch mode
|
||||
//
|
||||
less.watch = function () {
|
||||
if (!less.watchMode ){
|
||||
less.env = 'development';
|
||||
initRunningMode();
|
||||
}
|
||||
return this.watchMode = true
|
||||
less.watch = function () {
|
||||
if (!less.watchMode ){
|
||||
less.env = 'development';
|
||||
initRunningMode();
|
||||
}
|
||||
return this.watchMode = true
|
||||
};
|
||||
|
||||
less.unwatch = function () {clearInterval(less.watchTimer); return this.watchMode = false; };
|
||||
|
||||
function initRunningMode(){
|
||||
if (less.env === 'development') {
|
||||
less.optimization = 0;
|
||||
less.watchTimer = setInterval(function () {
|
||||
if (less.watchMode) {
|
||||
loadStyleSheets(function (e, root, _, sheet, env) {
|
||||
if (root) {
|
||||
createCSS(root.toCSS(less), sheet, env.lastModified);
|
||||
}
|
||||
});
|
||||
}
|
||||
}, less.poll);
|
||||
} else {
|
||||
less.optimization = 3;
|
||||
}
|
||||
if (less.env === 'development') {
|
||||
less.optimization = 0;
|
||||
less.watchTimer = setInterval(function () {
|
||||
if (less.watchMode) {
|
||||
loadStyleSheets(function (e, root, _, sheet, env) {
|
||||
if (e) {
|
||||
error(e, sheet.href);
|
||||
} else if (root) {
|
||||
createCSS(root.toCSS(less), sheet, env.lastModified);
|
||||
}
|
||||
});
|
||||
}
|
||||
}, less.poll);
|
||||
} else {
|
||||
less.optimization = 3;
|
||||
}
|
||||
}
|
||||
|
||||
if (/!watch/.test(location.hash)) {
|
||||
less.watch();
|
||||
less.watch();
|
||||
}
|
||||
|
||||
var cache = null;
|
||||
@@ -98,13 +100,17 @@ for (var i = 0; i < links.length; i++) {
|
||||
//
|
||||
var session_cache = '';
|
||||
less.modifyVars = function(record) {
|
||||
var str = session_cache;
|
||||
var str = session_cache;
|
||||
for (name in record) {
|
||||
str += ((name.slice(0,1) === '@')? '' : '@') + name +': '+
|
||||
((record[name].slice(-1) === ';')? record[name] : record[name] +';');
|
||||
}
|
||||
new(less.Parser)(new less.tree.parseEnv(less)).parse(str, function (e, root) {
|
||||
createCSS(root.toCSS(less), less.sheets[less.sheets.length - 1]);
|
||||
if (e) {
|
||||
error(e, "session_cache");
|
||||
} else {
|
||||
createCSS(root.toCSS(less), less.sheets[less.sheets.length - 1]);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@@ -113,6 +119,9 @@ less.refresh = function (reload) {
|
||||
startTime = endTime = new(Date);
|
||||
|
||||
loadStyleSheets(function (e, root, _, sheet, env) {
|
||||
if (e) {
|
||||
return error(e, sheet.href);
|
||||
}
|
||||
if (env.local) {
|
||||
log("loading " + sheet.href + " from cache.");
|
||||
} else {
|
||||
@@ -138,6 +147,9 @@ function loadStyles() {
|
||||
env.filename = document.location.href.replace(/#.*$/, '');
|
||||
|
||||
new(less.Parser)(env).parse(styles[i].innerHTML || '', function (e, cssAST) {
|
||||
if (e) {
|
||||
return error(e, "inline");
|
||||
}
|
||||
var css = cssAST.toCSS(less);
|
||||
var style = styles[i];
|
||||
style.type = 'text/css';
|
||||
@@ -276,21 +288,27 @@ function loadStyleSheet(sheet, callback, reload, remaining) {
|
||||
env.contents[href] = data; // Updating content cache
|
||||
env.paths = [hrefParts.path];
|
||||
env.filename = href;
|
||||
env.rootFilename = env.rootFilename || href;
|
||||
new(less.Parser)(env).parse(data, function (e, root) {
|
||||
if (e) { return error(e, href); }
|
||||
if (e) { return callback(e, null, null, sheet); }
|
||||
try {
|
||||
callback(e, root, data, sheet, { local: false, lastModified: lastModified, remaining: remaining }, href);
|
||||
removeNode(document.getElementById('less-error-message:' + extractId(href)));
|
||||
//TODO - there must be a better way? A generic less-to-css function that can both call error
|
||||
//and removeNode where appropriate
|
||||
//should also add tests
|
||||
if (env.rootFilename === href) {
|
||||
removeNode(document.getElementById('less-error-message:' + extractId(href)));
|
||||
}
|
||||
} catch (e) {
|
||||
error(e, href);
|
||||
callback(e, null, null, sheet);
|
||||
}
|
||||
});
|
||||
} catch (e) {
|
||||
error(e, href);
|
||||
callback(e, null, null, sheet);
|
||||
}
|
||||
}
|
||||
}, function (status, url) {
|
||||
callback({ type: 'File', message: "'" + url + "' wasn't found (" + status + ")\n" });
|
||||
callback({ type: 'File', message: "'" + url + "' wasn't found (" + status + ")" }, null, null, sheet);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -410,11 +428,11 @@ function log(str) {
|
||||
if (less.env == 'development' && typeof(console) !== "undefined") { console.log('less: ' + str) }
|
||||
}
|
||||
|
||||
function error(e, href) {
|
||||
var id = 'less-error-message:' + extractId(href);
|
||||
function error(e, rootHref) {
|
||||
var id = 'less-error-message:' + extractId(rootHref || "");
|
||||
var template = '<li><label>{line}</label><pre class="{class}">{content}</pre></li>';
|
||||
var elem = document.createElement('div'), timer, content, error = [];
|
||||
var filename = e.filename || href;
|
||||
var filename = e.filename || rootHref;
|
||||
var filenameNoPath = filename.match(/([^\/]+(\?.*)?)$/)[1];
|
||||
|
||||
elem.id = id;
|
||||
|
||||
@@ -12,7 +12,8 @@
|
||||
'dumpLineNumbers', // option - whether to dump line numbers
|
||||
'compress', // option - whether to compress
|
||||
'mime', // browser only - mime type for sheet import
|
||||
'entryPath' // browser only, path of entry less file
|
||||
'entryPath', // browser only, path of entry less file
|
||||
'rootFilename' // browser only, href of the entry less file
|
||||
];
|
||||
|
||||
tree.parseEnv = function(options) {
|
||||
|
||||
@@ -188,7 +188,7 @@ less.Parser.importer = function (file, paths, callback, env) {
|
||||
|
||||
if (!pathname) {
|
||||
|
||||
callback({ type: 'File', message: "'" + file + "' wasn't found.\n" });
|
||||
callback({ type: 'File', message: "'" + file + "' wasn't found" });
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -97,7 +97,7 @@ less.Parser = function Parser(env) {
|
||||
that.files[fullPath] = root; // Store the root
|
||||
|
||||
if (e && !that.error) { that.error = e; }
|
||||
|
||||
|
||||
callback(e, root, imported);
|
||||
|
||||
if (that.queue.length === 0) { finish(that.error); } // Call `finish` if we're done importing
|
||||
@@ -383,7 +383,7 @@ less.Parser = function Parser(env) {
|
||||
return function (options, variables) {
|
||||
var importError,
|
||||
evalEnv = new tree.evalEnv(options);
|
||||
|
||||
|
||||
//
|
||||
// Allows setting variables with a hash, so:
|
||||
//
|
||||
@@ -422,11 +422,6 @@ less.Parser = function Parser(env) {
|
||||
throw new(LessError)(e, env);
|
||||
}
|
||||
|
||||
if ((importError = parser.imports.error)) { // Check if there was an error during importing
|
||||
if (importError instanceof LessError) throw importError;
|
||||
else throw new(LessError)(importError, env);
|
||||
}
|
||||
|
||||
if (options.yuicompress && less.mode === 'node') {
|
||||
return require('ycssmin').cssmin(css);
|
||||
} else if (options.compress) {
|
||||
@@ -466,15 +461,24 @@ less.Parser = function Parser(env) {
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
finish = function (e) {
|
||||
e = error || e || parser.imports.error;
|
||||
|
||||
if (e) {
|
||||
if (!(e instanceof LessError)) {
|
||||
e = new(LessError)(e, env);
|
||||
}
|
||||
|
||||
if (this.imports.queue.length > 0) {
|
||||
finish = function (e) {
|
||||
e = error || e;
|
||||
if (e) callback(e);
|
||||
else callback(null, root);
|
||||
};
|
||||
} else {
|
||||
callback(error, root);
|
||||
callback(e);
|
||||
}
|
||||
else {
|
||||
callback(null, root);
|
||||
}
|
||||
};
|
||||
|
||||
if (this.imports.queue.length === 0) {
|
||||
finish();
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ tree.Import = function (path, imports, features, once, index, rootpath) {
|
||||
// Only pre-compile .less files
|
||||
if (! this.css) {
|
||||
imports.push(this.path, function (e, root, imported) {
|
||||
if (e) { e.index = index }
|
||||
if (e) { e.index = index; }
|
||||
if (imported && that.once) that.skip = imported;
|
||||
that.root = root || new(tree.Ruleset)([], []);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user