first clean the build context once

This commit is contained in:
Nacho Codoñer
2025-08-26 16:09:40 +02:00
parent 2a835bafda
commit 2a83ffba15
3 changed files with 83 additions and 1 deletions

View File

@@ -496,6 +496,79 @@ ${importContent}
`;
}
/**
* Cleans the build context files of the current environment
* Removes all build files and directories for the current environment
* Also cleans _build-* files from public and private folders
* @returns {void}
*/
export function cleanBuildContextFiles() {
const appDir = getMeteorAppDir();
const buildContextPath = path.join(appDir, RSPACK_BUILD_CONTEXT);
// Only proceed if the build context directory exists
if (!fs.existsSync(buildContextPath)) {
return;
}
// Get current environment
const env = {
...(isMeteorAppDevelopment() ? { isDevelopment: true } : { isProduction: true }),
isNative: isMeteorAppNative(),
};
try {
// Clean main module directories
const mainClientPath = path.dirname(path.join(buildContextPath, getBuildFilePath({ isMain: true, isClient: true, ...env })));
const mainServerPath = path.dirname(path.join(buildContextPath, getBuildFilePath({ isMain: true, isServer: true, ...env })));
// Clean test module directories if they exist
const testModulePath = path.dirname(path.join(buildContextPath, getBuildFilePath({ isTest: true, isTestModule: true })));
const testClientPath = path.dirname(path.join(buildContextPath, getBuildFilePath({ isTest: true, isClient: true })));
const testServerPath = path.dirname(path.join(buildContextPath, getBuildFilePath({ isTest: true, isServer: true })));
// Create a Set to ensure unique directory paths
const uniqueDirPaths = new Set([mainClientPath, mainServerPath, testModulePath, testClientPath, testServerPath]);
// Remove directories if they exist
[...uniqueDirPaths].forEach(dirPath => {
if (fs.existsSync(dirPath)) {
fs.rmSync(dirPath, { recursive: true, force: true });
}
});
// Clean _build-* files from public and private folders
const publicDir = path.join(appDir, 'public');
const privateDir = path.join(appDir, 'private');
[publicDir, privateDir].forEach(dir => {
if (fs.existsSync(dir)) {
try {
const files = fs.readdirSync(dir);
files.forEach(file => {
if (file.startsWith('_build-')) {
const filePath = path.join(dir, file);
fs.rmSync(filePath, { recursive: true, force: true });
}
});
// Also remove client-rspack.js from public directory if it exists
if (dir === publicDir) {
const clientRspackPath = path.join(dir, 'client-rspack.js');
if (fs.existsSync(clientRspackPath)) {
fs.rmSync(clientRspackPath, { force: true });
}
}
} catch (err) {
logError(`Failed to clean _build-* files from ${dir}: ${err.message}`);
}
}
});
} catch (error) {
logError(`Failed to clean build context files: ${error.message}`);
}
}
/**
* Ensures the rspack.config.js file exists at the project level
* Creates the file if it doesn't exist with the required template

View File

@@ -25,6 +25,7 @@ export const DEFAULT_METEOR_RSPACK_SWC_HELPERS_VERSION = '0.5.17';
* @property {string} INITIAL_ENTRYPONTS - Key for storing initial entrypoints
* @property {string} CLIENT_FIRST_COMPILE - Key for tracking client first compilation state
* @property {string} SERVER_FIRST_COMPILE - Key for tracking server first compilation state
* @property {string} BUILD_CONTEXT_FILES_CLEANED - Key for tracking if build context files have been cleaned
*/
export const GLOBAL_STATE_KEYS = {
CLIENT_PROCESS: 'rspack.clientProcess',
@@ -35,6 +36,7 @@ export const GLOBAL_STATE_KEYS = {
INITIAL_ENTRYPONTS: 'meteor.initialEntrypoints',
CLIENT_FIRST_COMPILE: 'rspack.clientFirstCompile',
SERVER_FIRST_COMPILE: 'rspack.serverFirstCompile',
BUILD_CONTEXT_FILES_CLEANED: 'rspack.buildContextFilesCleaned',
};
/**

View File

@@ -28,6 +28,7 @@ const {
const {
ensureRspackBuildContextExists,
ensureRspackConfigExists,
cleanBuildContextFiles,
} = require('./lib/build-context');
const {
@@ -47,6 +48,7 @@ const {
} = require('./lib/compilation');
const {
getGlobalState,
setGlobalState
} = require('meteor/tools-core/lib/global-state');
@@ -86,6 +88,12 @@ if (isMeteorAppRun() || isMeteorAppBuild() || isMeteorAppTest()) {
logInfo(`[i] Meteor Npm prefix: ${getNpmCommand([])?.prefix}`);
}
// Clean build context files only if they haven't been cleaned yet
if (!getGlobalState(GLOBAL_STATE_KEYS.BUILD_CONTEXT_FILES_CLEANED)) {
cleanBuildContextFiles();
setGlobalState(GLOBAL_STATE_KEYS.BUILD_CONTEXT_FILES_CLEANED, true);
}
// Ensure Rspack is installed
await ensureRspackInstalled();
@@ -94,7 +102,6 @@ if (isMeteorAppRun() || isMeteorAppBuild() || isMeteorAppTest()) {
await ensureRspackReactInstalled();
}
// Ensure the Rspack build context directory exists
ensureRspackBuildContextExists();