@@ -107,8 +107,7 @@ export const restOutPut = (conf: WebpackConfiguration, options: WebpackConfigura
107
107
} ;
108
108
} ;
109
109
110
- // 开发模式下 把 css 进行处理
111
- export const restDevModuleRuleCss = ( conf : WebpackConfiguration , ) : WebpackConfiguration => {
110
+ export const addMiniCssExtractPlugin = ( conf : WebpackConfiguration ) : WebpackConfiguration => {
112
111
return {
113
112
...conf ,
114
113
plugins : conf . plugins . concat ( [
@@ -120,6 +119,14 @@ export const restDevModuleRuleCss = (conf: WebpackConfiguration,): WebpackConfig
120
119
chunkFilename : 'static/css/[name].[contenthash:8].chunk.css' ,
121
120
} )
122
121
] ) ,
122
+ }
123
+ }
124
+
125
+ // node 环境 把 css 进行处理
126
+ export const restDevModuleRuleCss = ( conf : WebpackConfiguration , ) : WebpackConfiguration => {
127
+ return {
128
+ ...conf ,
129
+ plugins : conf . plugins . filter ( plugin => plugin && plugin . constructor && plugin . constructor . name !== "MiniCssExtractPlugin" ) ,
123
130
module : {
124
131
...conf . module ,
125
132
rules : getModuleCSSRules ( conf . module . rules , )
@@ -128,19 +135,35 @@ export const restDevModuleRuleCss = (conf: WebpackConfiguration,): WebpackConfig
128
135
}
129
136
130
137
/**
131
- * 1. 开发模式下, 去除 style-loader 改成 MiniCssExtractPlugin.lader,让他生成 css 文件
138
+ * 1. 去除 style-loader|mini- css-extract-plugin
132
139
* */
133
- export const getModuleCSSRules = ( rules : ( webpack . RuleSetRule | '...' ) [ ] , ) => {
140
+ export const getModuleCSSRules = ( rules : ( webpack . RuleSetRule | '...' ) [ ] , shouldUseSourceMap : boolean = false ) => {
134
141
const newRules : any = [ ] ;
142
+
143
+ const cssModuleOption = ( mode : "icss" | "local" ) => ( {
144
+ importLoaders : 3 ,
145
+ sourceMap : shouldUseSourceMap ,
146
+ modules : {
147
+ mode,
148
+ exportOnlyLocals : true
149
+ } ,
150
+ } ) ;
151
+
152
+
135
153
rules . forEach ( ( rule ) => {
136
154
if ( typeof rule === 'string' ) {
137
155
newRules . push ( rule ) ;
138
156
return ;
139
157
}
140
- if ( / s t y l e - l o a d e r / . test ( rule . loader ) ) {
158
+
159
+ if ( / ( s t y l e - l o a d e r | m i n i - c s s - e x t r a c t - p l u g i n ) / . test ( rule . loader ) ) {
160
+
161
+ } else if ( rule && typeof rule === 'object' && rule . test && / c s s - l o a d e r / . test ( rule . loader ) && ! / p o s t c s s - l o a d e r / . test ( rule . loader ) ) {
162
+ const isModule = [ getToString ( cssModuleRegex ) , getToString ( sassModuleRegex ) , getToString ( lessModuleRegex ) , ] . includes ( rule . test . toString ( ) )
141
163
newRules . push ( {
142
- loader : MiniCssExtractPlugin . loader ,
143
- } ) ;
164
+ loader : require . resolve ( "css-loader" ) ,
165
+ options : cssModuleOption ( isModule ? "local" : "icss" )
166
+ } )
144
167
} else if ( rule . oneOf ) {
145
168
const newOneOf = rule . oneOf . map ( ( item ) => {
146
169
if (
@@ -154,24 +177,31 @@ export const getModuleCSSRules = (rules: (webpack.RuleSetRule | '...')[],) => {
154
177
getToString ( lessRegex ) ,
155
178
] . includes ( item . test . toString ( ) )
156
179
) {
180
+ const isModule = [ getToString ( cssModuleRegex ) , getToString ( sassModuleRegex ) , getToString ( lessModuleRegex ) , ] . includes ( item . test . toString ( ) )
157
181
let newUse ;
158
182
if ( Array . isArray ( item . use ) ) {
159
183
newUse = item . use . map ( ( ite ) => {
160
- if ( typeof ite === 'string' && / s t y l e - l o a d e r / . test ( ite ) ) {
184
+ if ( typeof ite === 'string' && / ( s t y l e - l o a d e r | m i n i - c s s - e x t r a c t - p l u g i n ) / . test ( ite ) ) {
185
+ return false
186
+ } else if ( typeof ite === 'string' && / c s s - l o a d e r / . test ( ite ) && ! / p o s t c s s - l o a d e r / . test ( ite ) ) {
161
187
return {
162
- loader : MiniCssExtractPlugin . loader ,
163
- } ;
164
- } else if ( typeof ite === 'object' && / s t y l e - l o a d e r / . test ( ite . loader ) ) {
188
+ loader : require . resolve ( "css-loader" ) ,
189
+ options : cssModuleOption ( isModule ? "local" : "icss" )
190
+ }
191
+ } else if ( typeof ite === 'object' && / ( s t y l e - l o a d e r | m i n i - c s s - e x t r a c t - p l u g i n ) / . test ( ite . loader ) ) {
192
+ return false
193
+ } else if ( typeof ite === 'object' && / c s s - l o a d e r / . test ( ite . loader ) && ! / p o s t c s s - l o a d e r / . test ( ite . loader ) ) {
165
194
return {
166
- loader : MiniCssExtractPlugin . loader ,
167
- } ;
195
+ loader : require . resolve ( "css-loader" ) ,
196
+ options : cssModuleOption ( isModule ? "local" : "icss" )
197
+ }
168
198
}
169
199
return ite ;
170
- } ) ;
200
+ } ) . filter ( Boolean ) ;
171
201
}
172
202
return {
173
203
...item ,
174
- use : newUse || item . use ,
204
+ use : newUse || [ ] ,
175
205
} ;
176
206
}
177
207
return item ;
0 commit comments