import { createConfigItem } from '@babel/core' ; import { createBabelInputPluginFactory } from '@rollup/plugin-babel' ; import merge from 'lodash.merge' ; const replacements = [ { original : 'lodash(?!/fp)' , replacement : 'lodash-es' } ] ; function mergeConfigItems ( type , ... configItemsToMerge ) { const mergedItems = [ ] ; configItemsToMerge . forEach ( configItemToMerge => { configItemToMerge . forEach ( ( item ) => { const itemToMergeWithIndex = mergedItems . findIndex ( mergedItem => mergedItem . file . resolved === item . file . resolved ) ; if ( itemToMergeWithIndex === - 1 ) { mergedItems . push ( item ) ; return ; } mergedItems [ itemToMergeWithIndex ] = createConfigItem ( [ mergedItems [ itemToMergeWithIndex ] . file . resolved , merge ( mergedItems [ itemToMergeWithIndex ] . options , item . options ) , ] , { type } ) ; } ) ; } ) ; return mergedItems ; } function createConfigItems ( type , items ) { return items . map ( ( { name , ... options } ) => { return createConfigItem ( [ require . resolve ( name ) , options ] , { type } ) ; } ) ; } export const babelPluginJBrowse = createBabelInputPluginFactory ( ( ) => ( { options ( { custom : customOptions , ... pluginOptions } ) { return { customOptions , pluginOptions , } ; } , config ( config , { customOptions } ) { const defaultPlugins = createConfigItems ( 'plugin' , [ { name : 'babel-plugin-macros' } , { name : 'babel-plugin-annotate-pure-calls' } , { name : 'babel-plugin-dev-expression' } , customOptions . format !== 'cjs' && { name : 'babel-plugin-transform-rename-import' , replacements , } , { name : 'babel-plugin-polyfill-regenerator' , method : 'usage-pure' , } , { name : '@babel/plugin-proposal-class-properties' } , ] . filter ( Boolean ) ) ; const babelOptions = config . options || { } ; babelOptions . presets = babelOptions . presets || [ ] ; const presetEnvIdx = babelOptions . presets . findIndex ( ( preset ) => preset . file . request . includes ( '@babel/preset-env' ) ) ; if ( presetEnvIdx !== - 1 ) { const presetEnv = babelOptions . presets [ presetEnvIdx ] ; babelOptions . presets [ presetEnvIdx ] = createConfigItem ( [ presetEnv . file . resolved , merge ( { targets : customOptions . targets } , presetEnv . options , { modules : false , } ) , ] , { type : `preset` } ) ; } else { const defaultPresets = createConfigItems ( 'preset' , [ { name : '@babel/preset-env' , targets : customOptions . targets , modules : false , } , ] ) ; babelOptions . presets = mergeConfigItems ( 'preset' , defaultPresets , babelOptions . presets ) ; } babelOptions . plugins = mergeConfigItems ( 'plugin' , defaultPlugins , babelOptions . plugins || [ ] ) ; return babelOptions ; } , } ) ) ;