diff --git a/CHANGELOG.md b/CHANGELOG.md index 9220b1e..47f94f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,13 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## 1.2.0 (2023-08-28) + +### Features + +- import files based on the current directory - thanks [linhe0x0](https://github.com/linhe0x0) +- more unit tests — thanks [Kristoffer Nordström](https://github.com/42tte) + ## 1.1.0 (2023-08-10) ### Features diff --git a/index.js b/index.js index 9f34edf..ce597e1 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,4 @@ +const path = require("path"); const { readFileSync } = require("fs"); const resolve = require("resolve"); @@ -21,9 +22,15 @@ module.exports = () => { .replace(/['"]?\s*(\))?$/, ""); let replacement; + + let basedir = process.cwd(); + if (node.source && node.source.input && node.source.input.file) { + basedir = path.dirname(node.source.input.file); + } + try { let resolvedPath = resolve.sync(id, { - basedir: process.cwd(), + basedir, extensions: [".css"], moduleDirectory: ["web_modules", "node_modules"], packageFilter: (pkg) => { diff --git a/test/index.test.js b/test/index.test.js index f707e9e..d595ba7 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1,3 +1,4 @@ +let path = require("path"); let { test } = require("uvu"); let { equal, match } = require("uvu/assert"); let postcss = require("postcss"); @@ -6,9 +7,9 @@ let nestedImport = require("../"); // ----------------------------------------------------------------------------- -async function run(input, output, opts) { +async function run(input, output, opts, from) { let result = await postcss([nestedImport(opts)]).process(input, { - from: undefined + from }); equal(result.css, output); equal(result.warnings().length, 0); @@ -204,4 +205,23 @@ test("10 - package.json filter style, main & index", async () => { ); }); +test("11 - import based on current directory", async () => { + await run( + `@media (prefers-color-scheme: light) { + :root:not([data-theme='dark']) { + @nested-import './mocks/colors1.css'; + } +}`, + `@media (prefers-color-scheme: light) { + :root:not([data-theme='dark']) { + h1 { + color: red; +} + } +}`, + undefined, + path.join(__dirname, "app.css") + ); +}); + test.run();