Open History to see the last commit
1import type { 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 preserveDirectives from 'rollup-plugin-preserve-directives'
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
22}
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-persist-client-core',
49 packageDir: 'packages/query-persist-client-core',
50 jsName: 'QueryPersistClientCore',
51 outputFile: 'index',
52 entryFile: ['src/index.ts'],
53 globals: {
54 '@tanstack/query-core': 'QueryCore',
55 },
56 }),
57 ...buildConfigs({
58 name: 'query-async-storage-persister',
59 packageDir: 'packages/query-async-storage-persister',
60 jsName: 'QueryAsyncStoragePersister',
61 outputFile: 'index',
62 entryFile: 'src/index.ts',
63 globals: {
64 '@tanstack/query-persist-client-core': 'QueryPersistClientCore',
65 },
66 }),
67 ...buildConfigs({
68 name: 'query-broadcast-client-experimental',
69 packageDir: 'packages/query-broadcast-client-experimental',
70 jsName: 'QueryBroadcastClient',
71 outputFile: 'index',
72 entryFile: 'src/index.ts',
73 globals: {
74 '@tanstack/query-core': 'QueryCore',
75 'broadcast-channel': 'BroadcastChannel',
76 },
77 }),
78 ...buildConfigs({
79 name: 'query-sync-storage-persister',
80 packageDir: 'packages/query-sync-storage-persister',
81 jsName: 'QuerySyncStoragePersister',
82 outputFile: 'index',
83 entryFile: 'src/index.ts',
84 globals: {
85 '@tanstack/query-persist-client-core': 'QueryPersistClientCore',
86 },
87 }),
88 ...buildConfigs({
89 name: 'react-query',
90 packageDir: 'packages/react-query',
91 jsName: 'ReactQuery',
92 outputFile: 'index',
93 entryFile: [
94 'src/index.ts',
95 'src/reactBatchedUpdates.native.ts',
96 'src/useSyncExternalStore.native.ts',
97 ],
98 globals: {
99 react: 'React',
100 'react-dom': 'ReactDOM',
101 '@tanstack/query-core': 'QueryCore',
102 'use-sync-external-store/shim/index.js': 'UseSyncExternalStore',
103 'use-sync-external-store/shim/index.native.js':
104 'UseSyncExternalStoreNative',
105 'react-native': 'ReactNative',
106 },
107 bundleUMDGlobals: [
108 '@tanstack/query-core',
109 'use-sync-external-store/shim/index.js',
110 ],
111 }),
112 ...buildConfigs({
113 name: 'react-query-devtools',
114 packageDir: 'packages/react-query-devtools',
115 jsName: 'ReactQueryDevtools',
116 outputFile: 'index',
117 entryFile: 'src/index.ts',
118 globals: {
119 react: 'React',
120 'react-dom': 'ReactDOM',
121 '@tanstack/react-query': 'ReactQuery',
122 '@tanstack/match-sorter-utils': 'MatchSorterUtils',
123 'use-sync-external-store/shim/index.js': 'UseSyncExternalStore',
124 superjson: 'SuperJson',
125 },
126 bundleUMDGlobals: [
127 '@tanstack/match-sorter-utils',
128 'use-sync-external-store/shim/index.js',
129 'superjson',
130 ],
131 }),
132 ...buildConfigs({
133 name: 'react-query-devtools-prod',
134 packageDir: 'packages/react-query-devtools',
135 jsName: 'ReactQueryDevtools',
136 outputFile: 'index.prod',
137 entryFile: 'src/index.ts',
138 globals: {
139 react: 'React',
140 'react-dom': 'ReactDOM',
141 '@tanstack/react-query': 'ReactQuery',
142 '@tanstack/match-sorter-utils': 'MatchSorterUtils',
143 'use-sync-external-store/shim/index.js': 'UseSyncExternalStore',
144 superjson: 'SuperJson',
145 },
146 forceDevEnv: true,
147 forceBundle: true,
148 skipUmdBuild: true,
149 }),
150 ...buildConfigs({
151 name: 'react-query-persist-client',
152 packageDir: 'packages/react-query-persist-client',
153 jsName: 'ReactQueryPersistClient',
154 outputFile: 'index',
155 entryFile: 'src/index.ts',
156 globals: {
157 react: 'React',
158 '@tanstack/query-persist-client-core': 'QueryPersistClientCore',
159 '@tanstack/react-query': 'ReactQuery',
160 },
161 bundleUMDGlobals: ['@tanstack/query-persist-client-core'],
162 }),
163 ...buildConfigs({
164 name: 'solid-query',
165 packageDir: 'packages/solid-query',
166 jsName: 'SolidQuery',
167 outputFile: 'index',
168 entryFile: 'src/index.ts',
169 globals: {
170 'solid-js/store': 'SolidStore',
171 'solid-js': 'Solid',
172 '@tanstack/query-core': 'QueryCore',
173 },
174 bundleUMDGlobals: ['@tanstack/query-core'],
175 }),
176 ...buildConfigs({
177 name: 'vue-query',
178 packageDir: 'packages/vue-query',
179 jsName: 'VueQuery',
180 outputFile: 'index',
181 entryFile: 'src/index.ts',
182 globals: {
183 '@tanstack/query-core': 'QueryCore',
184 vue: 'Vue',
185 'vue-demi': 'Vue',
186 '@tanstack/match-sorter-utils': 'MatchSorter',
187 '@vue/devtools-api': 'DevtoolsApi',
188 },
189 bundleUMDGlobals: [
190 '@tanstack/query-core',
191 '@tanstack/match-sorter-utils',
192 '@vue/devtools-api',
193 ],
194 }),
195 ]
196}
197β
198function buildConfigs(opts: {
199 packageDir: string
200 name: string
201 jsName: string
202 outputFile: string
203 entryFile: string | string[]
204 globals: Record<string, string>
205 // This option allows to bundle specified dependencies for umd build
206 bundleUMDGlobals?: string[]
207 // Force prod env build
208 forceDevEnv?: boolean
209 forceBundle?: boolean
210 skipUmdBuild?: boolean
211}): RollupOptions[] {
212 const firstEntry = path.resolve(
213 opts.packageDir,
214 Array.isArray(opts.entryFile) ? opts.entryFile[0] : opts.entryFile,
215 )
216 const entries = Array.isArray(opts.entryFile)
217 ? opts.entryFile
218 : [opts.entryFile]
219 const input = entries.map((entry) => path.resolve(opts.packageDir, entry))
220 const externalDeps = Object.keys(opts.globals)
221β
222 const bundleUMDGlobals = opts.bundleUMDGlobals || []
223 const umdExternal = externalDeps.filter(
224 (external) => !bundleUMDGlobals.includes(external),
225 )
226β
227 const external = (moduleName) => externalDeps.includes(moduleName)
228 const banner = '' //createBanner(opts.name)
229β
230 const options: Options = {
231 input,
232 jsName: opts.jsName,
233 outputFile: opts.outputFile,
234 packageDir: opts.packageDir,
235 external,
236 banner,
237 globals: opts.globals,
238 forceDevEnv: opts.forceDevEnv || false,
239 forceBundle: opts.forceBundle || false,
240 }
241β
242 let builds = [mjs(options), esm(options), cjs(options)]
243β
244 if (!opts.skipUmdBuild) {
245 builds = builds.concat([
246 umdDev({ ...options, external: umdExternal, input: firstEntry }),
247 umdProd({ ...options, external: umdExternal, input: firstEntry }),
248 ])
249 }
250β
251 return builds
252}
253β
254function mjs({
255 input,
256 packageDir,
257 external,
258 banner,
259 outputFile,
260 forceDevEnv,
261 forceBundle,
262}: Options): RollupOptions {
263 const bundleOutput: OutputOptions = {
264 format: 'esm',
265 file: `${packageDir}/build/lib/${outputFile}.mjs`,
266 sourcemap: true,
267 banner,
268 }
269β
270 const normalOutput: OutputOptions = {
271 format: 'esm',
272 dir: `${packageDir}/build/lib`,
273 sourcemap: true,
274 banner,
275 preserveModules: true,
276 entryFileNames: '[name].mjs',
277 }
278β
279 return {
280 // ESM
281 external,
282 input,
283 output: forceBundle ? bundleOutput : normalOutput,
284 plugins: [
285 babelPlugin,
286 commonJS(),
287 nodeResolve({ extensions: ['.ts', '.tsx', '.native.ts'] }),
288 forceDevEnv ? forceEnvPlugin('development') : undefined,
289 preserveDirectives(),
290 ],
291 }
292}
293β
294function esm({
295 input,
296 packageDir,
297 external,
298 banner,
299 outputFile,
300 forceDevEnv,
301 forceBundle,
302}: Options): RollupOptions {
303 const bundleOutput: OutputOptions = {
304 format: 'esm',
305 file: `${packageDir}/build/lib/${outputFile}.esm.js`,
306 sourcemap: true,
307 banner,
308 }
309β
310 const normalOutput: OutputOptions = {
311 format: 'esm',
312 dir: `${packageDir}/build/lib`,
313 sourcemap: true,
314 banner,
315 preserveModules: true,
316 entryFileNames: '[name].esm.js',
317 }
318β
319 return {
320 // ESM
321 external,
322 input,
323 output: forceBundle ? bundleOutput : normalOutput,
324 plugins: [
325 babelPlugin,
326 commonJS(),
327 nodeResolve({ extensions: ['.ts', '.tsx', '.native.ts'] }),
328 forceDevEnv ? forceEnvPlugin('development') : undefined,
329 preserveDirectives(),
330 ],
331 }
332}
333β
334function cjs({
335 input,
336 external,
337 packageDir,
338 banner,
339 outputFile,
340 forceDevEnv,
341 forceBundle,
342}: Options): RollupOptions {
343 const bundleOutput: OutputOptions = {
344 format: 'cjs',
345 file: `${packageDir}/build/lib/${outputFile}.js`,
346 sourcemap: true,
347 exports: 'named',
348 banner,
349 }
350β
351 const normalOutput: OutputOptions = {
352 format: 'cjs',
353 dir: `${packageDir}/build/lib`,
354 sourcemap: true,
355 exports: 'named',
356 banner,
357 preserveModules: true,
358 entryFileNames: '[name].js',
359 }
360β
361 return {
362 // CJS
363 external,
364 input,
365 output: forceBundle ? bundleOutput : normalOutput,
366 plugins: [
367 babelPlugin,
368 commonJS(),
369 nodeResolve({ extensions: ['.ts', '.tsx', '.native.ts'] }),
370 forceDevEnv ? forceEnvPlugin('development') : undefined,
371 replace({
372 // TODO: figure out a better way to produce extensionless cjs imports
373 "require('./logger.js')": "require('./logger')",
374 "require('./reactBatchedUpdates.js')":
375 "require('./reactBatchedUpdates')",
376 "require('./useSyncExternalStore.js')":
377 "require('./useSyncExternalStore')",
378 preventAssignment: true,
379 delimiters: ['', ''],
380 }),
381 preserveDirectives(),
382 ],
383 }
384}
385β
386function umdDev({
387 input,
388 external,
389 packageDir,
390 outputFile,
391 globals,
392 banner,
393 jsName,
394}: Options): RollupOptions {
395 return {
396 // UMD (Dev)
397 external,
398 input,
399 output: {
400 format: 'umd',
401 sourcemap: true,
402 file: `${packageDir}/build/umd/${outputFile}.development.js`,
403 name: jsName,
404 globals,
405 banner,
406 },
407 plugins: [
408 commonJS(),
409 babelPlugin,
410 nodeResolve({ extensions: ['.ts', '.tsx', '.native.ts'] }),
411 forceEnvPlugin('development'),
412 ],
413 }
414}
415β
416function umdProd({
417 input,
418 external,
419 packageDir,
420 outputFile,
421 globals,
422 banner,
423 jsName,
424}: Options): RollupOptions {
425 return {
426 // UMD (Prod)
427 external,
428 input,
429 output: {
430 format: 'umd',
431 sourcemap: true,
432 file: `${packageDir}/build/umd/${outputFile}.production.js`,
433 name: jsName,
434 globals,
435 banner,
436 },
437 plugins: [
438 commonJS(),
439 babelPlugin,
440 nodeResolve({ extensions: ['.ts', '.tsx', '.native.ts'] }),
441 forceEnvPlugin('production'),
442 terser({
443 mangle: true,
444 compress: true,
445 }),
446 size({}),
447 visualizer({
448 filename: `${packageDir}/build/stats-html.html`,
449 gzipSize: true,
450 }),
451 visualizer({
452 filename: `${packageDir}/build/stats.json`,
453 json: true,
454 gzipSize: true,
455 }),
456 ],
457 }
458}
459β
460function createBanner(libraryName: string) {
461 return `/**
462 * ${libraryName}
463 *
464 * Copyright (c) TanStack
465 *
466 * This source code is licensed under the MIT license found in the
467 * LICENSE.md file in the root directory of this source tree.
468 *
469 * @license MIT
470 */`
471}