import * as fs from 'fs' ; import * as path from 'path' ; import * as rollup from 'rollup' ; import alias from 'rollup-plugin-alias' ; import builtins from 'rollup-plugin-node-builtins' ; import commonjs from 'rollup-plugin-commonjs' ; import license from 'rollup-plugin-license' ; import json from 'rollup-plugin-json' ; import globals from 'rollup-plugin-node-globals' ; import resolve from 'rollup-plugin-node-resolve' ; import typescript2 from 'rollup-plugin-typescript2' ; import ts from 'typescript' ; import { projects , preProcessFunc , preProcessProjects } from './projects' ; function getKeys ( p ) { const packageJsonFile = `${ process . cwd ( ) }/packages/${ p }/package.json` ; const data = fs . readFileSync ( packageJsonFile , 'utf-8' ) ; const { dependencies } = JSON . parse ( data ) ; const keys = dependencies ? Object . keys ( dependencies ) : [ ] ; return keys ; } async function bundles ( ) { await preProcessFunc ( preProcessProjects ) ; for ( const pkg of projects ) { const base = { input : path . join ( pkg . src , 'index.ts' ) , plugins : [ alias ( { elliptic : path . resolve ( __dirname , '../' , 'includes/elliptic/elliptic.js' ) , } ) , resolve ( { browser : true , jsnext : true , preferBuiltins : true , } ) , commonjs ( ) , globals ( ) , builtins ( ) , json ( ) , typescript2 ( { typescript : ts , tsconfig : path . join ( pkg . path , 'tsconfig.json' ) , tsconfigOverride : { module : 'esnext' , stripInternal : true , emitDeclarationOnly : false , composite : false , declaration : false , declarationMap : false , sourceMap : true , } , } ) , license ( { banner : `Test Banner` , } ) , ] , external : getKeys ( pkg . name ) , } ; const pkgBundler = await rollup . rollup ( base ) ; await pkgBundler . write ( { file : pkg . esm , name : pkg . globalName , format : 'esm' , sourcemap : true , } ) ; await pkgBundler . write ( { file : pkg . umd , exports : 'named' , name : pkg . globalName , globals : { ... getKeys ( pkg . name ) . reduce ( ( g , packages ) => { if ( packages === pkg . name ) { g [ pkg . scopedName ] = pkg . globalName ; } else { g [ packages ] = packages ; } return g ; } , { } ) , tslib : 'tslib' , } , format : 'umd' , sourcemap : true , } ) ; await pkgBundler . write ( { file : pkg . cjs , name : pkg . globalName , format : 'cjs' , sourcemap : true , } ) ; } } bundles ( ) ;