From 8fd9ac7322a5f0cf09f765fa2110b08e757ea20b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nacho=20Codo=C3=B1er?= Date: Mon, 15 Dec 2025 15:51:23 +0100 Subject: [PATCH] add `disablePlugins` helper to filter Rspack plugins by matchers --- npm-packages/meteor-rspack/index.d.ts | 9 ++++ .../meteor-rspack/lib/meteorRspackHelpers.js | 54 +++++++++++++++++++ npm-packages/meteor-rspack/rspack.config.js | 11 ++++ 3 files changed, 74 insertions(+) diff --git a/npm-packages/meteor-rspack/index.d.ts b/npm-packages/meteor-rspack/index.d.ts index 37104cd1a6..896b9703df 100644 --- a/npm-packages/meteor-rspack/index.d.ts +++ b/npm-packages/meteor-rspack/index.d.ts @@ -66,6 +66,15 @@ type MeteorEnv = Record & { * @returns A config object with merged configs */ extendConfig: (...configs: Record[]) => Record; + + /** + * Remove plugins from a Rspack config by name, RegExp, predicate, or array of them. + * @param matchers - String, RegExp, function, or array of them to match plugin names + * @returns The modified config object + */ + disablePlugins: ( + matchers: string | RegExp | ((plugin: any, index: number) => boolean) | Array boolean)> + ) => Record; } export type ConfigFactory = ( diff --git a/npm-packages/meteor-rspack/lib/meteorRspackHelpers.js b/npm-packages/meteor-rspack/lib/meteorRspackHelpers.js index 5938783b59..3200fe5089 100644 --- a/npm-packages/meteor-rspack/lib/meteorRspackHelpers.js +++ b/npm-packages/meteor-rspack/lib/meteorRspackHelpers.js @@ -152,6 +152,59 @@ function extendSwcConfig(swcConfig) { }); } +/** + * Remove plugins from a Rspack config by name, RegExp, predicate, or array of them. + * When using a function predicate, it receives both the plugin and its index in the plugins array. + * + * @param {object} config Rspack config object + * @param {string | RegExp | ((plugin: any, index: number) => boolean) | Array} matchers + * @returns {object} The modified config object + */ +function disablePlugins(config, matchers) { + if (!config || typeof config !== "object") { + throw new TypeError("disablePlugins: `config` must be an object"); + } + + const plugins = Array.isArray(config.plugins) ? config.plugins : []; + const kept = []; + + const list = Array.isArray(matchers) ? matchers : [matchers]; + + const getPluginName = (p) => { + if (!p) return ""; + return ( + (p.constructor && typeof p.constructor.name === "string" && p.constructor.name) || + (typeof p.name === "string" && p.name) || + (typeof p.pluginName === "string" && p.pluginName) || + (typeof p.__pluginName === "string" && p.__pluginName) || + "" + ); + }; + + const predicates = list.map((m) => { + if (typeof m === "function") return m; + if (m instanceof RegExp) { + return (p) => m.test(getPluginName(p)); + } + if (typeof m === "string") { + return (p) => getPluginName(p) === m; + } + throw new TypeError( + "disablePlugins: matchers must be string, RegExp, function, or array of them" + ); + }); + + plugins.forEach((p, index) => { + const matches = predicates.some((fn) => fn(p, index)); + if (!matches) { + kept.push(p); + } + }); + + config.plugins = kept; + return config; +} + module.exports = { compileWithMeteor, compileWithRspack, @@ -159,4 +212,5 @@ module.exports = { splitVendorChunk, extendSwcConfig, makeWebNodeBuiltinsAlias, + disablePlugins, }; diff --git a/npm-packages/meteor-rspack/rspack.config.js b/npm-packages/meteor-rspack/rspack.config.js index 069a175674..3055ea51a6 100644 --- a/npm-packages/meteor-rspack/rspack.config.js +++ b/npm-packages/meteor-rspack/rspack.config.js @@ -20,7 +20,9 @@ const { splitVendorChunk, extendSwcConfig, makeWebNodeBuiltinsAlias, + disablePlugins, } = require('./lib/meteorRspackHelpers.js'); +const { prepareMeteorRspackConfig } = require("./lib/meteorRspackConfigFactory"); // Safe require that doesn't throw if the module isn't found function safeRequire(moduleName) { @@ -287,6 +289,9 @@ module.exports = async function (inMeteor = {}, argv = {}) { Meteor.splitVendorChunk = () => splitVendorChunk(); Meteor.extendSwcConfig = (customSwcConfig) => extendSwcConfig(customSwcConfig); Meteor.extendConfig = (...configs) => mergeSplitOverlap(...configs); + Meteor.disablePlugins = matchers => prepareMeteorRspackConfig({ + disablePlugins: matchers, + }); // Add HtmlRspackPlugin function to Meteor Meteor.HtmlRspackPlugin = (options = {}) => { @@ -780,6 +785,12 @@ module.exports = async function (inMeteor = {}, argv = {}) { } } + const shouldDisablePlugins = config?.disablePlugins != null; + if (shouldDisablePlugins) { + config = disablePlugins(config, config.disablePlugins); + delete config.disablePlugins; + } + if (Meteor.isDebug || Meteor.isVerbose) { console.log('Config:', inspect(config, { depth: null, colors: true })); }