2020-02-26 09:48:12 +00:00
"use strict" ;
2020-04-18 06:46:52 +00:00
import path from "path" ;
2019-08-25 06:39:20 +00:00
import resolve from 'rollup-plugin-node-resolve' ;
import commonjs from 'rollup-plugin-commonjs' ;
import json from 'rollup-plugin-json' ;
import { terser } from "rollup-plugin-terser" ;
import { createFilter } from 'rollup-pluginutils' ;
2020-04-18 06:46:52 +00:00
function Replacer ( basePath , options = { } ) {
2019-08-25 06:39:20 +00:00
const filter = createFilter ( options . include , options . exclude ) ;
2020-04-18 06:46:52 +00:00
const suffixes = Object . keys ( options . replace ) ;
const pathUp = path . resolve ( basePath , ".." ) ;
2019-08-25 06:39:20 +00:00
return {
name : "file-replacer" ,
transform ( code , id ) {
2019-09-06 16:25:17 +00:00
/ *
console . log ( "------" ) ;
console . log ( "NAME" , id , id . match ( "node-resolve:empty.js$" ) ) ;
console . log ( code ) ;
console . log ( "------" ) ;
* /
2019-08-25 06:39:20 +00:00
if ( ! filter ( id ) ) { return null ; }
2019-09-06 16:25:17 +00:00
2019-08-25 06:39:20 +00:00
for ( let i = 0 ; i < suffixes . length ; i ++ ) {
const suffix = suffixes [ i ] ;
if ( id . match ( new RegExp ( suffix ) ) ) {
2020-04-18 06:46:52 +00:00
const newCode = options . replace [ suffix ] ;
2020-09-23 02:54:48 +00:00
//console.log(`Replace: ${ id.substring(pathUp.length + 1) } (${ code.length } => ${ newCode.length })`);
2019-08-25 06:39:20 +00:00
return {
2019-09-06 16:25:17 +00:00
code : newCode ,
map : { mappings : '' }
2019-08-25 06:39:20 +00:00
} ;
}
2020-04-18 06:46:52 +00:00
}
if ( id . substring ( 0 , basePath . length ) !== basePath ) {
2020-09-23 02:54:48 +00:00
//console.log(`Keep: ${ id.substring(pathUp.length + 1) }`);
2019-08-25 06:39:20 +00:00
}
2020-04-18 06:46:52 +00:00
2019-08-25 06:39:20 +00:00
return null ;
}
} ;
}
const undef = "module.exports = undefined;" ;
const empty = "module.exports = {};" ;
const brorand = "module.exports = function(length) { var result = new Uint8Array(length); (global.crypto || global.msCrypto).getRandomValues(result); return result; }" ;
const ellipticPackage = ( function ( ) {
const ellipticPackage = require ( './node_modules/elliptic/package.json' ) ;
return JSON . stringify ( { version : ellipticPackage . version } ) ;
} ) ( ) ;
2020-04-18 06:46:52 +00:00
function getConfig ( minify , buildModule , testing ) {
2019-08-25 20:20:34 +00:00
let input = "packages/ethers/lib/index.js"
2019-08-25 06:39:20 +00:00
let output = [ "umd" ] ;
let format = "umd" ;
let mainFields = [ "browser" , "main" ] ;
if ( buildModule ) {
2019-08-25 20:20:34 +00:00
input = "packages/ethers/lib.esm/index.js" ;
2019-08-25 06:39:20 +00:00
output = [ "esm" ] ;
format = "esm" ;
mainFields = [ "browser" , "module" , "main" ] ;
}
2020-04-18 06:46:52 +00:00
const replacer = Replacer ( path . resolve ( "packages" ) , {
2019-08-25 06:39:20 +00:00
replace : {
// Remove the precomputed secp256k1 points
"elliptic/lib/elliptic/precomputed/secp256k1.js$" : undef ,
// Remove curves we don't care about
"elliptic/curve/edwards.js$" : empty ,
"elliptic/curve/mont.js$" : empty ,
"elliptic/lib/elliptic/eddsa/.*$" : empty ,
// We only use the version from this JSON package
"elliptic/package.json$" : ellipticPackage ,
// Remove unneeded hashing algorithms
"hash.js/lib/hash/sha/1.js$" : empty ,
"hash.js/lib/hash/sha/224.js$" : empty ,
"hash.js/lib/hash/sha/384.js$" : empty ,
// Swap out borland for the random bytes we already have
"brorand/index.js$" : brorand ,
}
} ) ;
const plugins = [
replacer ,
json ( ) ,
resolve ( {
2020-09-23 02:54:48 +00:00
mainFields : mainFields ,
preferBuiltins : false
2019-08-25 06:39:20 +00:00
} ) ,
commonjs ( {
namedExports : {
"bn.js" : [ "BN" ] ,
2020-04-18 09:44:51 +00:00
"hash.js" : [ "hmac" , "ripemd160" , "sha256" , "sha512" ] ,
2019-11-23 12:38:13 +00:00
"elliptic" : [ "ec" ] ,
2020-03-12 17:53:34 +00:00
"scrypt-js" : [ "scrypt" , "syncScrypt" ] ,
2019-09-06 16:25:17 +00:00
} ,
} ) ,
2019-08-25 06:39:20 +00:00
] ;
if ( minify ) {
output . push ( "min" ) ;
plugins . push ( terser ( ) ) ;
}
2020-04-18 06:46:52 +00:00
const outputFile = [
"packages" ,
( testing ? "tests" : "ethers" ) ,
( "/dist/ethers." + output . join ( "." ) + ".js" )
] . join ( "/" ) ;
2019-08-25 06:39:20 +00:00
return {
input : input ,
output : {
file : outputFile ,
format : format ,
2019-11-23 12:38:13 +00:00
name : "ethers" ,
exports : "named"
2019-08-25 06:39:20 +00:00
} ,
2019-11-23 12:38:13 +00:00
context : "window" ,
2019-08-25 06:39:20 +00:00
treeshake : false ,
plugins : plugins
} ;
}
2020-04-18 06:46:52 +00:00
export default commandLineArgs => {
const testing = commandLineArgs . configTest ;
2020-04-18 09:44:51 +00:00
const buildModule = commandLineArgs . configModule ;
2020-04-18 06:46:52 +00:00
2020-04-18 09:44:51 +00:00
return [
getConfig ( false , buildModule , testing ) ,
getConfig ( true , buildModule , testing ) ,
] ;
2020-04-18 06:46:52 +00:00
}