Skip to content

Commit 921e5d5

Browse files
authored
fix(tools): import css from bare specifiers (#2834)
1 parent 94261f6 commit 921e5d5

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

.changeset/funny-teeth-rhyme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
"@patternfly/pfe-tools": patch
3+
---
4+
TypeScript CSS Import Transforms: allow importing from bare specifiers

tools/pfe-tools/typescript/transformers/css-imports.cjs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,20 @@ function cacheCssImportSpecsAbsolute(sourceFile) {
135135
});
136136
}
137137

138+
function getStylesheetContent(specifier, fileName, minify) {
139+
let content; let pathname;
140+
if (specifier.startsWith('./')) {
141+
const dir = pathToFileURL(fileName);
142+
const url = new URL(specifier, dir);
143+
({ pathname } = url);
144+
content = fs.readFileSync(url, 'utf-8');
145+
} else {
146+
pathname = require.resolve(specifier);
147+
content = fs.readFileSync(pathname, 'utf-8');
148+
}
149+
return minify ? minifyCss(content, pathname) : content;
150+
}
151+
138152
/**
139153
* Replace .css import specifiers with .css.js import specifiers
140154
* If the inline option is set, remove the import specifier and print the css
@@ -167,10 +181,7 @@ module.exports = function(program, {
167181
if (inline) {
168182
const cached = cssImportSpecImporterMap.get(specifierAbs);
169183
if (cached?.size === 1) {
170-
const dir = pathToFileURL(fileName);
171-
const url = new URL(specifier, dir);
172-
const content = fs.readFileSync(url, 'utf-8');
173-
const stylesheet = minify ? minifyCss(content, url.pathname) : content;
184+
const stylesheet = getStylesheetContent(specifier, fileName, minify);
174185
return [
175186
createLitCssImportStatement(ctx, node.getSourceFile()),
176187
createLitCssTaggedTemplateLiteral(

0 commit comments

Comments
 (0)