1
1
import { createUnplugin } from 'unplugin'
2
2
import type { Options } from './types'
3
3
import { pluginName } from './constants'
4
- import { getClassCacheSet } from 'tailwindcss-patch'
5
4
import { getGroupedEntries } from './utils'
6
- import { OutputAsset , OutputChunk } from 'rollup'
7
- import ClassGenerator from './classGenerator'
5
+ import type { OutputAsset , OutputChunk } from 'rollup'
8
6
import { htmlHandler } from './html'
9
7
import { jsHandler } from './js'
10
8
import { cssHandler } from './css'
11
9
import type { sources } from 'webpack'
12
10
import path from 'path'
13
11
import fs from 'fs'
12
+ import { getOptions } from './options'
14
13
14
+ // cache map
15
15
const outputCachedMap = new Map <
16
16
string ,
17
17
{
@@ -22,27 +22,7 @@ const outputCachedMap = new Map<
22
22
> ( )
23
23
24
24
export const unplugin = createUnplugin ( ( options : Options | undefined = { } , meta ) => {
25
- const isMangleClass = ( className : string ) => {
26
- // ignore className like 'filter','container'
27
- // it may be dangerous to mangle/rename all StringLiteral , so use /-/ test for only those with /-/ like:
28
- // bg-[#123456] w-1 etc...
29
- return / [ - : ] / . test ( className )
30
- }
31
- let classSet : Set < string >
32
- // let cached: boolean
33
- const classGenerator = new ClassGenerator ( options . classGenerator )
34
- function getCachedClassSet ( ) {
35
- const set = getClassCacheSet ( )
36
- set . forEach ( ( c ) => {
37
- if ( ! isMangleClass ( c ) ) {
38
- set . delete ( c )
39
- }
40
- } )
41
-
42
- classSet = set
43
-
44
- return classSet
45
- }
25
+ const { classGenerator, getCachedClassSet, isInclude } = getOptions ( options )
46
26
47
27
return {
48
28
name : pluginName ,
@@ -58,30 +38,36 @@ export const unplugin = createUnplugin((options: Options | undefined = {}, meta)
58
38
59
39
if ( Array . isArray ( groupedEntries . html ) && groupedEntries . html . length ) {
60
40
for ( let i = 0 ; i < groupedEntries . html . length ; i ++ ) {
61
- const [ , asset ] = groupedEntries . html [ i ] as [ string , OutputAsset ]
62
- asset . source = htmlHandler ( asset . source . toString ( ) , {
63
- classGenerator,
64
- runtimeSet
65
- } )
41
+ const [ file , asset ] = groupedEntries . html [ i ] as [ string , OutputAsset ]
42
+ if ( isInclude ( file ) ) {
43
+ asset . source = htmlHandler ( asset . source . toString ( ) , {
44
+ classGenerator,
45
+ runtimeSet
46
+ } )
47
+ }
66
48
}
67
49
}
68
50
if ( Array . isArray ( groupedEntries . js ) && groupedEntries . js . length ) {
69
51
for ( let i = 0 ; i < groupedEntries . js . length ; i ++ ) {
70
- const [ , chunk ] = groupedEntries . js [ i ] as [ string , OutputChunk ]
71
- chunk . code = jsHandler ( chunk . code , {
72
- runtimeSet,
73
- classGenerator
74
- } ) . code
52
+ const [ file , chunk ] = groupedEntries . js [ i ] as [ string , OutputChunk ]
53
+ if ( isInclude ( file ) ) {
54
+ chunk . code = jsHandler ( chunk . code , {
55
+ runtimeSet,
56
+ classGenerator
57
+ } ) . code
58
+ }
75
59
}
76
60
}
77
61
78
62
if ( Array . isArray ( groupedEntries . css ) && groupedEntries . css . length ) {
79
63
for ( let i = 0 ; i < groupedEntries . css . length ; i ++ ) {
80
- const [ , css ] = groupedEntries . css [ i ] as [ string , OutputAsset ]
81
- css . source = cssHandler ( css . source . toString ( ) , {
82
- classGenerator,
83
- runtimeSet
84
- } )
64
+ const [ file , css ] = groupedEntries . css [ i ] as [ string , OutputAsset ]
65
+ if ( isInclude ( file ) ) {
66
+ css . source = cssHandler ( css . source . toString ( ) , {
67
+ classGenerator,
68
+ runtimeSet
69
+ } )
70
+ }
85
71
}
86
72
}
87
73
}
@@ -120,7 +106,7 @@ export const unplugin = createUnplugin((options: Options | undefined = {}, meta)
120
106
// console.log(compiler.outputPath)
121
107
const runtimeSet = getCachedClassSet ( )
122
108
const groupedEntries = getGroupedEntries ( Object . entries ( assets ) )
123
-
109
+ // cache result
124
110
if ( ! runtimeSet . size ) {
125
111
const css = new Map ( )
126
112
const html = new Map ( )
@@ -150,75 +136,84 @@ export const unplugin = createUnplugin((options: Options | undefined = {}, meta)
150
136
if ( groupedEntries . html . length ) {
151
137
for ( let i = 0 ; i < groupedEntries . html . length ; i ++ ) {
152
138
const [ file , asset ] = groupedEntries . html [ i ]
153
- const html = htmlHandler ( asset . source ( ) . toString ( ) , {
154
- classGenerator,
155
- runtimeSet
156
- } )
157
- const source = new ConcatSource ( html )
158
- compilation . updateAsset ( file , source )
159
- }
160
- }
161
- outputCachedMap . forEach ( ( { html } , key ) => {
162
- if ( html . size ) {
163
- html . forEach ( ( asset , file ) => {
164
- const html = htmlHandler ( ( asset as sources . Source ) . source ( ) . toString ( ) , {
139
+ if ( isInclude ( file ) ) {
140
+ const html = htmlHandler ( asset . source ( ) . toString ( ) , {
165
141
classGenerator,
166
142
runtimeSet
167
143
} )
168
- overwriteServerSideAsset ( key , file , html )
169
- } )
170
- html . clear ( )
144
+ const source = new ConcatSource ( html )
145
+ compilation . updateAsset ( file , source )
146
+ }
171
147
}
172
- } )
148
+ }
173
149
174
150
if ( groupedEntries . js . length ) {
175
151
for ( let i = 0 ; i < groupedEntries . js . length ; i ++ ) {
176
152
const [ file , chunk ] = groupedEntries . js [ i ]
177
- const code = jsHandler ( chunk . source ( ) . toString ( ) , {
178
- runtimeSet,
179
- classGenerator
180
- } ) . code
181
- const source = new ConcatSource ( code )
182
- compilation . updateAsset ( file , source )
183
- }
184
- }
185
-
186
- outputCachedMap . forEach ( ( { js } , key ) => {
187
- if ( js . size ) {
188
- js . forEach ( ( chunk , file ) => {
189
- const rawCode = ( chunk as sources . Source ) . source ( ) . toString ( )
190
- const code = jsHandler ( rawCode , {
153
+ if ( isInclude ( file ) ) {
154
+ const code = jsHandler ( chunk . source ( ) . toString ( ) , {
191
155
runtimeSet,
192
156
classGenerator
193
157
} ) . code
194
-
195
- overwriteServerSideAsset ( key , file , code )
196
- } )
197
- js . clear ( )
158
+ const source = new ConcatSource ( code )
159
+ compilation . updateAsset ( file , source )
160
+ }
198
161
}
199
- } )
162
+ }
200
163
201
164
if ( groupedEntries . css . length ) {
202
165
for ( let i = 0 ; i < groupedEntries . css . length ; i ++ ) {
203
166
const [ file , css ] = groupedEntries . css [ i ]
204
- const newCss = cssHandler ( css . source ( ) . toString ( ) , {
205
- classGenerator,
206
- runtimeSet
207
- } )
208
- const source = new ConcatSource ( newCss )
209
- compilation . updateAsset ( file , source )
167
+ if ( isInclude ( file ) ) {
168
+ const newCss = cssHandler ( css . source ( ) . toString ( ) , {
169
+ classGenerator,
170
+ runtimeSet
171
+ } )
172
+ const source = new ConcatSource ( newCss )
173
+ compilation . updateAsset ( file , source )
174
+ }
210
175
}
211
176
}
177
+ // overwrite ssr server side chunk
178
+ outputCachedMap . forEach ( ( { js, html, css } , key ) => {
179
+ if ( html . size ) {
180
+ html . forEach ( ( asset , file ) => {
181
+ if ( isInclude ( file ) ) {
182
+ const html = htmlHandler ( ( asset as sources . Source ) . source ( ) . toString ( ) , {
183
+ classGenerator,
184
+ runtimeSet
185
+ } )
186
+ overwriteServerSideAsset ( key , file , html )
187
+ }
188
+ } )
189
+ html . clear ( )
190
+ }
191
+
192
+ if ( js . size ) {
193
+ js . forEach ( ( chunk , file ) => {
194
+ if ( isInclude ( file ) ) {
195
+ const rawCode = ( chunk as sources . Source ) . source ( ) . toString ( )
196
+ const code = jsHandler ( rawCode , {
197
+ runtimeSet,
198
+ classGenerator
199
+ } ) . code
200
+
201
+ overwriteServerSideAsset ( key , file , code )
202
+ }
203
+ } )
204
+ js . clear ( )
205
+ }
212
206
213
- outputCachedMap . forEach ( ( { css } , key ) => {
214
207
if ( css . size ) {
215
208
css . forEach ( ( style , file ) => {
216
- const newCss = cssHandler ( ( style as sources . Source ) . source ( ) . toString ( ) , {
217
- classGenerator,
218
- runtimeSet
219
- } )
220
-
221
- overwriteServerSideAsset ( key , file , newCss )
209
+ if ( isInclude ( file ) ) {
210
+ const newCss = cssHandler ( ( style as sources . Source ) . source ( ) . toString ( ) , {
211
+ classGenerator,
212
+ runtimeSet
213
+ } )
214
+
215
+ overwriteServerSideAsset ( key , file , newCss )
216
+ }
222
217
} )
223
218
css . clear ( )
224
219
}
0 commit comments