Open History to see the last commit
1import { 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 path from 'path'
9β
10type Options = {
11 input: string
12 packageDir: string
13 external: RollupOptions['external']
14 banner: string
15 jsName: string
16 outputFile: string
17}
18β
19const globals = {
20 react: 'React',
21 'react-dom': 'ReactDOM',
22 '@tanstack/react-location': 'ReactLocation',
23 '@tanstack/react-location-lite-experimental': 'ReactLocationLiteExperimental',
24 '@tanstack/react-location-devtools': 'ReactLocationDevtools',
25 '@tanstack/react-location-elements-to-routes':
26 'ReactLocationElementsToRoutes',
27 '@tanstack/react-location-simple-cache': 'ReactLocationSimpleCache',
28 '@tanstack/react-location-rank-routes': 'ReactLocationRankRoutes',
29 '@tanstack/react-location-jsurl': 'ReactLocationJsurl',
30 history: 'History',
31}
32β
33const externals = [...Object.keys(globals), /@babel\/runtime/, 'history']
34β
35const umdDevPlugin = (type: 'development' | 'production') =>
36 replace({
37 'process.env.NODE_ENV': `"${type}"`,
38 delimiters: ['', ''],
39 preventAssignment: true,
40 })
41β
42const babelPlugin = babel({
43 babelHelpers: 'bundled',
44 exclude: /node_modules/,
45 extensions: ['.ts', '.tsx'],
46})
47β
48export default function rollup(options: RollupOptions): RollupOptions[] {
49 return [
50 ...buildConfigs({
51 name: 'react-location',
52 packageDir: 'packages/react-location',
53 jsName: 'ReactLocation',
54 outputFile: 'react-location',
55 entryFile: 'src/index.tsx',
56 }),
57 ...buildConfigs({
58 name: 'react-location-lite-experimental',
59 packageDir: 'packages/react-location-lite-experimental',
60 jsName: 'ReactLocationLite',
61 outputFile: 'react-location-lite-experimental',
62 entryFile: 'src/index.tsx',
63 }),
64 ...buildConfigs({
65 name: 'react-location-devtools',
66 packageDir: 'packages/react-location-devtools',
67 jsName: 'ReactLocationDevtools',
68 outputFile: 'react-location-devtools',
69 entryFile: 'src/index.tsx',
70 }),
71 ...buildConfigs({
72 name: 'react-location-elements-to-routes',
73 packageDir: 'packages/react-location-elements-to-routes',
74 jsName: 'ReactLocationElementsToRoutes',
75 outputFile: 'react-location-elements-to-routes',
76 entryFile: 'src/index.tsx',
77 }),
78 ...buildConfigs({
79 name: 'react-location-simple-cache',
80 packageDir: 'packages/react-location-simple-cache',
81 jsName: 'ReactLocationSimpleCache',
82 outputFile: 'react-location-simple-cache',
83 entryFile: 'src/index.tsx',
84 }),
85 ...buildConfigs({
86 name: 'react-location-rank-routes',
87 packageDir: 'packages/react-location-rank-routes',
88 jsName: 'ReactLocationRankRoutes',
89 outputFile: 'react-location-rank-routes',
90 entryFile: 'src/index.tsx',
91 }),
92 ...buildConfigs({
93 name: 'react-location-jsurl',
94 packageDir: 'packages/react-location-jsurl',
95 jsName: 'ReactLocationJsurl',
96 outputFile: 'react-location-jsurl',
97 entryFile: 'src/index.tsx',
98 }),
99 ]
100}
101β
102function buildConfigs(opts: {
103 packageDir: string
104 name: string
105 jsName: string
106 outputFile: string
107 entryFile: string
108}): RollupOptions[] {
109 const input = path.resolve(opts.packageDir, opts.entryFile)
110 const externalDeps = [...externals]
111β
112 const external = (moduleName) => externalDeps.includes(moduleName)
113 const banner = createBanner(opts.name)
114β
115 const options: Options = {
116 input,
117 jsName: opts.jsName,
118 outputFile: opts.outputFile,
119 packageDir: opts.packageDir,
120 external,
121 banner,
122 }
123β
124 return [esm(options), cjs(options), umdDev(options), umdProd(options)]
125}
126β
127function esm({ input, packageDir, external, banner }: Options): RollupOptions {
128 return {
129 // ESM
130 external,
131 input,
132 output: {
133 format: 'esm',
134 sourcemap: true,
135 dir: `${packageDir}/build/esm`,
136 banner,
137 },
138 plugins: [babelPlugin, nodeResolve({ extensions: ['.ts', '.tsx'] })],
139 }
140}
141β
142function cjs({ input, external, packageDir, banner }: Options): RollupOptions {
143 return {
144 // CJS
145 external,
146 input,
147 output: {
148 format: 'cjs',
149 sourcemap: true,
150 dir: `${packageDir}/build/cjs`,
151 preserveModules: true,
152 exports: 'named',
153 banner,
154 },
155 plugins: [babelPlugin, nodeResolve({ extensions: ['.ts', '.tsx'] })],
156 }
157}
158β
159function umdDev({
160 input,
161 external,
162 packageDir,
163 outputFile,
164 banner,
165 jsName,
166}: Options): RollupOptions {
167 return {
168 // UMD (Dev)
169 external,
170 input,
171 output: {
172 format: 'umd',
173 sourcemap: true,
174 file: `${packageDir}/build/umd/index.development.js`,
175 name: jsName,
176 globals,
177 banner,
178 },
179 plugins: [
180 babelPlugin,
181 nodeResolve({ extensions: ['.ts', '.tsx'] }),
182 umdDevPlugin('development'),
183 ],
184 }
185}
186β
187function umdProd({
188 input,
189 external,
190 packageDir,
191 outputFile,
192 banner,
193 jsName,
194}: Options): RollupOptions {
195 return {
196 // UMD (Prod)
197 external,
198 input,
199 output: {
200 format: 'umd',
201 sourcemap: true,
202 file: `${packageDir}/build/umd/index.production.min.js`,
203 name: jsName,
204 globals,
205 banner,
206 },
207 plugins: [
208 babelPlugin,
209 nodeResolve({ extensions: ['.ts', '.tsx'] }),
210 umdDevPlugin('production'),
211 terser({
212 mangle: true,
213 compress: true,
214 }),
215 size({}),
216 visualizer({
217 filename: `${packageDir}/build/stats-html.html`,
218 gzipSize: true,
219 }),
220 visualizer({
221 filename: `${packageDir}/build/stats-react.json`,
222 json: true,
223 gzipSize: true,
224 }),
225 ],
226 }
227}
228β
229function createBanner(libraryName: string) {
230 return `/**
231 * ${libraryName}
232 *
233 * Copyright (c) TanStack
234 *
235 * This source code is licensed under the MIT license found in the
236 * LICENSE.md file in the root directory of this source tree.
237 *
238 * @license MIT
239 */`
240}