-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathrollup.config.mjs
313 lines (280 loc) · 7.89 KB
/
rollup.config.mjs
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import * as fs from 'fs'
import * as path from 'path'
import { DEFAULT_EXTENSIONS } from '@babel/core'
import alias from '@rollup/plugin-alias'
import { babel } from '@rollup/plugin-babel'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import terser from '@rollup/plugin-terser'
import virtual from '@rollup/plugin-virtual'
import { createFilter } from '@rollup/pluginutils'
import { transform } from '@svgr/core'
import { defineConfig } from 'rollup'
import { visualizer } from 'rollup-plugin-visualizer'
import { optimize } from 'svgo'
const extensions = [...DEFAULT_EXTENSIONS, '.ts', '.tsx', '.svg']
const config = {
input: {
icons: 'icons',
utils: 'utils',
},
output: {
icons: 'svg',
react: 'react',
utils: 'utils',
},
}
const iconBasePath = new URL(`./${config.input.icons}`, import.meta.url)
.pathname
const utilBasePath = new URL(`./${config.input.utils}`, import.meta.url)
.pathname
const iconFileNames = fs.readdirSync(config.input.icons, 'utf-8')
const iconNames = []
const iconImportLines = []
const iconExportLines = []
const iconObjectLines = []
const iconComponentTypes = []
function toPascalCase(str) {
return str
.replace(/[-_](.)/g, (_, c) => c.toUpperCase())
.replace(/^(.)/, (_, c) => c.toUpperCase())
}
iconFileNames.forEach((iconFileName) => {
const iconName = iconFileName.replace('.svg', '')
const iconModuleName = `${toPascalCase(iconName)}Icon`
iconNames.push(`'${iconName}'`)
iconImportLines.push(
`import ${iconModuleName} from '../${config.input.icons}/${iconFileName}'`
)
iconExportLines.push(`${iconModuleName},`)
iconObjectLines.push(`'${iconName}': ${iconModuleName},`)
iconComponentTypes.push(`export declare const ${iconModuleName}: BezierIcon`)
})
const ICONS_OBJECT = 'icons'
const entryModuleContent = `
${iconImportLines.join('\n')}
const ${ICONS_OBJECT} = { ${iconObjectLines.join('\n')} }
export { ${iconExportLines.concat(`${ICONS_OBJECT},`).join('\n')} }
export * from '${config.input.utils}'
`
const entryTypesContent = `
declare module '*.svg' {
const content: string
export default content
}
/**
* Since IconSource created by svgr is forwarded ref,
* Its correct type is React.ForwardRefExoticComponent<React.SVGProps<SVGSVGElement> & React.RefAttributes<SVGSVGElement>>,
* however, we keep React.FunctionComponent type for backward compatibility.
*/
export declare type IconSource = React.FunctionComponent<React.SVGProps<SVGSVGElement>>
export declare type BezierIcon = IconSource & { __bezier__icon: true }
export declare function isBezierIcon(arg: unknown): arg is BezierIcon
export declare function createBezierIcon(icon: IconSource): BezierIcon
export declare type IconName = ${iconNames.join(' | ')}
/**
* @deprecated If you import this module, all icons are bundled, so please import and use the individual icons.
* @example
* import { AllIcon } from '@channel.io/bezier-icons'
*/
export declare const icons: Record<IconName, BezierIcon>
${iconComponentTypes.join('\n')}
`.trim()
/**
* @type {import('rollup').PluginImpl}
*/
function emitFile({ fileName, source }) {
return {
name: 'emitFile',
buildEnd() {
if (source.length === 0) {
this.warn('source content is empty')
}
this.emitFile({
type: 'asset',
fileName,
source,
})
},
}
}
/**
* @type {import('rollup').PluginImpl}
*/
function svgBuild(options = {}) {
const filter = createFilter(options.include || '**/*.svg', options.exclude)
/**
* @type {import('svgo').Config}
*/
const svgoConfig = {
plugins: [
{
name: 'preset-default',
params: {
overrides: {
removeViewBox: false,
},
},
},
/**
* Set the `fill` attribute to `currentColor` for all `path` elements.
* This allows us to dynamically change the color of the SVG icon.
* @see https://gomakethings.com/currentcolor-and-svgs/#dynamic-svg-colors
*/
{
name: 'convertColors',
params: {
currentColor: true,
},
},
],
}
/**
* @type {import('@svgr/babel-plugin-transform-svg-component').Template}
*/
function reactIconTemplate({ imports, componentName, props, jsx }, { tpl }) {
return tpl`
${imports}
import { createBezierIcon } from '${config.input.utils}'
function ${componentName}(${props}) {
return (
${jsx}
)
}
export default createBezierIcon(forwardRef(${componentName}))
`
}
const optimizedSvgs = []
return {
name: 'svgBuild',
async transform(code, id) {
if (!filter(id) || !id.endsWith('.svg')) {
return null
}
const rawSvg = fs.readFileSync(id, 'utf8')
const { data: optimizedSvgCode } = optimize(rawSvg, {
...svgoConfig,
path: id,
})
optimizedSvgs.push({
name: path.basename(id),
svg: optimizedSvgCode,
})
const jsCode = await transform(
optimizedSvgCode,
{
/**
* If plugin-jsx is not present, the transformation will not work properly.
*/
plugins: ['@svgr/plugin-jsx'],
icon: true,
jsxRuntime: 'automatic',
ref: true,
template: reactIconTemplate,
},
{
filePath: id,
caller: { name: 'svgBuild' },
}
)
return {
code: jsCode,
ast: {
type: 'Program',
sourceType: 'module',
start: 0,
end: null,
body: [],
},
map: { mappings: '' },
}
},
buildEnd() {
/**
* Create optimized SVG asset files in that path.
*/
optimizedSvgs.forEach(({ name, svg }) => {
this.emitFile({
type: 'asset',
fileName: `${config.output.icons}/${name}`,
source: svg,
})
})
},
}
}
/**
* Split into individual chunks for smooth tree shaking.
*/
function manualChunks(id) {
if (id.startsWith(iconBasePath)) {
return id
.replace(iconBasePath, `${config.output.react}/`)
.replace('.svg', '')
}
if (id.startsWith(utilBasePath)) {
return id
.replace(utilBasePath, `${config.output.utils}/`)
.replace('.ts', '')
}
return undefined
}
export default defineConfig({
/**
* Instead of the actual src/index.ts, use a virtual entry point via virtual plugin.
*/
input: 'src/index.ts',
output: [
{
dir: 'dist',
format: 'cjs',
entryFileNames: '[name].js',
chunkFileNames: '[name].js',
/**
* "auto" combines both "esModule" and "default" by injecting helpers that contain code that detects at runtime
* if the required value contains the __esModule property.
* Adding this property is a hack implemented by TypeScript esModuleInterop,
* Babel and other tools to signify that the required value is the namespace of a transpiled ES module.:
*
* @see: https://rollupjs.org/configuration-options/#output-interop
*/
interop: 'auto',
manualChunks,
},
{
dir: 'dist',
format: 'esm',
entryFileNames: '[name].mjs',
chunkFileNames: '[name].mjs',
manualChunks,
},
],
external: ['react', 'react/jsx-runtime'],
plugins: [
virtual({ 'src/index.ts': entryModuleContent }),
nodeResolve({ extensions }),
svgBuild({ include: `${iconBasePath}/*.svg` }),
/**
* Module resolution is not working well inside the virtual module, so use the alias plugin to resolve the module manually.
*/
alias({
entries: [
{
find: config.input.utils,
replacement: utilBasePath,
},
],
}),
babel({
exclude: 'node_modules/**',
extensions,
envName: 'production',
babelHelpers: 'bundled',
}),
emitFile({
fileName: 'index.d.ts',
source: entryTypesContent,
}),
terser(),
visualizer(),
],
})