generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathesbuild.config.mjs
More file actions
179 lines (165 loc) · 4.45 KB
/
esbuild.config.mjs
File metadata and controls
179 lines (165 loc) · 4.45 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import esbuild from "esbuild";
import process from "process";
import builtins from "builtin-modules";
import fs from "fs";
import path from "path";
import dotenv from "dotenv";
import inlineWorkerPlugin from "esbuild-plugin-inline-worker";
import { sassPlugin } from "esbuild-sass-plugin";
// Load environment variables from .env file
dotenv.config();
// Respect release-it dry-run: skip writing build artifacts entirely
const __D_RY__ =
process.env.RELEASE_IT_DRY_RUN === "1" || process.env.RELEASE_IT === "1";
if (__D_RY__) {
console.log("[dry-run] skip esbuild production build");
process.exit(0);
}
const banner = `/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin
*/
`;
const prod = process.argv[2] === "production";
const outDir = prod ? "dist" : ".";
// Ensure dist directory exists in production mode
if (prod && !fs.existsSync("dist")) {
fs.mkdirSync("dist", { recursive: true });
}
// Update plugins to handle output directory
const renamePluginWithDir = {
name: "rename-styles",
setup(build) {
build.onEnd(() => {
const { outfile } = build.initialOptions;
const outcss = outfile.replace(/\.js$/, ".css");
const fixcss = outfile.replace(/main\.js$/, "styles.css");
if (fs.existsSync(outcss)) {
console.log("Renaming", outcss, "to", fixcss);
fs.renameSync(outcss, fixcss);
}
});
},
};
// Update CSS settings plugin to handle output directory
const cssSettingsPluginWithDir = {
name: "css-settings-plugin",
setup(build) {
build.onEnd(async (result) => {
// Path to the output CSS file
const cssOutfile = path.join(outDir, "styles.css");
// The settings comment to prepend (read from SCSS file now)
const indexFile = fs.existsSync("src/styles/index.scss")
? "src/styles/index.scss"
: "src/styles/index.css";
const settingsComment =
fs.readFileSync(indexFile, "utf8").split("*/")[0] + "*/\n\n";
if (fs.existsSync(cssOutfile)) {
// Read the current content
const cssContent = fs.readFileSync(cssOutfile, "utf8");
// Check if the settings comment is already there
if (!cssContent.includes("/* @settings")) {
// Prepend the settings comment
fs.writeFileSync(cssOutfile, settingsComment + cssContent);
}
}
});
},
};
// Copy manifest to output directory in production
const copyManifestPlugin = {
name: "copy-manifest",
setup(build) {
build.onEnd(() => {
if (prod) {
fs.copyFileSync(
"manifest.json",
path.join(outDir, "manifest.json"),
);
console.log("Copied manifest.json to", outDir);
}
});
},
};
const buildOptions = {
banner: {
js: banner,
},
define: {
// Inject base64-encoded secrets at build time (decoded at runtime)
"process.env.GOOGLE_CLIENT_SECRET_B64": JSON.stringify(
process.env.GOOGLE_CLIENT_SECRET_B64 || "",
),
},
minify: prod ? true : false,
entryPoints: ["src/index.ts"],
plugins: [
inlineWorkerPlugin({ workerName: "Task Genius Indexer" }),
sassPlugin({
// Enable modern SASS API
type: "css",
// Load paths for @use and @import
loadPaths: [path.resolve(process.cwd(), "src/styles")],
}),
renamePluginWithDir,
cssSettingsPluginWithDir,
copyManifestPlugin,
],
bundle: true,
alias: {
"@": path.resolve(process.cwd(), "src"),
},
external: [
"obsidian",
"electron",
"codemirror",
"@codemirror/autocomplete",
"@codemirror/closebrackets",
"@codemirror/collab",
"@codemirror/commands",
"@codemirror/comment",
"@codemirror/fold",
"@codemirror/gutter",
"@codemirror/highlight",
"@codemirror/history",
"@codemirror/language",
"@codemirror/lint",
"@codemirror/matchbrackets",
"@codemirror/panel",
"@codemirror/rangeset",
"@codemirror/rectangular-selection",
"@codemirror/search",
"@codemirror/state",
"@codemirror/stream-parser",
"@codemirror/text",
"@codemirror/tooltip",
"@codemirror/view",
"@lezer/common",
"@lezer/lr",
"@lezer/highlight",
"obsidian-typings",
...builtins,
],
format: "cjs",
target: "es2018",
logLevel: "info",
sourcemap: prod ? false : "inline",
treeShaking: true,
outfile: path.join(outDir, "main.js"),
pure: prod
? ["console.log", "console.time", "console.timeEnd", "console.info"]
: [],
};
if (prod) {
// Production build
esbuild.build(buildOptions).catch(() => process.exit(1));
} else {
// Development build with watch
esbuild
.context(buildOptions)
.then((ctx) => {
ctx.watch();
console.log("Watching for changes...");
})
.catch(() => process.exit(1));
}