-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrollup.config.mjs
More file actions
108 lines (94 loc) · 2.52 KB
/
rollup.config.mjs
File metadata and controls
108 lines (94 loc) · 2.52 KB
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
import path from 'path';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import alias from '@rollup/plugin-alias';
import typescript from 'rollup-plugin-typescript2';
import dts from 'rollup-plugin-dts';
import del from 'rollup-plugin-delete';
const modules = [
'Animatry',
'use-animatry'
];
const barrel = 'index';
const allEntries = [...modules, barrel];
const kebabToCamel = str =>
str.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
const getInputPath = name =>
path.join('src', name === barrel ? '' : name, 'index.ts');
const isExternal = id =>
id === 'react' ||
id === 'react-dom' ||
id.startsWith('animatry') ||
id.startsWith('animatry/core') ||
allEntries.some(name => id === `@${name}` || id.startsWith(`@${name}/`));
const rewriteFlatImports = {
name: 'rewrite-flat-imports',
generateBundle(_opts, bundle) {
const pattern = new RegExp(`@(${modules.join('|')})`, 'g');
for (const file of Object.values(bundle)) {
if (file.type !== 'chunk') continue;
file.code = file.code
.replace(pattern, (_, mod) => `./${kebabToCamel(mod)}.js`)
.replace(/\.\/([a-z0-9-]+)\/\1\.js/g, './$1.js')
.replace(/\.\/([a-z0-9-]+)\/index\.js/g, './$1.js');
}
}
};
const aliasEntries = modules.map(name => ({
find: `@${name}`,
replacement: path.resolve(`./src/${name}`),
}));
const noDecl = {
tsconfigOverride: {
compilerOptions: {
declaration: false,
declarationMap: false,
}
},
clean: true
};
// ESM Build
const esmPlugins = [
resolve(),
alias({ entries: aliasEntries }),
commonjs({ requireReturnsDefault: 'auto' }),
typescript({
tsconfig: './tsconfig.json',
...noDecl
}),
rewriteFlatImports
];
const createEsmConfig = name => ({
input: getInputPath(name),
output: {
file: `dist/esm/${kebabToCamel(name)}.js`,
format: 'esm',
},
external: isExternal,
plugins: esmPlugins,
});
const esmConfigs = allEntries.map(createEsmConfig);
// Type declarations
const createDtsConfig = name => ({
input: getInputPath(name),
output: {
file: `dist/types/${kebabToCamel(name)}.d.ts`,
format: 'es',
},
plugins: [
alias({ entries: aliasEntries }),
dts(),
name === barrel && del({
targets: ['dist/esm/*/', 'dist/umd/*'],
runOnce: true
}),
name === barrel && del({
targets: ['dist/esm/index.d.ts', 'dist/umd/index.d.ts']
})
].filter(Boolean)
});
const dtsConfigs = allEntries.map(createDtsConfig);
export default [
...esmConfigs,
...dtsConfigs
];