-
Notifications
You must be signed in to change notification settings - Fork 818
Expand file tree
/
Copy pathrollup.config.js
More file actions
127 lines (119 loc) · 4.15 KB
/
rollup.config.js
File metadata and controls
127 lines (119 loc) · 4.15 KB
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
import { resolve, dirname } from "path";
import esbuild from "rollup-plugin-esbuild";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import json from "@rollup/plugin-json";
import { visualizer } from "rollup-plugin-visualizer";
export const input = "./src/index.ts";
// Workaround for @rollup/plugin-node-resolve v16 not applying the package.json
// "browser" field file-level remappings for internal relative imports within
// transitive dependencies. Specifically, multiformats/esm/src/basics.js imports
// "./hashes/sha2.js" (Node.js version using `import crypto from 'crypto'`)
// instead of the browser-safe "./hashes/sha2-browser.js" (using Web Crypto API).
// This plugin runs before nodeResolve and redirects the import.
// Applies to multiformats <=9.x internal layout. Re-verify on upgrades.
// The failOnNodeBuiltins guard below will catch it if this workaround stops applying.
// See: https://github.com/WalletConnect/walletconnect-monorepo/issues/7197
const browserFieldOverrides = {
name: "browser-field-overrides",
resolveId(source, importer) {
if (!importer || !source.startsWith(".")) return null;
if (!importer.includes("node_modules/multiformats/")) return null;
const resolved = resolve(dirname(importer), source);
if (resolved.endsWith("/hashes/sha2.js")) {
return resolved.replace("/hashes/sha2.js", "/hashes/sha2-browser.js");
}
return null;
},
};
export const plugins = [
browserFieldOverrides,
nodeResolve({ preferBuiltins: false, browser: true, exportConditions: ["browser"] }),
json(),
commonjs(),
esbuild({
minify: true,
tsconfig: "./tsconfig.json",
loaders: {
".json": "json",
},
}),
visualizer(),
];
// Rollup's `external` option with a plain array only matches exact dependency names,
// so subpath imports like "uint8arrays/from-string" are not externalized and get bundled,
// pulling in transitive dependencies (e.g. multiformats -> Node.js crypto).
// This function returns a matcher that also externalizes subpath imports (dep + "/...").
export function isExternal(packageDependencies) {
return (id) => packageDependencies.some((dep) => id === dep || id.startsWith(dep + "/"));
}
// Fail the UMD (browser) build if Node.js built-in modules are referenced.
// This catches regressions where transitive dependencies leak Node.js-only
// imports (e.g. "crypto", "fs", "path") into the browser bundle.
function failOnNodeBuiltins(warning, defaultHandler) {
if (warning.code === "MISSING_NODE_BUILTINS") {
throw new Error(warning.message);
}
if (warning.code === "UNRESOLVED_IMPORT" && warning.exporter && isNodeBuiltin(warning.exporter)) {
throw new Error(`Node.js built-in "${warning.exporter}" imported by ${warning.id} — not allowed in browser bundles`);
}
defaultHandler(warning);
}
const NODE_BUILTINS = new Set([
"assert", "buffer", "child_process", "cluster", "crypto", "dgram", "dns",
"events", "fs", "http", "http2", "https", "net", "os", "path", "perf_hooks",
"punycode", "querystring", "readline", "stream", "string_decoder", "tls",
"tty", "url", "util", "v8", "vm", "worker_threads", "zlib",
]);
function isNodeBuiltin(id) {
return NODE_BUILTINS.has(id) || NODE_BUILTINS.has(id.replace("node:", ""));
}
export default function createConfig(
packageName,
packageDependencies,
umd = {},
cjs = {},
es = {},
extraBuilds = [],
) {
return [
{
input,
plugins,
onwarn: failOnNodeBuiltins,
output: {
file: "./dist/index.umd.js",
format: "umd",
exports: "named",
name: packageName,
sourcemap: true,
...umd,
},
},
{
input,
plugins,
external: isExternal(packageDependencies),
output: [
{
file: "./dist/index.cjs",
format: "cjs",
exports: "named",
interop: "auto",
name: packageName,
sourcemap: true,
...cjs,
},
{
file: "./dist/index.js",
format: "es",
exports: "named",
name: packageName,
sourcemap: true,
...es,
},
],
},
...extraBuilds,
];
}