import { REG_VUE } from '@tarojs/helper' ; import { DEFAULT_Components } from '@tarojs/runner-utils' ; import { getLoaderMeta } from './loader-meta' ; import { getVueLoaderPath } from './index' ; import type { IPluginContext } from '@tarojs/service' ; import type { RootNode , TemplateChildNode , ElementNode } from '@vue/compiler-core' ; export function modifyH5WebpackChain ( ctx , chain ) { setStyleLoader ( ctx , chain ) ; setVueLoader ( chain ) ; setLoader ( chain ) ; setTaroApiLoader ( chain ) ; } function setStyleLoader ( ctx , chain ) { const config = ctx . initialConfig . h5 || { } ; const { styleLoaderOption = { } } = config ; chain . module . rule ( 'customStyle' ) . merge ( { use : [ { loader : 'style-loader' , options : styleLoaderOption } ] } ) ; } function setVueLoader ( chain ) { const vueLoaderPath = getVueLoaderPath ( ) ; const { VueLoaderPlugin } = require ( vueLoaderPath ) ; chain . plugin ( 'vueLoaderPlugin' ) . use ( VueLoaderPlugin ) ; const vueLoaderOption = { transformAssetUrls : { video : [ 'src' , 'poster' ] , 'live-player' : 'src' , audio : 'src' , source : 'src' , image : 'src' , 'cover-image' : 'src' , 'taro-video' : [ 'src' , 'poster' ] , 'taro-live-player' : 'src' , 'taro-audio' : 'src' , 'taro-source' : 'src' , 'taro-image' : 'src' , 'taro-cover-image' : 'src' } , compilerOptions : { nodeTransforms : [ ( node ) => { if ( node . type === 1 ) { node = node as ElementNode ; const nodeName = node . tag ; if ( DEFAULT_Components . has ( nodeName ) ) { node . tag = `taro-${ nodeName }` ; node . tagType = 1 ; } } } ] } } ; chain . module . rule ( 'vue' ) . test ( REG_VUE ) . use ( 'vueLoader' ) . loader ( vueLoaderPath ) . options ( vueLoaderOption ) ; } function setLoader ( chain ) { chain . plugin ( 'mainPlugin' ) . tap ( args => { args [ 0 ] . loaderMeta = getLoaderMeta ( ) ; return args ; } ) ; } function setTaroApiLoader ( chain ) { chain . merge ( { module : { rule : { 'process-import-taro' : { test : /taro-h5[\\/]dist[\\/]index/ , loader : require . resolve ( './api-loader' ) } } } } ) ; }