-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.mjs
107 lines (101 loc) · 2.49 KB
/
rollup.config.mjs
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 json from "@rollup/plugin-json";
import nodeResolve from "@rollup/plugin-node-resolve";
import typescript from "@rollup/plugin-typescript";
/**
* @typedef {import("rollup").ModuleFormat} ModuleFormat
* @typedef {import("rollup").OutputOptions} OutputOptions
* @typedef {import("rollup").Plugin} Plugin
* @typedef {import("rollup").RollupOptions} RollupOptions
* @typedef {import("rollup").WarningHandlerWithDefault} WarningHandlerWithDefault
*/
/**
* @param {"commonjs" | "module"} type
* @returns {Plugin}
*/
function emitModulePackageJson(type) {
return {
name: "emit-module-package-json",
generateBundle() {
this.emitFile({
fileName: "package.json",
source: `{ "type": "${type}" }`,
type: "asset",
});
},
};
}
/**
* @type {WarningHandlerWithDefault}
*/
function onwarn(warning, rollupWarn) {
rollupWarn(warning);
if (warning.code === "CIRCULAR_DEPENDENCY") {
throw new Error("Please eliminate the circular dependencies listed above and retry the build");
}
}
/**
* @param {ModuleFormat} format
* @returns {"commonjs" | "module"}
*/
function packageType(format) {
switch (format) {
case "es":
case "esm":
return "module";
default:
return "commonjs";
}
}
/**
* @param {OutputOptions & { format: ModuleFormat }} output
* @param {Array<Plugin>} plugins
* @returns {RollupOptions}
*/
function buildConfig(output, plugins = []) {
return {
input: "src/index.ts",
output: {
externalLiveBindings: false,
generatedCode: {
arrowFunctions: true,
constBindings: true,
objectShorthand: true,
symbols: true,
},
sourcemap: true,
...output,
},
plugins: [
nodeResolve(),
json({ preferConst: true }),
commonjs({ sourceMap: true }),
typescript({ sourceMap: true, exclude: ["**/__tests__/**"] }),
emitModulePackageJson(packageType(output.format)),
...plugins,
],
strictDeprecations: true,
treeshake: {
moduleSideEffects: false,
propertyReadSideEffects: false,
tryCatchDeoptimization: false,
},
onwarn,
};
}
/**
* @param {Record<string, unknown>} _command
* @returns {Promise<RollupOptions | Array<RollupOptions>}
*/
export default async function config(_command) {
return [
buildConfig({
file: "dist/cjs/index.js",
format: "cjs",
}),
buildConfig({
file: "dist/esm/index.js",
format: "esm",
}),
];
}