-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.ts
105 lines (98 loc) · 2.73 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
import json from "@rollup/plugin-json";
import replace from "@rollup/plugin-replace";
import typescript from "@rollup/plugin-typescript";
import { Plugin, RollupOptions } from "rollup";
import dts from "rollup-plugin-dts";
import { externals } from "rollup-plugin-node-externals";
import tsconfig from "./tsconfig.json" assert { type: "json" };
type apiOrCliType = "api" | "cli";
function createPlugins(dev: boolean, apiOrCli: apiOrCliType): Plugin[] {
return [
replace({
preventAssignment: true,
values: {
"process.env.IMG_PROJECT_ENV": JSON.stringify(
dev ? "development" : "production",
),
"process.env.IMG_PROJECT_RUN_TYPE": JSON.stringify(apiOrCli),
},
}),
json({ preferConst: true }),
externals(),
typescript({
compilerOptions: {
removeComments: !dev,
},
}),
];
}
interface CreateConfigOptions {
api?: boolean;
cli?: boolean;
dev: boolean;
format: "cjs" | "esm";
}
function createConfig(
options: CreateConfigOptions & { cli: true; api: true },
): undefined;
function createConfig(
options: CreateConfigOptions & { cli: true },
): RollupOptions;
function createConfig(
options: CreateConfigOptions & { api: true },
): RollupOptions;
function createConfig(options: CreateConfigOptions): undefined;
function createConfig({
api,
cli,
dev,
format,
}: CreateConfigOptions): RollupOptions | undefined {
if (api && cli) {
return undefined;
} else if (api) {
return {
input: "./src/api/entrypoint.ts",
treeshake: "smallest",
output: {
sourcemap: true,
format,
file: `./dist/api.${dev ? "dev" : "prod"}.${
format === "cjs" ? "js" : "mjs"
}`,
},
plugins: createPlugins(dev, "api"),
};
} else if (cli) {
return {
input: "./src/cli/entrypoint.ts",
treeshake: "smallest",
output: {
sourcemap: true,
format,
file: `./dist/cli.${dev ? "dev" : "prod"}.${
format === "cjs" ? "js" : "mjs"
}`,
},
plugins: createPlugins(dev, "cli"),
};
}
return undefined;
}
const config: RollupOptions[] = [
createConfig({ cli: true, dev: true, format: "cjs" }),
createConfig({ cli: true, dev: false, format: "cjs" }),
createConfig({ api: true, dev: true, format: "esm" }),
createConfig({ api: true, dev: false, format: "esm" }),
createConfig({ api: true, dev: true, format: "cjs" }),
createConfig({ api: true, dev: false, format: "cjs" }),
{
input: "./src/api/entrypoint.ts",
output: { file: "./dist/api.d.ts", format: "es" },
plugins: [
dts({ compilerOptions: { baseUrl: tsconfig.compilerOptions.baseUrl } }),
json({ preferConst: true }),
],
},
];
export default config;