-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.ts
165 lines (133 loc) · 4.29 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import path from "node:path";
import commonjs from "@rollup/plugin-commonjs";
import node from "@rollup/plugin-node-resolve";
import replace from "@rollup/plugin-replace";
import { isArray, isPlainObject } from "remeda";
import { defineConfig } from "rollup";
import esbuild from "rollup-plugin-esbuild";
import { readFile, readJsonSync } from "./src/utils/fs";
import type PackageJson from "./package.json";
import type { Plugin, RollupOptions } from "rollup";
// eslint-disable-next-line import/order
import "dotenv/config";
const isWatchMode = process.env.ROLLUP_WATCH === "true";
const isHotreloadEnabled = isWatchMode && Boolean(process.env.OBSIDIAN_PLUGINS_DIR);
const START_MARK = "/* start */`\n";
const END_MARK = "\n`/* end */";
const wrap = (code: string) => `export default ${START_MARK}${code}${END_MARK};`;
const strip = (code: string) => {
const start = code.indexOf(START_MARK) + START_MARK.length;
const end = code.indexOf(END_MARK);
return code.slice(start, end);
};
const genManifest = () => {
const packageJson = readJsonSync<typeof PackageJson>("./package.json");
const { version, description, author, obsidian } = packageJson;
const { id, name, isDesktopOnly, minAppVersion } = obsidian;
const manifest: Manifest = {
id: isHotreloadEnabled ? `${id}-dev` : id,
name: isHotreloadEnabled ? `${name} (Dev)` : name,
version,
author: author.name,
authorUrl: author.url,
description,
isDesktopOnly,
minAppVersion,
};
return manifest;
};
const pluginHotreload = (file: string): Plugin => ({
name: "plugin:hotreload",
async options(options: RollupOptions) {
if (!isHotreloadEnabled) return null;
const { id } = genManifest();
if (isPlainObject(options.output)) {
options.output = [options.output];
} else if (!isArray(options.output)) {
options.output = [];
}
options.output.push({
...(options.output[0] || {}),
file: path.resolve(process.env.OBSIDIAN_PLUGINS_DIR!, id, file),
});
return options;
},
});
export default defineConfig([
{
input: "src/main.ts",
output: [{ file: "dist/main.js", format: "cjs" }],
external: ["obsidian", "electron"],
plugins: [
pluginHotreload("main.js"),
replace({
"process.env.MANIFEST": JSON.stringify(genManifest()),
preventAssignment: true,
}),
esbuild(),
node(),
commonjs(),
],
},
{
input: "src/styles.css",
output: [{ file: "dist/styles.css" }],
plugins: [
pluginHotreload("styles.css"),
{
name: "plugin:styles",
async load(id) {
const styles = await readFile(id);
return wrap(styles);
},
renderStart() {
this.addWatchFile("src/styles.css");
},
renderChunk(code) {
return strip(code);
},
},
],
},
{
input: "package.json",
output: [{ file: "dist/manifest.json" }].concat(
isWatchMode ? [] : { file: "manifest.json" },
),
plugins: [
pluginHotreload("manifest.json"),
{
name: "plugin:manifest",
load() {
const manifest = genManifest();
return wrap(JSON.stringify(manifest, null, 4));
},
renderStart() {
this.addWatchFile("package.json");
},
renderChunk(code) {
return strip(code);
},
},
],
},
{
input: ".hotreload",
output: [{ file: "/dev/null" }],
plugins: [
pluginHotreload(".hotreload"),
{
name: "plugin:dot-hotreload",
resolveId(id) {
return id;
},
load() {
return wrap("");
},
async renderChunk() {
return "";
},
},
],
},
]);