generated from essentialib/template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.mjs
89 lines (78 loc) · 2.99 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
import * as fs from "node:fs";
import chalk from "chalk";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { createRequire } from "node:module";
import typescript from "@rollup/plugin-typescript";
import { getBabelOutputPlugin as babel } from "@rollup/plugin-babel";
import dts from "rollup-plugin-dts";
import replace from '@rollup/plugin-replace';
const __dirname = dirname(fileURLToPath(import.meta.url));
const packageJson = createRequire(import.meta.url)("./package.json");
const allInOne = process.env.ALLINONE === "true";
const directories = packageJson.directories;
directories.dist = allInOne ? directories.dist + "/all" : directories.dist + "/preserve";
function getEntryPoints(dir) {
const entrypoints = [];
fs.readdirSync(dir).forEach(file => {
const path = join(dir, file);
if (fs.statSync(path).isDirectory()) {
const indexFile = join(path, "index.ts");
if (fs.existsSync(indexFile))
entrypoints.push(indexFile);
else
throw new Error(`index.ts not found in ${dir}`);
entrypoints.push(...getEntryPoints(path));
}
});
return entrypoints;
}
export default () => {
if (fs.existsSync(directories.dist)) {
const start = Date.now();
fs.rmSync(directories.dist, { recursive: true, force: true });
const end = Date.now();
console.log(chalk.green(`cleared ${chalk.bold(directories.dist)} in ${chalk.bold(end - start + "ms")}`));
}
return [
{
input: [ join(directories.lib, "index.ts"), ...(allInOne ? [] : getEntryPoints(directories.lib)) ],
output: {
dir: directories.dist,
format: "cjs",
preserveModules: !allInOne,
preserveModulesRoot: !allInOne ? directories.lib : undefined,
sourcemap: false
},
plugins: [
typescript({
exclude: "**/*.test.ts",
compilerOptions: {
removeComments: true,
declaration: !allInOne,
declarationDir: !allInOne ? directories.dist : undefined,
target: "es2020",
sourceMap: false
}
}),
babel({
presets: [ "@babel/preset-env" ],
plugins: [
"@babel/plugin-syntax-bigint",
"@babel/plugin-transform-named-capturing-groups-regex",
],
}),
replace({
"const IS_DIST = false": "const IS_DIST = true",
})
]
}, ...(allInOne ? [ {
input: directories.lib + "/index.ts",
output: {
file: directories.dist + "/index.d.ts",
format: "es"
},
plugins: [ dts() ]
} ] : [])
];
};