@@ -8,7 +8,22 @@ import ClassGenerator from './classGenerator'
8
8
import { htmlHandler } from './html'
9
9
import { jsHandler } from './js'
10
10
import { cssHandler } from './css'
11
- import type { } from 'webpack'
11
+ import type { sources } from 'webpack'
12
+ import path from 'path'
13
+
14
+ // const cachedHtmlSource = new Map<string, sources.Source | OutputAsset>()
15
+ // const cachedJsSource = new Map<string, sources.Source | OutputChunk>()
16
+ // const cachedCssSource = new Map<string, sources.Source | OutputAsset>()
17
+
18
+ const outputCachedMap = new Map <
19
+ string ,
20
+ {
21
+ html : Map < string , sources . Source | OutputAsset >
22
+ js : Map < string , sources . Source | OutputChunk >
23
+ css : Map < string , sources . Source | OutputAsset >
24
+ }
25
+ > ( )
26
+
12
27
export const unplugin = createUnplugin ( ( options : Options | undefined = { } , meta ) => {
13
28
const isMangleClass = ( className : string ) => {
14
29
// ignore className like 'filter','container'
@@ -31,6 +46,7 @@ export const unplugin = createUnplugin((options: Options | undefined = {}, meta)
31
46
32
47
return classSet
33
48
}
49
+
34
50
return {
35
51
name : pluginName ,
36
52
enforce : 'post' ,
@@ -77,21 +93,44 @@ export const unplugin = createUnplugin((options: Options | undefined = {}, meta)
77
93
webpack ( compiler ) {
78
94
const Compilation = compiler . webpack . Compilation
79
95
const { ConcatSource } = compiler . webpack . sources
96
+
80
97
compiler . hooks . compilation . tap ( pluginName , ( compilation ) => {
81
98
compilation . hooks . processAssets . tap (
82
99
{
83
100
name : pluginName ,
84
101
stage : Compilation . PROCESS_ASSETS_STAGE_SUMMARIZE
85
102
} ,
86
103
( assets ) => {
104
+ // nextjs webpack build multiple times
105
+ // server / manifest / client
87
106
// const resolvePath = require.resolve('tailwindcss')
88
107
// console.log(resolvePath)
108
+ // console.log(compiler.outputPath)
89
109
const runtimeSet = getCachedClassSet ( )
110
+ const groupedEntries = getGroupedEntries ( Object . entries ( assets ) )
111
+
90
112
if ( ! runtimeSet . size ) {
113
+ const css = new Map ( )
114
+ const html = new Map ( )
115
+ const js = new Map ( )
116
+ groupedEntries . css . forEach ( ( [ file , source ] ) => {
117
+ css . set ( file , source )
118
+ } )
119
+ groupedEntries . html . forEach ( ( [ file , source ] ) => {
120
+ html . set ( file , source )
121
+ } )
122
+ groupedEntries . js . forEach ( ( [ file , source ] ) => {
123
+ js . set ( file , source )
124
+ } )
125
+ outputCachedMap . set ( compiler . outputPath , {
126
+ css,
127
+ html,
128
+ js
129
+ } )
91
130
return
92
131
}
93
- const groupedEntries = getGroupedEntries ( Object . entries ( assets ) )
94
- if ( Array . isArray ( groupedEntries . html ) && groupedEntries . html . length ) {
132
+
133
+ if ( groupedEntries . html . length ) {
95
134
for ( let i = 0 ; i < groupedEntries . html . length ; i ++ ) {
96
135
const [ file , asset ] = groupedEntries . html [ i ]
97
136
const html = htmlHandler ( asset . source ( ) . toString ( ) , {
@@ -102,7 +141,21 @@ export const unplugin = createUnplugin((options: Options | undefined = {}, meta)
102
141
compilation . updateAsset ( file , source )
103
142
}
104
143
}
105
- if ( Array . isArray ( groupedEntries . js ) && groupedEntries . js . length ) {
144
+ outputCachedMap . forEach ( ( { html } , key ) => {
145
+ if ( html . size ) {
146
+ html . forEach ( ( asset , file ) => {
147
+ const html = htmlHandler ( ( asset as sources . Source ) . source ( ) . toString ( ) , {
148
+ classGenerator,
149
+ runtimeSet
150
+ } )
151
+ const source = new ConcatSource ( html )
152
+ compilation . emitAsset ( path . resolve ( key , file ) , source )
153
+ } )
154
+ html . clear ( )
155
+ }
156
+ } )
157
+
158
+ if ( groupedEntries . js . length ) {
106
159
for ( let i = 0 ; i < groupedEntries . js . length ; i ++ ) {
107
160
const [ file , chunk ] = groupedEntries . js [ i ]
108
161
const code = jsHandler ( chunk . source ( ) . toString ( ) , {
@@ -114,7 +167,21 @@ export const unplugin = createUnplugin((options: Options | undefined = {}, meta)
114
167
}
115
168
}
116
169
117
- if ( Array . isArray ( groupedEntries . css ) && groupedEntries . css . length ) {
170
+ outputCachedMap . forEach ( ( { js } , key ) => {
171
+ if ( js . size ) {
172
+ js . forEach ( ( chunk , file ) => {
173
+ const code = jsHandler ( ( chunk as sources . Source ) . source ( ) . toString ( ) , {
174
+ runtimeSet,
175
+ classGenerator
176
+ } ) . code
177
+ const source = new ConcatSource ( code )
178
+ compilation . emitAsset ( path . resolve ( key , file ) , source )
179
+ } )
180
+ js . clear ( )
181
+ }
182
+ } )
183
+
184
+ if ( groupedEntries . css . length ) {
118
185
for ( let i = 0 ; i < groupedEntries . css . length ; i ++ ) {
119
186
const [ file , css ] = groupedEntries . css [ i ]
120
187
const newCss = cssHandler ( css . source ( ) . toString ( ) , {
@@ -125,6 +192,20 @@ export const unplugin = createUnplugin((options: Options | undefined = {}, meta)
125
192
compilation . updateAsset ( file , source )
126
193
}
127
194
}
195
+
196
+ outputCachedMap . forEach ( ( { css } , key ) => {
197
+ if ( css . size ) {
198
+ css . forEach ( ( style , file ) => {
199
+ const newCss = cssHandler ( ( style as sources . Source ) . source ( ) . toString ( ) , {
200
+ classGenerator,
201
+ runtimeSet
202
+ } )
203
+ const source = new ConcatSource ( newCss )
204
+ compilation . emitAsset ( path . resolve ( key , file ) , source )
205
+ } )
206
+ css . clear ( )
207
+ }
208
+ } )
128
209
}
129
210
)
130
211
} )
0 commit comments