Skip to content

Commit

Permalink
build(postcss): fix composes plugin folder check (#2421)
Browse files Browse the repository at this point in the history
  • Loading branch information
Barsnes authored Sep 16, 2024
1 parent 6385e55 commit b201787
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion apps/storybook/.storybook/preview.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import '@digdir/designsystemet-css/index.css';
import '../../../packages/css/index.css';
import '@digdir/designsystemet-theme/digdir.css';

import { withThemeByDataAttribute } from '@storybook/addon-themes';
Expand Down
2 changes: 1 addition & 1 deletion apps/storybook/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extends": "../../tsconfig.json",
"include": [".", "./.storybook/**/*"]
"include": [".", "./.storybook/**/*", "../../packages/css/postcss.config.js"]
}
26 changes: 17 additions & 9 deletions packages/css/postcss.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const postcss = require('postcss');
const fs = require('node:fs');
const path = require('node:path');

module.exports = {
plugins: [
Expand All @@ -17,21 +18,28 @@ function postcssComposes() {
return {
postcssPlugin: '@composes', // Allows `@composes classname from './file.css'` directive
AtRule: {
composes: async (rule) => {
composes: async (rule, { AtRule }) => {
const cache = {};
const sanitizedParams = rule.params.replace(/["']/g, '').trim();
const [selector, from] = sanitizedParams.split(/\s+from\s+/);
const resolvedFrom = path.resolve(
path.dirname(rule.source.input.file),
from,
);

if (!cache[from])
cache[from] = await postcss([]).process(fs.readFileSync(from), {
from,
});
if (!cache[resolvedFrom])
cache[resolvedFrom] = await postcss([]).process(
fs.readFileSync(resolvedFrom),
{
from: resolvedFrom,
},
);

cache[from].root.walkRules((from) => {
if (from.selector.startsWith(`.${selector}`))
cache[resolvedFrom].root.walkRules((fromRule) => {
if (fromRule.selector.startsWith(`.${selector}`))
rule.replaceWith(
from.clone({
selector: from.selector.replace(`.${selector}`, '&'),
fromRule.clone({
selector: fromRule.selector.replace(`.${selector}`, '&'),
}),
);
});
Expand Down

0 comments on commit b201787

Please sign in to comment.