-
Notifications
You must be signed in to change notification settings - Fork 347
Expand file tree
/
Copy pathrollup.config.mjs
More file actions
297 lines (268 loc) · 9.46 KB
/
rollup.config.mjs
File metadata and controls
297 lines (268 loc) · 9.46 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import fs from "node:fs";
import path from "node:path";
import process from "node:process";
import { builtinModules } from "node:module";
import { fileURLToPath } from "node:url";
import commonjs from "@rollup/plugin-commonjs";
import inject from "@rollup/plugin-inject";
import resolve from "@rollup/plugin-node-resolve";
import replace from "@rollup/plugin-replace";
import terser from "@rollup/plugin-terser";
import esbuild from "rollup-plugin-esbuild";
import nodePolyfills from "rollup-plugin-polyfill-node";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const packageJson = JSON.parse(
fs.readFileSync(path.join(__dirname, "package.json"), "utf8"),
);
const useAxios = process.env.USE_AXIOS === "true";
// Bundle these dependencies into the lib output instead of leaving bare
// imports. `@stellar/js-xdr` ships a webpack UMD bundle as `main`, which
// hides its named exports from Node ESM's cjs-module-lexer; inlining it
// at build time sidesteps the interop entirely.
// TODO: remove this once js-xdr ships an ESM build with proper named exports. Until then, it converts js-xdr's UMD export into a shape rollup can analyze and re-export from our ESM output.
const inlinedDependencies = new Set(["@stellar/js-xdr"]);
const externalPackages = new Set(
[
...Object.keys(packageJson.dependencies ?? {}),
...Object.keys(packageJson.peerDependencies ?? {}),
].filter((name) => !inlinedDependencies.has(name)),
);
const builtinPackageIds = new Set([
...builtinModules,
...builtinModules.map((moduleId) => `node:${moduleId}`),
]);
function isExternalDependency(id) {
if (builtinPackageIds.has(id)) {
return true;
}
for (const dependencyName of externalPackages) {
if (id === dependencyName || id.startsWith(`${dependencyName}/`)) {
return true;
}
}
return false;
}
// Under TS module:nodenext, source imports are written with `.js` extensions
// that point at `.ts` files on disk. Teach rollup to try the `.ts` sibling when
// the `.js` file doesn't exist.
function resolveJsSourceSpecifier() {
return {
name: "resolve-js-source-specifier",
resolveId(source, importer) {
if (!importer || !source.startsWith(".") || !source.endsWith(".js")) {
return null;
}
const resolvedSourcePath = path.resolve(path.dirname(importer), source);
const sourceCandidates = [
resolvedSourcePath,
resolvedSourcePath.slice(0, -3) + ".ts",
];
for (const candidatePath of sourceCandidates) {
if (fs.existsSync(candidatePath)) {
return candidatePath;
}
}
return null;
},
};
}
// Axios variant: rewrite relative `./http-client/index.js` (or bare
// `./http-client`) imports to `./http-client/axios.js` so Horizon/rpc/
// stellartoml/federation wire up to the axios client. Mirrors the old
// babel plugin (config/babel-plugin-alias-http-client.js) and the webpack
// NormalModuleReplacementPlugin used for the browser bundle.
function aliasHttpClientToAxios() {
const patterns = [
{
match: /(^|\/)http-client\/index\.js$/,
replace: "$1http-client/axios.js",
},
{ match: /(^|\/)http-client$/, replace: "$1http-client/axios" },
];
const rewrite = (value) => {
for (const { match, replace: r } of patterns) {
if (match.test(value)) return value.replace(match, r);
}
return value;
};
return {
name: "alias-http-client-to-axios",
resolveId(source, importer) {
if (!importer || !source.startsWith(".")) return null;
const rewritten = rewrite(source);
if (rewritten === source) return null;
return this.resolve(rewritten, importer, { skipSelf: true });
},
};
}
// Browser bundle only: rollup-plugin-polyfill-node ships a legacy `buffer-es6`
// polyfill that lacks BigInt accessors (readBigUInt64BE, writeBigInt64BE, ...).
// SDK source uses bare `Buffer.from(...)` etc. which our `inject` plugin rewrites
// into `import { Buffer } from "buffer"`. If polyfill-node resolves that
// specifier first, `Buffer.from()` returns a legacy buffer instance; js-xdr's
// `Buffer.isBuffer(t)` duck-types it as a Buffer (it sets `_isBuffer = true`),
// stores it as `_buffer`, and later `_buffer.readBigUInt64BE(...)` throws
// `is not a function`. Pin `buffer` (and `node:buffer`) to the real npm package
// (`buffer@6.0.3+`) before polyfill-node has a chance to intercept.
function aliasBufferToNpmBuffer() {
return {
name: "alias-buffer-to-npm-buffer",
resolveId(source, importer) {
if (source !== "buffer" && source !== "node:buffer") return null;
return this.resolve("buffer/index.js", importer, { skipSelf: true });
},
};
}
// Browser bundle only: rollup-plugin-polyfill-node's zlib polyfill omits the
// `constants` object that axios's HTTP adapter accesses at module init
// (`zlib.constants.Z_SYNC_FLUSH`). Back it with browserify-zlib, which has
// all the Z_* constants but exposes them as flat top-level exports. Wrap it
// in a virtual module that re-exports everything and also groups the
// constants into a `constants` namespace, matching Node's zlib shape.
function aliasZlibToBrowserifyZlib() {
const VIRTUAL_ID = "\0zlib-with-constants";
return {
name: "alias-zlib-to-browserify-zlib",
resolveId(source) {
if (source === "zlib" || source === "node:zlib") return VIRTUAL_ID;
return null;
},
load(id) {
if (id !== VIRTUAL_ID) return null;
return [
`import * as __bz from "browserify-zlib";`,
`export * from "browserify-zlib";`,
`const constants = Object.fromEntries(`,
` Object.entries(__bz).filter(([k]) => /^(Z_|BROTLI_|DEFLATE|INFLATE|GZIP|GUNZIP|UNZIP)/.test(k))`,
`);`,
`export { constants };`,
`export default { ...__bz, constants };`,
].join("\n");
},
};
}
function createOutput(dir, format) {
return {
dir,
format,
exports: "named",
interop: format === "cjs" ? "auto" : undefined,
preserveModules: true,
preserveModulesRoot: "src",
sourcemap: true,
entryFileNames: "[name].js",
};
}
const replaceVersion = replace({
preventAssignment: true,
values: {
__PACKAGE_VERSION__: JSON.stringify(packageJson.version),
},
});
const libSharedPlugins = [
// Alias plugin must run BEFORE resolveJsSourceSpecifier so it still sees
// the original relative specifier (e.g. "../http-client/index.js").
...(useAxios ? [aliasHttpClientToAxios()] : []),
resolveJsSourceSpecifier(),
// TODO: remove this plugin once js-xdr ships an ESM build with proper named exports. Until then, it converts js-xdr's UMD export into a shape rollup can analyze and re-export from our ESM output.
// resolve + commonjs are needed to pull `@stellar/js-xdr` into the bundle.
// External deps short-circuit before reaching these plugins, so other
// dependencies remain bare imports.
resolve({
extensions: [".ts", ".mjs", ".js", ".json"],
}),
// TODO: remove this plugin once js-xdr ships an ESM build with proper named exports. Until then, it converts js-xdr's UMD export into a shape rollup can analyze and re-export from our ESM output.
commonjs(),
esbuild({
sourceMap: true,
target: "es2022",
tsconfig: "tsconfig.json",
loaders: {
".js": "js",
".ts": "ts",
},
}),
inject({
Buffer: ["buffer", "Buffer"],
}),
replaceVersion,
];
const libBaseDir = useAxios ? "lib/axios" : "lib";
// Additional entry points beyond src/index.ts. These ensure their output
// files exist even when rollup's tree-shaking would otherwise inline their
// exports: the `exports` map in package.json references `http-client/axios`
// as a public subpath, and tests import `http-client` directly. Listing them
// as entries guarantees emission without relying on graph reachability.
const libEntries = {
index: "src/index.ts",
"http-client/index": "src/http-client/index.ts",
"http-client/axios": "src/http-client/axios.ts",
"cli/index": "src/cli/index.ts",
};
/** Library build — preserves modules, externalizes dependencies. */
const libConfig = {
input: libEntries,
external: isExternalDependency,
plugins: libSharedPlugins,
output: [
createOutput(`${libBaseDir}/esm`, "esm"),
createOutput(`${libBaseDir}/cjs`, "cjs"),
],
};
/** Bundled dist build — single UMD file with all dependencies included.
* Intended for direct use in browsers or via CDNs. */
const distEntry = useAxios ? "src/browser-axios.ts" : "src/browser.ts";
const distBaseName = useAxios ? "stellar-sdk-axios" : "stellar-sdk";
const distPlugins = [
...(useAxios ? [aliasHttpClientToAxios()] : []),
resolveJsSourceSpecifier(),
aliasBufferToNpmBuffer(),
aliasZlibToBrowserifyZlib(),
resolve({
browser: true,
preferBuiltins: false,
extensions: [".ts", ".mjs", ".js", ".json"],
}),
commonjs(),
nodePolyfills(),
esbuild({
sourceMap: true,
target: "es2022",
tsconfig: "tsconfig.json",
loaders: {
".js": "js",
".ts": "ts",
},
}),
inject({
Buffer: ["buffer", "Buffer"],
}),
replaceVersion,
];
const distConfig = {
input: distEntry,
plugins: distPlugins,
output: [
{
file: `dist/${distBaseName}.js`,
format: "umd",
name: "StellarSdk",
exports: "named",
sourcemap: true,
},
{
file: `dist/${distBaseName}.min.js`,
format: "umd",
name: "StellarSdk",
exports: "named",
sourcemap: true,
plugins: [
terser({
format: { ascii_only: true },
}),
],
},
],
};
export default [libConfig, distConfig];