Skip to content

Commit

Permalink
[FIX] Support absolute import paths in less files (#107)
Browse files Browse the repository at this point in the history
Absolute paths in imports are now correctly passed through to the filesystem without making them relative to the current directory.
  • Loading branch information
tobiasso85 committed Dec 16, 2019
1 parent 7616974 commit 266b06d
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
a {
color: green;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
html {
color: green;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
b {
color: green;
}
39 changes: 39 additions & 0 deletions test/test-custom-fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
});
});
});
});

0 comments on commit 266b06d

Please sign in to comment.