forked from polkadot-js/extension
-
Notifications
You must be signed in to change notification settings - Fork 61
/
rollup.config.mjs
109 lines (94 loc) · 2.59 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
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
// Copyright 2017-2022 @subwallet/extension 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 `polkadot-${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-polkadot-${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/extension-dapp'
];
const external = [
...pkgs,
'@polkadot/networks',
'@polkadot/util',
'@polkadot/util-crypto'
];
const entries = ['extension-base', 'extension-chains', 'extension-inject'].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 || {})
},
resolve: {
fallback: {
"url": false,
"zlib": false,
"https": false,
"http": false,
}
}
});
});