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