@@ -8,21 +8,26 @@ import replace from '@rollup/plugin-replace'
8
8
import type {
9
9
ModuleFormat ,
10
10
OutputOptions ,
11
+ RollupCache ,
11
12
RollupOptions
12
13
} from 'rollup'
13
14
import dts from 'rollup-plugin-dts'
14
15
import { defineRollupSwcOption , swc } from 'rollup-plugin-swc3'
15
16
import { fileURLToPath } from 'url'
16
17
18
+ let cache : RollupCache
19
+
20
+ const dtsOutput = new Set < [ string , string ] > ( )
21
+
17
22
const outputDir = fileURLToPath ( new URL ( 'dist' , import . meta. url ) )
18
23
19
- const externalDeps = [
24
+ const external = [
20
25
'@emotion/react' ,
21
26
'@emotion/styled' ,
22
27
'@emotion/react/jsx-runtime' ,
23
28
'@emotion/react/jsx-dev-runtime' ,
24
29
'@mui/material' ,
25
- / @ m u i \ /m a t e r i a l \/ . * / ,
30
+ ' @mui/material/styles' ,
26
31
'copy-to-clipboard' ,
27
32
'zustand' ,
28
33
'zustand/context' ,
@@ -32,50 +37,17 @@ const externalDeps = [
32
37
'react-dom' ,
33
38
'react-dom/client'
34
39
]
35
-
36
- const aliasPlugin = alias ( {
37
- entries : [
38
- { find : 'react' , replacement : '@emotion/react' } ,
39
- { find : 'react/jsx-runtime' , replacement : '@emotion/react/jsx-runtime' } ,
40
- { find : 'react/jsx-dev-runtime' , replacement : '@emotion/react/jsx-dev-runtime' }
41
- ]
42
- } )
43
-
44
- const replacePlugin = replace ( {
45
- preventAssignment : true ,
46
- 'process.env.NODE_ENV' : JSON . stringify ( 'production' ) ,
47
- 'typeof window' : JSON . stringify ( 'object' )
48
- } )
49
-
50
- const esmTransformImportsPlugin : [ string , Record < string , any > ] = [
51
- '@swc/plugin-transform-imports' ,
52
- {
53
- '@mui/material' : { transform : '@mui/material/{{member}}/index.js' } ,
54
- '@mui/material/styles' : { transform : '@mui/material/styles/{{member}}.js' }
55
- }
56
- ]
57
-
58
- const cjsTransformImportsPlugin : [ string , Record < string , any > ] = [
59
- '@swc/plugin-transform-imports' ,
60
- {
61
- '@mui/material' : { transform : '@mui/material/node/{{member}}' } ,
62
- '@mui/material/styles' : { transform : '@mui/material/node/styles/{{member}}' }
63
- }
64
- ]
65
-
66
40
const outputMatrix = (
67
41
name : string , format : ModuleFormat [ ] ) : OutputOptions [ ] => {
68
42
const baseName = basename ( name )
69
- return format . map ( format => ( {
43
+ return format . flatMap ( format => ( {
70
44
file : resolve ( outputDir , `${ baseName } .${ format === 'es' ? 'm' : '' } js` ) ,
71
45
sourcemap : false ,
72
46
name : 'JsonViewer' ,
73
47
format,
74
48
banner : `/// <reference types="./${ baseName } .d.ts" />` ,
75
- globals : externalDeps . reduce ( ( object , module ) => {
76
- if ( typeof module === 'string' ) {
77
- object [ module ] = module
78
- }
49
+ globals : external . reduce ( ( object , module ) => {
50
+ object [ module ] = module
79
51
return object
80
52
} , { } as Record < string , string > )
81
53
} ) )
@@ -84,64 +56,84 @@ const outputMatrix = (
84
56
const buildMatrix = ( input : string , output : string , config : {
85
57
format : ModuleFormat [ ]
86
58
browser : boolean
87
- } ) : RollupOptions [ ] => {
88
- return [
89
- ... config . format . map ( format => ( {
90
- input,
91
- output : outputMatrix ( output , [ format ] ) ,
92
- external : config . browser ? [ ] : externalDeps ,
93
-
94
- plugins : [
95
- ! config . browser && aliasPlugin ,
96
- config . browser && replacePlugin ,
97
- commonjs ( ) ,
98
- nodeResolve ( ) ,
99
- swc ( defineRollupSwcOption ( {
100
- jsc : {
101
- externalHelpers : true ,
102
- parser : {
103
- syntax : 'typescript' ,
104
- tsx : true
105
- } ,
106
- transform : {
107
- react : {
108
- runtime : 'automatic ' ,
109
- importSource : '@emotion/react'
59
+ dts : boolean
60
+ } ) : RollupOptions => {
61
+ if ( config . dts ) {
62
+ dtsOutput . add ( [ input , output ] )
63
+ }
64
+ return {
65
+ input ,
66
+ output : outputMatrix ( output , config . format ) ,
67
+ cache ,
68
+ external : config . browser ? [ ] : external ,
69
+ plugins : [
70
+ alias ( {
71
+ entries : config . browser
72
+ ? [ ]
73
+ : [
74
+ { find : 'react' , replacement : '@emotion/react' } ,
75
+ {
76
+ find : 'react/jsx-dev-runtime' ,
77
+ replacement : '@emotion/react/jsx-dev-runtime'
78
+ } ,
79
+ {
80
+ find : 'react/jsx-runtime ' ,
81
+ replacement : '@emotion/react/jsx-runtime '
110
82
}
111
- } ,
112
- experimental : {
113
- plugins : config . browser
114
- ? [ ]
115
- : format === 'es'
116
- ? [ esmTransformImportsPlugin ]
117
- : [ cjsTransformImportsPlugin ]
83
+ ]
84
+ } ) ,
85
+ config . browser && replace ( {
86
+ preventAssignment : true ,
87
+ 'process.env.NODE_ENV' : JSON . stringify ( 'production' ) ,
88
+ 'typeof window' : JSON . stringify ( 'object' )
89
+ } ) ,
90
+ commonjs ( ) ,
91
+ nodeResolve ( ) ,
92
+ swc ( defineRollupSwcOption ( {
93
+ jsc : {
94
+ externalHelpers : true ,
95
+ parser : {
96
+ syntax : 'typescript' ,
97
+ tsx : true
98
+ } ,
99
+ transform : {
100
+ react : {
101
+ runtime : 'automatic' ,
102
+ importSource : '@emotion/react'
118
103
}
119
104
}
120
- } ) )
121
- ]
122
- } ) ) ,
123
- {
124
- input,
125
- output : {
126
- file : resolve ( outputDir , `${ output } .d.ts` ) ,
127
- format : 'es'
128
- } ,
129
- plugins : [
130
- dts ( )
131
- ]
132
- }
133
- ]
105
+ }
106
+ } ) )
107
+ ]
108
+ }
109
+ }
110
+
111
+ const dtsMatrix = ( ) : RollupOptions [ ] => {
112
+ return [ ...dtsOutput . values ( ) ] . flatMap ( ( [ input , output ] ) => ( {
113
+ input,
114
+ cache,
115
+ output : {
116
+ file : resolve ( outputDir , `${ output } .d.ts` ) ,
117
+ format : 'es'
118
+ } ,
119
+ plugins : [
120
+ dts ( )
121
+ ]
122
+ } ) )
134
123
}
135
124
136
125
const build : RollupOptions [ ] = [
137
- ...buildMatrix ( './src/index.tsx' , 'index' , {
138
- format : [ 'es' , 'cjs' ] ,
139
- browser : false
126
+ buildMatrix ( './src/index.tsx' , 'index' , {
127
+ format : [ 'es' , 'umd' ] ,
128
+ browser : false ,
129
+ dts : true
140
130
} ) ,
141
- ... buildMatrix ( './src/browser.tsx' , 'browser' , {
131
+ buildMatrix ( './src/browser.tsx' , 'browser' , {
142
132
format : [ 'es' , 'umd' ] ,
143
- browser : true
144
- } )
133
+ browser : true ,
134
+ dts : true
135
+ } ) ,
136
+ ...dtsMatrix ( )
145
137
]
146
138
147
139
export default build
0 commit comments