-
Notifications
You must be signed in to change notification settings - Fork 535
/
rollup.config.js
192 lines (174 loc) · 4.43 KB
/
rollup.config.js
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import commonjs from '@rollup/plugin-commonjs'
import resolve from '@rollup/plugin-node-resolve'
import babel from '@rollup/plugin-babel'
import replace from '@rollup/plugin-replace'
import terser from '@rollup/plugin-terser'
import glob from 'fast-glob'
import {visualizer} from 'rollup-plugin-visualizer'
import postcss from 'rollup-plugin-postcss'
import packageJson from './package.json'
const input = new Set([
// "exports"
// "."
'src/index.ts',
// "./experimental"
'src/experimental/index.ts',
// "./drafts"
'src/drafts/index.ts',
// "./deprecated"
'src/deprecated/index.ts',
// Make sure all members are exported
'src/constants.ts',
...glob.sync(
[
// "./lib-esm/hooks/*"
'src/hooks/*',
// "./lib-esm/polyfills/*"
'src/polyfills/*',
// "./lib-esm/utils/*"
'src/utils/*',
// for backward compatbility, see https://github.com/primer/react/pull/3740
'src/ActionMenu/index.ts',
],
{
cwd: __dirname,
ignore: [
'**/__tests__/**',
'*.stories.tsx',
// File currently imports from package.json
'src/utils/test-deprecations.tsx',
// Files use dependencies which are not listed by package
'src/utils/testing.tsx',
'src/utils/test-matchers.tsx',
],
},
),
])
const extensions = ['.js', '.jsx', '.ts', '.tsx']
const ESM_ONLY = new Set([
'@github/combobox-nav',
'@github/markdown-toolbar-element',
'@github/paste-markdown',
'@github/relative-time-element',
'@lit-labs/react',
'@oddbird/popover-polyfill',
])
const dependencies = [
...Object.keys(packageJson.peerDependencies ?? {}),
...Object.keys(packageJson.dependencies ?? {}),
...Object.keys(packageJson.devDependencies ?? {}),
]
function createPackageRegex(name) {
return new RegExp(`^${name}(/.*)?`)
}
const baseConfig = {
input: Array.from(input),
plugins: [
babel({
extensions,
exclude: /node_modules/,
babelHelpers: 'inline',
babelrc: false,
configFile: false,
presets: [
'@babel/preset-typescript',
[
'@babel/preset-react',
{
modules: false,
},
],
],
plugins: [
'macros',
'add-react-displayname',
'dev-expression',
'babel-plugin-styled-components',
'@babel/plugin-proposal-nullish-coalescing-operator',
'@babel/plugin-proposal-optional-chaining',
[
'babel-plugin-transform-replace-expressions',
{
replace: {
__DEV__: "process.env.NODE_ENV !== 'production'",
},
},
],
],
}),
resolve({
extensions,
}),
commonjs({
extensions,
}),
postcss({
extract: 'components.css',
autoModules: false,
modules: {generateScopedName: 'prc_[local]_[hash:base64:5]'},
// plugins are defined in postcss.config.js
}),
],
onwarn(warning, defaultHandler) {
// Dependencies or modules may use "use client" as an indicator for React
// Server Components that this module should only be loaded on the client.
if (warning.code === 'MODULE_LEVEL_DIRECTIVE' && warning.message.includes('use client')) {
return
}
defaultHandler(warning)
},
}
export default [
// ESM
{
...baseConfig,
external: dependencies.map(createPackageRegex),
output: {
interop: 'auto',
dir: 'lib-esm',
format: 'esm',
preserveModules: true,
preserveModulesRoot: 'src',
},
},
// CommonJS
{
...baseConfig,
external: dependencies.filter(name => !ESM_ONLY.has(name)).map(createPackageRegex),
output: {
interop: 'auto',
dir: 'lib',
format: 'commonjs',
preserveModules: true,
preserveModulesRoot: 'src',
exports: 'auto',
},
},
// Bundles
{
...baseConfig,
input: 'src/index.ts',
external: ['styled-components', 'react', 'react-dom'],
plugins: [
replace({
'process.env.NODE_ENV': JSON.stringify('production'),
preventAssignment: true,
}),
...baseConfig.plugins,
terser(),
visualizer({sourcemap: true}),
],
output: ['esm', 'umd'].map(format => ({
interop: 'auto',
file: `dist/browser.${format}.js`,
format,
sourcemap: true,
name: 'primer',
globals: {
react: 'React',
'react-dom': 'ReactDOM',
'styled-components': 'styled',
},
})),
},
]