2023-08-04 10:45:04 +02:00
|
|
|
//@ts-check
|
|
|
|
|
2022-02-16 11:26:13 +02:00
|
|
|
import esbuild from "esbuild";
|
|
|
|
import process from "process";
|
|
|
|
import builtins from "builtin-modules";
|
|
|
|
import sveltePlugin from "esbuild-svelte";
|
|
|
|
import sveltePreprocess from "svelte-preprocess";
|
2022-07-21 06:06:42 +02:00
|
|
|
import fs from "node:fs";
|
2023-08-04 10:45:04 +02:00
|
|
|
// import terser from "terser";
|
|
|
|
import { minify } from "terser";
|
2024-05-30 11:52:20 +02:00
|
|
|
import inlineWorkerPlugin from "esbuild-plugin-inline-worker";
|
2022-02-16 11:26:13 +02:00
|
|
|
const banner = `/*
|
2023-08-04 10:45:04 +02:00
|
|
|
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD AND TERSER
|
2022-02-16 11:26:13 +02:00
|
|
|
if you want to view the source, please visit the github repository of this plugin
|
|
|
|
*/
|
|
|
|
`;
|
|
|
|
|
|
|
|
const prod = process.argv[2] === "production";
|
2024-05-28 09:56:26 +02:00
|
|
|
const dev = process.argv[2] === "dev";
|
|
|
|
|
|
|
|
const keepTest = !prod || dev;
|
2023-08-04 10:45:04 +02:00
|
|
|
|
|
|
|
const terserOpt = {
|
2024-05-28 09:56:26 +02:00
|
|
|
sourceMap: !prod
|
|
|
|
? {
|
|
|
|
url: "inline",
|
|
|
|
}
|
|
|
|
: {},
|
2023-08-04 10:45:04 +02:00
|
|
|
format: {
|
|
|
|
indent_level: 2,
|
|
|
|
beautify: true,
|
|
|
|
comments: "some",
|
|
|
|
ecma: 2018,
|
|
|
|
preamble: banner,
|
2024-05-28 09:56:26 +02:00
|
|
|
webkit: true,
|
2023-08-04 10:45:04 +02:00
|
|
|
},
|
|
|
|
parse: {
|
|
|
|
// parse options
|
|
|
|
},
|
|
|
|
compress: {
|
|
|
|
// compress options
|
|
|
|
defaults: false,
|
|
|
|
evaluate: true,
|
|
|
|
inline: 3,
|
|
|
|
join_vars: true,
|
|
|
|
loops: true,
|
|
|
|
passes: prod ? 4 : 1,
|
|
|
|
reduce_vars: true,
|
|
|
|
reduce_funcs: true,
|
|
|
|
arrows: true,
|
|
|
|
collapse_vars: true,
|
|
|
|
comparisons: true,
|
|
|
|
lhs_constants: true,
|
|
|
|
hoist_props: true,
|
|
|
|
side_effects: true,
|
2023-10-02 10:53:41 +02:00
|
|
|
if_return: true,
|
|
|
|
ecma: 2018,
|
|
|
|
unused: true,
|
2023-08-04 10:45:04 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
ecma: 2018, // specify one of: 5, 2015, 2016, etc.
|
|
|
|
enclose: false, // or specify true, or "args:values"
|
|
|
|
keep_classnames: true,
|
|
|
|
keep_fnames: true,
|
|
|
|
ie8: false,
|
|
|
|
module: false,
|
|
|
|
// nameCache: null, // or specify a name cache object
|
|
|
|
safari10: false,
|
2024-05-28 09:56:26 +02:00
|
|
|
toplevel: false,
|
|
|
|
};
|
2023-08-04 10:45:04 +02:00
|
|
|
|
|
|
|
const manifestJson = JSON.parse(fs.readFileSync("./manifest.json") + "");
|
|
|
|
const packageJson = JSON.parse(fs.readFileSync("./package.json") + "");
|
2022-07-28 12:19:37 +02:00
|
|
|
const updateInfo = JSON.stringify(fs.readFileSync("./updates.md") + "");
|
2023-08-04 10:45:04 +02:00
|
|
|
|
|
|
|
/** @type esbuild.Plugin[] */
|
2024-05-28 09:56:26 +02:00
|
|
|
const plugins = [
|
|
|
|
{
|
|
|
|
name: "my-plugin",
|
|
|
|
setup(build) {
|
|
|
|
let count = 0;
|
|
|
|
build.onEnd(async (result) => {
|
|
|
|
if (count++ === 0) {
|
|
|
|
console.log("first build:", result);
|
|
|
|
} else {
|
|
|
|
console.log("subsequent build:");
|
|
|
|
}
|
|
|
|
if (prod) {
|
|
|
|
console.log("Performing terser");
|
|
|
|
const src = fs.readFileSync("./main_org.js").toString();
|
|
|
|
// @ts-ignore
|
|
|
|
const ret = await minify(src, terserOpt);
|
|
|
|
if (ret && ret.code) {
|
|
|
|
fs.writeFileSync("./main.js", ret.code);
|
|
|
|
}
|
|
|
|
console.log("Finished terser");
|
|
|
|
} else {
|
|
|
|
fs.copyFileSync("./main_org.js", "./main.js");
|
2023-08-04 10:45:04 +02:00
|
|
|
}
|
2024-05-28 09:56:26 +02:00
|
|
|
});
|
|
|
|
},
|
2023-08-04 10:45:04 +02:00
|
|
|
},
|
2024-05-28 09:56:26 +02:00
|
|
|
];
|
2023-08-04 10:45:04 +02:00
|
|
|
|
2024-05-30 11:52:20 +02:00
|
|
|
const externals = ["obsidian", "electron", "crypto", "@codemirror/autocomplete", "@codemirror/collab", "@codemirror/commands", "@codemirror/language", "@codemirror/lint", "@codemirror/search", "@codemirror/state", "@codemirror/view", "@lezer/common", "@lezer/highlight", "@lezer/lr"];
|
2023-08-04 10:45:04 +02:00
|
|
|
const context = await esbuild.context({
|
|
|
|
banner: {
|
|
|
|
js: banner,
|
|
|
|
},
|
|
|
|
entryPoints: ["src/main.ts"],
|
|
|
|
bundle: true,
|
|
|
|
define: {
|
2024-05-28 09:56:26 +02:00
|
|
|
MANIFEST_VERSION: `"${manifestJson.version}"`,
|
|
|
|
PACKAGE_VERSION: `"${packageJson.version}"`,
|
|
|
|
UPDATE_INFO: `${updateInfo}`,
|
|
|
|
global: "window",
|
2023-08-04 10:45:04 +02:00
|
|
|
},
|
2024-05-30 11:52:20 +02:00
|
|
|
external: externals,
|
2023-08-04 10:45:04 +02:00
|
|
|
// minifyWhitespace: true,
|
|
|
|
format: "cjs",
|
|
|
|
target: "es2018",
|
|
|
|
logLevel: "info",
|
|
|
|
platform: "browser",
|
|
|
|
sourcemap: prod ? false : "inline",
|
|
|
|
treeShaking: true,
|
|
|
|
outfile: "main_org.js",
|
2024-05-22 15:04:22 +02:00
|
|
|
mainFields: ["browser", "module", "main"],
|
2023-08-04 10:45:04 +02:00
|
|
|
minifyWhitespace: false,
|
|
|
|
minifySyntax: false,
|
|
|
|
minifyIdentifiers: false,
|
|
|
|
minify: false,
|
2024-05-22 15:04:22 +02:00
|
|
|
dropLabels: prod && !keepTest ? ["TEST", "DEV"] : [],
|
2023-08-04 10:45:04 +02:00
|
|
|
// keepNames: true,
|
|
|
|
plugins: [
|
2024-05-30 11:52:20 +02:00
|
|
|
inlineWorkerPlugin({
|
|
|
|
external: externals,
|
|
|
|
treeShaking: true,
|
|
|
|
}),
|
2023-08-04 10:45:04 +02:00
|
|
|
sveltePlugin({
|
|
|
|
preprocess: sveltePreprocess(),
|
2024-05-22 15:04:22 +02:00
|
|
|
compilerOptions: { css: "injected", preserveComments: false },
|
2023-08-04 10:45:04 +02:00
|
|
|
}),
|
2024-05-28 09:56:26 +02:00
|
|
|
...plugins,
|
2023-08-04 10:45:04 +02:00
|
|
|
],
|
2024-05-28 09:56:26 +02:00
|
|
|
});
|
2023-08-04 10:45:04 +02:00
|
|
|
|
2024-05-28 09:56:26 +02:00
|
|
|
if (prod || dev) {
|
2023-08-04 10:45:04 +02:00
|
|
|
await context.rebuild();
|
|
|
|
process.exit(0);
|
|
|
|
} else {
|
|
|
|
await context.watch();
|
|
|
|
}
|