-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
126 lines (121 loc) · 3.24 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
import typescript from "@rollup/plugin-typescript";
import nodeResolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import path from "path";
import pkg from "./package.json";
import { terser } from "rollup-plugin-terser";
import camelCase from "lodash/camelCase";
const tsconfigPath = path.resolve(__dirname, "./tsconfig.json");
const ensureArray = maybeArr =>
Array.isArray(maybeArr) ? maybeArr : [maybeArr];
const extensions = [".js"];
const deps = Object.keys(pkg.dependencies || {});
const peerDeps = Object.keys(pkg.peerDependencies || {});
const umdExportName = (() => {
const pkgNameParts = pkg.name.split("/");
const pkgName = camelCase(
pkgNameParts.length > 1 ? pkgNameParts[1] : pkgNameParts[0]
);
return pkgName[0].toUpperCase() + pkgName.substring(1);
})();
const createConfig = ({
input,
output,
tsOptions = {},
external = "peers",
min = false,
...props
}) => {
return {
input: input ? input : "src/index.ts",
output: ensureArray(output).map(format =>
Object.assign({}, format, {
// UMD global export name
name: umdExportName,
exports: "named"
})
),
external: (() => {
if (external === "peers") return peerDeps;
else if (external === "all") return deps.concat(peerDeps);
else return [];
})(),
onwarn(warning, warn) {
if (warning.code === "CIRCULAR_DEPENDENCY") return;
warn(warning);
},
plugins: [
typescript({
tsconfig: tsconfigPath,
...tsOptions
}),
nodeResolve({
mainFields: ["main", "module", "jsnext:main"],
extensions
}),
commonjs({
include: "node_modules/**"
}),
min &&
terser({
compress: {
pure_getters: true,
unsafe: true,
unsafe_comps: true,
warnings: false
}
})
].filter(Boolean),
...props
};
};
export default [
// --- CommonJS
createConfig({
output: {
format: "cjs",
file: "dist/cjs/index.js"
},
external: "none"
}),
// --- ES Module
createConfig({
output: {
format: "esm",
file: "dist/esm/index.js"
},
tsOptions: {
target: "es6"
},
external: "none"
}),
// --- ES Module for Web Browser
createConfig({
output: {
format: "esm",
file: "dist/mjs/index.mjs"
},
tsOptions: {
target: "es6"
},
external: "none",
min: true
}),
// --- UMD Development
createConfig({
output: {
file: "dist/umd-dev/index.js",
format: "umd"
},
external: "none"
}),
// --- UMD Production
createConfig({
output: {
file: "dist/umd-prod/index.js",
format: "umd"
},
external: "none",
min: true
})
];