ethers.js/rollup-pre-alias.config.js

85 lines
2.8 KiB
JavaScript
Raw Permalink Normal View History

2020-11-15 01:42:36 +03:00
"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 @ethersproject imports, merge anything else
plugins.push(resolveNode({
//resolveOnly: ((name === "ethers") ? []: [ /^(?!(@ethersproject|ethers))/ ]),
resolveOnly: [ /^(?!(@ethersproject|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;