import webpack = require ( 'webpack' ) ; import * as qs from 'querystring' ; import * as loaderUtils from 'loader-utils' ; const selfPath = require . resolve ( './index' ) ; const stylePostLoaderPath = require . resolve ( './stylePostLoader' ) ; const styleInlineLoaderPath = require . resolve ( './styleInlineLoader' ) ; interface Loader { request : string ; path : string ; query : string ; pitchExecuted : boolean ; } const isESLintLoader = ( l ) => /(\/|\\|@)eslint-loader/ . test ( l . path ) ; const isNullLoader = ( l ) => /(\/|\\|@)null-loader/ . test ( l . path ) ; const isCSSLoader = ( l ) => /(\/|\\|@)css-loader/ . test ( l . path ) ; const isCacheLoader = ( l ) => /(\/|\\|@)cache-loader/ . test ( l . path ) ; const isNotPitcher = ( l ) => l . path !== __filename ; const pitcher = ( code ) => code ; export const pitch = function ( ) { const context = this as webpack . loader . LoaderContext ; const rawLoaders = context . loaders . filter ( isNotPitcher ) ; let loaders = rawLoaders ; if ( loaders . some ( isNullLoader ) ) { return ; } const query = qs . parse ( context . resourceQuery . slice ( 1 ) ) ; const isInlineBlock = /\.vue$/ . test ( context . resourcePath ) ; if ( isInlineBlock ) { loaders = loaders . filter ( ( l ) => ! isESLintLoader ( l ) ) ; } const seen = new Map ( ) ; loaders = loaders . filter ( ( loader ) => { const identifier = typeof loader === 'string' ? loader : loader . path + loader . query ; if ( ! seen . has ( identifier ) ) { seen . set ( identifier , true ) ; return true ; } } ) ; if ( query . type === `style` ) { const cssLoaderIndex = loaders . findIndex ( isCSSLoader ) ; if ( cssLoaderIndex > - 1 ) { const afterLoaders = query . inline != null ? [ styleInlineLoaderPath ] : loaders . slice ( 0 , cssLoaderIndex + 1 ) ; const beforeLoaders = loaders . slice ( cssLoaderIndex + 1 ) ; return genProxyModule ( [ ... afterLoaders , stylePostLoaderPath , ... beforeLoaders ] , context , ! ! query . module || query . inline != null ) ; } } if ( query . type === `custom` && shouldIgnoreCustomBlock ( loaders ) ) { return `` ; } return genProxyModule ( loaders , context , query . type !== 'template' ) ; } ; function genProxyModule ( loaders , context , exportDefault = true ) { const request = genRequest ( loaders , context ) ; return ( ( exportDefault ? `export { default } from ${ request }; ` : `` ) + `export * from ${ request }` ) ; } function genRequest ( loaders , context ) { const loaderStrings = loaders . map ( ( loader ) => { return typeof loader === 'string' ? loader : loader . request ; } ) ; const resource = context . resourcePath + context . resourceQuery ; return loaderUtils . stringifyRequest ( context , '-!' + [ ... loaderStrings , resource ] . join ( '!' ) ) ; } function shouldIgnoreCustomBlock ( loaders ) { const actualLoaders = loaders . filter ( ( loader ) => { if ( loader . path === selfPath ) { return false ; } if ( isCacheLoader ( loader ) ) { return false ; } return true ; } ) ; return actualLoaders . length === 0 ; } export default pitcher ;