-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathesbuild.config.js
More file actions
109 lines (98 loc) · 2.96 KB
/
Copy pathesbuild.config.js
File metadata and controls
109 lines (98 loc) · 2.96 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
import esbuild from "esbuild";
import builtins from "builtin-modules";
import { sassPlugin } from "esbuild-sass-plugin";
import fs from "fs";
import path from "path";
import { createRequire } from "module";
const require = createRequire(import.meta.url);
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";
// This patches Dexie during build-time, removing the global registration/guard-check
// Done to avoid conflict with other plugins using Dexie
const dexieIsolatePlugin = {
name: "dexie-isolate",
setup(build) {
build.onLoad(
{ filter: /dexie[\\/]import-wrapper(-prod)?\.mjs$/, namespace: "file" },
(args) => {
let contents = fs.readFileSync(args.path, "utf8");
contents = contents
.replace(/const DexieSymbol = Symbol\.for\("Dexie"\);\n/, "")
.replace(
/const Dexie = globalThis\[DexieSymbol\] \|\| \(globalThis\[DexieSymbol\] = _Dexie\);\n/,
"const Dexie = _Dexie;\n",
)
.replace(
/if \(_Dexie\.semVer !== Dexie\.semVer\) \{\n\s+throw new Error\(`Two different versions of Dexie loaded in the same app: \$\{_Dexie\.semVer\} and \$\{Dexie\.semVer\}`\);\n\s+\}\n/,
"",
);
if (
contents.includes("DexieSymbol") ||
contents.includes("Symbol.for")
) {
throw new Error(
`[dexie-isolate] Failed to patch ${args.path} — Symbol.for still present. ` +
`Check that the source text matches the replace patterns.`,
);
}
console.log(`[dexie-isolate] Patched: ${path.basename(args.path)}`);
return { contents, loader: "js" };
},
);
},
};
const context = await esbuild.context({
metafile: true,
banner: {
js: banner,
},
entryPoints: ["src/main.ts", "src/ui/styles/styles.scss"],
entryNames: "[name]",
outdir: ".",
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,
],
format: "cjs",
target: "es2018",
logLevel: "info",
sourcemap: prod ? false : "inline",
treeShaking: true,
define: {
"process.env.NODE_ENV": prod ? '"production"' : '"development"',
},
minify: prod,
minifyWhitespace: prod,
minifyIdentifiers: prod,
minifySyntax: prod,
plugins: [
dexieIsolatePlugin,
sassPlugin({
type: "css",
outputStyle: "compressed",
}),
],
});
if (prod) {
const result = await context.rebuild();
fs.writeFileSync("meta.json", JSON.stringify(result.metafile, null, 2));
process.exit(0);
} else {
await context.watch(); // Watch for file changes in dev mode
}