From 2a83ffba1580dc351475e7ddb5fcce9f23707c4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nacho=20Codo=C3=B1er?= Date: Tue, 26 Aug 2025 16:09:40 +0200 Subject: [PATCH] first clean the build context once --- packages/rspack/lib/build-context.js | 73 ++++++++++++++++++++++++++++ packages/rspack/lib/constants.js | 2 + packages/rspack/rspack_plugin.js | 9 +++- 3 files changed, 83 insertions(+), 1 deletion(-) diff --git a/packages/rspack/lib/build-context.js b/packages/rspack/lib/build-context.js index 85b2149928..584faec447 100644 --- a/packages/rspack/lib/build-context.js +++ b/packages/rspack/lib/build-context.js @@ -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 diff --git a/packages/rspack/lib/constants.js b/packages/rspack/lib/constants.js index 54e4e000dd..80d3a8d74f 100644 --- a/packages/rspack/lib/constants.js +++ b/packages/rspack/lib/constants.js @@ -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', }; /** diff --git a/packages/rspack/rspack_plugin.js b/packages/rspack/rspack_plugin.js index 327f5c186f..90f2f45e77 100644 --- a/packages/rspack/rspack_plugin.js +++ b/packages/rspack/rspack_plugin.js @@ -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();