import { getWorker } from "../worker-client" ; import { AcornNode , Plugin } from "rollup" ; import QuickLRU from "quick-lru" ; import resolveFrom from "resolve-from" ; import semver from "semver" ; const lru = new QuickLRU < string , { code : string ; promise : Promise < { code : string ; map : any ; ast : AcornNode ; } > ; } > ( { maxSize : 1000 , } ) ; let extensionRegex = /\.[tj]sx?$/ ; let externalHelpersCache = new Map < string , { ast : AcornNode ; code : string ; } > ( ) ; const resolvedBabelCore = require . resolve ( "@babel/core" ) ; const babelHelpers = require ( resolveFrom ( resolvedBabelCore , "@babel/helpers" ) ) ; const babelGenerator = require ( resolveFrom ( resolvedBabelCore , "@babel/generator" ) ) ; const babelHelpersModuleStart = "\0rollupPluginBabelHelpers/" ; function babelRuntimeVersionRangeHasHelper ( name , versionRange ) { let minVersion = babelHelpers . minVersion ( name ) ; return ( ! semver . intersects ( `<${ minVersion }` , versionRange ) && ! semver . intersects ( `>=8.0.0` , versionRange ) ) ; } let rollupPluginBabel = ( { cwd , reportTransformedFile , babelRuntime , } ) : Plugin => { const babelRuntimeVersion = semver . valid ( babelRuntime ?. range ) ? `^${ babelRuntime ?. range }` : babelRuntime ?. range ; const resolveIdForBabelHelper = babelRuntimeVersion === undefined || babelRuntime === undefined || ! semver . validRange ( babelRuntimeVersion ) ? ( helper ) => `${ babelHelpersModuleStart }${ helper }` : ( helper ) => { if ( babelRuntimeVersionRangeHasHelper ( helper , babelRuntimeVersion ) ) { return `${ babelRuntime . name }/helpers/${ helper }` ; } return `${ babelHelpersModuleStart }${ helper }` ; } ; return { name : "babel" , resolveId ( id , parent ) { const currentIsBabelHelper = id . startsWith ( babelHelpersModuleStart ) ; if ( ! currentIsBabelHelper ) { if ( parent && parent . startsWith ( babelHelpersModuleStart ) ) { return resolveIdForBabelHelper ( id ) ; } return null ; } return resolveIdForBabelHelper ( id . slice ( babelHelpersModuleStart . length ) ) ; } , load ( id ) { let helperName = id . replace ( /\0rollupPluginBabelHelpers\// , "" ) ; if ( helperName === id ) { return null ; } let helpersSourceDescription = externalHelpersCache . get ( helperName ) ; if ( helpersSourceDescription === undefined ) { const helperNodes = babelHelpers . get ( helperName ) . nodes ; let helpers = babelGenerator . default ( { type : "Program" , body : helperNodes , } ) . code ; helpersSourceDescription = { ast : this . parse ( helpers , undefined ) , code : helpers , } ; externalHelpersCache . set ( helperName , helpersSourceDescription ) ; } return helpersSourceDescription ; } , transform ( code , filename ) { if ( typeof filename !== "string" || filename [ 0 ] === "\0" || ! extensionRegex . test ( filename ) || filename . includes ( "node_modules" ) ) { return null ; } if ( lru . has ( filename ) ) { let cachedResult = lru . get ( filename ) ! ; if ( code === cachedResult . code ) { return cachedResult . promise . then ( ( result ) => { const ast = JSON . parse ( JSON . stringify ( result . ast ) ) ; return { code : result . code , map : result . map , ast , meta : { babel : { ast , codeAtBabelTime : result . code } , } , } ; } ) ; } } let promise = getWorker ( ) . transformBabel ( code , cwd , filename ) . then ( ( x ) => { reportTransformedFile ( filename ) ; const ast = this . parse ( x . code ! , undefined ) ; return { code : x . code , ast : JSON . parse ( JSON . stringify ( ast ) ) , map : x . map , meta : { babel : { ast , codeAtBabelTime : x . code } , } , } ; } ) ; lru . set ( filename , { code , promise } ) ; return promise ; } , } ; } ; export default rollupPluginBabel ;