Skip to content

Commit

Permalink
fix: use currentDirectory rather than entryPath for data-uri impo…
Browse files Browse the repository at this point in the history
…rts.

fixes #27.
  • Loading branch information
bingnz committed Nov 13, 2018
1 parent 6382883 commit 660d11c
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 58 deletions.
71 changes: 19 additions & 52 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions src/data-uri-visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class DataUriVisitor {
}

public visitCall(callNode: Less.CallNode) {
const { ruleNode, importedFile, entryPath } = this.getImportInfo(
const { ruleNode, importedFile, filePath } = this.getImportInfo(
callNode
);

Expand All @@ -31,7 +31,7 @@ export class DataUriVisitor {
}

this.addImport({
directory: entryPath ? path.normalize(entryPath) : "",
directory: filePath ? path.normalize(filePath) : "",
relativePath: importedFile
});

Expand All @@ -52,7 +52,7 @@ export class DataUriVisitor {

private getImportInfo(
ruleNode: Less.CallNode
): { ruleNode: Less.CallNode; importedFile?: string; entryPath?: string } {
): { ruleNode: Less.CallNode; importedFile?: string; filePath?: string } {
if (ruleNode.name !== "data-uri" || ruleNode.args.length === 0) {
return { ruleNode };
}
Expand All @@ -67,8 +67,9 @@ export class DataUriVisitor {
typeof ruleNode.fileInfo === "function"
? ruleNode.fileInfo()
: ruleNode.currentFileInfo;
const entryPath = fileInfo.entryPath;

return { ruleNode, importedFile, entryPath };
const filePath = fileInfo.currentDirectory;

return { ruleNode, importedFile, filePath };
}
}
15 changes: 15 additions & 0 deletions test/import-lister.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,21 @@ function readFileAsStream(file, type) {
);
});

describe("when passing in a file with a data-uri with content in a subdirectory", () => {
const filePath =
"./test/list-imports-cases/file-with-data-uri-subdirectory/file.less";
it("should return the referenced file and image as an imports", async () => {
const f = await readFile(new File({ path: filePath }));
const importList = await importLister.listImports(f);
expect(
importList.map(x => x.path.split(path.sep).join("!")).sort()
).to.deep.equal([
"test!list-imports-cases!file-with-data-uri-subdirectory!content!image.svg",
"test!list-imports-cases!file-with-data-uri-subdirectory!content!include.less"
]);
});
});

describe("when passing in a file with a data-uri with MIME type and import by reference", () => {
const filePath =
"./test/list-imports-cases/file-with-data-uri-mime-type/file.less";
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.a { background-image: data-uri('image.svg') }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import "content/include.less";
3 changes: 2 additions & 1 deletion test/mocha.opts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
test/**/*.js
test/**/*.js
--timeout 5000

0 comments on commit 660d11c

Please sign in to comment.