Skip to content

Commit

Permalink
Merge pull request #103 from /issues/100
Browse files Browse the repository at this point in the history
Fix bugs
  • Loading branch information
activeguild authored Sep 11, 2024
2 parents 81679ba + 502480d commit 315e31a
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 11 deletions.
6 changes: 6 additions & 0 deletions example/react-sass/src/App.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ $green: #00c851;
.test {
@apply px-1.5;
}

@supports (--num: 1.5) {
.myclass {
display: block;
}
}
1 change: 1 addition & 0 deletions example/react-sass/src/App.module.scss.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ declare const classNames: typeof globalClassNames & {
readonly active: 'active'
readonly input: 'input'
readonly test: 'test'
readonly myclass: 'myclass'
}
export default classNames
1 change: 1 addition & 0 deletions example/react-sass/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { FC } from 'react'
import styles from './App.module.scss'
import { classNamesFunc } from 'classnames-generics'
import { User } from './User/User'
import styles2 from './exclude/Exclude.module.scss'

const classNames = classNamesFunc<keyof typeof styles>()
type Props = {
Expand Down
5 changes: 5 additions & 0 deletions example/react-sass/src/exclude/Exclude.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.exclude {
&.active {
background-color: red;
}
}
1 change: 1 addition & 0 deletions example/react-sass/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default defineConfig({
outputFilePath: path.resolve(__dirname, './src/@types/style.d.ts'),
},
prettierFilePath: path.resolve('../../.prettierrc.cjs'),
excludePath: ['./src/exclude/*'],
esmExport: true,
// typeName: {
// replacement: (fileName) => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vite-plugin-sass-dts",
"version": "1.3.24",
"version": "1.3.25",
"engines": {
"node": ">=20"
},
Expand Down
17 changes: 11 additions & 6 deletions src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { CSSJSObj, GetParseCaseFunction } from './type'

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

export const extractClassNameKeys = (
Expand All @@ -12,14 +13,18 @@ export const extractClassNameKeys = (
): Map<string, boolean> => {
return Object.entries(obj).reduce<Map<string, boolean>>(
(curr, [key, value]) => {
console.log(key, value)
if (importRe.test(key)) return curr
const splitKeys = key.split(keySeparatorRe)
for (const splitKey of splitKeys) {
if (parentKey === ':export' || splitKey.startsWith('.')) {
if (toParseCase) {
curr.set(toParseCase(splitKey.replace('.', '').trim()), true)
} else {
curr.set(splitKey.replace('.', '').trim(), true)

if (!supportRe.test(key)) {
for (const splitKey of splitKeys) {
if (parentKey === ':export' || splitKey.startsWith('.')) {
if (toParseCase) {
curr.set(toParseCase(splitKey.replace('.', '').trim()), true)
} else {
curr.set(splitKey.replace('.', '').trim(), true)
}
}
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import prettier from 'prettier'
const { resolveConfig } = prettier

import type { Plugin as VitePlugin } from 'vite'
import { Plugin as VitePlugin, createFilter } from 'vite'
import { main } from './main'
import type { FinalConfig, PluginOptions } from './type'
import { isCSSModuleRequest } from './util'

export default function Plugin(option: PluginOptions = {}): VitePlugin {
let cacheConfig: FinalConfig
let filter: ReturnType<typeof createFilter>
const enabledMode = option.enabledMode || ['development']
return {
name: 'vite-plugin-sass-dts',
async configResolved(config) {
filter = createFilter(undefined, option.excludePath)
const prettierOptions =
(await resolveConfig(option.prettierFilePath || config.root)) || {}
cacheConfig = {
Expand All @@ -20,15 +22,16 @@ export default function Plugin(option: PluginOptions = {}): VitePlugin {
}
},
handleHotUpdate(context) {
if (!isCSSModuleRequest(context.file)) return
if (!isCSSModuleRequest(context.file) || !filter(context.file)) return
main(context.file, cacheConfig, option)
return
},
transform(code, id) {
const fileName = id.replace(/(?:\?|&)(used|direct|inline|vue).*/, '')
if (
!enabledMode.includes(cacheConfig.env.MODE) ||
!isCSSModuleRequest(fileName)
!isCSSModuleRequest(fileName) ||
!filter(id)
) {
// returning undefined will signal vite that the file has not been transformed
// avoiding warnings about source maps not being generated
Expand All @@ -40,7 +43,7 @@ export default function Plugin(option: PluginOptions = {}): VitePlugin {
)
},
watchChange(id) {
if (isCSSModuleRequest(id)) {
if (isCSSModuleRequest(id) && filter(id)) {
this.addWatchFile(id)
}
},
Expand Down
1 change: 1 addition & 0 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type PluginOptions = {
esmExport?: boolean
outputDir?: string
sourceDir?: string
excludePath?: string | RegExp | Array<string | RegExp>
prettierFilePath?: string
}

Expand Down

1 comment on commit 315e31a

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bundled size for the package is listed below:

dist: 42.97 KB

Please sign in to comment.