Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: invalid dependencies with working directory #531

Merged
merged 1 commit into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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