import builtins = require ( 'builtin-modules' ) ; import dotProp = require ( 'dot-prop' ) ; import { CoreProperties as PackageManifest } from '@schemastore/package' ; import { Plugin , OutputOptions , IsExternal , InputOptions } from 'rollup' ; export default function subpathExternals ( pkg , output ? ) { const external = dotProp . get ( pkg , 'rollup.external' , [ ] ) ; const bundled = dotProp . get ( pkg , 'rollup.bundle' , [ ] ) ; const { format } = output || { } ; const { dependencies = { } , peerDependencies = { } } = pkg ; let pkgDeps ; if ( external . length ) { pkgDeps = external ; } else if ( format === 'umd' ) { pkgDeps = Object . keys ( peerDependencies ) ; } else { pkgDeps = Object . keys ( dependencies ) . concat ( Object . keys ( peerDependencies ) ) ; } if ( bundled . length ) { const inlined = new Set ( bundled ) ; pkgDeps = pkgDeps . filter ( dep => ! inlined . has ( dep ) ) ; } const subPathImport = new RegExp ( `^(${ pkgDeps . join ( '|' ) })/` ) ; const resolvedExternals = new Set ( pkgDeps . concat ( builtins ) ) ; const externalPredicate = ( id , _parentId , isResolved ) => { if ( isResolved === true ) { return resolvedExternals . has ( id ) ; } if ( id [ 0 ] === '.' ) { return false ; } if ( resolvedExternals . has ( id ) ) { return true ; } if ( subPathImport . test ( id ) ) { resolvedExternals . add ( id ) ; return true ; } return false ; } ; return { name : 'subpath-externals' , options : ( opts ) : InputOptions => { opts . external = externalPredicate ; return opts ; } , } ; }