Skip to content

Commit 315e31a

Browse files
authored
Merge pull request #103 from /issues/100
Fix bugs
2 parents 81679ba + 502480d commit 315e31a

9 files changed

+34
-11
lines changed

example/react-sass/src/App.module.scss

+6
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,9 @@ $green: #00c851;
2020
.test {
2121
@apply px-1.5;
2222
}
23+
24+
@supports (--num: 1.5) {
25+
.myclass {
26+
display: block;
27+
}
28+
}

example/react-sass/src/App.module.scss.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ declare const classNames: typeof globalClassNames & {
55
readonly active: 'active'
66
readonly input: 'input'
77
readonly test: 'test'
8+
readonly myclass: 'myclass'
89
}
910
export default classNames

example/react-sass/src/App.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { FC } from 'react'
22
import styles from './App.module.scss'
33
import { classNamesFunc } from 'classnames-generics'
44
import { User } from './User/User'
5+
import styles2 from './exclude/Exclude.module.scss'
56

67
const classNames = classNamesFunc<keyof typeof styles>()
78
type Props = {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.exclude {
2+
&.active {
3+
background-color: red;
4+
}
5+
}

example/react-sass/vite.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export default defineConfig({
4242
outputFilePath: path.resolve(__dirname, './src/@types/style.d.ts'),
4343
},
4444
prettierFilePath: path.resolve('../../.prettierrc.cjs'),
45+
excludePath: ['./src/exclude/*'],
4546
esmExport: true,
4647
// typeName: {
4748
// replacement: (fileName) => {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vite-plugin-sass-dts",
3-
"version": "1.3.24",
3+
"version": "1.3.25",
44
"engines": {
55
"node": ">=20"
66
},

src/extract.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { CSSJSObj, GetParseCaseFunction } from './type'
33

44
// [Note]: @apply for Tailwind
55
const importRe = new RegExp(/^(@import|@apply)/)
6+
const supportRe = new RegExp(/^(@support)/)
67
const keySeparatorRe = new RegExp(/(?=[\s.:[\]><+,()])/g)
78

89
export const extractClassNameKeys = (
@@ -12,14 +13,18 @@ export const extractClassNameKeys = (
1213
): Map<string, boolean> => {
1314
return Object.entries(obj).reduce<Map<string, boolean>>(
1415
(curr, [key, value]) => {
16+
console.log(key, value)
1517
if (importRe.test(key)) return curr
1618
const splitKeys = key.split(keySeparatorRe)
17-
for (const splitKey of splitKeys) {
18-
if (parentKey === ':export' || splitKey.startsWith('.')) {
19-
if (toParseCase) {
20-
curr.set(toParseCase(splitKey.replace('.', '').trim()), true)
21-
} else {
22-
curr.set(splitKey.replace('.', '').trim(), true)
19+
20+
if (!supportRe.test(key)) {
21+
for (const splitKey of splitKeys) {
22+
if (parentKey === ':export' || splitKey.startsWith('.')) {
23+
if (toParseCase) {
24+
curr.set(toParseCase(splitKey.replace('.', '').trim()), true)
25+
} else {
26+
curr.set(splitKey.replace('.', '').trim(), true)
27+
}
2328
}
2429
}
2530
}

src/index.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import prettier from 'prettier'
22
const { resolveConfig } = prettier
33

4-
import type { Plugin as VitePlugin } from 'vite'
4+
import { Plugin as VitePlugin, createFilter } from 'vite'
55
import { main } from './main'
66
import type { FinalConfig, PluginOptions } from './type'
77
import { isCSSModuleRequest } from './util'
88

99
export default function Plugin(option: PluginOptions = {}): VitePlugin {
1010
let cacheConfig: FinalConfig
11+
let filter: ReturnType<typeof createFilter>
1112
const enabledMode = option.enabledMode || ['development']
1213
return {
1314
name: 'vite-plugin-sass-dts',
1415
async configResolved(config) {
16+
filter = createFilter(undefined, option.excludePath)
1517
const prettierOptions =
1618
(await resolveConfig(option.prettierFilePath || config.root)) || {}
1719
cacheConfig = {
@@ -20,15 +22,16 @@ export default function Plugin(option: PluginOptions = {}): VitePlugin {
2022
}
2123
},
2224
handleHotUpdate(context) {
23-
if (!isCSSModuleRequest(context.file)) return
25+
if (!isCSSModuleRequest(context.file) || !filter(context.file)) return
2426
main(context.file, cacheConfig, option)
2527
return
2628
},
2729
transform(code, id) {
2830
const fileName = id.replace(/(?:\?|&)(used|direct|inline|vue).*/, '')
2931
if (
3032
!enabledMode.includes(cacheConfig.env.MODE) ||
31-
!isCSSModuleRequest(fileName)
33+
!isCSSModuleRequest(fileName) ||
34+
!filter(id)
3235
) {
3336
// returning undefined will signal vite that the file has not been transformed
3437
// avoiding warnings about source maps not being generated
@@ -40,7 +43,7 @@ export default function Plugin(option: PluginOptions = {}): VitePlugin {
4043
)
4144
},
4245
watchChange(id) {
43-
if (isCSSModuleRequest(id)) {
46+
if (isCSSModuleRequest(id) && filter(id)) {
4447
this.addWatchFile(id)
4548
}
4649
},

src/type.ts

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export type PluginOptions = {
1414
esmExport?: boolean
1515
outputDir?: string
1616
sourceDir?: string
17+
excludePath?: string | RegExp | Array<string | RegExp>
1718
prettierFilePath?: string
1819
}
1920

0 commit comments

Comments
 (0)