-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.ts
180 lines (163 loc) · 3.82 KB
/
rollup.config.ts
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
import typescript from "@rollup/plugin-typescript";
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import dts from "rollup-plugin-dts";
import aliasPlugin from "@rollup/plugin-alias";
import json from "@rollup/plugin-json"
import terser from "@rollup/plugin-terser";
import { defineConfig, RollupOptions } from "rollup";
import path from "path";
import { fileURLToPath } from "url";
import pkg from "./package.json";
import maxmin from "maxmin";
import chalk from "chalk";
const pkgName = pkg.name;
const libName = pkgName.split("/").pop();
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const namedInput = "src/index.ts";
const external = Object.keys(pkg.peerDependencies || {});
function bundleSize() {
return {
name: "rollup-plugin-bundle-size",
generateBundle(options, bundle) {
const asset = path.basename(options.file);
const size = maxmin(bundle[asset].code, bundle[asset].code, true);
console.log(`Created bundle ${chalk.cyan(asset)}: ${size.substr(size.indexOf(' → ') + 3)}`);
}
};
}
function buildConfig({ browser = true, minifiedVersion = true, isBundle = true, alias = [], ...config }): RollupOptions[] {
const { file } = config.output;
const ext = path.extname(file);
const basename = path.basename(file, ext);
const extArr = ext.split('.');
extArr.shift();
function getPlugins(minified: boolean) {
const plugins = [
aliasPlugin({
entries: [{ find: "@", replacement: path.resolve(__dirname, "./src") }],
}),
json(),
resolve({ browser }),
]
if (isBundle) {
plugins.push(commonjs());
}
if (minified) {
plugins.push(terser());
plugins.push(bundleSize());
}
plugins.push(...(config.plugins || []));
return plugins;
}
const build = ({ minified }) => ({
input: namedInput,
...config,
output: [{
...config.output,
file: `${path.dirname(file)}/${basename}.${(minified ? ['min', ...extArr] : extArr).join('.')}`
}],
external,
plugins: getPlugins(minified)
});
const configs = [build({ minified: false })];
if (minifiedVersion) {
configs.push(build({ minified: true }));
}
return configs;
}
export default defineConfig(() => {
const year = new Date().getFullYear();
const banner = `// ${libName} v${pkg.version} Copyright (c) ${year} ${pkg.author} and contributors`;
const config = [
//Node.js ESM Bundle
...buildConfig({
browser: false,
minifiedVersion: false,
output: {
file: "dist/node/index.js",
format: "esm",
sourcemap: true,
banner,
inlineDynamicImports: true,
},
plugins: [
typescript({
tsconfig: "./tsconfig.json",
sourceMap: true,
})
]
}),
//Node.js CJS Bundle
...buildConfig({
browser: false,
minifiedVersion: false,
output: {
file: "dist/node/index.cjs",
format: "cjs",
sourcemap: true,
banner,
inlineDynamicImports: true,
},
plugins: [
typescript({
tsconfig: "./tsconfig.json",
sourceMap: true,
})
]
}),
//Browser ESM Bundle
...buildConfig({
browser: true,
output:
{
file: "dist/browser/index.umd.js",
format: "umd",
name: libName,
sourcemap: true,
banner,
inlineDynamicImports: true,
},
plugins: [
typescript({
tsconfig: "./tsconfig.json",
sourceMap: true,
})
]
}),
//Browser CJS Bundle
...buildConfig({
browser: true,
output:
{
file: "dist/browser/index.cjs",
format: "cjs",
name: libName,
sourcemap: true,
banner,
inlineDynamicImports: true,
},
plugins: [
typescript({
tsconfig: "./tsconfig.json",
sourceMap: true,
})
]
}),
//declaration file
...buildConfig({
browser: false,
minifiedVersion: false,
output:
{
file: "dist/index.d.ts",
format: "esm",
sourcemap: true,
},
plugins: [
dts(),
]
})
]
return config
})