Skip to content

Commit b201787

Browse files
authored
build(postcss): fix composes plugin folder check (#2421)
1 parent 6385e55 commit b201787

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

apps/storybook/.storybook/preview.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import '@digdir/designsystemet-css/index.css';
1+
import '../../../packages/css/index.css';
22
import '@digdir/designsystemet-theme/digdir.css';
33

44
import { withThemeByDataAttribute } from '@storybook/addon-themes';

apps/storybook/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"extends": "../../tsconfig.json",
3-
"include": [".", "./.storybook/**/*"]
3+
"include": [".", "./.storybook/**/*", "../../packages/css/postcss.config.js"]
44
}

packages/css/postcss.config.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const postcss = require('postcss');
22
const fs = require('node:fs');
3+
const path = require('node:path');
34

45
module.exports = {
56
plugins: [
@@ -17,21 +18,28 @@ function postcssComposes() {
1718
return {
1819
postcssPlugin: '@composes', // Allows `@composes classname from './file.css'` directive
1920
AtRule: {
20-
composes: async (rule) => {
21+
composes: async (rule, { AtRule }) => {
2122
const cache = {};
2223
const sanitizedParams = rule.params.replace(/["']/g, '').trim();
2324
const [selector, from] = sanitizedParams.split(/\s+from\s+/);
25+
const resolvedFrom = path.resolve(
26+
path.dirname(rule.source.input.file),
27+
from,
28+
);
2429

25-
if (!cache[from])
26-
cache[from] = await postcss([]).process(fs.readFileSync(from), {
27-
from,
28-
});
30+
if (!cache[resolvedFrom])
31+
cache[resolvedFrom] = await postcss([]).process(
32+
fs.readFileSync(resolvedFrom),
33+
{
34+
from: resolvedFrom,
35+
},
36+
);
2937

30-
cache[from].root.walkRules((from) => {
31-
if (from.selector.startsWith(`.${selector}`))
38+
cache[resolvedFrom].root.walkRules((fromRule) => {
39+
if (fromRule.selector.startsWith(`.${selector}`))
3240
rule.replaceWith(
33-
from.clone({
34-
selector: from.selector.replace(`.${selector}`, '&'),
41+
fromRule.clone({
42+
selector: fromRule.selector.replace(`.${selector}`, '&'),
3543
}),
3644
);
3745
});

0 commit comments

Comments
 (0)