-
Notifications
You must be signed in to change notification settings - Fork 22
/
rollup.config.js
68 lines (60 loc) · 1.71 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
import babel from "rollup-plugin-babel";
import nodeResolve from "rollup-plugin-node-resolve";
import replace from "rollup-plugin-replace";
import pkg from "./package.json";
const extensions = [".tsx", ".ts", ".js"];
const external = [
...Object.keys(pkg.peerDependencies || {}),
...Object.keys(pkg.dependencies || {})
];
const loose = true;
const createBabelConfig = isESM => {
return {
presets: [
["@babel/preset-typescript", { loose, modules: false }],
["@babel/preset-env", { loose, modules: false }]
],
plugins: [
"babel-plugin-macros",
["@babel/plugin-transform-runtime", { useESModules: isESM }],
"@babel/plugin-transform-react-jsx"
]
};
};
const createExternalPredicate = externalArr => {
if (externalArr.length === 0) {
return () => false;
}
const pattern = new RegExp(`^(${externalArr.join("|")})($|/)`);
return id => pattern.test(id);
};
const createConfig = ({ output, browser = false, isESM = false }) => ({
input: "src/index.tsx",
output: output.map(format => ({ exports: "named", ...format })),
external: createExternalPredicate(external),
plugins: [
nodeResolve({ extensions }),
babel({ extensions, runtimeHelpers: true, ...createBabelConfig(isESM) }),
replace({
"process.env.BROWSER": JSON.stringify(browser)
})
]
});
export default [
createConfig({
output: [{ file: pkg.main, format: "cjs" }]
}),
createConfig({
output: [{ file: pkg.module, format: "esm" }],
isESM: true
}),
createConfig({
output: [{ file: pkg.browser[pkg.main], format: "cjs" }],
browser: true
}),
createConfig({
output: [{ file: pkg.browser[pkg.module], format: "esm" }],
browser: true,
isESM: true
})
];