@@ -3,6 +3,7 @@ import { Options } from './interfaces'
33import { handleSource } from './universal-handler'
44import { isStyleFile , isTemplateFile } from './utilities'
55import { FileType } from './enum'
6+ import { ConcatSource } from 'webpack-sources'
67
78export default class MiniProgramTailwindWebpackPlugin implements WebpackPluginInstance {
89
@@ -20,50 +21,97 @@ export default class MiniProgramTailwindWebpackPlugin implements WebpackPluginIn
2021
2122 apply ( compiler : Compiler ) {
2223
23- const { webpack } = compiler
24- const { sources, Compilation } = webpack
25- const { ConcatSource } = sources
24+ const isWebpackV5 = compiler . webpack && compiler . webpack . version >= '5'
2625
27- compiler . hooks . thisCompilation . tap (
28- MiniProgramTailwindWebpackPlugin . pluginName ,
29- compilation => {
26+ if ( isWebpackV5 ) {
3027
31- compilation . hooks . processAssets . tap (
32- {
33- name : MiniProgramTailwindWebpackPlugin . pluginName ,
34- stage : Compilation . PROCESS_ASSETS_STAGE_SUMMARIZE ,
35- } ,
36- assets => {
28+ const { webpack } = compiler
29+ const { sources, Compilation } = webpack
30+ const { ConcatSource } = sources
3731
38- for ( const pathname in assets ) {
32+ compiler . hooks . thisCompilation . tap (
33+ MiniProgramTailwindWebpackPlugin . pluginName ,
34+ compilation => {
3935
40- const originalSource = assets [ pathname ]
41- const rawSource = originalSource . source ( ) . toString ( )
36+ compilation . hooks . processAssets . tap (
37+ {
38+ name : MiniProgramTailwindWebpackPlugin . pluginName ,
39+ stage : Compilation . PROCESS_ASSETS_STAGE_SUMMARIZE ,
40+ } ,
41+ assets => {
4242
43- let handledSource = ''
43+ for ( const pathname in assets ) {
44+
45+ const originalSource = assets [ pathname ]
46+ const rawSource = originalSource . source ( ) . toString ( )
47+
48+ let handledSource = ''
49+
50+ if ( isStyleFile ( pathname ) ) {
51+ handledSource = handleSource ( FileType . Style , rawSource , this . options )
52+ } else if ( isTemplateFile ( pathname ) ) {
53+ handledSource = handleSource ( FileType . Template , rawSource , this . options )
54+ }
55+
56+ if ( handledSource ) {
57+
58+ const source = new ConcatSource ( handledSource )
59+
60+ compilation . updateAsset ( pathname , source )
61+
62+ }
4463
45- if ( isStyleFile ( pathname ) ) {
46- handledSource = handleSource ( FileType . Style , rawSource , this . options )
47- } else if ( isTemplateFile ( pathname ) ) {
48- handledSource = handleSource ( FileType . Template , rawSource , this . options )
4964 }
5065
51- if ( handledSource ) {
66+ } ,
67+
68+ )
69+
70+ } ,
71+ )
72+
73+ } else {
74+
75+ compiler . hooks . thisCompilation . tap (
76+ MiniProgramTailwindWebpackPlugin . pluginName ,
77+ compilation => {
78+
79+ compilation . hooks . afterOptimizeAssets . tap (
80+ MiniProgramTailwindWebpackPlugin . pluginName ,
81+ assets => {
82+
83+ for ( const pathname in assets ) {
84+
85+ const originalSource = assets [ pathname ]
86+ const rawSource = originalSource . source ( ) . toString ( )
87+
88+ let handledSource = ''
89+
90+ if ( isStyleFile ( pathname ) ) {
91+ handledSource = handleSource ( FileType . Style , rawSource , this . options )
92+ } else if ( isTemplateFile ( pathname ) ) {
93+ handledSource = handleSource ( FileType . Template , rawSource , this . options )
94+ }
95+
96+ if ( handledSource ) {
97+
98+ const source = new ConcatSource ( handledSource )
5299
53- const source = new ConcatSource ( handledSource )
100+ // @ts -ignore
101+ compilation . updateAsset ( pathname , source )
54102
55- compilation . updateAsset ( pathname , source )
103+ }
56104
57105 }
58106
59- }
107+ } ,
60108
61- } ,
109+ )
62110
63- )
111+ } ,
112+ )
64113
65- } ,
66- )
114+ }
67115
68116 }
69117
0 commit comments