-
Notifications
You must be signed in to change notification settings - Fork 7
/
rollup.config.js
139 lines (123 loc) · 4.26 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
/**
* rollup.config.js
*
* This file is used by rollup to generate distribution
* files in the `dist/` folder which can be used without
* mondification by placing all ethers libraries and
* ancillary packages in the same folder.
*
* The package name is determined from the package name,
* for example a package named `@ethers-ancillary/foobar`
* will contain the following in the `dist/` folder:
* - foobar.esm.js
* - foobar.esm.min.js
* - foobar.umd.js
* - foobar.umd.min.js
*
*/
//import commonjs from '@rollup/plugin-commonjs';
//import inject from "@rollup/plugin-inject";
//import resolveNode from "@rollup/plugin-node-resolve";
//import nodePolyfills from "rollup-plugin-node-polyfills";
import { terser } from "rollup-plugin-terser";
// Get the library name based on the package name.
// Semi-official packages managed in the npm @ethers-ancillary
// namespace are managed in the ethers-io GitHub account, and
// must NOT begin with `ext-`. Any other package will be prefixed
// with `ext-` to prevent name collisions of external packages
// with the semi-official packages.
const libName = (function() {
let libName = require("./package.json").name.toLowerCase();
// Check for semi-official @ethers-ancillary packages
const match = libName.match(/^@ethers-ancillary\/([a-z][a-z0-9-]*)$/);
if (match) {
if (match[1].substring(0, 4) !== "ext-") { return match[1]; }
} else {
const compName = libName.split("/").pop();
if (compName.match(/^[a-z][a-z0-9]*$/)) { return "ext-" + compName; }
}
throw new Error(`package does not follow naming convention: ${ JSON.stringify(libName) }`);
})();
// Replaces ES2015 import statements with relative paths so things
// work in browsers. For example, with `targets={ ethers: "./ethers.js" }`
// import { ethers } from "ethers";
// becomes:
// import { ethers } from "./ethers.js";
function reanchorPlugin(targets) {
const matcher = /import(\s*\{[a-z0-9_\s,$]*\}\s*|(\s+[a-z0-9_$\s]*\s+))from\s*(['"]([^'"]*)['"]);/gi;
function update(code) {
code = code.replace(matcher, (all, imps, _imps, source) => {
const imp = source.substring(1, source.length - 1)
const repl = targets[imp];
if (repl) {
console.log(`Re-anchoring ${ imp } to ${ repl }`);
const quote = source[0];
return `import ${ imps } from ${ quote }${ repl }${ quote };`;
}
return all;
});
return code;
}
return {
name: "reanchor",
async generateBundle(output, bundle) {
for (const name in bundle) {
bundle[name].code = update(bundle[name].code);
}
}
};
}
function camelCase(text) {
const words = text.split(/[^a-z0-9]+/);
const result = words.map((word, index) => {
word = word.toLowerCase();
if (index > 0) { word = word[0].toUpperCase() + word.substring(1); }
return word
}).join("");
if (result.length === 0) { throw new Error(`invalid text for camelCase: ${ JSON.stringify(text) }`); }
return result;
}
// Create each config
function getConfig(input, format, minify) {
const plugins = [
// inject({ Buffer: [ "buffer-es6", "Buffer" ], }),
// commonjs({ mainFields: [ "module", "browser", "main" ] }),
// nodePolyfills({ }),
// resolveNode({ preferBuiltins: false })
];
let name = undefined;
if (format === "esm") {
plugins.push({
"ethers": "./ethers.esm.min.js"
});
} else {
name = `_ethers_${ camelCase(libName) }`;
}
const comps = [ `./dist/${ libName }`, format ];
if (minify) {
comps.push("min");
plugins.push(terser());
}
comps.push("js");
return {
input: input,
output: {
file: comps.join("."),
name,
format,
// globals
},
context: "window",
// treeshake: false,
// treeshake: true,
external: [ "ethers" ],
plugins: plugins
};
}
// The complete list of generated dist files
module.exports = [
getConfig("./lib.esm/index.js", "umd"),
getConfig("./lib.esm/index.js", "umd", true),
getConfig("./lib.esm/index.js", "esm"),
getConfig("./lib.esm/index.js", "esm", true)
];