Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for #2384 and caching enabled with modifyVars set #2735

Merged
merged 3 commits into from
Jan 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions lib/less-browser/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* used in the browser distributed version of less
* to kick-start less using the browser api
*/
/*global window */
/*global window, document */

// shim Promise if required
require('promise/polyfill.js');
Expand All @@ -15,11 +15,37 @@ var less = module.exports = require("./index")(window, options);

window.less = less;

var css, head, style;

// Always restore page visibility
function resolveOrReject(data) {
if (data.filename) {
console.warn(data);
}
if (!options.async) {
head.removeChild(style);
}
}

if (options.onReady) {
if (/!watch/.test(window.location.hash)) {
less.watch();
}
// Simulate synchronous stylesheet loading by blocking page rendering
if (!options.async) {
css = 'body { display: none !important }';
head = document.head || document.getElementsByTagName('head')[0];
style = document.createElement('style');

style.type = 'text/css';
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}

head.appendChild(style);
}
less.registerStylesheetsImmediately();
less.pageLoadFinished = less.refresh(less.env === 'development');
less.pageLoadFinished = less.refresh(less.env === 'development').then(resolveOrReject, resolveOrReject);
}
15 changes: 11 additions & 4 deletions lib/less-browser/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,32 @@ module.exports = function(window, options, logger) {
} catch (_) {}
}
return {
setCSS: function(path, lastModified, styles) {
setCSS: function(path, lastModified, modifyVars, styles) {
if (cache) {
logger.info('saving ' + path + ' to cache.');
try {
cache.setItem(path, styles);
cache.setItem(path + ':timestamp', lastModified);
if (modifyVars) {
cache.setItem(path + ':vars', JSON.stringify(modifyVars));
}
} catch(e) {
//TODO - could do with adding more robust error handling
logger.error('failed to save "' + path + '" to local storage for caching.');
}
}
},
getCSS: function(path, webInfo) {
getCSS: function(path, webInfo, modifyVars) {
var css = cache && cache.getItem(path),
timestamp = cache && cache.getItem(path + ':timestamp');
timestamp = cache && cache.getItem(path + ':timestamp'),
vars = cache && cache.getItem(path + ':vars');

modifyVars = modifyVars || {};

if (timestamp && webInfo.lastModified &&
(new Date(webInfo.lastModified).valueOf() ===
new Date(timestamp).valueOf())) {
new Date(timestamp).valueOf()) &&
(!modifyVars && !vars || JSON.stringify(modifyVars) === vars)) {
// Use local copy
return css;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/less-browser/file-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module.exports = function(options, logger) {
FileManager.prototype.doXHR = function doXHR(url, type, callback, errback) {

var xhr = getXMLHttpRequest();
var async = options.isFileProtocol ? options.fileAsync : options.async;
var async = options.isFileProtocol ? options.fileAsync : true;

if (typeof xhr.overrideMimeType === 'function') {
xhr.overrideMimeType('text/css');
Expand Down
17 changes: 7 additions & 10 deletions lib/less-browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,13 @@ module.exports = function(window, options) {
if (webInfo) {
webInfo.remaining = remaining;

if (!instanceOptions.modifyVars) {
var css = cache.getCSS(path, webInfo);
if (!reload && css) {
webInfo.local = true;
callback(null, css, data, sheet, webInfo, path);
return;
}
var css = cache.getCSS(path, webInfo, instanceOptions.modifyVars);
if (!reload && css) {
webInfo.local = true;
callback(null, css, data, sheet, webInfo, path);
return;
}

}

//TODO add tests around how this behaves when reloading
Expand All @@ -132,9 +131,7 @@ module.exports = function(window, options) {
callback(e);
} else {
result.css = postProcessCSS(result.css);
if (!instanceOptions.modifyVars) {
cache.setCSS(sheet.href, webInfo.lastModified, result.css);
}
cache.setCSS(sheet.href, webInfo.lastModified, instanceOptions.modifyVars, result.css);
callback(null, result.css, data, sheet, webInfo, path);
}
});
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,18 @@
"devDependencies": {
"diff": "^1.0",
"grunt": "^0.4.5",
"grunt-browserify": "~3.5.0",
"grunt-contrib-clean": "^0.6.0",
"grunt-contrib-concat": "^0.5.0",
"grunt-contrib-connect": "^0.9.0",
"grunt-contrib-jasmine": "^0.8.2",
"grunt-contrib-jshint": "^0.11.0",
"grunt-contrib-uglify": "^0.8.0",
"grunt-jscs": "^1.6.0",
"grunt-saucelabs": "^8.3.2",
"grunt-shell": "^1.1.1",
"grunt-browserify": "~3.5.0",
"jit-grunt": "^0.9.1",
"time-grunt": "^1.0.0",
"grunt-saucelabs": "^8.3.2"
"time-grunt": "^1.0.0"
},
"keywords": [
"compile less",
Expand Down