@@ -12,10 +12,6 @@ import type { sources } from 'webpack'
12
12
import path from 'path'
13
13
import fs from 'fs'
14
14
15
- // const cachedHtmlSource = new Map<string, sources.Source | OutputAsset>()
16
- // const cachedJsSource = new Map<string, sources.Source | OutputChunk>()
17
- // const cachedCssSource = new Map<string, sources.Source | OutputAsset>()
18
-
19
15
const outputCachedMap = new Map <
20
16
string ,
21
17
{
@@ -94,8 +90,21 @@ export const unplugin = createUnplugin((options: Options | undefined = {}, meta)
94
90
webpack ( compiler ) {
95
91
const Compilation = compiler . webpack . Compilation
96
92
const { ConcatSource } = compiler . webpack . sources
97
- function getEmitAssetPath ( outputPath : string , file : string ) {
98
- return path . relative ( compiler . context , path . resolve ( outputPath , file ) )
93
+ function getAssetPath ( outputPath : string , file : string , abs : boolean = true ) {
94
+ const fn = abs ? path . resolve : path . relative
95
+ return fn ( compiler . context , path . resolve ( outputPath , file ) )
96
+ }
97
+
98
+ function overwriteServerSideAsset ( outputPath : string , file : string , data : string ) {
99
+ const abs = getAssetPath ( outputPath , file )
100
+ const rel = getAssetPath ( outputPath , file , false )
101
+ try {
102
+ fs . writeFileSync ( abs , data , 'utf-8' )
103
+ console . log ( '[tailwindcss-mangle]: ' + rel + ' overwrited successfully' )
104
+ } catch ( error ) {
105
+ console . log ( '[tailwindcss-mangle]: ' + rel + ' overwrited fail!' )
106
+ console . log ( error )
107
+ }
99
108
}
100
109
compiler . hooks . compilation . tap ( pluginName , ( compilation ) => {
101
110
compilation . hooks . processAssets . tap (
@@ -156,8 +165,7 @@ export const unplugin = createUnplugin((options: Options | undefined = {}, meta)
156
165
classGenerator,
157
166
runtimeSet
158
167
} )
159
- const source = new ConcatSource ( html )
160
- compilation . emitAsset ( getEmitAssetPath ( key , file ) , source )
168
+ overwriteServerSideAsset ( key , file , html )
161
169
} )
162
170
html . clear ( )
163
171
}
@@ -183,11 +191,8 @@ export const unplugin = createUnplugin((options: Options | undefined = {}, meta)
183
191
runtimeSet,
184
192
classGenerator
185
193
} ) . code
186
- // fs.writeFileSync(path.basename(file), rawCode)
187
- // fs.writeFileSync(path.basename(file) + '.out', code)
188
- const source = new ConcatSource ( code )
189
- // can not overwrite webpack server side output
190
- compilation . emitAsset ( getEmitAssetPath ( key , file ) , source )
194
+
195
+ overwriteServerSideAsset ( key , file , code )
191
196
} )
192
197
js . clear ( )
193
198
}
@@ -212,8 +217,8 @@ export const unplugin = createUnplugin((options: Options | undefined = {}, meta)
212
217
classGenerator,
213
218
runtimeSet
214
219
} )
215
- const source = new ConcatSource ( newCss )
216
- compilation . emitAsset ( getEmitAssetPath ( key , file ) , source )
220
+
221
+ overwriteServerSideAsset ( key , file , newCss )
217
222
} )
218
223
css . clear ( )
219
224
}
0 commit comments