forked from ethers-io/ethers.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.js
146 lines (121 loc) · 4.28 KB
/
rollup.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"use strict";
import path from "path";
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';
function Replacer(basePath, options = {}) {
const filter = createFilter(options.include, options.exclude);
const suffixes = Object.keys(options.replace);
const pathUp = path.resolve(basePath, "..");
return {
name: "file-replacer",
transform(code, id) {
/*
console.log("------");
console.log("NAME", id, id.match("node-resolve:empty.js$"));
console.log(code);
console.log("------");
*/
if (!filter(id)) { return null; }
for (let i = 0; i < suffixes.length; i++) {
const suffix = suffixes[i];
if (id.match(new RegExp(suffix))) {
const newCode = options.replace[suffix];
//console.log(`Replace: ${ id.substring(pathUp.length + 1) } (${ code.length } => ${ newCode.length })`);
return {
code: newCode,
map: { mappings: '' }
};
}
}
if (id.substring(0, basePath.length) !== basePath) {
//console.log(`Keep: ${ id.substring(pathUp.length + 1) }`);
}
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 });
})();
function getConfig(minify, buildModule, testing) {
let input = "packages/ethers/lib/index.js"
let output = [ "umd" ];
let format = "umd";
let mainFields = [ "browser", "main" ];
if (buildModule) {
input = "packages/ethers/lib.esm/index.js";
output = [ "esm" ];
format = "esm";
mainFields = [ "browser", "module", "main" ];
}
const replacer = Replacer(path.resolve("packages"), {
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({
mainFields: mainFields,
preferBuiltins: false
}),
commonjs({
namedExports: {
"bn.js": [ "BN" ],
"hash.js": [ "hmac", "ripemd160", "sha256", "sha512" ],
"elliptic": [ "ec" ],
"scrypt-js": [ "scrypt", "syncScrypt" ],
},
}),
];
if (minify) {
output.push("min");
plugins.push(terser());
}
const outputFile = [
"packages",
(testing ? "tests": "ethers"),
("/dist/ethers." + output.join(".") + ".js")
].join("/");
return {
input: input,
output: {
file: outputFile,
format: format,
name: "ethers",
exports: "named"
},
context: "window",
treeshake: false,
plugins: plugins
};
}
export default commandLineArgs => {
const testing = commandLineArgs.configTest;
const buildModule = commandLineArgs.configModule;
return [
getConfig(false, buildModule, testing),
getConfig(true, buildModule, testing),
];
}