Skip to content

Commit

Permalink
fix: invalid dependencies with working directory (#531)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Dec 27, 2023
1 parent 5055381 commit 2ec31a6
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ function createWebpackLessPlugin(loaderContext, implementation) {
return super.loadFile(result, ...args);
}

loaderContext.addDependency(path.normalize(result.filename));
const absoluteFilename = path.isAbsolute(result.filename)
? result.filename
: path.resolve(".", result.filename);

loaderContext.addDependency(path.normalize(absoluteFilename));

return result;
}
Expand Down
17 changes: 17 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,23 @@ exports[`loader should resolve all imports: errors 1`] = `[]`;

exports[`loader should resolve all imports: warnings 1`] = `[]`;

exports[`loader should resolve in working directory: css 1`] = `
".test {
color: red;
}
a {
color: red;
}
body {
margin: 0;
}
"
`;

exports[`loader should resolve in working directory: errors 1`] = `[]`;

exports[`loader should resolve in working directory: warnings 1`] = `[]`;

exports[`loader should resolve nested imports: css 1`] = `
".top-import {
background: red;
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/3rd/b.less
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a{color:red}
6 changes: 6 additions & 0 deletions test/fixtures/resolve-working-directory/index.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@import './resolve-working-directory-a.less';
@import '3rd/b.less';

body {
margin: 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.test {
color: red;
}
8 changes: 8 additions & 0 deletions test/helpers/getCodeFromLess.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ const pathMap = {
"less-package-2",
"index.less"
),
"./resolve-working-directory-a.less": path.resolve(
__dirname,
"..",
"fixtures",
"resolve-working-directory",
"resolve-working-directory-a.less"
),
"3rd/b.less": path.resolve(__dirname, "..", "fixtures", "3rd", "b.less"),
};

class ResolvePlugin extends less.FileManager {
Expand Down
19 changes: 19 additions & 0 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,25 @@ describe("loader", () => {
expect(getErrors(stats)).toMatchSnapshot("errors");
});

it("should resolve in working directory", async () => {
const oldCwd = process.cwd();

process.chdir(path.resolve(__dirname, "fixtures"));

const testId = "./resolve-working-directory/index.less";
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
const codeFromLess = await getCodeFromLess(testId);

expect(codeFromBundle.css).toBe(codeFromLess.css);
expect(codeFromBundle.css).toMatchSnapshot("css");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");

process.chdir(oldCwd);
});

// TODO bug on windows
it.skip("should work with circular imports", async () => {
const testId = "./circular.less";
Expand Down

0 comments on commit 2ec31a6

Please sign in to comment.