From f3aeca4bacc9f71f1118e6fcd5f9ea48e42e3040 Mon Sep 17 00:00:00 2001 From: Lucy Dryaeva <48590492+ShadiestGoat@users.noreply.github.com> Date: Fri, 1 Dec 2023 11:12:51 +0000 Subject: [PATCH] Add esbuild overwrites (#580) * Add esbuild overwrites * Use a patch function > config * Lint * Fix esbuild extra imports * Lint <3 --- bin/index.mts | 64 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 20 deletions(-) diff --git a/bin/index.mts b/bin/index.mts index 2581ef27d..244a84fa5 100755 --- a/bin/index.mts +++ b/bin/index.mts @@ -12,6 +12,7 @@ import { rmSync, writeFileSync, } from "fs"; +import { cwd } from "process"; import esbuild from "esbuild"; import path from "path"; import updateNotifier from "update-notifier"; @@ -39,6 +40,17 @@ export const directory = process.cwd(); const dirname = path.dirname(fileURLToPath(import.meta.url)); const packageJson = JSON.parse(readFileSync(path.resolve(dirname, "package.json"), "utf-8")); +let extraESBuildConfig = new Promise<(current: esbuild.BuildOptions) => esbuild.BuildOptions>( + (resolve) => resolve((v) => v), +); + +if (existsSync("./esbuild.extra.mjs")) { + extraESBuildConfig = new Promise((resolve) => { + import(path.join(cwd(), "esbuild.extra.mjs")).then((v) => { + resolve(v.default); + }); + }); +} const updateMessage = `Update available ${chalk.dim("{currentVersion}")}${chalk.reset( " → ", @@ -382,13 +394,17 @@ async function buildPlugin({ watch, noInstall, production, noReload, addon }: Ar const targets: Array> = []; + const overwrites = await extraESBuildConfig; + if ("renderer" in manifest) { targets.push( - esbuild.context({ - ...common, - entryPoints: [path.join(folderPath, manifest.renderer)], - outfile: `${distPath}/renderer.js`, - }), + esbuild.context( + overwrites({ + ...common, + entryPoints: [path.join(folderPath, manifest.renderer)], + outfile: `${distPath}/renderer.js`, + }), + ), ); manifest.renderer = "renderer.js"; @@ -396,11 +412,13 @@ async function buildPlugin({ watch, noInstall, production, noReload, addon }: Ar if ("plaintextPatches" in manifest) { targets.push( - esbuild.context({ - ...common, - entryPoints: [path.join(folderPath, manifest.plaintextPatches)], - outfile: `${distPath}/plaintextPatches.js`, - }), + esbuild.context( + overwrites({ + ...common, + entryPoints: [path.join(folderPath, manifest.plaintextPatches)], + outfile: `${distPath}/plaintextPatches.js`, + }), + ), ); manifest.plaintextPatches = "plaintextPatches.js"; @@ -464,13 +482,17 @@ async function buildTheme({ watch, noInstall, production, noReload, addon }: Arg const targets: Array> = []; + const overwrites = await extraESBuildConfig; + if (main) { targets.push( - esbuild.context({ - ...common, - entryPoints: [main], - outfile: `${distPath}/main.css`, - }), + esbuild.context( + overwrites({ + ...common, + entryPoints: [main], + outfile: `${distPath}/main.css`, + }), + ), ); manifest.main = "main.css"; @@ -478,11 +500,13 @@ async function buildTheme({ watch, noInstall, production, noReload, addon }: Arg if (splash) { targets.push( - esbuild.context({ - ...common, - entryPoints: [splash], - outfile: `${distPath}/splash.css`, - }), + esbuild.context( + overwrites({ + ...common, + entryPoints: [splash], + outfile: `${distPath}/splash.css`, + }), + ), ); manifest.plaintextPatches = "splash.css";