Skip to content

Commit 3ed08b2

Browse files
joshblackCopilot
andauthored
feat(rollup-plugin): add support for css files to plugin (#7294)
Co-authored-by: Copilot <[email protected]>
1 parent eddd934 commit 3ed08b2

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

packages/postcss-preset-primer/src/__tests__/__snapshots__/preset.test.ts.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
exports[`postcss-preset-primer > css nesting 1`] = `".selector[data-active]{color:var(--fgColor-active)}"`;
44

5+
exports[`postcss-preset-primer > mixins > @mixin activeIndicatorLine 1`] = `".selector{background:var(--borderColor-accent-emphasis,var(--color-accent-emphasis));border-radius:var(--borderRadius-medium,.375rem);content:"";height:calc(100% - var(--base-size-8,.5rem));left:calc(var(--base-size-8,.5rem)*-1);position:absolute;top:var(--base-size-4,.25rem);width:var(--base-size-4,.25rem)}"`;
6+
7+
exports[`postcss-preset-primer > mixins > @mixin buttonReset 1`] = `".selector{align-items:center;appearance:none;background:none;border:0;color:inherit;cursor:pointer;display:inline-flex;font:inherit;margin:0;padding:0;text-align:start}.selector::-moz-focus-inner{border:0}"`;
8+
59
exports[`postcss-preset-primer > mixins > @mixin focusOutline 1`] = `".selector{box-shadow:none;outline:2px solid var(--focus-outlineColor,var(--color-accent-fg));outline-offset:-2px}"`;
610

711
exports[`postcss-preset-primer > mixins > @mixin focusOutlineOnEmphasis 1`] = `".selector{box-shadow:inset 0 0 0 3px var(--fgColor-onEmphasis,var(--color-fg-on-emphasis));outline:2px solid var(--focus-outlineColor,var(--color-accent-fg));outline-offset:-2px}"`;

packages/postcss-preset-primer/src/__tests__/preset.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ describe('postcss-preset-primer', () => {
3737
})
3838

3939
describe('mixins', () => {
40+
test('@mixin activeIndicatorLine', async () => {
41+
const result = await process(`
42+
.selector {
43+
@mixin activeIndicatorLine;
44+
}
45+
`)
46+
expect(result).toMatchSnapshot()
47+
})
48+
4049
test('@mixin focusOutline', async () => {
4150
const result = await process(`
4251
.selector {
@@ -54,5 +63,14 @@ describe('postcss-preset-primer', () => {
5463
`)
5564
expect(result).toMatchSnapshot()
5665
})
66+
67+
test('@mixin buttonReset', async () => {
68+
const result = await process(`
69+
.selector {
70+
@mixin buttonReset;
71+
}
72+
`)
73+
expect(result).toMatchSnapshot()
74+
})
5775
})
5876
})

packages/rollup-plugin-import-css/src/index.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type {Plugin} from 'rollup'
2+
import fs from 'node:fs/promises'
23
import path from 'node:path'
34
import {createHash} from 'node:crypto'
45
import postcss from 'postcss'
@@ -40,18 +41,33 @@ export function importCSS(options: ImportCSSOptions): Plugin {
4041
return {
4142
id: source,
4243
external: true,
44+
moduleSideEffects: true,
4345
}
4446
}
47+
48+
if (source.endsWith('.css')) {
49+
const id = path.resolve(path.dirname(importer), source)
50+
return path.format({
51+
dir: path.dirname(id),
52+
base: `${path.basename(id)}.js`,
53+
})
54+
}
4555
},
46-
async transform(code, id) {
47-
if (!id.endsWith('.css')) {
56+
async load(id) {
57+
if (id.endsWith('.css.js')) {
58+
return ''
59+
}
60+
},
61+
async transform(_code, id) {
62+
if (!id.endsWith('.css.js')) {
4863
return
4964
}
5065

66+
const sourceId = path.join(path.dirname(id), path.basename(id, '.js'))
67+
const code = await fs.readFile(sourceId, 'utf8')
5168
const hash = getSourceHash(code)
52-
const relativePath = path.relative(rootDirectory, id)
53-
const name = path.basename(relativePath, '.module.css')
54-
69+
const relativePath = path.relative(rootDirectory, sourceId)
70+
const name = path.basename(relativePath, relativePath.endsWith('.module.css') ? '.module.css' : '.css')
5571
const fileName = path.join(
5672
path.dirname(relativePath),
5773
path.format({
@@ -66,7 +82,7 @@ export function importCSS(options: ImportCSSOptions): Plugin {
6682
// the classes is used, then the associated styles for those classes is
6783
// also included
6884

69-
let cssModuleClasses = null
85+
let cssModuleClasses: {[name: string]: string} | null = null
7086
const result = await postcss([
7187
...postcssPlugins,
7288
postcssModules({
@@ -108,8 +124,12 @@ export function importCSS(options: ImportCSSOptions): Plugin {
108124
return {
109125
code: `
110126
import '${cssSource}';
111-
export default ${JSON.stringify(cssModuleClasses)}
127+
${
128+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
129+
cssModuleClasses !== null ? `export default ${JSON.stringify(cssModuleClasses)}` : ''
130+
}
112131
`,
132+
moduleSideEffects: 'no-treeshake',
113133
}
114134
},
115135
}

0 commit comments

Comments
 (0)