forked from Rate-Limiting-Nullifier/rlnjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.mjs
93 lines (84 loc) · 2.77 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
/* eslint-disable import/no-extraneous-dependencies */
import typescript from 'rollup-plugin-typescript2'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import json from '@rollup/plugin-json'
import nodePolyfills from 'rollup-plugin-polyfill-node'
import replace from '@rollup/plugin-replace'
import { visualizer } from 'rollup-plugin-visualizer'
import cleaner from 'rollup-plugin-cleaner'
import * as fs from 'fs'
const input = 'src/index.ts'
const pkg = JSON.parse(fs.readFileSync('./package.json', 'utf-8'))
const banner = `/**
* @module ${pkg.name}
* @version ${pkg.version}
* @file ${pkg.description}
* @copyright Ethereum Foundation 2022
* @license ${pkg.license}
* @see [Github]{@link ${pkg.homepage}}
*/`
const typescriptPlugin = typescript({
tsconfig: 'tsconfig.build.json',
useTsconfigDeclarationDir: true,
})
const visualizerPlugin = visualizer({
emitFile: true,
filename: 'stats.html',
template: 'sunburst',
})
const nodePlugins = [
typescriptPlugin,
// `browser: false` is required for `fs` and other Node.js core modules to be resolved correctly
nodeResolve({ browser: false }),
// To accept commonjs modules and convert them to ES module, since rollup only bundle ES modules by default
commonjs(),
// Parse JSON files and make them ES modules. Required when bundling circomlib
json(),
]
const browserPlugins = [
typescriptPlugin,
replace({
// Replace `process.browser` with `true` to avoid `process is not defined` error
// This is because ffjavascript and snarkjs use `process.browser` to check if it's running in the browser,
// but process is undefined in the browser and referencing `process.browser` causes an error.
// Ref: https://github.com/iden3/ffjavascript/blob/e670bfeb17e80b961eab77e15a6b9eca8e31a0be/src/threadman.js#L43
'process.browser': JSON.stringify(true),
// To avoid unexpected behavior that the warning suggests.
'preventAssignment': true,
}),
// Resolve the import from node_modules.
// `browser: true` is required for `window` not to be undefined
// Ref: https://github.com/iden3/snarkjs/blob/782894ab72b09cfad4dd8b517599d5e7b2340468/src/taskmanager.js#L20-L24
nodeResolve({ browser: true }),
commonjs(),
json(),
// Replace node built-in modules with polyfills
nodePolyfills(),
]
export default [
// Node.js build
{
input,
output: { file: pkg.main, format: 'cjs', banner },
external: Object.keys(pkg.dependencies),
plugins: [
cleaner({
targets: [
'./dist/',
],
}),
...nodePlugins,
visualizerPlugin,
],
},
// Browser build
{
input,
output: { file: pkg.module, format: 'es', banner },
plugins: [
...browserPlugins,
visualizerPlugin,
],
},
]