-
Notifications
You must be signed in to change notification settings - Fork 13
/
rollup.config.mjs
70 lines (55 loc) · 2.19 KB
/
rollup.config.mjs
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
// Copyright 2017-2022 @subwallet/base authors & contributors
// SPDX-License-Identifier: Apache-2.0
import pluginAlias from '@rollup/plugin-alias';
import pluginCommonjs from '@rollup/plugin-commonjs';
import pluginInject from '@rollup/plugin-inject';
import pluginJson from '@rollup/plugin-json';
import {nodeResolve as pluginResolve} from '@rollup/plugin-node-resolve';
import fs from 'fs';
import path from 'path';
import pluginCleanup from 'rollup-plugin-cleanup';
function sanitizePkg(pkg) {
return pkg.replace('@subwallet/', '');
}
function createName(input) {
return `subwallet-${sanitizePkg(input)}`
.toLowerCase()
.replace(/[^a-zA-Z0-9]+(.)/g, (_, c) => c.toUpperCase());
}
export function createInput(pkg, _index) {
const partialPath = `packages/${sanitizePkg(pkg)}/build`;
const index = (_index || fs.existsSync(path.join(process.cwd(), partialPath, 'bundle.js')) ? 'bundle.js' : (JSON.parse(fs.readFileSync(path.join(process.cwd(), partialPath, 'package.json'), 'utf8')).browser || 'index.js'));
return `${partialPath}/${index}`;
}
export function createOutput(_pkg, external, globals) {
const pkg = sanitizePkg(_pkg);
return {
file: `packages/${pkg}/build/bundle-subwallet-${pkg}.js`, format: 'umd', globals: external.reduce((all, pkg) => ({
[pkg]: createName(pkg), ...all
}), {...globals}), intro: 'const global = window;', name: createName(_pkg), preferConst: true
};
}
export function createBundle({entries = {}, external, globals = {}, index, inject = {}, pkg}) {
return {
external,
input: createInput(pkg, index),
output: createOutput(pkg, external, globals),
plugins: [pluginAlias({entries}), pluginJson(), pluginCommonjs(), pluginInject(inject), pluginResolve({browser: true}), pluginCleanup()]
};
}
const pkgs = [
'@subwallet/chain-list',
];
const external = [...pkgs];
const entries = ['chain-list'].reduce((all, p) => ({
...all, [`@subwallet/${p}`]: path.resolve(process.cwd(), `packages/${p}/build`)
}), {});
const overrides = {};
export default pkgs.map((pkg) => {
const override = (overrides[pkg] || {});
return createBundle({
external, pkg, ...override, entries: {
...entries, ...(override.entries || {})
}
});
});