-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.ts
148 lines (140 loc) · 3.77 KB
/
rollup.config.ts
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
import nodeResolve from '@rollup/plugin-node-resolve';
import path from 'path';
import { RollupOptions } from 'rollup';
import del from 'rollup-plugin-delete';
// @ts-expect-error no typedefs exist for this plugin
import multiInput from 'rollup-plugin-multi-input';
import externals from 'rollup-plugin-node-externals';
import { swc } from 'rollup-plugin-swc3';
import typescript from 'rollup-plugin-typescript2';
const isWatchMode = process.argv.includes('--watch');
const extensions = ['.ts', '.tsx'];
// Exporting this for generating barrel-files in scripts/entrypoints.ts
export const PACKAGES = ['server', 'client', 'react-query', 'next'] as const;
export const INPUTS: Record<typeof PACKAGES[number], string[]> = {
server: [
'src/index.ts',
'src/adapters/aws-lambda/index.ts',
'src/adapters/express.ts',
'src/adapters/fastify/index.ts',
'src/adapters/next.ts',
'src/adapters/node-http/index.ts',
'src/adapters/standalone.ts',
'src/adapters/ws.ts',
'src/adapters/fetch/index.ts',
'src/http/index.ts',
'src/rpc/index.ts',
'src/observable/index.ts',
'src/subscription.ts',
// Utils that can be shared with clients
'src/shared/index.ts',
],
client: [
'src/index.ts',
'src/links/httpLink.ts',
'src/links/httpBatchLink.ts',
'src/links/splitLink.ts',
'src/links/loggerLink.ts',
'src/links/wsLink.ts',
],
'react-query': ['src/index.ts', 'src/ssg/index.ts', 'src/shared/index.ts'],
next: ['src/index.ts'],
};
export default function rollup(): RollupOptions[] {
return [
...buildConfig({
input: INPUTS.server,
packageDir: 'packages/server',
}),
...buildConfig({
input: INPUTS.client,
packageDir: 'packages/client',
}),
...buildConfig({
input: INPUTS['react-query'],
packageDir: 'packages/react-query',
}),
...buildConfig({
input: INPUTS.next,
packageDir: 'packages/next',
}),
];
}
type Options = {
input: string[];
packageDir: string;
};
function buildConfig({ input, packageDir }: Options): RollupOptions[] {
const resolvedInput = input.map((file) => path.resolve(packageDir, file));
const options: Options = {
input: resolvedInput,
packageDir,
};
return [types(options), lib(options)];
}
function types({ input, packageDir }: Options): RollupOptions {
return {
input,
output: {
dir: `${packageDir}/dist`,
},
plugins: [
!isWatchMode &&
del({
targets: `${packageDir}/dist`,
}),
multiInput({ relative: path.resolve(packageDir, 'src/') }),
externals({
packagePath: path.resolve(packageDir, 'package.json'),
deps: true,
devDeps: true,
peerDeps: true,
}),
typescript({
tsconfig: path.resolve(packageDir, 'tsconfig.build.json'),
tsconfigOverride: { emitDeclarationOnly: true },
abortOnError: !isWatchMode,
}),
],
};
}
function lib({ input, packageDir }: Options): RollupOptions {
return {
input,
output: [
{
dir: `${packageDir}/dist`,
format: 'cjs',
entryFileNames: '[name].js',
chunkFileNames: '[name]-[hash].js',
},
{
dir: `${packageDir}/dist`,
format: 'esm',
entryFileNames: '[name].mjs',
chunkFileNames: '[name]-[hash].mjs',
},
],
plugins: [
multiInput({ relative: path.resolve(packageDir, 'src/') }),
externals({
packagePath: path.resolve(packageDir, 'package.json'),
}),
nodeResolve({
extensions,
}),
swc({
tsconfig: false,
jsc: {
target: 'es2020',
transform: {
react: {
useBuiltins: true,
},
},
externalHelpers: true,
},
}),
],
};
}