diff --git a/lib/controllers/prepare-controller.ts b/lib/controllers/prepare-controller.ts index 1210c316d4..57c738e176 100644 --- a/lib/controllers/prepare-controller.ts +++ b/lib/controllers/prepare-controller.ts @@ -2,9 +2,11 @@ import { watch, WatchOptions, FSWatcher } from "chokidar"; import { EventEmitter } from "events"; import * as _ from "lodash"; import * as path from "path"; +import * as semver from "semver"; import { IAnalyticsService, IDictionary, + IErrors, IFileSystem, IHooksService, } from "../common/declarations"; @@ -68,6 +70,7 @@ export class PrepareController private $platformController: IPlatformController, public $hooksService: IHooksService, private $fs: IFileSystem, + private $errors: IErrors, private $logger: ILogger, private $options: IOptions, private $mobileHelper: Mobile.IMobileHelper, @@ -155,6 +158,7 @@ export class PrepareController projectData, ); + this.ensureRuntimeMeetsBundlerMinimum(prepareData.platform, projectData); await this.trackRuntimeVersion(prepareData.platform, projectData); this.$logger.info("Preparing project..."); @@ -640,6 +644,49 @@ export class PrepareController }); } + /** + * Fails when the installed runtime is older than the minimum the installed + * bundler declares for `platform`. Versions compare on major.minor.patch + * only, so prerelease and local builds of the minimum's release satisfy it. + */ + private ensureRuntimeMeetsBundlerMinimum( + platform: string, + projectData: IProjectData, + ): void { + const requirement = this.$bundlerCompilerService.getViteMinRuntimeVersion( + platform, + projectData, + ); + if (!requirement) { + return; + } + + const runtime = this.$projectDataService.getRuntimePackage( + projectData.projectDir, + platform as SupportedPlatform, + ); + const installed = semver.coerce(runtime?.version); + const minimum = semver.coerce(requirement.minVersion); + if (!installed || !minimum) { + this.$logger.trace( + `Skipping the ${requirement.bundlerPackageName} runtime check for ${platform}: cannot compare runtime version "${runtime?.version}" with minimum "${requirement.minVersion}".`, + ); + return; + } + if (semver.gte(installed, minimum)) { + return; + } + + const bundler = requirement.bundlerVersion + ? `${requirement.bundlerPackageName} ${requirement.bundlerVersion}` + : requirement.bundlerPackageName; + this.$errors.fail( + `${bundler} requires ${runtime.name} ${minimum.version} or newer for ${platform.toLowerCase()}, ` + + `but the project has ${runtime.name} ${runtime.version} installed. ` + + `Update the runtime with "npm install --save-dev ${runtime.name}@^${minimum.version}" and run the command again.`, + ); + } + private isFileWatcherPaused() { return this.pausedFileWatch; } diff --git a/lib/services/bundler/bundler-compiler-service.ts b/lib/services/bundler/bundler-compiler-service.ts index 7a3c1f7afb..1f43dbd382 100644 --- a/lib/services/bundler/bundler-compiler-service.ts +++ b/lib/services/bundler/bundler-compiler-service.ts @@ -70,10 +70,10 @@ const BUNDLER_STOP_TIMEOUT_MS = 5000; const VITE_APP_RESTART_MESSAGE_TYPE = "nativescript:restart"; /** - * Set to "1" on `vite serve` when it runs alone: `@nativescript/vite` then - * writes `bundle.mjs` and the app's static files itself and sends the build - * messages a `vite build --watch` would. "0" in the CLI's environment keeps - * the build process. + * Set to "1" on `vite serve` when it runs alone. `@nativescript/vite` releases + * that list `nativescriptCli.singleProcessDev` may read it to write + * `bundle.mjs` and the app's static files themselves and send the build + * messages a `vite build --watch` would. */ const VITE_SINGLE_PROCESS_ENV = "NS_VITE_SINGLE_PROCESS"; @@ -805,9 +805,6 @@ export class BundlerCompilerService platform: string, projectData: IProjectData, ): boolean { - if (process.env[VITE_SINGLE_PROCESS_ENV] === "0") { - return false; - } return this.getViteCliCapabilityPlatforms( "singleProcessDev", projectData, @@ -826,6 +823,27 @@ export class BundlerCompilerService ); } + public getViteMinRuntimeVersion( + platform: string, + projectData: IProjectData, + ): IBundlerRuntimeRequirement | null { + if (this.getBundler() !== "vite") { + return null; + } + const packageJSON = this.readBundlerPackageJSON(projectData); + const minVersion = + packageJSON?.nativescriptCli?.minRuntimeVersion?.[platform.toLowerCase()]; + if (typeof minVersion !== "string") { + return null; + } + return { + bundlerPackageName: this.getBundlerPackageName(), + bundlerVersion: + typeof packageJSON.version === "string" ? packageJSON.version : null, + minVersion, + }; + } + /** * The platforms the installed `@nativescript/vite` lists under * `nativescriptCli.` in its package.json. @@ -834,19 +852,20 @@ export class BundlerCompilerService capability: "singleProcessDev" | "devServerFile", projectData: IProjectData, ): string[] { + const platforms = + this.readBundlerPackageJSON(projectData)?.nativescriptCli?.[capability]; + return Array.isArray(platforms) ? platforms : []; + } + + private readBundlerPackageJSON(projectData: IProjectData): any { try { const packageJSONPath = resolvePackageJSONPath( this.getBundlerPackageName(), { paths: [projectData.projectDir] }, ); - if (!packageJSONPath) { - return []; - } - const platforms = - this.$fs.readJson(packageJSONPath)?.nativescriptCli?.[capability]; - return Array.isArray(platforms) ? platforms : []; + return packageJSONPath ? this.$fs.readJson(packageJSONPath) : null; } catch { - return []; + return null; } } diff --git a/lib/services/bundler/bundler.ts b/lib/services/bundler/bundler.ts index 485e08e500..544ccb7f12 100644 --- a/lib/services/bundler/bundler.ts +++ b/lib/services/bundler/bundler.ts @@ -36,6 +36,23 @@ declare global { * `platform`, instead of baking one origin into the bundle. */ usesDevServerFile(platform: string, projectData: IProjectData): boolean; + /** + * The minimum runtime version the installed `@nativescript/vite` + * declares for `platform` under `nativescriptCli.minRuntimeVersion`; + * null when it declares none or the bundler is not vite. + */ + getViteMinRuntimeVersion( + platform: string, + projectData: IProjectData, + ): IBundlerRuntimeRequirement | null; + } + + interface IBundlerRuntimeRequirement { + bundlerPackageName: string; + /** Null when the bundler's package.json has no version. */ + bundlerVersion: string | null; + /** As declared; may not be valid semver. */ + minVersion: string; } /** diff --git a/test/controllers/prepare-controller.ts b/test/controllers/prepare-controller.ts index cb784b9b65..6eddc2e6ae 100644 --- a/test/controllers/prepare-controller.ts +++ b/test/controllers/prepare-controller.ts @@ -47,6 +47,7 @@ function createTestInjector(data: { hasNativeChanges: boolean }): IInjector { compileWithoutWatch: async () => { isCompileWithoutWatchCalled = true; }, + getViteMinRuntimeVersion: (): IBundlerRuntimeRequirement => null, }); injector.register("mobileHelper", MobileHelper); @@ -256,4 +257,82 @@ describe("prepareController", () => { }); }); }); + + describe("bundler runtime minimum", () => { + const prepareWith = async ( + requirement: IBundlerRuntimeRequirement, + runtimeVersion: string, + ) => { + const injector = createTestInjector({ hasNativeChanges: false }); + injector.resolve("bundlerCompilerService").getViteMinRuntimeVersion = + () => requirement; + injector.resolve("projectDataService").getRuntimePackage = () => ({ + name: "@nativescript/ios", + version: runtimeVersion, + }); + const prepareController: PrepareController = + injector.resolve("prepareController"); + await prepareController.prepare({ + ...prepareData, + watch: false, + platform: "ios", + }); + }; + const vite = (minVersion: string): IBundlerRuntimeRequirement => ({ + bundlerPackageName: "@nativescript/vite", + bundlerVersion: "2.0.0", + minVersion, + }); + + it("prepares when the bundler declares no minimum for the platform", async () => { + await prepareWith(null, "8.0.0"); + + assert.isTrue(isCompileWithoutWatchCalled); + }); + + it("stops before bundling when the runtime is older", async () => { + await assert.isRejected( + prepareWith(vite("9.2.0"), "9.1.3"), + '@nativescript/vite 2.0.0 requires @nativescript/ios 9.2.0 or newer for ios, but the project has @nativescript/ios 9.1.3 installed. Update the runtime with "npm install --save-dev @nativescript/ios@^9.2.0" and run the command again.', + ); + + assert.isFalse(isCompileWithoutWatchCalled); + assert.isFalse(isNativePrepareCalled); + }); + + it("rejects a prerelease of an older version", async () => { + await assert.isRejected( + prepareWith(vite("9.2.0"), "9.1.3-next.2026-12-01-100"), + "requires @nativescript/ios 9.2.0 or newer", + ); + }); + + it("accepts prerelease and PR builds of the minimum's version", async () => { + await prepareWith(vite("9.2.0"), "9.2.0-next.2026-12-10-123"); + assert.isTrue(isCompileWithoutWatchCalled); + + isCompileWithoutWatchCalled = false; + await prepareWith(vite("9.2.0"), "9.2.0-pr.625-1a2b3c4"); + assert.isTrue(isCompileWithoutWatchCalled); + }); + + it("accepts a newer runtime", async () => { + await prepareWith(vite("9.2.0"), "9.3.1"); + + assert.isTrue(isCompileWithoutWatchCalled); + }); + + it("skips the check when a version cannot be compared", async () => { + await prepareWith(vite("9.2.0"), null); + assert.isTrue(isCompileWithoutWatchCalled); + + isCompileWithoutWatchCalled = false; + await prepareWith(vite("9.2.0"), "next"); + assert.isTrue(isCompileWithoutWatchCalled); + + isCompileWithoutWatchCalled = false; + await prepareWith(vite("latest"), "9.1.0"); + assert.isTrue(isCompileWithoutWatchCalled); + }); + }); }); diff --git a/test/services/bundler/bundler-compiler-service.ts b/test/services/bundler/bundler-compiler-service.ts index 671c9390c8..1a37b0170d 100644 --- a/test/services/bundler/bundler-compiler-service.ts +++ b/test/services/bundler/bundler-compiler-service.ts @@ -423,27 +423,33 @@ describe("BundlerCompilerService", () => { ); }); - it("hands over the token in two-process mode too", async () => { - const previous = process.env.NS_VITE_SINGLE_PROCESS; - process.env.NS_VITE_SINGLE_PROCESS = "0"; - try { - assert.equal( - ( - await (bundlerCompilerService).getViteChildEnv( - "ios", - { watch: true, hmr: true }, - { projectDir }, - ) - ).NS_HMR_TOKEN, - "token-ios", - ); - } finally { - if (previous === undefined) { - delete process.env.NS_VITE_SINGLE_PROCESS; - } else { - process.env.NS_VITE_SINGLE_PROCESS = previous; - } - } + it("hands over the token in single-process mode too", async () => { + fs.writeFileSync( + path.join( + projectDir, + "node_modules", + "@nativescript", + "vite", + "package.json", + ), + JSON.stringify({ + name: "@nativescript/vite", + nativescriptCli: { + singleProcessDev: ["ios"], + devServerFile: ["ios"], + }, + }), + ); + assert.equal( + ( + await (bundlerCompilerService).getViteChildEnv( + "ios", + { watch: true, hmr: true }, + { projectDir }, + ) + ).NS_HMR_TOKEN, + "token-ios", + ); }); it("passes no token for a platform the package does not list", async () => { @@ -1026,7 +1032,6 @@ describe("BundlerCompilerService", () => { describe("supportsViteSingleProcess", () => { let projectDir: string; - const originalEnv = process.env.NS_VITE_SINGLE_PROCESS; const writeVitePackage = (pkg: object) => { const dir = path.join( @@ -1048,16 +1053,10 @@ describe("BundlerCompilerService", () => { (bundlerCompilerService).getBundler = () => "vite"; testInjector.resolve("fs").readJson = (file: string) => JSON.parse(fs.readFileSync(file, "utf8")); - delete process.env.NS_VITE_SINGLE_PROCESS; }); afterEach(() => { fs.rmSync(projectDir, { recursive: true, force: true }); - if (originalEnv === undefined) { - delete process.env.NS_VITE_SINGLE_PROCESS; - } else { - process.env.NS_VITE_SINGLE_PROCESS = originalEnv; - } }); it("reads the platforms the installed @nativescript/vite declares", () => { @@ -1071,19 +1070,12 @@ describe("BundlerCompilerService", () => { assert.isFalse(supports("android")); }); - it("is off for a package that declares nothing, or when the user opts out", () => { + it("is off for a package that declares nothing", () => { writeVitePackage({ name: "@nativescript/vite" }); assert.isFalse(supports("ios")); - - writeVitePackage({ - name: "@nativescript/vite", - nativescriptCli: { singleProcessDev: ["ios"] }, - }); - process.env.NS_VITE_SINGLE_PROCESS = "0"; - assert.isFalse(supports("ios")); }); - it("reads the platforms whose devices use dev-server.json, opted out of single-process or not", () => { + it("reads the platforms whose devices use dev-server.json, apart from singleProcessDev", () => { const uses = (platform: string) => bundlerCompilerService.usesDevServerFile(platform, { projectDir, @@ -1101,7 +1093,6 @@ describe("BundlerCompilerService", () => { devServerFile: ["ios", "android"], }, }); - process.env.NS_VITE_SINGLE_PROCESS = "0"; assert.isTrue(uses("iOS")); assert.isTrue(uses("android")); assert.isFalse(uses("visionOS")); @@ -1120,6 +1111,87 @@ describe("BundlerCompilerService", () => { }); }); + describe("getViteMinRuntimeVersion", () => { + let projectDir: string; + + const writeVitePackage = (pkg: object) => { + const dir = path.join( + projectDir, + "node_modules", + "@nativescript", + "vite", + ); + fs.mkdirSync(dir, { recursive: true }); + fs.writeFileSync(path.join(dir, "package.json"), JSON.stringify(pkg)); + }; + const minRuntime = (platform: string) => + bundlerCompilerService.getViteMinRuntimeVersion(platform, { + projectDir, + }); + + beforeEach(() => { + projectDir = fs.mkdtempSync(path.join(os.tmpdir(), "ns-vite-mrv-")); + (bundlerCompilerService).getBundler = () => "vite"; + testInjector.resolve("fs").readJson = (file: string) => + JSON.parse(fs.readFileSync(file, "utf8")); + }); + + afterEach(() => { + fs.rmSync(projectDir, { recursive: true, force: true }); + }); + + it("is null for a package without minRuntimeVersion", () => { + writeVitePackage({ + name: "@nativescript/vite", + version: "2.0.0", + nativescriptCli: { singleProcessDev: ["ios"] }, + }); + + assert.isNull(minRuntime("ios")); + }); + + it("is null for a platform minRuntimeVersion does not list", () => { + writeVitePackage({ + name: "@nativescript/vite", + version: "2.0.0", + nativescriptCli: { minRuntimeVersion: { ios: "9.2.0" } }, + }); + + assert.isNull(minRuntime("android")); + }); + + it("reads the platform's minimum with the package's version", () => { + writeVitePackage({ + name: "@nativescript/vite", + version: "2.0.0", + nativescriptCli: { + minRuntimeVersion: { ios: "9.2.0", visionos: "9.3.0" }, + }, + }); + + assert.deepEqual(minRuntime("iOS"), { + bundlerPackageName: "@nativescript/vite", + bundlerVersion: "2.0.0", + minVersion: "9.2.0", + }); + assert.equal(minRuntime("visionos").minVersion, "9.3.0"); + }); + + it("is null when no @nativescript/vite is installed", () => { + assert.isNull(minRuntime("ios")); + }); + + it("never applies to webpack", () => { + writeVitePackage({ + name: "@nativescript/vite", + nativescriptCli: { minRuntimeVersion: { ios: "9.2.0" } }, + }); + (bundlerCompilerService).getBundler = () => "webpack"; + + assert.isNull(minRuntime("ios")); + }); + }); + describe("stopBundlerCompiler", () => { const platformData = { platformNameLowerCase: "ios",