Create custom plugin for import management

This commit is contained in:
Albert Portnoy 2023-01-07 00:13:14 -06:00
parent 57990db20a
commit 73ea47f69a
No known key found for this signature in database
3 changed files with 37 additions and 30 deletions

View file

@ -23,7 +23,6 @@
"license": "ISC",
"devDependencies": {
"@electron/asar": "^3.2.1",
"@fal-works/esbuild-plugin-global-externals": "^2.1.2",
"@types/node": "^18.11.2",
"@typescript-eslint/eslint-plugin": "^5.40.1",
"@typescript-eslint/parser": "^5.40.1",

View file

@ -2,7 +2,6 @@ lockfileVersion: 5.4
specifiers:
'@electron/asar': ^3.2.1
'@fal-works/esbuild-plugin-global-externals': ^2.1.2
'@types/node': ^18.11.2
'@typescript-eslint/eslint-plugin': ^5.40.1
'@typescript-eslint/parser': ^5.40.1
@ -18,7 +17,6 @@ specifiers:
devDependencies:
'@electron/asar': 3.2.1
'@fal-works/esbuild-plugin-global-externals': 2.1.2
'@types/node': 18.11.2
'@typescript-eslint/eslint-plugin': 5.40.1_ukgdydjtebaxmxfqp5v5ulh64y
'@typescript-eslint/parser': 5.40.1_z4bbprzjrhnsfa24uvmcbu7f5q
@ -103,10 +101,6 @@ packages:
- supports-color
dev: true
/@fal-works/esbuild-plugin-global-externals/2.1.2:
resolution: {integrity: sha512-cEee/Z+I12mZcFJshKcCqC8tuX5hG3s+d+9nZ3LabqKF1vKdF41B92pJVCBggjAGORAeOzyyDDKrZwIkLffeOQ==}
dev: true
/@humanwhocodes/config-array/0.10.7:
resolution: {integrity: sha512-MDl6D6sBsaV452/QSdX+4CXIjZhIcI0PELsxUjk4U828yd58vk3bTIvk/6w5FY+4hIy9sLW0sfrV7K7Kc++j/w==}
engines: {node: '>=10.10.0'}

View file

@ -1,5 +1,4 @@
import esbuild from "esbuild";
import { globalExternals } from "@fal-works/esbuild-plugin-global-externals";
import path, { join } from "path";
import fs, { existsSync, rmSync } from "fs";
import _manifest from "../manifest.json";
@ -10,26 +9,41 @@ const manifest: Plugin = _manifest;
const NODE_VERSION = "14";
const CHROME_VERSION = "91";
const globalModules = {
replugged: {
varName: "replugged",
namedExports: [
"Injector",
"Logger",
"webpack",
"common",
"notices",
"commands",
"settings",
"quickCSS",
"themes",
"ignition",
"plugins",
"util",
"logger",
"types",
],
defaultExport: true,
const globalModules: esbuild.Plugin = {
name: "globalModules",
setup: (build) => {
build.onResolve({ filter: /^replugged.+$/ }, (args) => {
if (args.kind !== "import-statement") return;
return {
errors: [
{
text: `Importing from a path (${args.path}) is not supported. Instead, please import from "replugged" and destructure the required modules.`,
},
],
};
});
build.onResolve({ filter: /^replugged$/ }, (args) => {
if (args.kind !== "import-statement") return;
return {
path: args.path,
namespace: "replugged",
};
});
build.onLoad(
{
filter: /.*/,
namespace: "replugged",
},
() => {
return {
contents: "module.exports = window.replugged",
};
},
);
},
};
@ -88,7 +102,7 @@ if ("renderer" in manifest) {
target: `chrome${CHROME_VERSION}`,
outfile: "dist/renderer.js",
format: "esm" as esbuild.Format,
plugins: [globalExternals(globalModules), install],
plugins: [globalModules, install],
}),
);
@ -134,7 +148,7 @@ if ("plaintextPatches" in manifest) {
target: `chrome${CHROME_VERSION}`,
outfile: "dist/plaintextPatches.js",
format: "esm" as esbuild.Format,
plugins: [globalExternals(globalModules), install],
plugins: [globalModules, install],
}),
);