-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesbuild.config.mjs
113 lines (104 loc) · 2.97 KB
/
esbuild.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import esbuild from "esbuild";
import process from "process";
import builtins from "builtin-modules";
import { promises as fs } from 'fs';
import chokidar from "chokidar"; // Add chokidar for watching files
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");
// Define a custom plugin to handle 'node:' prefixed imports
/*
const nodePrefixedImports = {
name: 'node-prefixed-imports',
setup(build) {
// Intercept import paths starting with 'node:'
build.onResolve({ filter: /^node:(.+)$/ }, args => {
let actual = args.path.slice(5); // Remove 'node:' prefix
// Remove trailing slash if present
//actual = actual.replace(/\/$/, '');
return { path: actual, external: true }; // Treat as external
});
},
};
*/
async function fixProcessSlash(filePath) {
try {
let contents = await fs.readFile(filePath, "utf8");
const occurrences = (contents.match(/require\(["']process\/["']\)/g) || []).length;
if (occurrences > 0) {
console.log(`Found ${occurrences} occurrences of \`require("process/")\`. Fixing...`);
contents = contents.replace(/require\(["']process\/["']\)/g, 'require("process")');
await fs.writeFile(filePath, contents, "utf8");
console.log("Fix applied successfully.");
}
} catch (err) {
console.error(`Error applying fix: ${err.message}`);
}
}
const context = await esbuild.context({
banner: {
js: banner,
},
entryPoints: ["main.ts"],
bundle: true,
external: [
"obsidian",
"electron",
"@codemirror/autocomplete",
"@codemirror/collab",
"@codemirror/commands",
"@codemirror/language",
"@codemirror/lint",
"@codemirror/search",
"@codemirror/state",
"@codemirror/view",
"@lezer/common",
"@lezer/highlight",
"@lezer/lr",
...builtins, // Includes all standard Node.js built-in modules
"node:fs", // Explicitly handle node: prefixed imports
"node:fs/promises",
"node:path",
"node:url",
"node:crypto",
"node:process",
"node:events",
"node:stream",
"keytar",
],
format: "cjs",
//target: "es2018",
target: "es2020", // Updated target to ES2020
logLevel: "info",
sourcemap: prod ? false : "inline",
treeShaking: true,
outfile: "main.js",
minify: prod,
platform: 'node', // Important for Node.js environment
//plugins: [nodePrefixedImports], // Add the custom plugin here
});
/*if (prod) {
await context.rebuild();
process.exit(0);
} else {
await context.watch();
}*/
if (prod) {
// Production mode: build once and apply the fix
await context.rebuild();
await fixProcessSlash("./main.js");
process.exit(0);
} else {
// Development mode: watch for changes
await context.watch();
console.log("Watching for changes...");
// Watch for changes to main.js and apply the fix dynamically
chokidar.watch("./main.js").on("change", async () => {
console.log("Detected change in main.js. Applying fix...");
await fixProcessSlash("./main.js");
});
}