Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

rollup.config.ts

420 lines10.4 KB

1import { OutputOptions, RollupOptions } from 'rollup'
2import babel from '@rollup/plugin-babel'
3import { terser } from 'rollup-plugin-terser'
4import size from 'rollup-plugin-size'
5import visualizer from 'rollup-plugin-visualizer'
6import replace from '@rollup/plugin-replace'
7import nodeResolve from '@rollup/plugin-node-resolve'
8import commonJS from '@rollup/plugin-commonjs'
9import path from 'path'
10import svelte from 'rollup-plugin-svelte'
11​
12type Options = {
13 input: string | string[]
14 packageDir: string
15 external: RollupOptions['external']
16 banner: string
17 jsName: string
18 outputFile: string
19 globals: Record<string, string>
20 forceDevEnv: boolean
21 forceBundle: boolean
23​
24const forceEnvPlugin = (type: 'development' | 'production') =>
25 replace({
26 'process.env.NODE_ENV': `"${type}"`,
27 delimiters: ['', ''],
28 preventAssignment: true,
29 })
30​
31const babelPlugin = babel({
32 babelHelpers: 'bundled',
33 exclude: /node_modules/,
34 extensions: ['.ts', '.tsx', '.native.ts'],
35})
36​
37export default function rollup(options: RollupOptions): RollupOptions[] {
38 return [
39 ...buildConfigs({
40 name: 'query-core',
41 packageDir: 'packages/query-core',
42 jsName: 'QueryCore',
43 outputFile: 'index',
44 entryFile: ['src/index.ts', 'src/logger.native.ts'],
45 globals: {},
46 }),
47 ...buildConfigs({
48 name: 'query-async-storage-persister',
49 packageDir: 'packages/query-async-storage-persister',
50 jsName: 'QueryAsyncStoragePersister',
51 outputFile: 'index',
52 entryFile: 'src/index.ts',
53 globals: {
54 '@tanstack/react-query-persist-client': 'ReactQueryPersistClient',
55 },
56 }),
57 ...buildConfigs({
58 name: 'query-broadcast-client-experimental',
59 packageDir: 'packages/query-broadcast-client-experimental',
60 jsName: 'QueryBroadcastClient',
61 outputFile: 'index',
62 entryFile: 'src/index.ts',
63 globals: {
64 '@tanstack/query-core': 'QueryCore',
65 'broadcast-channel': 'BroadcastChannel',
66 },
67 }),
68 ...buildConfigs({
69 name: 'query-sync-storage-persister',
70 packageDir: 'packages/query-sync-storage-persister',
71 jsName: 'QuerySyncStoragePersister',
72 outputFile: 'index',
73 entryFile: 'src/index.ts',
74 globals: {
75 '@tanstack/react-query-persist-client': 'ReactQueryPersistClient',
76 },
77 }),
78 ...buildConfigs({
79 name: 'react-query',
80 packageDir: 'packages/react-query',
81 jsName: 'ReactQuery',
82 outputFile: 'index',
83 entryFile: ['src/index.ts', 'src/reactBatchedUpdates.native.ts', 'src/useSyncExternalStore.native.ts'],
84 globals: {
85 react: 'React',
86 'react-dom': 'ReactDOM',
87 '@tanstack/query-core': 'QueryCore',
88 'use-sync-external-store/shim/index.js': 'UseSyncExternalStore',
89 'use-sync-external-store/shim/index.native.js': 'UseSyncExternalStoreNative',
90 'react-native': 'ReactNative',
91 },
92 bundleUMDGlobals: [
93 '@tanstack/query-core',
94 'use-sync-external-store/shim/index.js',
95 ],
96 }),
97 ...buildConfigs({
98 name: 'react-query-devtools',
99 packageDir: 'packages/react-query-devtools',
100 jsName: 'ReactQueryDevtools',
101 outputFile: 'index',
102 entryFile: 'src/index.ts',
103 globals: {
104 react: 'React',
105 'react-dom': 'ReactDOM',
106 '@tanstack/react-query': 'ReactQuery',
107 '@tanstack/match-sorter-utils': 'MatchSorterUtils',
108 'use-sync-external-store/shim/index.js': 'UseSyncExternalStore',
109 },
110 bundleUMDGlobals: [
111 '@tanstack/match-sorter-utils',
112 'use-sync-external-store/shim/index.js',
113 ],
114 }),
115 ...buildConfigs({
116 name: 'react-query-devtools-prod',
117 packageDir: 'packages/react-query-devtools',
118 jsName: 'ReactQueryDevtools',
119 outputFile: 'index.prod',
120 entryFile: 'src/index.ts',
121 globals: {
122 react: 'React',
123 'react-dom': 'ReactDOM',
124 '@tanstack/react-query': 'ReactQuery',
125 '@tanstack/match-sorter-utils': 'MatchSorterUtils',
126 'use-sync-external-store/shim/index.js': 'UseSyncExternalStore',
127 },
128 forceDevEnv: true,
129 forceBundle: true,
130 skipUmdBuild: true,
131 }),
132 ...buildConfigs({
133 name: 'react-query-persist-client',
134 packageDir: 'packages/react-query-persist-client',
135 jsName: 'ReactQueryPersistClient',
136 outputFile: 'index',
137 entryFile: 'src/index.ts',
138 globals: {
139 react: 'React',
140 '@tanstack/query-core': 'QueryCore',
141 '@tanstack/react-query': 'ReactQuery',
142 },
143 }),
144 ]
146​
147function buildConfigs(opts: {
148 packageDir: string
149 name: string
150 jsName: string
151 outputFile: string
152 entryFile: string | string[]
153 globals: Record<string, string>
154 // This option allows to bundle specified dependencies for umd build
155 bundleUMDGlobals?: string[]
156 // Force prod env build
157 forceDevEnv?: boolean
158 forceBundle?: boolean
159 skipUmdBuild?: boolean
160}): RollupOptions[] {
161 const firstEntry = path.resolve(
162 opts.packageDir,
163 Array.isArray(opts.entryFile) ? opts.entryFile[0] : opts.entryFile,
164 )
165 const entries = Array.isArray(opts.entryFile)
166 ? opts.entryFile
167 : [opts.entryFile]
168 const input = entries.map((entry) => path.resolve(opts.packageDir, entry))
169 const externalDeps = Object.keys(opts.globals)
170​
171 const bundleUMDGlobals = opts.bundleUMDGlobals || []
172 const umdExternal = externalDeps.filter(
173 (external) => !bundleUMDGlobals.includes(external),
174 )
175​
176 const external = (moduleName) => externalDeps.includes(moduleName)
177 const banner = '' //createBanner(opts.name)
178​
179 const options: Options = {
180 input,
181 jsName: opts.jsName,
182 outputFile: opts.outputFile,
183 packageDir: opts.packageDir,
184 external,
185 banner,
186 globals: opts.globals,
187 forceDevEnv: opts.forceDevEnv || false,
188 forceBundle: opts.forceBundle || false,
189 }
190​
191 let builds = [mjs(options), esm(options), cjs(options)]
192​
193 if (!opts.skipUmdBuild) {
194 builds = builds.concat([
195 umdDev({ ...options, external: umdExternal, input: firstEntry }),
196 umdProd({ ...options, external: umdExternal, input: firstEntry }),
197 ])
198 }
199​
200 return builds
202​
203function mjs({
204 input,
205 packageDir,
206 external,
207 banner,
208 outputFile,
209 forceDevEnv,
210 forceBundle,
211}: Options): RollupOptions {
212 const bundleOutput: OutputOptions = {
213 format: 'esm',
214 file: `${packageDir}/build/lib/${outputFile}.mjs`,
215 sourcemap: true,
216 banner,
217 }
218​
219 const normalOutput: OutputOptions = {
220 format: 'esm',
221 dir: `${packageDir}/build/lib`,
222 sourcemap: true,
223 banner,
224 preserveModules: true,
225 entryFileNames: '[name].mjs',
226 }
227​
228 return {
229 // ESM
230 external,
231 input,
232 output: forceBundle ? bundleOutput : normalOutput,
233 plugins: [
234 svelte(),
235 babelPlugin,
236 commonJS(),
237 nodeResolve({ extensions: ['.ts', '.tsx', '.native.ts'] }),
238 forceDevEnv ? forceEnvPlugin('development') : undefined,
239 ],
240 }
242​
243function esm({
244 input,
245 packageDir,
246 external,
247 banner,
248 outputFile,
249 forceDevEnv,
250 forceBundle,
251}: Options): RollupOptions {
252 const bundleOutput: OutputOptions = {
253 format: 'esm',
254 file: `${packageDir}/build/lib/${outputFile}.esm.js`,
255 sourcemap: true,
256 banner,
257 }
258​
259 const normalOutput: OutputOptions = {
260 format: 'esm',
261 dir: `${packageDir}/build/lib`,
262 sourcemap: true,
263 banner,
264 preserveModules: true,
265 entryFileNames: '[name].esm.js',
266 }
267​
268 return {
269 // ESM
270 external,
271 input,
272 output: forceBundle ? bundleOutput : normalOutput,
273 plugins: [
274 svelte(),
275 babelPlugin,
276 commonJS(),
277 nodeResolve({ extensions: ['.ts', '.tsx', '.native.ts'] }),
278 forceDevEnv ? forceEnvPlugin('development') : undefined,
279 ],
280 }
282​
283function cjs({
284 input,
285 external,
286 packageDir,
287 banner,
288 outputFile,
289 forceDevEnv,
290 forceBundle,
291}: Options): RollupOptions {
292 const bundleOutput: OutputOptions = {
293 format: 'cjs',
294 file: `${packageDir}/build/lib/${outputFile}.js`,
295 sourcemap: true,
296 exports: 'named',
297 banner,
298 }
299​
300 const normalOutput: OutputOptions = {
301 format: 'cjs',
302 dir: `${packageDir}/build/lib`,
303 sourcemap: true,
304 exports: 'named',
305 banner,
306 preserveModules: true,
307 entryFileNames: '[name].js',
308 }
309​
310 return {
311 // CJS
312 external,
313 input,
314 output: forceBundle ? bundleOutput : normalOutput,
315 plugins: [
316 svelte(),
317 babelPlugin,
318 commonJS(),
319 nodeResolve({ extensions: ['.ts', '.tsx', '.native.ts'] }),
320 forceDevEnv ? forceEnvPlugin('development') : undefined,
321 replace({
322 // TODO: figure out a better way to produce extensionless cjs imports
323 "require('./logger.js')": "require('./logger')",
324 "require('./reactBatchedUpdates.js')": "require('./reactBatchedUpdates')",
325 "require('./useSyncExternalStore.js')": "require('./useSyncExternalStore')",
326 preventAssignment: true,
327 delimiters: ['', ''],
328 }),
329 ],
330 }
332​
333function umdDev({
334 input,
335 external,
336 packageDir,
337 outputFile,
338 globals,
339 banner,
340 jsName,
341}: Options): RollupOptions {
342 return {
343 // UMD (Dev)
344 external,
345 input,
346 output: {
347 format: 'umd',
348 sourcemap: true,
349 file: `${packageDir}/build/umd/${outputFile}.development.js`,
350 name: jsName,
351 globals,
352 banner,
353 },
354 plugins: [
355 svelte(),
356 commonJS(),
357 babelPlugin,
358 nodeResolve({ extensions: ['.ts', '.tsx', '.native.ts'] }),
359 forceEnvPlugin('development'),
360 ],
361 }
363​
364function umdProd({
365 input,
366 external,
367 packageDir,
368 outputFile,
369 globals,
370 banner,
371 jsName,
372}: Options): RollupOptions {
373 return {
374 // UMD (Prod)
375 external,
376 input,
377 output: {
378 format: 'umd',
379 sourcemap: true,
380 file: `${packageDir}/build/umd/${outputFile}.production.js`,
381 name: jsName,
382 globals,
383 banner,
384 },
385 plugins: [
386 svelte(),
387 commonJS(),
388 babelPlugin,
389 nodeResolve({ extensions: ['.ts', '.tsx', '.native.ts'] }),
390 forceEnvPlugin('production'),
391 terser({
392 mangle: true,
393 compress: true,
394 }),
395 size({}),
396 visualizer({
397 filename: `${packageDir}/build/stats-html.html`,
398 gzipSize: true,
399 }),
400 visualizer({
401 filename: `${packageDir}/build/stats.json`,
402 json: true,
403 gzipSize: true,
404 }),
405 ],
406 }
408​
409function createBanner(libraryName: string) {
410 return `/**
411 * ${libraryName}
412 *
413 * Copyright (c) TanStack
414 *
415 * This source code is licensed under the MIT license found in the
416 * LICENSE.md file in the root directory of this source tree.
417 *
418 * @license MIT
419 */`