diff --git a/src/utils.js b/src/utils.js index 60b32df..e4a0c00 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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; } diff --git a/test/__snapshots__/loader.test.js.snap b/test/__snapshots__/loader.test.js.snap index 704b77e..74823d3 100644 --- a/test/__snapshots__/loader.test.js.snap +++ b/test/__snapshots__/loader.test.js.snap @@ -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; diff --git a/test/fixtures/3rd/b.less b/test/fixtures/3rd/b.less new file mode 100644 index 0000000..93db057 --- /dev/null +++ b/test/fixtures/3rd/b.less @@ -0,0 +1 @@ +a{color:red} \ No newline at end of file diff --git a/test/fixtures/resolve-working-directory/index.less b/test/fixtures/resolve-working-directory/index.less new file mode 100644 index 0000000..24deca3 --- /dev/null +++ b/test/fixtures/resolve-working-directory/index.less @@ -0,0 +1,6 @@ +@import './resolve-working-directory-a.less'; +@import '3rd/b.less'; + +body { + margin: 0; +} diff --git a/test/fixtures/resolve-working-directory/resolve-working-directory-a.less b/test/fixtures/resolve-working-directory/resolve-working-directory-a.less new file mode 100644 index 0000000..6446ebf --- /dev/null +++ b/test/fixtures/resolve-working-directory/resolve-working-directory-a.less @@ -0,0 +1,3 @@ +.test { + color: red; +} diff --git a/test/helpers/getCodeFromLess.js b/test/helpers/getCodeFromLess.js index 2301619..21a34a4 100644 --- a/test/helpers/getCodeFromLess.js +++ b/test/helpers/getCodeFromLess.js @@ -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 { diff --git a/test/loader.test.js b/test/loader.test.js index a37914c..3439b22 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -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";