Skip to content

Commit 2ec31a6

Browse files
authoredDec 27, 2023
fix: invalid dependencies with working directory (#531)
1 parent 5055381 commit 2ec31a6

File tree

7 files changed

+59
-1
lines changed

7 files changed

+59
-1
lines changed
 

Diff for: ‎src/utils.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ function createWebpackLessPlugin(loaderContext, implementation) {
135135
return super.loadFile(result, ...args);
136136
}
137137

138-
loaderContext.addDependency(path.normalize(result.filename));
138+
const absoluteFilename = path.isAbsolute(result.filename)
139+
? result.filename
140+
: path.resolve(".", result.filename);
141+
142+
loaderContext.addDependency(path.normalize(absoluteFilename));
139143

140144
return result;
141145
}

Diff for: ‎test/__snapshots__/loader.test.js.snap

+17
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,23 @@ exports[`loader should resolve all imports: errors 1`] = `[]`;
378378

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

381+
exports[`loader should resolve in working directory: css 1`] = `
382+
".test {
383+
color: red;
384+
}
385+
a {
386+
color: red;
387+
}
388+
body {
389+
margin: 0;
390+
}
391+
"
392+
`;
393+
394+
exports[`loader should resolve in working directory: errors 1`] = `[]`;
395+
396+
exports[`loader should resolve in working directory: warnings 1`] = `[]`;
397+
381398
exports[`loader should resolve nested imports: css 1`] = `
382399
".top-import {
383400
background: red;

Diff for: ‎test/fixtures/3rd/b.less

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a{color:red}

Diff for: ‎test/fixtures/resolve-working-directory/index.less

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@import './resolve-working-directory-a.less';
2+
@import '3rd/b.less';
3+
4+
body {
5+
margin: 0;
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.test {
2+
color: red;
3+
}

Diff for: ‎test/helpers/getCodeFromLess.js

+8
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ const pathMap = {
133133
"less-package-2",
134134
"index.less"
135135
),
136+
"./resolve-working-directory-a.less": path.resolve(
137+
__dirname,
138+
"..",
139+
"fixtures",
140+
"resolve-working-directory",
141+
"resolve-working-directory-a.less"
142+
),
143+
"3rd/b.less": path.resolve(__dirname, "..", "fixtures", "3rd", "b.less"),
136144
};
137145

138146
class ResolvePlugin extends less.FileManager {

Diff for: ‎test/loader.test.js

+19
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,25 @@ describe("loader", () => {
869869
expect(getErrors(stats)).toMatchSnapshot("errors");
870870
});
871871

872+
it("should resolve in working directory", async () => {
873+
const oldCwd = process.cwd();
874+
875+
process.chdir(path.resolve(__dirname, "fixtures"));
876+
877+
const testId = "./resolve-working-directory/index.less";
878+
const compiler = getCompiler(testId);
879+
const stats = await compile(compiler);
880+
const codeFromBundle = getCodeFromBundle(stats, compiler);
881+
const codeFromLess = await getCodeFromLess(testId);
882+
883+
expect(codeFromBundle.css).toBe(codeFromLess.css);
884+
expect(codeFromBundle.css).toMatchSnapshot("css");
885+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
886+
expect(getErrors(stats)).toMatchSnapshot("errors");
887+
888+
process.chdir(oldCwd);
889+
});
890+
872891
// TODO bug on windows
873892
it.skip("should work with circular imports", async () => {
874893
const testId = "./circular.less";

0 commit comments

Comments
 (0)
Please sign in to comment.