Skip to content

Commit

Permalink
enable filter
Browse files Browse the repository at this point in the history
  • Loading branch information
UpperCod committed Nov 8, 2023
1 parent 8b76d13 commit 89a281c
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 31 deletions.
7 changes: 4 additions & 3 deletions src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ const mapTransform = (

if (regExp && !regExp.test(prop)) continue;

const token = regExp ? prop.replace(regExp, "") : prop;
const token = regExp && options.use ? prop.replace(regExp, "") : prop;
const currentRegExp = options.use ? regExp : null;

if (!token) continue;

Expand Down Expand Up @@ -94,7 +95,7 @@ const mapTransform = (
(_: string, type: string, variable: string) => {
const inRoot = customProperties[variable];
return type === "$" && inRoot && options.scope != ":root"
? `var(--${variable.replace(regExp, "")})`
? `var(--${variable.replace(currentRegExp, "")})`
: `var(${prefix}${variable})`;
}
);
Expand All @@ -107,7 +108,7 @@ const mapTransform = (
const isSlotted = selector.startsWith("::slotted");
let id =
parentPrefix +
props.join("-").replace(regExp, "") +
props.join("-").replace(currentRegExp, "") +
parentSuffix;

if (isHostContext) {
Expand Down
2 changes: 2 additions & 0 deletions tests/expect-filter.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tests/expect-host.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion tests/expect-import.css

This file was deleted.

1 change: 1 addition & 0 deletions tests/expect-root-from-json.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion tests/expect-root.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions tests/expect-use-default.css

This file was deleted.

48 changes: 32 additions & 16 deletions tests/postcss.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,60 @@ import postcss from "postcss";
import postcssTokens from "../src/postcss-tokens";
import { readFile, writeFile } from "fs/promises";

test("result import", async () => {
const result = await postcss([
postcssTokens({ prefix: "my-dsprefix" }),
]).process(`@tokens "./tokens.json" use(font);`, {
from: "./tests/demo.css",
});

// await writeFile("./tests/expect-import.css", result.css);
assert.is(result.css, await readFile("./tests/expect-import.css", "utf8"));
});

test("result host, with root", async () => {
const result = await postcss([postcssTokens()]).process(
`@tokens "./tokens.json" prefix(my-ds) scope(:root);`,
{
from: "./tests/demo.css",
}
);
// await writeFile("./tests/expect-root-from-json.css", result.css);
assert.is(
result.css,
await readFile("./tests/expect-root-from-json.css", "utf8")
);
});

test("result file yaml", async () => {
const result = await postcss([postcssTokens()]).process(
`
@tokens "./tokens.yaml" scope(:root) prefix(my-ds);
`,
{
from: "./tests/demo.css",
}
);

// await writeFile("./tests/expect-root.css", result.css);
assert.is(result.css, await readFile("./tests/expect-root.css", "utf8"));
});

test("result file yaml", async () => {
const result = await postcss([postcssTokens()]).process(
`
@tokens "./tokens.yaml" scope(:root) prefix(my-ds);
@tokens "./tokens.yaml" prefix(my-ds);
`,
{
from: "./tests/demo.css",
}
);

// await writeFile("./tests/expect-use-default.css", result.css);
assert.is(
result.css,
await readFile("./tests/expect-use-default.css", "utf8")
// await writeFile("./tests/expect-host.css", result.css);
assert.is(result.css, await readFile("./tests/expect-host.css", "utf8"));
});

test("result file yaml", async () => {
const result = await postcss([postcssTokens()]).process(
`
@tokens "./tokens.yaml" prefix(my-ds) filter(font-border);
`,
{
from: "./tests/demo.css",
}
);

// await writeFile("./tests/expect-filter.css", result.css);
assert.is(result.css, await readFile("./tests/expect-filter.css", "utf8"));
});

test.run();

0 comments on commit 89a281c

Please sign in to comment.