1
- import type { Options , ClassSetOutputOptions } from './types'
1
+ import type { Options , ClassSetOutputOptions , ClassMapOutputOptions } from './types'
2
2
import { getClassCacheSet } from 'tailwindcss-patch'
3
3
import ClassGenerator from './classGenerator'
4
- import { createGlobMatcher } from './utils'
5
- import fs from 'fs'
6
- import path from 'path'
7
- import { pluginName } from './constants'
8
-
9
- export function mkCacheDirectory ( cwd = process . cwd ( ) ) {
10
- const cacheDirectory = path . resolve ( cwd , 'node_modules' , '.cache' , pluginName )
11
-
12
- const exists = fs . existsSync ( cacheDirectory )
13
- if ( ! exists ) {
14
- fs . mkdirSync ( cacheDirectory , {
15
- recursive : true
16
- } )
17
- }
18
- return cacheDirectory
19
- }
4
+ import { createGlobMatcher , isMangleClass , cacheDump } from './utils'
20
5
21
6
export function getOptions ( options : Options | undefined = { } ) {
22
7
const includeMatcher = createGlobMatcher ( options . include , true )
@@ -26,40 +11,39 @@ export function getOptions(options: Options | undefined = {}) {
26
11
return includeMatcher ( file ) && ! excludeMatcher ( file )
27
12
}
28
13
29
- const isMangleClass = ( className : string ) => {
30
- // ignore className like 'filter','container'
31
- // it may be dangerous to mangle/rename all StringLiteral , so use /-/ test for only those with /-/ like:
32
- // bg-[#123456] w-1 etc...
33
- return / [ - : ] / . test ( className )
34
- }
35
14
let classSet : Set < string >
36
15
37
16
const classSetOutputOptions : ClassSetOutputOptions = {
38
- filename : path . resolve ( process . cwd ( ) , 'node_modules' , '.cache' , pluginName , ' classSet.json') ,
17
+ filename : ' classSet.json',
39
18
type : 'partial'
40
19
}
20
+
21
+ const classMapOutputOptions : ClassMapOutputOptions = {
22
+ filename : 'classMap.json'
23
+ }
24
+
41
25
if ( typeof options . classSetOutput === 'object' ) {
42
26
Object . assign ( classSetOutputOptions , options . classSetOutput )
43
27
}
44
-
45
- function writeClassSetJson ( set : Set < string > ) {
46
- mkCacheDirectory ( )
47
- fs . writeFileSync ( classSetOutputOptions . filename , JSON . stringify ( Array . from ( set ) , null , 2 ) , 'utf-8' )
28
+ if ( typeof options . classMapOutput === 'object' ) {
29
+ Object . assign ( classMapOutputOptions , options . classMapOutput )
48
30
}
31
+
49
32
// let cached: boolean
50
33
const classGenerator = new ClassGenerator ( options . classGenerator )
51
34
function getCachedClassSet ( ) {
52
35
const set = getClassCacheSet ( )
53
- if ( set . size && options . classSetOutput && classSetOutputOptions . type === 'all' ) {
54
- writeClassSetJson ( set )
36
+ const isOutput = set . size && options . classSetOutput
37
+ if ( isOutput && classSetOutputOptions . type === 'all' ) {
38
+ cacheDump ( classSetOutputOptions . filename , set , classSetOutputOptions . dir )
55
39
}
56
40
set . forEach ( ( c ) => {
57
41
if ( ! isMangleClass ( c ) ) {
58
42
set . delete ( c )
59
43
}
60
44
} )
61
- if ( set . size && options . classSetOutput && classSetOutputOptions . type === 'partial' ) {
62
- writeClassSetJson ( set )
45
+ if ( isOutput && classSetOutputOptions . type === 'partial' ) {
46
+ cacheDump ( classSetOutputOptions . filename , set , classSetOutputOptions . dir )
63
47
}
64
48
65
49
classSet = set
@@ -72,6 +56,8 @@ export function getOptions(options: Options | undefined = {}) {
72
56
classGenerator,
73
57
includeMatcher,
74
58
excludeMatcher,
75
- isInclude
59
+ isInclude,
60
+ classSetOutputOptions,
61
+ classMapOutputOptions
76
62
}
77
63
}
0 commit comments