-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
rollup.config.ts
135 lines (126 loc) · 3.38 KB
/
rollup.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/// <reference types="node" />
import { basename, resolve } from 'node:path'
import commonjs from '@rollup/plugin-commonjs'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import replace from '@rollup/plugin-replace'
import type {
ModuleFormat,
OutputOptions,
RollupCache,
RollupOptions
} from 'rollup'
import dts from 'rollup-plugin-dts'
import { defineRollupSwcOption, swc } from 'rollup-plugin-swc3'
import { fileURLToPath } from 'url'
let cache: RollupCache
const dtsOutput = new Set<[string, string]>()
const outputDir = fileURLToPath(new URL('dist', import.meta.url))
const external = [
'@mui/material',
'@mui/material/styles',
'copy-to-clipboard',
'zustand',
'react',
'react/jsx-runtime',
'react-dom',
'react-dom/client'
]
const outputMatrix = (
name: string, format: ModuleFormat[]): OutputOptions[] => {
const baseName = basename(name)
return format.flatMap(format => ({
file: resolve(outputDir, `${baseName}.${format === 'es' ? 'm' : ''}js`),
sourcemap: false,
name: 'JsonViewer',
format,
banner: `/// <reference types="./${baseName}.d.ts" />`,
globals: external.reduce((object, module) => {
object[module] = module
return object
}, {} as Record<string, string>)
}))
}
const buildMatrix = (input: string, output: string, config: {
format: ModuleFormat[]
browser: boolean
dts: boolean
}): RollupOptions => {
if (config.dts) {
dtsOutput.add([input, output])
}
return {
input,
output: outputMatrix(output, config.format),
cache,
external: config.browser ? [] : external,
plugins: [
config.browser && replace({
preventAssignment: true,
'process.env.NODE_ENV': JSON.stringify('production'),
'typeof window': JSON.stringify('object')
}),
commonjs(),
nodeResolve(),
swc(defineRollupSwcOption({
jsc: {
externalHelpers: true,
parser: {
syntax: 'typescript',
tsx: true
},
transform: {
react: {
runtime: 'automatic'
}
}
},
env: {
// we have to copy this configuration because swc is not handling `.browserslistrc` properly
// see https://github.com/swc-project/swc/issues/3365
targets: 'and_chr 91,and_ff 89,and_qq 10.4,and_uc 12.12,android 91,baidu 7.12,chrome 90,edge 91,firefox 78,ios_saf 12.2,kaios 2.5,op_mini all,op_mob 76'
},
tsconfig: false
}))
],
/**
* Ignore "use client" waning
* @see {@link https://github.com/TanStack/query/pull/5161#issuecomment-1477389761 Preserve 'use client' directives}
*/
onwarn (warning, warn) {
if (
warning.code === 'MODULE_LEVEL_DIRECTIVE' &&
warning.message.includes('"use client"')
) {
return
}
warn(warning)
}
}
}
const dtsMatrix = (): RollupOptions[] => {
return [...dtsOutput.values()].flatMap(([input, output]) => ({
input,
cache,
output: {
file: resolve(outputDir, `${output}.d.ts`),
format: 'es'
},
plugins: [
dts()
]
}))
}
const build: RollupOptions[] = [
buildMatrix('./src/index.tsx', 'index', {
format: ['es', 'umd'],
browser: false,
dts: true
}),
buildMatrix('./src/browser.tsx', 'browser', {
format: ['es', 'umd'],
browser: true,
dts: true
}),
...dtsMatrix()
]
export default build