@@ -8,26 +8,21 @@ import replace from '@rollup/plugin-replace'
8
8
import type {
9
9
ModuleFormat ,
10
10
OutputOptions ,
11
- RollupCache ,
12
11
RollupOptions
13
12
} from 'rollup'
14
13
import dts from 'rollup-plugin-dts'
15
14
import { defineRollupSwcOption , swc } from 'rollup-plugin-swc3'
16
15
import { fileURLToPath } from 'url'
17
16
18
- let cache : RollupCache
19
-
20
- const dtsOutput = new Set < [ string , string ] > ( )
21
-
22
17
const outputDir = fileURLToPath ( new URL ( 'dist' , import . meta. url ) )
23
18
24
- const external = [
19
+ const externalDeps = [
25
20
'@emotion/react' ,
26
21
'@emotion/styled' ,
27
22
'@emotion/react/jsx-runtime' ,
28
23
'@emotion/react/jsx-dev-runtime' ,
29
24
'@mui/material' ,
30
- ' @mui/material/styles' ,
25
+ / @ m u i \ /m a t e r i a l \/ . * / ,
31
26
'copy-to-clipboard' ,
32
27
'zustand' ,
33
28
'zustand/context' ,
@@ -37,17 +32,50 @@ const external = [
37
32
'react-dom' ,
38
33
'react-dom/client'
39
34
]
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
+
40
66
const outputMatrix = (
41
67
name : string , format : ModuleFormat [ ] ) : OutputOptions [ ] => {
42
68
const baseName = basename ( name )
43
- return format . flatMap ( format => ( {
69
+ return format . map ( format => ( {
44
70
file : resolve ( outputDir , `${ baseName } .${ format === 'es' ? 'm' : '' } js` ) ,
45
71
sourcemap : false ,
46
72
name : 'JsonViewer' ,
47
73
format,
48
74
banner : `/// <reference types="./${ baseName } .d.ts" />` ,
49
- globals : external . reduce ( ( object , module ) => {
50
- object [ module ] = module
75
+ globals : externalDeps . reduce ( ( object , module ) => {
76
+ if ( typeof module === 'string' ) {
77
+ object [ module ] = module
78
+ }
51
79
return object
52
80
} , { } as Record < string , string > )
53
81
} ) )
@@ -56,84 +84,64 @@ const outputMatrix = (
56
84
const buildMatrix = ( input : string , output : string , config : {
57
85
format : ModuleFormat [ ]
58
86
browser : boolean
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 '
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'
82
110
}
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'
111
+ } ,
112
+ experimental : {
113
+ plugins : config . browser
114
+ ? [ ]
115
+ : format === 'es'
116
+ ? [ esmTransformImportsPlugin ]
117
+ : [ cjsTransformImportsPlugin ]
103
118
}
104
119
}
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
- } ) )
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
+ ]
123
134
}
124
135
125
136
const build : RollupOptions [ ] = [
126
- buildMatrix ( './src/index.tsx' , 'index' , {
127
- format : [ 'es' , 'umd' ] ,
128
- browser : false ,
129
- dts : true
137
+ ...buildMatrix ( './src/index.tsx' , 'index' , {
138
+ format : [ 'es' , 'cjs' ] ,
139
+ browser : false
130
140
} ) ,
131
- buildMatrix ( './src/browser.tsx' , 'browser' , {
141
+ ... buildMatrix ( './src/browser.tsx' , 'browser' , {
132
142
format : [ 'es' , 'umd' ] ,
133
- browser : true ,
134
- dts : true
135
- } ) ,
136
- ...dtsMatrix ( )
143
+ browser : true
144
+ } )
137
145
]
138
146
139
147
export default build
0 commit comments