-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.js
85 lines (76 loc) · 1.78 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
// @ts-check
import path from "node:path";
import process from "node:process";
import fs from "node:fs";
import { defineConfig } from "rollup";
import terser from "@rollup/plugin-terser";
import nodeResolve from "@rollup/plugin-node-resolve";
import replace from "@rollup/plugin-replace";
import esbuild from "rollup-plugin-esbuild";
const extensions = [".js", ".ts"];
const { root } = path.parse(process.cwd());
/**
* @param {string} id
* @returns {boolean}
*/
function external(id) {
return !id.startsWith(".") && !id.startsWith(root);
}
/**
* @param {string} input
* @param {string} output
* @returns {import("rollup").RollupOptions}
*/
function createESMConfig(input, output) {
return {
external,
input,
output: {
file: output,
format: "esm",
sourcemap: true,
},
plugins: [
esbuild({
target: "es2022",
}),
nodeResolve({ extensions }),
replace({
"import.meta.vitest": "undefined",
"preventAssignment": false,
}),
terser({
compress: {
ecma: 2020,
passes: 5,
},
ecma: 2020,
format: {
comments: false,
},
nameCache: {},
toplevel: true,
}),
],
};
}
const srcPath = path.join(process.cwd(), "src");
/**
* @returns {string[]}
*/
function files() {
try {
const items = fs.readdirSync(srcPath);
const fileNames = items
.filter(item => fs.statSync(path.join(srcPath, item)).isFile())
.map(item => path.parse(item).name);
return fileNames;
}
catch (err) {
console.error("Error reading directory:", err);
return [];
}
}
export default defineConfig(files().filter(file => file !== "types").flatMap(file => [
createESMConfig(`src/${file}.ts`, `dist/${file}.js`),
]));