add setCache function to control Rspack cache configuration

This commit is contained in:
Nacho Codoñer
2025-10-13 15:06:17 +02:00
parent c909820d0a
commit fa1a1dc77a
3 changed files with 54 additions and 16 deletions

View File

@@ -54,6 +54,30 @@ function compileWithRspack(deps, { options = {} } = {}) {
});
}
/**
* Enable or disable Rspack cache config
* Usage: setCache(false)
*
* @param {boolean} enabled
* @param {Record<string, object>} cacheConfig
* @returns {Record<string, object>} `{ 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,
};

View File

@@ -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

View File

@@ -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.