-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.js
107 lines (101 loc) · 2.79 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
import commonjs from '@rollup/plugin-commonjs';
import peerDepsExternal from 'rollup-plugin-peer-deps-external';
import postcss from 'rollup-plugin-postcss';
import resolve from '@rollup/plugin-node-resolve';
import typescript from 'rollup-plugin-typescript2';
import { typescriptPaths } from 'rollup-plugin-typescript-paths';
const createStyledComponentsTransformer = require('typescript-plugin-styled-components')
.default;
const styledComponentsTransformer = createStyledComponentsTransformer();
const lazyloadedComponents = ['Button', 'Anchor'];
const manualChunks = (id, { getModuleInfo }) => {
const regExp = new RegExp(
`/components/.*/(${lazyloadedComponents.join('|')})`
);
const match = regExp.exec(id);
if (match) {
const component = match[1]; // e.g. "Button"
const dependentEntryPoints = [];
// we use a Set here so we handle each module at most once. This
// prevents infinite loops in case of circular dependencies
const idsToHandle = new Set(getModuleInfo(id).dynamicImporters);
for (const moduleId of idsToHandle) {
const { isEntry, dynamicImporters, importers } = getModuleInfo(moduleId);
if (isEntry || dynamicImporters.length > 0) {
dependentEntryPoints.push(moduleId);
}
// The Set iterator is intelligent enough to iterate over elements that
// are added during iteration
for (const importerId of importers) {
idsToHandle.add(importerId);
}
}
// If there is a unique entry, we put it into into a chunk based on the entry name
if (dependentEntryPoints.length === 1) {
return `${
dependentEntryPoints[0].split('/').slice(-1)[0].split('.')[0]
}.${component}`;
}
// For multiple entries, we put it into a "shared" chunk
if (dependentEntryPoints.length > 1) {
return `shared.${component}`;
}
}
};
const plugins = [
peerDepsExternal(),
resolve(),
commonjs(),
typescript({
useTsconfigDeclarationDir: true,
transformers: [
() => ({
before: [styledComponentsTransformer],
}),
],
}),
typescriptPaths({
absolute: true,
}),
postcss(),
];
const commonConfig = {
input: 'src/index.ts',
output: [
{
dir: 'build',
format: 'esm',
sourcemap: true,
entryFileNames: '[name].esm.js',
chunkFileNames: '[name].esm.js',
},
],
manualChunks,
plugins,
};
export default [
{
...commonConfig,
output: [
{
dir: 'build',
format: 'esm',
sourcemap: true,
entryFileNames: '[name].esm.js',
chunkFileNames: '[name].esm.js',
},
],
},
{
...commonConfig,
output: [
{
dir: 'build',
format: 'cjs',
sourcemap: true,
entryFileNames: '[name].js',
chunkFileNames: '[name].js',
},
],
},
];