diff --git a/packages/rspack/rspack_plugin.js b/packages/rspack/rspack_plugin.js index dc08d88150..cb76094fb5 100644 --- a/packages/rspack/rspack_plugin.js +++ b/packages/rspack/rspack_plugin.js @@ -74,154 +74,156 @@ const { getNpmCommand, } = require('meteor/tools-core/lib/npm'); -// Get entry points from Meteor configuration -setGlobalState(GLOBAL_STATE_KEYS.INITIAL_ENTRYPONTS, getMeteorAppEntrypoints()); +if (isMeteorAppRun() || isMeteorAppBuild()) { + // Get entry points from Meteor configuration + setGlobalState(GLOBAL_STATE_KEYS.INITIAL_ENTRYPONTS, getMeteorAppEntrypoints()); -// Main entry point - using top-level await -try { - if (isMeteorAppDebug() || isMeteorAppConfigModernVerbose()) { - logInfo(`[i] Meteor Npx prefix: ${getNpxCommand([])?.prefix}`); - logInfo(`[i] Meteor Npm prefix: ${getNpmCommand([])?.prefix}`); - } - - // Ensure Rspack is installed - await ensureRspackInstalled(); - - // Check if Rspack React is installed - if (checkReactInstalled()) { - await ensureRspackReactInstalled(); - } - - - // Ensure the Rspack build context directory exists - ensureRspackBuildContextExists(); - - // Ensure the rspack.config.js file exists at the project level - ensureRspackConfigExists(); - - // Configure Meteor settings for Rspack - configureMeteorForRspack(); - - // Register cleanup handler - process.on('exit', cleanup); - process.on('SIGINT', () => { - cleanup(); - process.exit(); - }); - - // When running `meteor run` command - if (isMeteorAppRun()) { - // Setup compilation tracking and callbacks - const { - clientFirstCompile, - serverFirstCompile, - clientFirstCompilePromise, - serverFirstCompilePromise, - onCompileClient, - onCompileServer, - } = setupCompilationTracking(); - - // For 'run' command, start Rspack in appropriate modes with distinct callbacks - if (isMeteorAppDevelopment()) { - startRspackClientServe({ onCompile: onCompileClient }); - startRspackServerWatch({ onCompile: onCompileServer }); - } else if (isMeteorAppProduction()) { - runRspackBuild({ - isClient: true, - isServer: false, - watch: true, - onCompile: onCompileClient, - }); - runRspackBuild({ - isServer: true, - isClient: false, - watch: true, - onCompile: onCompileServer, - }); + // Main entry point - using top-level await + try { + if (isMeteorAppDebug() || isMeteorAppConfigModernVerbose()) { + logInfo(`[i] Meteor Npx prefix: ${getNpxCommand([])?.prefix}`); + logInfo(`[i] Meteor Npm prefix: ${getNpmCommand([])?.prefix}`); } - // Wait for first compilation to complete - await waitForFirstCompilation(clientFirstCompile, serverFirstCompile, clientFirstCompilePromise, serverFirstCompilePromise); + // Ensure Rspack is installed + await ensureRspackInstalled(); - // When running `meteor test` command - } else if (isMeteorAppTest()) { - const initialEntrypoints = getMeteorInitialAppEntrypoints(); + // Check if Rspack React is installed + if (checkReactInstalled()) { + await ensureRspackReactInstalled(); + } - // Setup compilation tracking and callbacks - const { - clientFirstCompile, - serverFirstCompile, - clientFirstCompilePromise, - serverFirstCompilePromise, - onCompileClient, - onCompileServer, - } = setupCompilationTracking(); - // When run test for full app, run Rspack app server as well - if (isMeteorAppTestFullApp()) { - await runRspackBuild({ - isTest: false, - isServer: true, - isClient: false, - }); + // Ensure the Rspack build context directory exists + ensureRspackBuildContextExists(); - if (isMeteorAppTestWatch()) { + // Ensure the rspack.config.js file exists at the project level + ensureRspackConfigExists(); + + // Configure Meteor settings for Rspack + configureMeteorForRspack(); + + // Register cleanup handler + process.on('exit', cleanup); + process.on('SIGINT', () => { + cleanup(); + process.exit(); + }); + + // When running `meteor run` command + if (isMeteorAppRun()) { + // Setup compilation tracking and callbacks + const { + clientFirstCompile, + serverFirstCompile, + clientFirstCompilePromise, + serverFirstCompilePromise, + onCompileClient, + onCompileServer, + } = setupCompilationTracking(); + + // For 'run' command, start Rspack in appropriate modes with distinct callbacks + if (isMeteorAppDevelopment()) { + startRspackClientServe({ onCompile: onCompileClient }); + startRspackServerWatch({ onCompile: onCompileServer }); + } else if (isMeteorAppProduction()) { + runRspackBuild({ + isClient: true, + isServer: false, + watch: true, + onCompile: onCompileClient, + }); runRspackBuild({ isServer: true, isClient: false, - isTest: false, watch: true, + onCompile: onCompileServer, }); } - } - - // When testModule is specified for client or server, run Rspack considering those files - if (initialEntrypoints?.testClient || initialEntrypoints?.testServer) { - runRspackBuild({ - isTest: true, - isClient: true, - isServer: false, - watch: isMeteorAppTestWatch(), - onCompile: onCompileClient, - label: 'Test', - }); - - runRspackBuild({ - isTest: true, - isClient: false, - isServer: true, - watch: isMeteorAppTestWatch(), - onCompile: onCompileServer, - label: 'Test', - }); // Wait for first compilation to complete await waitForFirstCompilation(clientFirstCompile, serverFirstCompile, clientFirstCompilePromise, serverFirstCompilePromise); - // When testModule is specified as a single file or not specified - } else { - runRspackBuild({ - isTest: true, - isTestModule: true, - isClient: false, - isServer: true, - watch: isMeteorAppTestWatch(), - onCompile: onCompileServer, - label: 'Test', - }); - await waitForFirstCompilation(clientFirstCompile, serverFirstCompile, clientFirstCompilePromise, serverFirstCompilePromise, { target: 'server' }); - } + // When running `meteor test` command + } else if (isMeteorAppTest()) { + const initialEntrypoints = getMeteorInitialAppEntrypoints(); - // When running `meteor build` command - } else if (isMeteorAppBuild()) { - // For 'build' command, run Rspack build without watch mode - // Run client and server builds in parallel and wait for both to complete - await Promise.all([ - runRspackBuild({ isClient: true, isServer: false }), - runRspackBuild({ isServer: true, isClient: false }), - ]); + // Setup compilation tracking and callbacks + const { + clientFirstCompile, + serverFirstCompile, + clientFirstCompilePromise, + serverFirstCompilePromise, + onCompileClient, + onCompileServer, + } = setupCompilationTracking(); + + // When run test for full app, run Rspack app server as well + if (isMeteorAppTestFullApp()) { + await runRspackBuild({ + isTest: false, + isServer: true, + isClient: false, + }); + + if (isMeteorAppTestWatch()) { + runRspackBuild({ + isServer: true, + isClient: false, + isTest: false, + watch: true, + }); + } + } + + // When testModule is specified for client or server, run Rspack considering those files + if (initialEntrypoints?.testClient || initialEntrypoints?.testServer) { + runRspackBuild({ + isTest: true, + isClient: true, + isServer: false, + watch: isMeteorAppTestWatch(), + onCompile: onCompileClient, + label: 'Test', + }); + + runRspackBuild({ + isTest: true, + isClient: false, + isServer: true, + watch: isMeteorAppTestWatch(), + onCompile: onCompileServer, + label: 'Test', + }); + + // Wait for first compilation to complete + await waitForFirstCompilation(clientFirstCompile, serverFirstCompile, clientFirstCompilePromise, serverFirstCompilePromise); + + // When testModule is specified as a single file or not specified + } else { + runRspackBuild({ + isTest: true, + isTestModule: true, + isClient: false, + isServer: true, + watch: isMeteorAppTestWatch(), + onCompile: onCompileServer, + label: 'Test', + }); + await waitForFirstCompilation(clientFirstCompile, serverFirstCompile, clientFirstCompilePromise, serverFirstCompilePromise, { target: 'server' }); + } + + // When running `meteor build` command + } else if (isMeteorAppBuild()) { + // For 'build' command, run Rspack build without watch mode + // Run client and server builds in parallel and wait for both to complete + await Promise.all([ + runRspackBuild({ isClient: true, isServer: false }), + runRspackBuild({ isServer: true, isClient: false }), + ]); + } + } catch (error) { + logError(`Rspack plugin error: ${error.message}`); + throw error; } -} catch (error) { - logError(`Rspack plugin error: ${error.message}`); - throw error; }