Skip to content

Commit

Permalink
Relative url's option for node lessc
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeapage committed Dec 28, 2012
1 parent addf87a commit eb5c9fb
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 16 deletions.
32 changes: 24 additions & 8 deletions bin/lessc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ var options = {
paths: [],
color: true,
strictImports: false,
rootpath: ''
rootpath: '',
relativeUrls: false
};
var continueProcessing = true,
currentErrorcode;
Expand Down Expand Up @@ -65,12 +66,17 @@ args = args.filter(function (arg) {
options.color = false;
break;
case 'include-path':
options.paths = match[2].split(os.type().match(/Windows/) ? ';' : ':')
.map(function(p) {
if (p) {
return path.resolve(process.cwd(), p);
}
});
if (!match[2]) {
sys.puts("include-path option requires a parameter");
continueProcessing = false;
} else {
options.paths = match[2].split(os.type().match(/Windows/) ? ';' : ':')
.map(function(p) {
if (p) {
return path.resolve(process.cwd(), p);
}
});
}
break;
case 'O0': options.optimization = 0; break;
case 'O1': options.optimization = 1; break;
Expand All @@ -80,7 +86,16 @@ args = args.filter(function (arg) {
break;
case 'rp':
case 'rootpath':
options.rootpath = path.normalize(match[2] + '/');
if (!match[2]) {
sys.puts("rootpath option requires a parameter");
continueProcessing = false;
} else {
options.rootpath = path.normalize(match[2] + '/').replace('\\', '/');
}
break;
case "ru":
case "relative-urls":
options.relativeUrls = true;
break;
}
});
Expand Down Expand Up @@ -126,6 +141,7 @@ var parseLessFile = function (e, data) {
optimization: options.optimization,
filename: input,
rootpath: options.rootpath,
relativeUrls: options.relativeUrls,
strictImports: options.strictImports,
dumpLineNumbers: options.dumpLineNumbers
}).parse(data, function (err, tree) {
Expand Down
13 changes: 10 additions & 3 deletions lib/less/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,17 @@ function loadStyleSheet(sheet, callback, reload, remaining) {
var css = cache && cache.getItem(href);
var timestamp = cache && cache.getItem(href + ':timestamp');
var styles = { css: css, timestamp: timestamp };
var rootpath = hrefParts.path;
//TODO - sheet.rootpath might be a unique path that needs using
// or it might be the url of the last sheet
// hrefParts.path is the full url path in the current sheet
// we need to take sheet.rootpath and if that is set, combine it
// somehow with the relative part of the href?
// also need to take into account sheet.relativeUrls
var rootpath = hrefParts.path;

xhr(href, sheet.type, function (data, lastModified) {
// Store data this session
session_cache += data.replace(/@import .+?;/ig, '');
// Store data this session
session_cache += data.replace(/@import .+?;/ig, '');

if (!reload && styles && lastModified &&
(new(Date)(lastModified).valueOf() ===
Expand All @@ -233,6 +239,7 @@ function loadStyleSheet(sheet, callback, reload, remaining) {
mime: sheet.type,
filename: href,
rootpath: rootpath,
relativeUrls: sheet.relativeUrls,
contents: contents, // Passing top importing parser content cache ref down.
files: files,
dumpLineNumbers: less.dumpLineNumbers
Expand Down
3 changes: 2 additions & 1 deletion lib/less/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ less.Parser.importer = function (file, paths, callback, env) {
// then rootpath should become 'less/module/nav/'
// - If path of imported file is '../mixins.less' and rootpath is 'less/',
// then rootpath should become 'less/../'
if(!/^(?:[a-z-]+:|\/)/.test(file) && j != -1) {
if(env.relativeUrls && !/^(?:[a-z-]+:|\/)/.test(file) && j != -1) {
rootpath = rootpath + file.slice(0, j+1); // append (sub|sup) directory path of imported file
}

Expand All @@ -127,6 +127,7 @@ less.Parser.importer = function (file, paths, callback, env) {
contents: env.contents,
files: env.files,
syncImport: env.syncImport,
relativeUrls: env.relativeUrls,
rootpath: rootpath,
dumpLineNumbers: env.dumpLineNumbers
}).parse(data, function (e, root) {
Expand Down
4 changes: 3 additions & 1 deletion lib/less/lessc_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var lessc_helper = {

//Print command line options
printUsage: function() {
sys.puts("usage: lessc [options] <source> [destination]");
sys.puts("usage: lessc [option option=parameter ...] <source> [destination]");
sys.puts("");
sys.puts("If source is set to `-' (dash or hyphen-minus), input is read from stdin.");
sys.puts("");
Expand All @@ -48,6 +48,8 @@ var lessc_helper = {
sys.puts(" media query which is compatible with the SASS");
sys.puts(" format, and 'all' which will do both.");
sys.puts(" -rp, --rootpath Set rootpath for url rewriting in relative imports and urls.");
sys.puts(" Works with or withour the relative-urls option.");
sys.puts(" -ru, --relative-urls re-write relative urls to the base less file.");
sys.puts("");
sys.puts("Report bugs to: http://github.com/cloudhead/less.js/issues");
sys.puts("Home page: <http://lesscss.org/>");
Expand Down
11 changes: 9 additions & 2 deletions lib/less/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1500,8 +1500,15 @@ if (less.mode === 'browser' || less.mode === 'rhino') {
// We pass `true` as 3rd argument, to force the reload of the import.
// This is so we can get the syntax tree as opposed to just the CSS output,
// as we need this to evaluate the current stylesheet.
// __ Now using the hack of passing a ref to top parser's content cache in the 1st arg. __
loadStyleSheet({ href: path, title: path, type: env.mime, contents: env.contents, files: env.files, rootpath: env.rootpath }, function (e, root, data, sheet, _, path) {
loadStyleSheet({
href: path,
title: path,
type: env.mime,
contents: env.contents,
files: env.files,
rootpath: env.rootpath,
relativeUrls: env.relativeUrls },
function (e, root, data, sheet, _, path) {
if (e && typeof(env.errback) === "function") {
env.errback.call(null, path, paths, callback, env);
} else {
Expand Down
2 changes: 1 addition & 1 deletion test/less-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ less.tree.functions._color = function (str) {

sys.puts("\n" + stylize("LESS", 'underline') + "\n");

runTestSet();
runTestSet({relativeUrls: true});

runTestSet(null, "errors/", function(name, err, compiledLess, doReplacements) {
fs.readFile(path.join('test/less/', name) + '.txt', 'utf8', function (e, expectedErr) {
Expand Down

0 comments on commit eb5c9fb

Please sign in to comment.