@@ -1859,87 +1859,97 @@ try {
18591859
18601860 // 应用过rules后,注入mpx相关资源编译loader
18611861 normalModuleFactory . hooks . afterResolve . tap ( 'MpxWebpackPlugin' , ( { createData } ) => {
1862- const { queryObj, resourcePath } = parseRequest ( createData . request )
1862+ const { queryObj } = parseRequest ( createData . request )
18631863 const loaders = createData . loaders
1864-
1865- const isVue = queryObj . vue && resourcePath ?. endsWith ( '.vue' )
1866-
1867- function processLoader ( ) {
1868- if ( ( ( queryObj . mpx || isVue ) && queryObj . mpx !== MPX_PROCESSED_FLAG ) ) {
1869- const type = queryObj . type
1870- const extract = queryObj . extract
1871-
1872- if ( type === 'styles' || ( isVue && type === 'style' ) ) {
1873- let insertBeforeIndex = - 1
1874- // 单次遍历收集所有索引
1875- loaders . forEach ( ( loader , index ) => {
1876- const currentLoader = toPosix ( loader . loader )
1877- if ( currentLoader . includes ( 'node_modules/stylus-loader' ) || currentLoader . includes ( 'node_modules/sass-loader' ) || currentLoader . includes ( 'node_modules/less-loader' ) ) {
1878- insertBeforeIndex = index
1864+ const type = queryObj . type
1865+ const mpxProcessFlag = queryObj . mpx && queryObj . mpx !== MPX_PROCESSED_FLAG
1866+ const vueProcessFlag = queryObj . vue && queryObj . mpx !== MPX_PROCESSED_FLAG
1867+ if ( mpxProcessFlag || vueProcessFlag ) {
1868+ const extract = queryObj . extract
1869+ if ( type === 'styles' || type === 'style' ) {
1870+ const loaderTypes = {
1871+ 'node_modules/stylus-loader' : - 1 ,
1872+ 'node_modules/sass-loader' : - 1 ,
1873+ 'node_modules/less-loader' : - 1 ,
1874+ 'node_modules/css-loader' : - 1
1875+ }
1876+ loaders . forEach ( ( loader , index ) => {
1877+ const currentLoader = toPosix ( loader . loader )
1878+ for ( const key in loaderTypes ) {
1879+ if ( currentLoader . includes ( key ) ) {
1880+ loaderTypes [ key ] = index
1881+ break
18791882 }
1880- } )
1883+ }
1884+ } )
1885+ const insertStripLoaders = ( index ) => {
1886+ if ( index !== - 1 ) {
1887+ loaders . splice ( index , 0 , { loader : styleStripConditionalPath } )
1888+ loaders . splice ( index + 2 , 0 , { loader : styleStripConditionalPath } )
1889+ return true
1890+ }
1891+ return false
1892+ }
18811893
1882- if ( insertBeforeIndex !== - 1 ) {
1883- loaders . splice ( insertBeforeIndex + 1 , 0 , { loader : styleStripConditionalPath } )
1894+ const priorities = [ 'node_modules/stylus-loader' , 'node_modules/less-loader' , 'node_modules/sass-loader' , 'node_modules/css-loader' ]
1895+ for ( const loaderKey of priorities ) {
1896+ if ( insertStripLoaders ( loaderTypes [ loaderKey ] ) ) {
1897+ break
18841898 }
18851899 }
1900+ }
18861901
1887- if ( ! isVue ) {
1888- switch ( type ) {
1889- case 'styles' :
1890- case 'template' : {
1891- let insertBeforeIndex = - 1
1892- const info = typeLoaderProcessInfo [ type ]
1893- loaders . forEach ( ( loader , index ) => {
1894- const currentLoader = toPosix ( loader . loader )
1895- if ( currentLoader . includes ( info [ 0 ] ) ) {
1896- loader . loader = info [ 1 ]
1897- insertBeforeIndex = index
1898- } else if ( currentLoader . includes ( info [ 1 ] ) ) {
1899- insertBeforeIndex = index
1900- }
1901- } )
1902- if ( insertBeforeIndex > - 1 ) {
1903- loaders . splice ( insertBeforeIndex + 1 , 0 , {
1904- loader : info [ 2 ]
1905- } )
1902+ if ( mpxProcessFlag ) {
1903+ switch ( type ) {
1904+ case 'styles' :
1905+ case 'template' : {
1906+ let insertBeforeIndex = - 1
1907+ const info = typeLoaderProcessInfo [ type ]
1908+ loaders . forEach ( ( loader , index ) => {
1909+ const currentLoader = toPosix ( loader . loader )
1910+ if ( currentLoader . includes ( info [ 0 ] ) ) {
1911+ loader . loader = info [ 1 ]
1912+ insertBeforeIndex = index
1913+ } else if ( currentLoader . includes ( info [ 1 ] ) ) {
1914+ insertBeforeIndex = index
19061915 }
1907- break
1916+ } )
1917+ if ( insertBeforeIndex > - 1 ) {
1918+ loaders . splice ( insertBeforeIndex + 1 , 0 , {
1919+ loader : info [ 2 ]
1920+ } )
19081921 }
1909- case 'json' :
1910- if ( queryObj . isTheme ) {
1911- loaders . unshift ( {
1912- loader : jsonThemeCompilerPath
1913- } )
1914- } else if ( queryObj . isPlugin ) {
1915- loaders . unshift ( {
1916- loader : jsonPluginCompilerPath
1917- } )
1918- } else {
1919- loaders . unshift ( {
1920- loader : jsonCompilerPath
1921- } )
1922- }
1923- break
1924- case 'wxs' :
1922+ break
1923+ }
1924+ case 'json' :
1925+ if ( queryObj . isTheme ) {
19251926 loaders . unshift ( {
1926- loader : wxsLoaderPath
1927+ loader : jsonThemeCompilerPath
19271928 } )
1928- break
1929- }
1930- if ( extract ) {
1929+ } else if ( queryObj . isPlugin ) {
1930+ loaders . unshift ( {
1931+ loader : jsonPluginCompilerPath
1932+ } )
1933+ } else {
1934+ loaders . unshift ( {
1935+ loader : jsonCompilerPath
1936+ } )
1937+ }
1938+ break
1939+ case 'wxs' :
19311940 loaders . unshift ( {
1932- loader : extractorPath
1941+ loader : wxsLoaderPath
19331942 } )
1934- }
1943+ break
1944+ }
1945+ if ( extract ) {
1946+ loaders . unshift ( {
1947+ loader : extractorPath
1948+ } )
19351949 }
1936-
1937- createData . resource = addQuery ( createData . resource , { mpx : MPX_PROCESSED_FLAG } , true )
19381950 }
1951+ createData . resource = addQuery ( createData . resource , { mpx : MPX_PROCESSED_FLAG } , true )
19391952 }
1940-
1941- processLoader ( )
1942-
19431953 // mpxStyleOptions 为 mpx style 文件的标识,避免 Vue 文件插入 styleCompiler 后导致 vue scoped 样式隔离失效
19441954 if ( isWeb ( mpx . mode ) && queryObj . mpxStyleOptions ) {
19451955 const firstLoader = loaders [ 0 ] ? toPosix ( loaders [ 0 ] . loader ) : ''
0 commit comments