forked from primer/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.js
178 lines (162 loc) · 3.96 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
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 glob from 'fast-glob'
import {terser} from 'rollup-plugin-terser'
import visualizer from 'rollup-plugin-visualizer'
import packageJson from './package.json'
const input = new Set([
// "exports"
// "."
'src/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/*'
],
{
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 external = [
...Object.keys(packageJson.peerDependencies ?? {}),
...Object.keys(packageJson.dependencies ?? {}),
...Object.keys(packageJson.devDependencies ?? {})
]
function isExternal(external) {
return id => {
// Match on import paths that are the same as dependencies listed in
// `package.json`
const match = external.find(pkg => {
return id === pkg
})
if (match) {
return true
}
// In some cases, there may be a subpath import from a module which should
// also be treated as external. For example:
//
// External: @primer/behaviors
// Import: @primer/behaviors/utils
return external.some(pkg => {
// Include the / to not match imports like: @primer/behaviors-for-acme/utils
return id.startsWith(`${pkg}/`)
})
}
}
const baseConfig = {
input: Array.from(input),
plugins: [
// Note: it's important that the babel plugin is ordered first for plugins
// like babel-plugin-preval to work as-intended
babel({
extensions,
exclude: /node_modules/,
babelHelpers: 'inline',
babelrc: false,
configFile: false,
presets: [
'@babel/preset-typescript',
[
'@babel/preset-react',
{
modules: false
}
]
],
plugins: [
'macros',
'preval',
'add-react-displayname',
'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'"
}
}
]
]
}),
commonjs(),
resolve({
extensions
})
]
}
export default [
// ESM
{
...baseConfig,
external: isExternal(external),
output: {
dir: 'lib-esm',
format: 'esm',
preserveModules: true,
preserveModulesRoot: 'src'
}
},
// CommonJS
{
...baseConfig,
external: isExternal(external),
output: {
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 => ({
file: `dist/browser.${format}.js`,
format,
sourcemap: true,
name: 'primer',
globals: {
react: 'React',
'react-dom': 'ReactDOM',
'styled-components': 'styled'
}
}))
}
]