forked from ethers-io/ethers.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup-pre-alias.config.js
84 lines (70 loc) · 2.83 KB
/
rollup-pre-alias.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
"use strict";
import fs from "fs";
import path from "path";
import commonjs from '@rollup/plugin-commonjs';
import resolveNode from "@rollup/plugin-node-resolve";
import replace from "@rollup/plugin-replace";
// We only need this for its version (we inject it into a require)
import elliptic from "elliptic";
function getSigningKeyConfig() {
const plugins = [ ];
// Remove the buffer check from BN.js
plugins.push(replace({
"require('buffer')": "/*RicMoo:ethers*/(null)",
include: "**/lib/bn.js",
delimiters: [ '', '' ]
}));
// Replace the package.json in elliptic
plugins.push(replace({
"require('../package.json')": `/*RicMoo:ethers*/{ version: "${ elliptic.version }" }`,
include: "**/lib/elliptic.js",
delimiters: [ '', '' ]
}));
// Nuke a bunch of requires we don't need in elliptic
const thrower = "(function() { throw new Error('unsupported'); })";
const crash = "(null).crash()";
[
{ name: "./edwards", filename: "curve/index.js" },
{ name: "./mont", filename: "curve/index.js" },
{ name: "./elliptic/eddsa", filename: "lib/elliptic.js" },
{ name: "brorand", filename: "ec/index.js", text: thrower },
{ name: "brorand", filename: "lib/elliptic.js", text: thrower },
{ name: "./precomputed/secp256k1", filename: "elliptic/curves.js", text: crash },
].forEach(({ name, filename, text }) => {
if (text == null) { text = "(null)"; }
const replacement = {
include: `**/${ filename }`,
delimiters: [ '', '' ]
};
replacement[`require('${ name }')`] = `/*RicMoo:ethers:require(${ name })*/${ text }`,
plugins.push(replace(replacement));
});
// Keep @into-the-fathom imports, merge anything else
plugins.push(resolveNode({
//resolveOnly: ((name === "ethers") ? []: [ /^(?!(@into-the-fathom|ethers))/ ]),
resolveOnly: [ /^(?!(@into-the-fathom|ethers|bn\.js|hash\.js))/ ],
mainFields: [ "module", "browser", "main" ],
preferBuiltins: false
}));
// Our CommonJS dependencies that are not rollup-friendly
plugins.push(commonjs({ }));
// Write out a dummy TypeScript definition
const typeDef = "//This file generated by rollup-pre-alias.config.js; do NOT modify\ndeclare const EC: any;\nexport { EC };"
fs.writeFileSync(path.resolve(__dirname, "packages/signing-key/lib._esm/browser-elliptic.d.ts"), typeDef);
return {
input: `packages/signing-key/lib._esm/elliptic.js`,
output: {
file: `packages/signing-key/lib._esm/browser-elliptic.js`,
format: "esm",
sourcemap: true,
exports: "named"
},
context: "window",
treeshake: false,
plugins
};
}
const configs = [
getSigningKeyConfig()
];
export default configs;