import * as webpack from 'webpack' ; import { getOptions , stringifyRequest } from 'loader-utils' ; import * as path from 'path' ; interface PageConfig { content : any ; path : string ; } export default function ( this ) { const options = getOptions ( this ) ; const config = getPageConfig ( options . config , this . resourcePath ) ; const configString = JSON . stringify ( config ) ; const stringify = ( s ) : string => stringifyRequest ( this , s ) ; const { isNeedRawLoader , importFrameworkStatement , mockAppStatement , frameworkArgs , creator , creatorLocation } = options . loaderMeta ; const appConfig = options . appConfig ; const frameworkArgsArray = frameworkArgs . split ( ',' ) ; frameworkArgsArray . splice ( frameworkArgsArray . length - 1 , 1 , 'appConfig' ) ; const frameworkArgsCopy = frameworkArgsArray . join ( ',' ) ; const raw = path . join ( __dirname , 'raw.js' ) ; const componentPath = isNeedRawLoader ? `${ raw }!${ this . resourcePath }` : this . request . split ( '!' ) . slice ( 1 ) . join ( '!' ) ; const runtimePath = Array . isArray ( options . runtimePath ) ? options . runtimePath : [ options . runtimePath ] ; const setReconciler = runtimePath . reduce ( ( res , item ) => { return res + `import '${ item }'\n` ; } , '' ) ; const { globalObject } = this . _compilation . outputOptions ; const prerender = `
if (typeof PRERENDER !== 'undefined') {
  ${ globalObject }._prerender = inst
}` ; return `${ setReconciler }
import { createPageConfig, window } from '@tarojs/runtime'
import { ${ creator } } from '${ creatorLocation }'
import component from ${ stringify ( componentPath ) }
${ importFrameworkStatement }
var config = ${ configString };
var appConfig = ${ JSON . stringify ( appConfig ) };
window.__taroAppConfig = appConfig
${ mockAppStatement }
${ creator }(App, ${ frameworkArgsCopy })
${ config . enableShareTimeline ? 'component.enableShareTimeline = true' : '' }
${ config . enableShareAppMessage ? 'component.enableShareAppMessage = true' : '' }
var inst = Page(createPageConfig(component, '${ options . name }', {}, config || {}))
${ options . prerender ? prerender : '' }
` ; } function getPageConfig ( configs , resourcePath ) { const configPath = removeExt ( resourcePath ) + '.config' ; for ( const name in configs ) { const config = configs [ name ] ; if ( removeExt ( configs [ name ] . path ) === configPath ) { return config . content ; } } return { } ; } function removeExt ( file ) { return path . join ( path . dirname ( file ) , path . basename ( file , path . extname ( file ) ) ) ; }