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