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