@@ -6,6 +6,10 @@ const { STYLE_LOADER } = require('../core/constant');
66
77module . exports = {
88
9+ getLoaderLabel ( loader ) {
10+ return this . utils . getLoaderLabel ( loader , this ) ;
11+ } ,
12+
913 getConfigLoader ( config ) {
1014 const { loaders = { } } = config ;
1115 const { rules = [ ] } = config . module || { } ;
@@ -26,18 +30,29 @@ module.exports = {
2630 }
2731
2832 return rules . find ( loader => {
29- const label = this . utils . getLoaderLabel ( loader ) ;
33+ const label = this . getLoaderLabel ( loader ) ;
3034 if ( name === label || mappingName === label ) {
3135 return true ;
3236 }
3337 return false ;
3438 } ) ;
3539 } ,
3640
37- getLoaderByName ( name ) {
41+ getMergeLoaderByName ( name ) {
3842 const defaultLoader = this . utils . cloneDeep ( this . loaders [ name ] ) ;
3943 const configLoader = this . getConfigLoaderByName ( name ) ;
40- return this . mergeLoader ( { [ name ] : configLoader } , { [ name ] : defaultLoader } ) [ name ] ;
44+ return this . mergeLoader ( { [ name ] : configLoader } , { [ name ] : defaultLoader } ) ;
45+ } ,
46+
47+ getLoaderByName ( name ) {
48+ const loader = this . getMergeLoaderByName ( name ) ;
49+ const options = this . merge ( this . config . loaderOptions , this . loaders . options ) ;
50+ const loaders = this . mergeLoaderOption ( loader , options ) ;
51+ const webpackLoaders = this . createLoader ( loaders ) ;
52+ if ( webpackLoaders . length ) {
53+ return webpackLoaders [ 0 ] ;
54+ }
55+ return { } ;
4156 } ,
4257
4358 initLoader ( ) {
@@ -143,6 +158,17 @@ module.exports = {
143158 return null ;
144159 } ,
145160
161+ getTsLoader ( ) {
162+ const itemLoader = this . getLoaderByName ( 'ts' ) ;
163+ if ( itemLoader && itemLoader . use ) {
164+ const tsLoader = itemLoader . use . find ( loader => {
165+ return this . utils . isObject ( loader ) && loader . loader === 'ts-loader' ;
166+ } ) ;
167+ return tsLoader ;
168+ }
169+ return null ;
170+ } ,
171+
146172 addLoader ( loader ) {
147173 if ( loader . test && ( loader . use || loader . loader ) ) {
148174 const loaderInfo = { } ;
@@ -167,7 +193,7 @@ module.exports = {
167193 const sourceLoaders = Array . isArray ( cloneLoaders ) ? { } : cloneLoaders ;
168194 if ( Array . isArray ( cloneLoaders ) ) {
169195 cloneLoaders . forEach ( loader => {
170- const label = this . utils . getLoaderLabel ( loader ) ;
196+ const label = this . getLoaderLabel ( loader ) ;
171197 if ( this . isWebpackLoader ( loader ) ) {
172198 sourceLoaders [ label ] = this . merge ( loader , { enable : true } ) ;
173199 } else {
@@ -213,7 +239,7 @@ module.exports = {
213239 }
214240 if ( Array . isArray ( itemLoader . use ) ) {
215241 itemLoader . use . forEach ( ( loader , index ) => {
216- const label = this . utils . getLoaderLabel ( loader ) ;
242+ const label = this . getLoaderLabel ( loader ) ;
217243 const mLabel = this . loaderKeyLabelMapping [ name ] ;
218244 const configOptions = itemLoader . options && ( label === mLabel || label === name ) ? itemLoader . options : { } ;
219245 const options = this . merge ( loaderOptions [ label ] , { options : configOptions } ) ;
@@ -275,7 +301,7 @@ module.exports = {
275301 } ) ;
276302 loaderNames . forEach ( name => {
277303 const itemLoader = loaders [ name ] ;
278- [ 'type' , 'enable' , 'postcss' , 'framework' , 'loader' , 'options' ] . forEach ( propery => {
304+ [ 'type' , 'enable' , 'postcss' , 'framework' , 'loader' , 'options' , 'name' ] . forEach ( propery => {
279305 delete itemLoader [ propery ] ;
280306 } ) ;
281307 } ) ;
@@ -320,13 +346,8 @@ module.exports = {
320346 // https://github.com/TypeStrong/ts-loader/pull/782
321347 // vue-loader 14 版本中,需在 options 重复配置 ts-loader,升级 15 以后可移除
322348 if ( this . typescript ) {
323- const use = this . utils . isFunction ( this . typescript . use ) ? this . typescript . use . apply ( this ) : this . typescript . use ;
324- const tsLoader = use . find ( item => {
325- return item . loader === 'ts-loader' ;
326- } ) ;
327- if ( tsLoader ) {
328- loaders . ts = tsLoader ;
329- }
349+ const tsLoader = this . getTsLoader ( ) ;
350+ loaders . ts = tsLoader ;
330351 }
331352 return { preLoaders, loaders } ;
332353 } ,
0 commit comments