-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrollup.config.mjs
More file actions
89 lines (81 loc) · 2.16 KB
/
Copy pathrollup.config.mjs
File metadata and controls
89 lines (81 loc) · 2.16 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
// Copyright AGNTCY Contributors (https://github.com/agntcy)
// SPDX-License-Identifier: Apache-2.0
import json from '@rollup/plugin-json';
import { readFileSync } from 'fs';
import typescript from 'rollup-plugin-typescript2';
import { nodeResolve } from '@rollup/plugin-node-resolve';
const pkg = JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8'));
const rollupPlugins = [
nodeResolve(),
typescript({
tsconfigOverride: {
exclude: ['test/**'],
},
}),
json({
preferConst: true,
}),
fixBufEsmImportSpecifiers(),
];
/** Keep npm dependencies external so Rollup does not rewrite `this` in bundled deps. */
const externalPackages = [
...Object.keys(pkg.dependencies ?? {}),
...Object.keys(pkg.optionalDependencies ?? {}),
];
function isExternal(id) {
if (id.startsWith('node:')) {
return true;
}
if (id.startsWith('.') || id.startsWith('/') || id.startsWith('\0')) {
return false;
}
return externalPackages.some((dep) => id === dep || id.startsWith(`${dep}/`));
}
/**
* Node ESM requires explicit .js extensions for @buf protobuf subpath imports.
*/
function fixBufEsmImportSpecifiers() {
return {
name: 'fix-buf-esm-import-specifiers',
renderChunk(code, chunk) {
if (!chunk.fileName.endsWith('.mjs')) {
return null;
}
const updated = code.replace(
/from (['"])(@buf\/[^'"]+)(['"])/g,
(_, quote, specifier, endQuote) =>
specifier.endsWith('.js')
? `from ${quote}${specifier}${endQuote}`
: `from ${quote}${specifier}.js${endQuote}`,
);
if (updated === code) {
return null;
}
return { code: updated, map: null };
},
};
}
export default [
// Cross ES module (dist/index.mjs)
{
input: 'src/index.ts',
output: {
file: pkg.exports['.']['import'],
format: 'es',
sourcemap: true,
},
plugins: rollupPlugins,
external: isExternal,
},
// Cross CJS module (dist/index.cjs)
{
input: 'src/index.ts',
output: {
file: pkg.exports['.']['require'],
format: 'cjs',
sourcemap: true,
},
plugins: rollupPlugins,
external: isExternal,
},
];