From fa1a1dc77a9839bf93fc34b25e574580eeac0cc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nacho=20Codo=C3=B1er?= Date: Mon, 13 Oct 2025 15:06:17 +0200 Subject: [PATCH] add `setCache` function to control Rspack cache configuration --- .../meteor-rspack/lib/meteorRspackHelpers.js | 25 ++++++++++++++++ npm-packages/meteor-rspack/rspack.config.js | 16 ++++++++-- .../rspack-bundler-integration.md | 29 ++++++++++--------- 3 files changed, 54 insertions(+), 16 deletions(-) diff --git a/npm-packages/meteor-rspack/lib/meteorRspackHelpers.js b/npm-packages/meteor-rspack/lib/meteorRspackHelpers.js index 4645238079..cde87ba899 100644 --- a/npm-packages/meteor-rspack/lib/meteorRspackHelpers.js +++ b/npm-packages/meteor-rspack/lib/meteorRspackHelpers.js @@ -54,6 +54,30 @@ function compileWithRspack(deps, { options = {} } = {}) { }); } +/** + * Enable or disable Rspack cache config + * Usage: setCache(false) + * + * @param {boolean} enabled + * @param {Record} cacheConfig + * @returns {Record} `{ meteorRspackConfigX: { cache: {} } }` + */ +function setCache( + enabled, + cacheConfig = { cache: true, experiments: { cache: true } }, +) { + return prepareMeteorRspackConfig( + enabled + ? cacheConfig + : { + cache: false, // disable cache + experiments: { + cache: false, // disable persistent cache (experimental flag) + }, + }, + ); +} + /** * Build an alias map that disables ALL Node core modules in a web build. * - Includes both 'fs' and 'node:fs' keys @@ -77,5 +101,6 @@ function makeWebNodeBuiltinsAlias(extras = []) { module.exports = { compileWithMeteor, compileWithRspack, + setCache, makeWebNodeBuiltinsAlias, }; diff --git a/npm-packages/meteor-rspack/rspack.config.js b/npm-packages/meteor-rspack/rspack.config.js index 4290a5ee26..f92961aabd 100644 --- a/npm-packages/meteor-rspack/rspack.config.js +++ b/npm-packages/meteor-rspack/rspack.config.js @@ -14,6 +14,7 @@ const { mergeMeteorRspackFragments } = require("./lib/meteorRspackConfigFactory. const { compileWithMeteor, compileWithRspack, + setCache, makeWebNodeBuiltinsAlias, } = require('./lib/meteorRspackHelpers.js'); @@ -218,12 +219,23 @@ module.exports = async function (inMeteor = {}, argv = {}) { const buildOutputDir = path.resolve(projectDir, buildContext, outputDir); Meteor.buildOutputDir = buildOutputDir; + const cacheStrategy = createCacheStrategy( + mode, + (Meteor.isClient && 'client') || 'server', + { projectConfigPath, configPath } + ); + // Expose Meteor's helpers to expand Rspack configs Meteor.compileWithMeteor = deps => compileWithMeteor(deps); Meteor.compileWithRspack = deps => compileWithRspack(deps, { options: Meteor.swcConfigOptions, }); + Meteor.setCache = enabled => + setCache( + !!enabled, + enabled === 'memory' ? undefined : cacheStrategy + ); // Add HtmlRspackPlugin function to Meteor Meteor.HtmlRspackPlugin = (options = {}) => { @@ -443,7 +455,7 @@ module.exports = async function (inMeteor = {}, argv = {}) { }, }, }), - ...merge(createCacheStrategy(mode, "client", { projectConfigPath, configPath }), { experiments: { css: true } }) + ...merge(cacheStrategy, { experiments: { css: true } }) }; @@ -526,7 +538,7 @@ module.exports = async function (inMeteor = {}, argv = {}) { watchOptions, devtool: isDevEnvironment || isNative || isTest ? 'source-map' : 'hidden-source-map', ...((isDevEnvironment || (isTest && !isTestEager) || isNative) && - createCacheStrategy(mode, "server", { projectConfigPath, configPath })), + cacheStrategy), }; // Load and apply project-level overrides for the selected build diff --git a/v3-docs/docs/about/modern-build-stack/rspack-bundler-integration.md b/v3-docs/docs/about/modern-build-stack/rspack-bundler-integration.md index 602745ebe2..300e88c7ac 100644 --- a/v3-docs/docs/about/modern-build-stack/rspack-bundler-integration.md +++ b/v3-docs/docs/about/modern-build-stack/rspack-bundler-integration.md @@ -137,20 +137,21 @@ module.exports = defineConfig(Meteor => { You can use flags to control the final configuration based on the environment. The available flags are passed in the `Meteor` parameter. -| Flag | Type | Description | -| ------------------ | -------- |-----------------------------------------------------------| -| `isDevelopment` | boolean | True when running in development mode | -| `isProduction` | boolean | True when running in production mode | -| `isClient` | boolean | True when building or running client code | -| `isServer` | boolean | True when building or running server code | -| `isTest` | boolean | True when running in test mode | -| `isDebug` | boolean | True when debug mode is enabled | -| `isRun` | boolean | True when running the project with `meteor run` | -| `isBuild` | boolean | True when building the project with `meteor build` | -| `swcConfigOptions` | object | Project-level SWC config available for reusing | -| `HtmlRspackPlugin` | function | Custom HtmlRspackPlugin function for extending the config | -| `compileWithMeteor` | function | Forces given npm deps (string[]) to be compiled by Meteor | -| `compileWithRspack` | function | Forces given npm deps (string[]) to be compiled by Rspack | +| Flag | Type | Description | +|---------------------| -------- |-----------------------------------------------------------------------------| +| `isDevelopment` | boolean | True when running in development mode | +| `isProduction` | boolean | True when running in production mode | +| `isClient` | boolean | True when building or running client code | +| `isServer` | boolean | True when building or running server code | +| `isTest` | boolean | True when running in test mode | +| `isDebug` | boolean | True when debug mode is enabled | +| `isRun` | boolean | True when running the project with `meteor run` | +| `isBuild` | boolean | True when building the project with `meteor build` | +| `swcConfigOptions` | object | Project-level SWC config available for reusing | +| `HtmlRspackPlugin` | function | Custom HtmlRspackPlugin function for extending the config | +| `compileWithMeteor` | function | Forces given npm deps (string[]) to be compiled by Meteor | +| `compileWithRspack` | function | Forces given npm deps (string[]) to be compiled by Rspack | +| `setCache` | function | Enables or disables cache. Accepts true (persistent, default), false, or 'memory' | Some configurations in the Rspack config are reserved for the Meteor-Rspack setup to work, such as Rspack options inside the `entry` and `output` objects. These will trigger warnings if modified. All other settings can be overridden, giving you the flexibility to make any setup compatible with the modern bundler.