From 87e6fa8647b6ddfcd73036fd62f2357ed820d510 Mon Sep 17 00:00:00 2001 From: Vendicated Date: Sat, 10 Aug 2024 17:25:40 +0200 Subject: [PATCH] build: improve errors when breaking module boundaries --- scripts/build/build.mjs | 6 +++--- scripts/build/buildWeb.mjs | 11 ++++++++--- scripts/build/common.mjs | 25 +++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/scripts/build/build.mjs b/scripts/build/build.mjs index 817c2cec3..623f9f940 100755 --- a/scripts/build/build.mjs +++ b/scripts/build/build.mjs @@ -21,7 +21,7 @@ import esbuild from "esbuild"; import { readdir } from "fs/promises"; import { join } from "path"; -import { BUILD_TIMESTAMP, commonOpts, exists, globPlugins, IS_DEV, IS_REPORTER, IS_STANDALONE, IS_UPDATER_DISABLED, resolvePluginName, VERSION, watch } from "./common.mjs"; +import { BUILD_TIMESTAMP, commonOpts, exists, globPlugins, IS_DEV, IS_REPORTER, IS_STANDALONE, IS_UPDATER_DISABLED, resolvePluginName, VERSION, commonRendererPlugins, watch } from "./common.mjs"; const defines = { IS_STANDALONE, @@ -131,7 +131,7 @@ await Promise.all([ sourcemap, plugins: [ globPlugins("discordDesktop"), - ...commonOpts.plugins + ...commonRendererPlugins ], define: { ...defines, @@ -180,7 +180,7 @@ await Promise.all([ sourcemap, plugins: [ globPlugins("vencordDesktop"), - ...commonOpts.plugins + ...commonRendererPlugins ], define: { ...defines, diff --git a/scripts/build/buildWeb.mjs b/scripts/build/buildWeb.mjs index bc15ccced..deab86610 100644 --- a/scripts/build/buildWeb.mjs +++ b/scripts/build/buildWeb.mjs @@ -23,7 +23,7 @@ import { appendFile, mkdir, readdir, readFile, rm, writeFile } from "fs/promises import { join } from "path"; import Zip from "zip-local"; -import { BUILD_TIMESTAMP, commonOpts, globPlugins, IS_DEV, IS_REPORTER, VERSION } from "./common.mjs"; +import { BUILD_TIMESTAMP, commonOpts, globPlugins, IS_DEV, IS_REPORTER, VERSION, commonRendererPlugins } from "./common.mjs"; /** * @type {esbuild.BuildOptions} @@ -36,7 +36,7 @@ const commonOptions = { external: ["~plugins", "~git-hash", "/assets/*"], plugins: [ globPlugins("web"), - ...commonOpts.plugins, + ...commonRendererPlugins ], target: ["esnext"], define: { @@ -116,7 +116,12 @@ await Promise.all( } }) ] -); +).catch(err => { + console.error("Build failed"); + console.error(err.message); + if (!commonOpts.watch) + process.exit(1); +});; /** * @type {(dir: string) => Promise} diff --git a/scripts/build/common.mjs b/scripts/build/common.mjs index c46a559a7..247557e3c 100644 --- a/scripts/build/common.mjs +++ b/scripts/build/common.mjs @@ -28,6 +28,7 @@ import { join, relative } from "path"; import { promisify } from "util"; import { getPluginTarget } from "../utils.mjs"; +import { builtinModules } from "module"; /** @type {import("../../package.json")} */ const PackageJSON = JSON.parse(readFileSync("package.json")); @@ -292,6 +293,18 @@ export const stylePlugin = { } }; +/** + * @type {(filter: RegExp, message: string) => import("esbuild").Plugin} + */ +export const banImportPlugin = (filter, message) => ({ + name: "ban-imports", + setup: build => { + build.onResolve({ filter }, () => { + return { errors: [{ text: message }] }; + }); + } +}); + /** * @type {import("esbuild").BuildOptions} */ @@ -311,3 +324,15 @@ export const commonOpts = { // Work around https://github.com/evanw/esbuild/issues/2460 tsconfig: "./scripts/build/tsconfig.esbuild.json" }; + +const escapedBuiltinModules = builtinModules + .map(m => m.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&")) + .join("|"); +const builtinModuleRegex = new RegExp(`^(node:)?(${escapedBuiltinModules})$`); + +export const commonRendererPlugins = [ + banImportPlugin(builtinModuleRegex, "Cannot import node inbuilt modules in browser code. You need to use a native.ts file"), + banImportPlugin(/^react$/, "Cannot import from react. React and hooks should be imported from @webpack/common"), + banImportPlugin(/^electron(\/.*)?$/, "Cannot import electron in browser code. You need to use a native.ts file"), + ...commonOpts.plugins +];