From 266b06d9b091d34e6f279fbdf567702bcb9dbaed Mon Sep 17 00:00:00 2001 From: Tobias Sorn Date: Mon, 16 Dec 2019 10:45:39 +0100 Subject: [PATCH] [FIX] Support absolute import paths in less files (#107) Absolute paths in imports are now correctly passed through to the filesystem without making them relative to the current directory. --- lib/index.js | 9 ++++- .../lib3/comments/themes/bar/library-RTL.css | 19 +++++++++ .../lib3/comments/themes/bar/library.css | 19 +++++++++ .../comments/themes/bar/library.source.less | 15 +++++++ .../lib3/comments/themes/bar/my2.less | 3 ++ .../comments/lib3/comments/themes/my3.less | 3 ++ .../lib3/comments/themes/other/sub/my.less | 3 ++ test/test-custom-fs.js | 39 +++++++++++++++++++ 8 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 test/expected/libraries/scopes/comments/lib3/comments/themes/bar/library-RTL.css create mode 100644 test/expected/libraries/scopes/comments/lib3/comments/themes/bar/library.css create mode 100644 test/fixtures/libraries/scopes/comments/lib3/comments/themes/bar/library.source.less create mode 100644 test/fixtures/libraries/scopes/comments/lib3/comments/themes/bar/my2.less create mode 100644 test/fixtures/libraries/scopes/comments/lib3/comments/themes/my3.less create mode 100644 test/fixtures/libraries/scopes/comments/lib3/comments/themes/other/sub/my.less diff --git a/lib/index.js b/lib/index.js index d3a88d7f..97a9d5e8 100644 --- a/lib/index.js +++ b/lib/index.js @@ -150,7 +150,14 @@ Builder.prototype.build = function(options) { } function fileHandler(file, currentFileInfo, handleDataAndCallCallback, callback) { - const pathname = path.posix.join(currentFileInfo.currentDirectory, file); + let pathname; + + // support absolute paths such as "/resources/my/base.less" + if (path.posix.isAbsolute(file)) { + pathname = path.posix.normalize(file); + } else { + pathname = path.posix.join(currentFileInfo.currentDirectory, file); + } that.fileUtils.readFile(pathname, options.rootPaths).then(function(result) { if (!result) { diff --git a/test/expected/libraries/scopes/comments/lib3/comments/themes/bar/library-RTL.css b/test/expected/libraries/scopes/comments/lib3/comments/themes/bar/library-RTL.css new file mode 100644 index 00000000..7defa42d --- /dev/null +++ b/test/expected/libraries/scopes/comments/lib3/comments/themes/bar/library-RTL.css @@ -0,0 +1,19 @@ +/** + * This is a sample comment! + */ +.myRule1 { + /** + * This is a sample comment inside a rule + * with a different color value! + */ + color: #ffffff; +} +b { + color: green; +} +a { + color: green; +} +html { + color: green; +} diff --git a/test/expected/libraries/scopes/comments/lib3/comments/themes/bar/library.css b/test/expected/libraries/scopes/comments/lib3/comments/themes/bar/library.css new file mode 100644 index 00000000..7defa42d --- /dev/null +++ b/test/expected/libraries/scopes/comments/lib3/comments/themes/bar/library.css @@ -0,0 +1,19 @@ +/** + * This is a sample comment! + */ +.myRule1 { + /** + * This is a sample comment inside a rule + * with a different color value! + */ + color: #ffffff; +} +b { + color: green; +} +a { + color: green; +} +html { + color: green; +} diff --git a/test/fixtures/libraries/scopes/comments/lib3/comments/themes/bar/library.source.less b/test/fixtures/libraries/scopes/comments/lib3/comments/themes/bar/library.source.less new file mode 100644 index 00000000..78bba0c7 --- /dev/null +++ b/test/fixtures/libraries/scopes/comments/lib3/comments/themes/bar/library.source.less @@ -0,0 +1,15 @@ +/** + * This is a sample comment! + */ + +.myRule1 { + /** + * This is a sample comment inside a rule + * with a different color value! + */ + color: #ffffff; +} + +@import "/lib3/comments/themes/other/sub/my.less"; +@import "./my2.less"; +@import "../my3.less"; \ No newline at end of file diff --git a/test/fixtures/libraries/scopes/comments/lib3/comments/themes/bar/my2.less b/test/fixtures/libraries/scopes/comments/lib3/comments/themes/bar/my2.less new file mode 100644 index 00000000..e0ce796c --- /dev/null +++ b/test/fixtures/libraries/scopes/comments/lib3/comments/themes/bar/my2.less @@ -0,0 +1,3 @@ +a { + color: green; +} \ No newline at end of file diff --git a/test/fixtures/libraries/scopes/comments/lib3/comments/themes/my3.less b/test/fixtures/libraries/scopes/comments/lib3/comments/themes/my3.less new file mode 100644 index 00000000..dde7ff1b --- /dev/null +++ b/test/fixtures/libraries/scopes/comments/lib3/comments/themes/my3.less @@ -0,0 +1,3 @@ +html { + color: green; +} \ No newline at end of file diff --git a/test/fixtures/libraries/scopes/comments/lib3/comments/themes/other/sub/my.less b/test/fixtures/libraries/scopes/comments/lib3/comments/themes/other/sub/my.less new file mode 100644 index 00000000..75255f9a --- /dev/null +++ b/test/fixtures/libraries/scopes/comments/lib3/comments/themes/other/sub/my.less @@ -0,0 +1,3 @@ +b { + color: green; +} \ No newline at end of file diff --git a/test/test-custom-fs.js b/test/test-custom-fs.js index 25caccd7..8712eb5a 100644 --- a/test/test-custom-fs.js +++ b/test/test-custom-fs.js @@ -18,6 +18,7 @@ const assert = require("assert"); const readFile = require("./common/helper").readFile; const fs = require("fs"); +const path = require("path"); // tested module const Builder = require("../").Builder; @@ -72,5 +73,43 @@ describe("(custom fs) CSS Scoping of", function() { assert.equal(statCalls, 19, "fs.stat should have been called 19 times"); }); }); + + + it("should return same CSS for bar with absolute import paths", function() { + const readFileCalls = []; + + + let statCalls = 0; + + return new Builder({ + fs: { + readFile: function(...args) { + readFileCalls.push(args[0]); + return fs.readFile(...args); + }, + stat: function(...args) { + statCalls++; + return fs.stat(...args); + } + } + }).build({ + lessInputPath: "lib3/comments/themes/bar/library.source.less", + rootPaths: [ + "test/fixtures/libraries/scopes/comments" + ] + }).then(function(result) { + assert.equal(result.css, readFile("test/expected/libraries/scopes/comments/lib3/comments/themes/bar/library.css"), "CSS scoping should be correctly generated"); + assert.equal(result.cssRtl, readFile("test/expected/libraries/scopes/comments/lib3/comments/themes/bar/library-RTL.css"), "Rtl CSS scoping should be correctly generated"); + + const basePath = path.join("test/fixtures/libraries/scopes/comments/lib3/comments", "themes"); + assert.deepEqual(readFileCalls, [ + path.join(basePath, "bar/library.source.less"), + path.join(basePath, "other/sub/my.less"), + path.join(basePath, "bar/my2.less"), + path.join(basePath, "my3.less") + ], "fs.readFile should have been called with import paths"); + assert.equal(statCalls, 10, "fs.stat should have been called 19 times"); + }); + }); }); });