import { IPlugin } from "./i-plugin" ; import BasePlugin from "./base-plugin" ; import { error , red } from "../utils/log" ; import { AppletType } from "../type/applet-type" ; import { jsTransForm } from "../js/js-transform" ; import BabelPluginWechatToAlipay from "../babel-plugin/babel-plugin-wechat-to-alipay" ; import BabelPluginAlipayToWechat from "../babel-plugin/babel-plugin-alipay-to-wechat" ; author CaMnter
  class JsPlugin extends BasePlugin { private readonly _babelPlugin : Function ; constructor ( target , expect ) { super ( target , expect ) ; this . _babelPlugin = this . getBabelPlugin ( this . _target , this . _expect ) ; if ( ! this . _babelPlugin ) { throw new Error ( `JsPlugin # constructor #「babelPlugin」error: ${ this . _babelPlugin }` ) ; } } run ( code ) { if ( ! code || '' === code ) { throw new Error ( `JsPlugin # run #「code」error: ${ code }` ) ; } this . _result = code ; this . plugins && Array . isArray ( this . plugins ) && this . plugins . length > 0 && this . plugins . forEach ( ( plugin ) => { try { this . _result = plugin . run ( code ) ; } catch ( e ) { error ( red , `JsPlugin # run # extra plugin error: ${ e }` ) ; } } ) ; this . _result = jsTransForm ( this . _result , this . _babelPlugin ) ; return this . _result ; } getBabelPlugin ( target , expect ) { switch ( target ) { case AppletType . wx : switch ( expect ) { case AppletType . my : return new BabelPluginWechatToAlipay ( ) . createPlugin ( ) ; break ; } break ; case AppletType . my : switch ( expect ) { case AppletType . wx : return new BabelPluginAlipayToWechat ( ) . createPlugin ( ) ; break ; } break ; } throw new Error ( `JsPlugin # getBabelPlugin #「babelPlugin」undefined error` ) ; } get babelPlugin ( ) : Function { return this . _babelPlugin ; } get result ( ) : string | undefined { return this . _result ; } } export default JsPlugin ;