Skip to content

Commit a6bd124

Browse files
authored
fix(storybook): postcss parsing error for node_modules paths (#2321)
1 parent 639ce55 commit a6bd124

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

.storybook/postcss.config.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,26 @@ const warnCleaner = require("postcss-warn-cleaner");
55
const simpleBuilder = require("@spectrum-css/component-builder-simple/css/processors.js");
66
const legacyBuilder = require("@spectrum-css/component-builder/css/processors.js");
77

8+
/**
9+
* Determines the package name from a file path
10+
* @param {string} filePath
11+
* @returns {string}
12+
*/
13+
function getPackageFromPath(filePath) {
14+
return filePath.match(`(components|@spectrum-css)\/(.*?)\/`)?.[2];
15+
}
16+
817
module.exports = (ctx) => {
918
let plugins = [];
1019
const componentPath = resolve(__dirname, "../components");
11-
const folderName = ctx.file.includes('node_modules') ? relative(resolve(__dirname, "../../node_modules/@spectrum-css"), ctx.file)?.split(sep)?.shift() : relative(componentPath, ctx.file).split("/")[0];
20+
/** @todo put together a more robust fallback determination */
21+
const folderName = getPackageFromPath(ctx.file) ?? "tokens";
1222
const pkgPath = resolve(componentPath, folderName, "package.json");
1323

24+
/**
25+
* For our token libraries, include a little extra parsing to allow duplicate
26+
* token values to exist in parallel and be toggled using args in storybook.
27+
*/
1428
if (["expressvars", "vars", "tokens"].includes(folderName)) {
1529
const isExpress = folderName === "expressvars";
1630
const modifier = basename(ctx.file, ".css").startsWith("spectrum")
@@ -43,6 +57,10 @@ module.exports = (ctx) => {
4357
: []),
4458
];
4559
} else if (existsSync(pkgPath)) {
60+
/**
61+
* If a path has a package.json, we can assume it's a component and
62+
* we want to leverage the correct plugins for it.
63+
*/
4664
const { devDependencies } = require(pkgPath);
4765
if (
4866
Object.keys(devDependencies).includes("@spectrum-css/component-builder")
@@ -57,7 +75,9 @@ module.exports = (ctx) => {
5775
}
5876
}
5977

60-
// For storybook, add a tool to suppress autoprefixer warnings
78+
/**
79+
* For storybook, add a tool to suppress unnecessary warnings
80+
*/
6181
plugins.push(
6282
warnCleaner({
6383
ignoreFiles: "**/*.css",

0 commit comments

Comments
 (0)