import { gte } from "semver" ; import { WebpackConfigurator } from "../main" ; export function createBabelLoader ( configurator ) { const presets = [ [ require ( "@babel/preset-env" ) . default , { modules : false , targets : computeBabelEnvTarget ( configurator . isRenderer , configurator . electronVersion ) , } ] , ] ; const plugins = [ require ( "@babel/plugin-syntax-dynamic-import" ) . default , ] ; if ( configurator . type !== "main" && configurator . hasDependency ( "element-ui" ) ) { plugins . push ( [ require ( "babel-plugin-component" ) , { libraryName : "element-ui" , styleLibraryName : "theme-chalk" } ] ) ; } addBabelItem ( presets , configurator . getMatchingDevDependencies ( { includes : [ "babel-preset-" , "@babel/preset-" ] , excludes : [ "babel-preset-env" , "@babel/preset-env" ] , } ) ) ; addBabelItem ( plugins , configurator . getMatchingDevDependencies ( { includes : [ "babel-plugin-" , "@babel/plugin-" ] , excludes : [ "babel-plugin-syntax-dynamic-import" , "@babel/plugin-syntax-dynamic-import" ] , } ) ) ; return { loader : "babel-loader" , options : { presets , plugins } } ; } function addBabelItem ( to , names ) { for ( const name of names ) { const module = require ( name ) ; to . push ( [ module . default || module ] ) ; } } function computeBabelEnvTarget ( isRenderer , electronVersion ) { if ( isRenderer ) { return { electron : electronVersion } ; } let nodeVersion = "7.4.0" ; if ( gte ( electronVersion , "3.0.0" ) ) { nodeVersion = "10.2.0" ; } else if ( gte ( electronVersion , "2.0.0" ) ) { nodeVersion = "8.9.3" ; } else if ( gte ( electronVersion , "1.8.2" ) ) { nodeVersion = "8.2.1" ; } else if ( gte ( electronVersion , "1.7.3" ) ) { nodeVersion = "7.9.0" ; } return { node : nodeVersion } ; }