/
rollup.config.js
135 lines (111 loc) · 3.33 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
127
128
129
130
131
132
133
134
135
import { strict as assert } from "node:assert";
import fs from "node:fs/promises";
import path from "node:path";
import process from "node:process";
import typescript from "@rollup/plugin-typescript";
import MagicString from "magic-string";
import dts from "rollup-plugin-dts";
assert.match(process.versions.node, /^20/, "This library must be build using Node.js LTS version.");
const output = {
dir: "./build",
};
const packageConfigText = await fs.readFile(new URL("./package.json", import.meta.url), { encoding: "utf8" });
const { version } = /** @type {{ version: string }} */ (JSON.parse(packageConfigText));
/** @returns {import("rollup").Plugin} */
function tidyJs() {
const binEntry = "bin.js";
const tstycheEntry = "tstyche.js";
return {
name: "tidy-js",
renderChunk(code, chunkInfo) {
if (chunkInfo.fileName === tstycheEntry) {
const magicString = new MagicString(code);
magicString.replaceAll("__version__", version);
return {
code: magicString.toString(),
map: magicString.generateMap({ hires: true }),
};
}
return null;
},
async writeBundle() {
const cliEntryFilePath = path.resolve(output.dir, binEntry);
await fs.chmod(cliEntryFilePath, 0o755);
},
};
}
/** @returns {import("rollup").Plugin} */
function tidyDts() {
const tstycheEntry = "tstyche.d.ts";
return {
name: "tidy-dts",
renderChunk(code, chunkInfo) {
if (chunkInfo.fileName === tstycheEntry) {
const magicString = new MagicString(code);
magicString.replaceAll("import", "import type");
magicString.replaceAll("const enum", "enum");
magicString.replaceAll("__version__", version);
return {
code: magicString.toString(),
map: magicString.generateMap({ hires: true }),
};
}
return null;
},
};
}
/** @type {Array<import("rollup").RollupOptions>} */
const config = [
{
external: [/^node:/],
input: {
index: "./source/types.ts",
tstyche: "./source/tstyche.ts",
},
output,
plugins: [
// @ts-expect-error TODO: https://github.com/rollup/plugins/issues/1541
typescript({ tsconfig: "./source/tsconfig.json" }),
dts({ tsconfig: "./source/tsconfig.json" }),
tidyDts(),
],
},
{
input: "./source/types.ts",
output: {
file: "./build/index.d.cts",
format: "cjs",
},
plugins: [
// @ts-expect-error TODO: https://github.com/rollup/plugins/issues/1541
typescript({ tsconfig: "./source/tsconfig.json" }),
dts({ tsconfig: "./source/tsconfig.json" }),
],
},
{
external: [/^node:/, "./tstyche.js"],
input: {
bin: "./source/bin.ts",
index: "./source/main.ts",
tstyche: "./source/tstyche.ts",
},
output,
plugins: [
// @ts-expect-error TODO: https://github.com/rollup/plugins/issues/1541
typescript({ compilerOptions: { removeComments: true }, tsconfig: "./source/tsconfig.json" }),
tidyJs(),
],
},
{
input: "./source/main.ts",
output: {
file: "./build/index.cjs",
format: "cjs",
},
plugins: [
// @ts-expect-error TODO: https://github.com/rollup/plugins/issues/1541
typescript({ compilerOptions: { removeComments: true }, tsconfig: "./source/tsconfig.json" }),
],
},
];
export default config;