generated from RebeccaStevens/template-typescript-node-package
/
rollup.config.ts
107 lines (97 loc) · 2.63 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
import rollupPluginReplace from "@rollup/plugin-replace";
import rollupPluginTypescript from "@rollup/plugin-typescript";
import {
type PreRenderedChunk,
type RollupOptions,
type ModuleFormat,
} from "rollup";
import rollupPluginAutoExternal from "rollup-plugin-auto-external";
import rollupPluginDts from "rollup-plugin-dts";
/**
* Get the intended boolean value from the given string.
*/
function getBoolean(value: unknown) {
if (value === undefined) {
return false;
}
const asNumber = Number(value);
return Number.isNaN(asNumber)
? String(value).toLowerCase() === "false"
? false
: Boolean(String(value))
: Boolean(asNumber);
}
const buildTypesOnly = getBoolean(process.env["BUILD_TYPES_ONLY"]);
const formats = ["esm", "cjs"] as const satisfies ReadonlyArray<ModuleFormat>;
const common = {
output: {
sourcemap: false,
dir: "dist",
},
treeshake: {
annotations: true,
moduleSideEffects: [],
propertyReadSideEffects: false,
unknownGlobalSideEffects: false,
},
} satisfies RollupOptions;
const runtimes = {
...common,
input: {
functions: "./src/functions/index.ts",
"functions-higher-order": "./src/functions-ho/index.ts",
"units-converters": "./src/units/converters/index.ts",
},
output: formats.map((format) => {
return {
...common.output,
format,
entryFileNames: runtimeFileNamer,
chunkFileNames: runtimeFileNamer,
};
function runtimeFileNamer(info: Readonly<PreRenderedChunk>) {
return `${info.isEntry ? "" : "_"}${info.name}.${
format === "esm" ? "mjs" : "cjs"
}`;
}
}),
plugins: [
rollupPluginAutoExternal(),
rollupPluginTypescript({
tsconfig: "tsconfig.build.json",
}),
rollupPluginReplace({
"import.meta.vitest": "undefined",
}),
],
} satisfies RollupOptions;
const types = {
...common,
input: {
index: "./src/index.ts",
functions: "./src/functions/index.ts",
"functions-higher-order": "./src/functions-ho/index.ts",
units: "./src/units/index.ts",
"units-converters": "./src/units/converters/index.ts",
},
output: formats.map((format) => {
return {
...common.output,
format,
entryFileNames: typesFileNamer,
chunkFileNames: typesFileNamer,
};
function typesFileNamer(info: Readonly<PreRenderedChunk>) {
return `${info.isEntry ? "" : "_"}${info.name}.d.${
format === "esm" ? "mts" : "cts"
}`;
}
}),
plugins: [
rollupPluginDts({
tsconfig: "tsconfig.build.json",
}),
],
} satisfies RollupOptions;
const configs = buildTypesOnly ? [types] : [runtimes, types];
export default configs;