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

feat: port the PR#8 onto latest main #15

Merged
merged 5 commits into from
Aug 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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const path = require("path");
const { readFileSync } = require("fs");
const resolve = require("resolve");

Expand All @@ -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) => {
Expand Down
24 changes: 22 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
let path = require("path");
let { test } = require("uvu");
let { equal, match } = require("uvu/assert");
let postcss = require("postcss");
Expand All @@ -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);
Expand Down Expand Up @@ -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();