build: improve errors when breaking module boundaries
This commit is contained in:
parent
003e4a08d5
commit
87e6fa8647
3 changed files with 36 additions and 6 deletions
|
@ -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,
|
||||
|
|
|
@ -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<string[]>}
|
||||
|
|
|
@ -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
|
||||
];
|
||||
|
|
Loading…
Reference in a new issue