forked from TexteaInc/json-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.ts
147 lines (135 loc) · 3.78 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
136
137
138
139
140
141
142
143
144
145
146
147
/// <reference types="node" />
import { basename, resolve } from 'node:path'
import alias from '@rollup/plugin-alias'
import commonjs from '@rollup/plugin-commonjs'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import replace from '@rollup/plugin-replace'
import type {
ModuleFormat,
OutputOptions,
RollupOptions
} from 'rollup'
import dts from 'rollup-plugin-dts'
import { defineRollupSwcOption, swc } from 'rollup-plugin-swc3'
import { fileURLToPath } from 'url'
const outputDir = fileURLToPath(new URL('dist', import.meta.url))
const externalDeps = [
'@emotion/react',
'@emotion/styled',
'@emotion/react/jsx-runtime',
'@emotion/react/jsx-dev-runtime',
'@mui/material',
/@mui\/material\/.*/,
'copy-to-clipboard',
'zustand',
'zustand/context',
'zustand/middleware',
'react',
'react/jsx-runtime',
'react-dom',
'react-dom/client'
]
const aliasPlugin = alias({
entries: [
{ find: 'react', replacement: '@emotion/react' },
{ find: 'react/jsx-runtime', replacement: '@emotion/react/jsx-runtime' },
{ find: 'react/jsx-dev-runtime', replacement: '@emotion/react/jsx-dev-runtime' }
]
})
const replacePlugin = replace({
preventAssignment: true,
'process.env.NODE_ENV': JSON.stringify('production'),
'typeof window': JSON.stringify('object')
})
const esmTransformImportsPlugin: [string, Record<string, any>] = [
'@swc/plugin-transform-imports',
{
'@mui/material': { transform: '@mui/material/{{member}}/index.js' },
'@mui/material/styles': { transform: '@mui/material/styles/{{member}}.js' }
}
]
const cjsTransformImportsPlugin: [string, Record<string, any>] = [
'@swc/plugin-transform-imports',
{
'@mui/material': { transform: '@mui/material/node/{{member}}' },
'@mui/material/styles': { transform: '@mui/material/node/styles/{{member}}' }
}
]
const outputMatrix = (
name: string, format: ModuleFormat[]): OutputOptions[] => {
const baseName = basename(name)
return format.map(format => ({
file: resolve(outputDir, `${baseName}.${format === 'es' ? 'm' : ''}js`),
sourcemap: false,
name: 'JsonViewer',
format,
banner: `/// <reference types="./${baseName}.d.ts" />`,
globals: externalDeps.reduce((object, module) => {
if (typeof module === 'string') {
object[module] = module
}
return object
}, {} as Record<string, string>)
}))
}
const buildMatrix = (input: string, output: string, config: {
format: ModuleFormat[]
browser: boolean
}): RollupOptions[] => {
return [
...config.format.map(format => ({
input,
output: outputMatrix(output, [format]),
external: config.browser ? [] : externalDeps,
plugins: [
!config.browser && aliasPlugin,
config.browser && replacePlugin,
commonjs(),
nodeResolve(),
swc(defineRollupSwcOption({
jsc: {
externalHelpers: true,
parser: {
syntax: 'typescript',
tsx: true
},
transform: {
react: {
runtime: 'automatic',
importSource: '@emotion/react'
}
},
experimental: {
plugins: config.browser
? []
: format === 'es'
? [esmTransformImportsPlugin]
: [cjsTransformImportsPlugin]
}
}
}))
]
})),
{
input,
output: {
file: resolve(outputDir, `${output}.d.ts`),
format: 'es'
},
plugins: [
dts()
]
}
]
}
const build: RollupOptions[] = [
...buildMatrix('./src/index.tsx', 'index', {
format: ['es', 'cjs'],
browser: false
}),
...buildMatrix('./src/browser.tsx', 'browser', {
format: ['es', 'umd'],
browser: true
})
]
export default build