generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathesbuild.config.mjs
153 lines (143 loc) · 4.81 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
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
import builtins from "builtin-modules";
import chokidar from "chokidar";
import esbuild from "esbuild";
import { postcssModules, sassPlugin } from "esbuild-sass-plugin";
import fs from "fs";
import process from "process";
import Tsm from "typed-scss-modules";
import { lezerPlugin } from "./esbuild-lezer-plugin.mjs";
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";
// generates and regenerates scss type declarations
export const scssTypesPlugin = (stylesPattern, watch) => {
const generateScssTypeDeclarations = async (pattern, options) => {
console.log("GENERATE SCSS TYPES", pattern, options, Tsm);
await Tsm.default(pattern, options);
};
let setupDone = false;
return {
name: "scss-types-plugin",
setup(build) {
console.log("setup", stylesPattern);
build.onStart(async () => {
console.log("onstart");
await generateScssTypeDeclarations(stylesPattern);
setupDone = true;
});
if (watch) {
console.log("setup watch");
chokidar.watch(stylesPattern).on(
"all", // was on(change)
async (event, filename) => {
console.log("onwatch", event, filename);
if (event == "add" && !setupDone) return;
if (!(event == "change" || event == "add")) return;
await generateScssTypeDeclarations(filename);
}
);
}
},
};
};
// renames `main.css` to `styles.css` in build dir
const renameCompiledCSS = {
name: "rename-compiled-css",
setup(build) {
build.onEnd(() => {
const { outfile } = build.initialOptions;
const outCssSrc = outfile.replace(/\.js$/, ".css");
const outCssDst = outfile.replace(/main\.js$/, "styles.css");
if (fs.existsSync(outCssSrc)) {
console.log(`RENAME COMPILED CSS: ${outCssSrc} -> ${outCssDst}`);
fs.renameSync(outCssSrc, outCssDst);
}
});
},
};
// copies `manifest.json` to build dir
const copyManifest = {
name: "copy-manifest",
setup(build) {
build.onEnd(() => {
const { outfile } = build.initialOptions;
const manifestSrc = "manifest.json";
const manifestDst = outfile.replace(/main\.js$/, "manifest.json");
if (fs.existsSync(manifestSrc)) {
console.log(`COPY MANIFEST: ${manifestSrc} -> ${manifestDst}`);
fs.copyFileSync(manifestSrc, manifestDst);
}
});
},
};
const context = await esbuild.context({
banner: {
js: banner,
},
entryPoints: ["src/main.tsx"],
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: "es6",
logLevel: "info",
// sourcemap: prod ? false : "inline",
sourcemap: true,
metafile: false,
// minifyWhitespace: true,
// ref: https://github.com/mgmeyers/obsidian-kanban/blob/05e43e09100f8c8efd7a4cd5ccb391b850e65f28/esbuild.config.js#L129C8-L129C8
inject: ["src/preact-globals.ts"],
treeShaking: true,
outfile: "build/main.js",
alias: {
react: "preact/compat",
"react-dom": "preact/compat",
},
plugins: [
lezerPlugin("./src/language/grammar/otl.grammar", !prod),
scssTypesPlugin("src/styles/**/*.scss", !prod),
sassPlugin({
basedir: ".",
transform: postcssModules({
localsConvention: "camelCaseOnly",
// generateScopedName: "typing--[local]",
generateScopedName: (name, filename, css) => {
console.log("gen scoped name", name, filename);
if (filename.includes("notranspile")) return name;
if (/^[A-Z]/.test(name)) {
// only prefix uppercase names
return `typing--${name.toLowerCase()}`;
}
return name;
},
// generateScopedName: "[name]__[local]",
}),
type: "css",
}),
renameCompiledCSS,
copyManifest,
],
});
if (prod) {
await context.rebuild();
process.exit(0);
} else {
await context.watch();
}