Skip to content

Commit cd5ac31

Browse files
authored
feat: css another round of fixes (#66)
* add astro to the icon-color-plugin * fix getconfig * add changeset
1 parent 9a041c1 commit cd5ac31

File tree

8 files changed

+81
-41
lines changed

8 files changed

+81
-41
lines changed

.changeset/twelve-pens-jog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@cypress-design/css': patch
3+
---
4+
5+
- Add astro compatibility
6+
- Fix config merge (lodash is messing up with arrays)

css/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"src"
88
],
99
"scripts": {
10-
"build": "tsc"
10+
"build": "tsc -p ./tsconfig.build.json"
1111
},
1212
"dependencies": {
1313
"@windicss/plugin-interaction-variants": "^1.0.0",

css/src/get-config.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { describe, expect, test, vi } from 'vitest'
2+
import { getConfig } from './get-config'
3+
import createPlugin from 'windicss/plugin'
4+
5+
describe('getConfig', () => {
6+
test('should return the correct config', () => {
7+
const { config } = getConfig({})
8+
const defaultPluginsLength = config.plugins?.length || 0
9+
expect(
10+
getConfig({
11+
config: {
12+
plugins: [createPlugin(vi.fn(), { name: 'test' })],
13+
},
14+
}).config.plugins
15+
).toHaveLength(defaultPluginsLength + 1)
16+
})
17+
})

css/src/get-config.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import * as path from 'path'
2+
import { mergeWith } from 'lodash'
3+
import { UserOptions } from 'vite-plugin-windicss'
4+
import windiConfig from './windi.config'
5+
6+
function customizer(objValue: any, srcValue: any) {
7+
if (Array.isArray(objValue) && Array.isArray(srcValue)) {
8+
return objValue.concat(srcValue)
9+
}
10+
return undefined
11+
}
12+
13+
export function getConfig(options: UserOptions) {
14+
const scan = typeof options.scan === 'boolean' ? {} : options.scan ?? {}
15+
const include = Array.isArray(scan?.include)
16+
? scan.include
17+
: scan.include
18+
? [scan.include]
19+
: []
20+
21+
const currentPackagePath = path.dirname(
22+
require.resolve('@cypress-design/css/package.json')
23+
)
24+
25+
const config = mergeWith({}, windiConfig, options.config, customizer)
26+
27+
return {
28+
...options,
29+
config,
30+
scan: {
31+
...(scan || {}),
32+
include: [
33+
...include,
34+
path.resolve(
35+
currentPackagePath,
36+
'..', // remove css/ from path
37+
'*/dist/*.@(js|css)' // look for all component files
38+
),
39+
],
40+
},
41+
}
42+
}

css/src/icon-color-plugins.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,8 @@ export const IconDuotoneColorsPlugin = createPlugin(
205205
return `.${className}:hover, .${className}:focus`
206206
})
207207
})
208-
}
208+
},
209+
{ name: 'cypress-icon-duotone-colors' }
209210
)
210211

211212
const prefixes = ['', 'hover', 'focus', 'hocus'] as const
@@ -277,7 +278,7 @@ function isValidWindiColor(value: string) {
277278
* to be kept in the windicss css file after purgecss
278279
*/
279280
export const IconExtractor: Extractor = {
280-
extensions: ['vue', 'js', 'ts', 'tsx'],
281+
extensions: ['vue', 'js', 'ts', 'tsx', 'astro'],
281282
extractor: (code, id) => {
282283
const { tags, classes = [], attributes } = DefaultExtractor(code, id)
283284

css/src/index.ts

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,7 @@
1-
import * as path from 'path'
21
import type { UserOptions } from 'vite-plugin-windicss'
32
import VitePlugin from 'vite-plugin-windicss'
43
import WebpackPlugin from 'windicss-webpack-plugin'
5-
import windiConfig from './windi.config'
6-
import { merge } from 'lodash'
7-
8-
function getConfig(options: UserOptions) {
9-
const scan = typeof options.scan === 'boolean' ? {} : options.scan ?? {}
10-
const include = Array.isArray(scan?.include)
11-
? scan.include
12-
: scan.include
13-
? [scan.include]
14-
: []
15-
16-
const currentPackagePath = path.dirname(
17-
require.resolve('@cypress-design/css/package.json')
18-
)
19-
20-
return {
21-
...options,
22-
config: merge(windiConfig, options.config),
23-
scan: {
24-
...(scan || {}),
25-
include: [
26-
...include,
27-
path.resolve(
28-
currentPackagePath,
29-
'..', // remove css/ from path
30-
'*/dist/*.@(js|css)' // look for all component files
31-
),
32-
],
33-
},
34-
}
35-
}
4+
import { getConfig } from './get-config'
365

376
export const CyCSSVitePlugin = (options: UserOptions = {}) =>
387
VitePlugin(getConfig(options))

css/src/windi.config.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import { defineConfig } from 'windicss/helpers'
22
// @ts-ignore
3-
import InteractionVariants from '@windicss/plugin-interaction-variants'
3+
import PluginInteractionVariants from '@windicss/plugin-interaction-variants'
4+
import PluginFilters from 'windicss/plugin/filters'
45
import { IconDuotoneColorsPlugin, IconExtractor } from './icon-color-plugins'
56
import { colors } from './colors'
67
import { shortcuts } from './shortcuts'
78

9+
// give the plugins a name for debugging purposes
10+
PluginInteractionVariants.config = { name: 'windi-interaction-variants' }
11+
PluginFilters.config = { name: 'windi-filters' }
12+
813
export default defineConfig({
914
// This adds !important to all utility classes.
1015
// https://csswizardry.com/2016/05/the-importance-of-important/
@@ -44,11 +49,7 @@ export default defineConfig({
4449
'no-hover',
4550
],
4651
},
47-
plugins: [
48-
IconDuotoneColorsPlugin,
49-
InteractionVariants,
50-
require('windicss/plugin/filters'),
51-
],
52+
plugins: [IconDuotoneColorsPlugin, PluginInteractionVariants, PluginFilters],
5253
shortcuts,
5354
extract: {
5455
extractors: [IconExtractor],

css/tsconfig.build.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"exclude": ["**/*.test.ts"]
4+
}

0 commit comments

Comments
 (0)