forked from solana-labs/solana-web3.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.js
216 lines (205 loc) · 5.69 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import alias from '@rollup/plugin-alias';
import babel from '@rollup/plugin-babel';
import commonjs from '@rollup/plugin-commonjs';
import * as fs from 'fs';
import json from '@rollup/plugin-json';
import path from 'path';
import nodeResolve from '@rollup/plugin-node-resolve';
import replace from '@rollup/plugin-replace';
import {terser} from 'rollup-plugin-terser';
const env = process.env.NODE_ENV;
const extensions = ['.js', '.ts'];
function generateConfig(configType, format) {
const browser = configType === 'browser' || configType === 'react-native';
const bundle = format === 'iife';
const config = {
input: 'src/index.ts',
plugins: [
alias({
entries: [
{
find: /^\./, // Relative paths.
replacement: '.',
async customResolver(source, importer, options) {
const resolved = await this.resolve(source, importer, {
skipSelf: true,
...options,
});
if (resolved == null) {
return;
}
const {id: resolvedId} = resolved;
const directory = path.dirname(resolvedId);
const moduleFilename = path.basename(resolvedId);
const forkPath = path.join(
directory,
'__forks__',
configType,
moduleFilename,
);
const hasForkCacheKey = `has_fork:${forkPath}`;
let hasFork = this.cache.get(hasForkCacheKey);
if (hasFork === undefined) {
hasFork = fs.existsSync(forkPath);
this.cache.set(hasForkCacheKey, hasFork);
}
if (hasFork) {
return forkPath;
}
},
},
],
}),
commonjs(),
nodeResolve({
browser,
dedupe: ['bn.js', 'buffer'],
extensions,
preferBuiltins: !browser,
}),
babel({
exclude: '**/node_modules/**',
extensions,
babelHelpers: bundle ? 'bundled' : 'runtime',
plugins: bundle ? [] : ['@babel/plugin-transform-runtime'],
}),
replace({
preventAssignment: true,
values: {
'process.env.NODE_ENV': JSON.stringify(env),
'process.env.BROWSER': JSON.stringify(browser),
'process.env.npm_package_version': JSON.stringify(
process.env.npm_package_version,
),
},
}),
],
onwarn: function (warning, rollupWarn) {
rollupWarn(warning);
if (warning.code === 'CIRCULAR_DEPENDENCY') {
throw new Error(
'Please eliminate the circular dependencies listed ' +
'above and retry the build',
);
}
},
treeshake: {
moduleSideEffects: false,
},
};
if (!browser) {
// Prevent dependencies from being bundled
config.external = [
/@babel\/runtime/,
'@noble/hashes/hmac',
'@noble/hashes/sha256',
'@noble/hashes/sha3',
'@noble/hashes/sha512',
'@noble/ed25519',
'@noble/secp256k1',
'@solana/buffer-layout',
'bigint-buffer',
'bn.js',
'borsh',
'bs58',
'buffer',
'crypto-hash',
'jayson/lib/client/browser',
'node-fetch',
'rpc-websockets',
'superstruct',
];
}
switch (configType) {
case 'browser':
case 'react-native':
switch (format) {
case 'iife': {
config.external = ['http', 'https', 'node-fetch'];
config.output = [
{
file: 'lib/index.iife.js',
format: 'iife',
name: 'solanaWeb3',
sourcemap: true,
},
{
file: 'lib/index.iife.min.js',
format: 'iife',
name: 'solanaWeb3',
sourcemap: true,
plugins: [terser({mangle: false, compress: false})],
},
];
break;
}
default: {
config.output = [
{
file: `lib/index.${
configType === 'react-native' ? 'native' : 'browser.cjs'
}.js`,
format: 'cjs',
sourcemap: true,
},
configType === 'browser'
? {
file: 'lib/index.browser.esm.js',
format: 'es',
sourcemap: true,
}
: null,
].filter(Boolean);
// Prevent dependencies from being bundled
config.external = [
/@babel\/runtime/,
'@solana/buffer-layout',
'@noble/hashes/hmac',
'@noble/hashes/sha256',
'@noble/hashes/sha3',
'@noble/hashes/sha512',
'@noble/ed25519',
'@noble/secp256k1',
'bigint-buffer',
'bn.js',
'borsh',
'bs58',
'buffer',
'crypto-hash',
'http',
'https',
'jayson/lib/client/browser',
'node-fetch',
'react-native-url-polyfill',
'rpc-websockets',
'superstruct',
];
break;
}
}
break;
case 'node':
config.output = [
{
file: 'lib/index.cjs.js',
format: 'cjs',
sourcemap: true,
},
{
file: 'lib/index.esm.js',
format: 'es',
sourcemap: true,
},
];
break;
default:
throw new Error(`Unknown configType: ${configType}`);
}
return config;
}
export default [
generateConfig('node'),
generateConfig('browser'),
generateConfig('browser', 'iife'),
generateConfig('react-native'),
];