From 897d87436a2375e6b0f946f78799c5f0f1b596f5 Mon Sep 17 00:00:00 2001 From: Alexandre Gigliotti Date: Wed, 2 Dec 2015 10:56:10 -0800 Subject: [PATCH] Fix bug where urls in LESS were not rebased relative to the HTML filename. --- lib/link-less.js | 17 ++++++++++++----- test/fixtures/basic.less | 2 +- .../{invalidImport.less => invalid-import.less} | 0 .../{invalidSyntax.less => invalid-syntax.less} | 0 .../{invalidUrl.less => invalid-url.less} | 0 test/index.js | 12 +++++++++--- 6 files changed, 22 insertions(+), 9 deletions(-) rename test/fixtures/{invalidImport.less => invalid-import.less} (100%) rename test/fixtures/{invalidSyntax.less => invalid-syntax.less} (100%) rename test/fixtures/{invalidUrl.less => invalid-url.less} (100%) diff --git a/lib/link-less.js b/lib/link-less.js index e258a57..3be6841 100644 --- a/lib/link-less.js +++ b/lib/link-less.js @@ -8,9 +8,16 @@ const Ru = require('@panosoft/ramda-utils'); const forEachIndexed = R.addIndex(R.forEach); -const render = R.curryN(2, co.wrap(function * (options, filename) { - options = R.merge(options || {}, { filename }); - const contents = yield fs.readFile(filename, 'utf8'); +const render = R.curryN(3, co.wrap(function * (options, basedir, href) { + const relativePath = path.relative(basedir, path.dirname(href)); + const rootpath = relativePath ? `${relativePath}/` : false; + options = R.merge(options || {}, { + async: true, + relativeUrls: true, + filename: href, + rootpath + }); + const contents = yield fs.readFile(href, 'utf8'); return yield less.render(contents, options); })); /** @@ -26,7 +33,7 @@ const render = R.curryN(2, co.wrap(function * (options, filename) { */ const inlineLess = co.wrap(function * ($, filename, options) { options = Ru.defaults({ less: {} }, options || {}); - options = Ru.defaults({ relativeUrls: true }, options.less); + options = options.less; const basedir = path.dirname(filename); var files = []; try { @@ -36,7 +43,7 @@ const inlineLess = co.wrap(function * ($, filename, options) { const getHref = element => path.resolve(basedir, $(element).attr('href')); const hrefs = R.map(getHref, links); files = R.concat(files, hrefs); - const outputs = yield R.map(render(options), hrefs); + const outputs = yield R.map(render(options, basedir), hrefs); const imports = R.flatten(R.map(R.prop('imports'), outputs)); files = R.concat(files, imports); const styles = R.map(output => $('