Skip to content

Commit e5877ed

Browse files
committed
feat(vite): refuse runtimes older than the version @nativescript/vite requires
@nativescript/vite may declare `nativescriptCli.minRuntimeVersion` in its package.json, keyed by platform (e.g. `{ "ios": "9.2.0" }`). When it does, prepare (and so build, run, debug and deploy) fails before bundling if the project's installed runtime for that platform is older, naming the platform, both versions and the npm command that updates the runtime. Versions compare on major.minor.patch with prerelease tags dropped, so 9.2.0-next.* and PR builds of 9.2.0 satisfy a 9.2.0 minimum while 9.1.x prereleases do not. No field, no entry for the platform, or a version that cannot be parsed skips the check, leaving current packages unaffected. The check runs in PrepareController.prepareCore after the platform is added, since every command that bundles goes through it and the runtime version is known by then.
1 parent f66f555 commit e5877ed

5 files changed

Lines changed: 253 additions & 7 deletions

File tree

‎lib/controllers/prepare-controller.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import { watch, WatchOptions, FSWatcher } from "chokidar";
22
import { EventEmitter } from "events";
33
import * as _ from "lodash";
44
import * as path from "path";
5+
import * as semver from "semver";
56
import {
67
IAnalyticsService,
78
IDictionary,
9+
IErrors,
810
IFileSystem,
911
IHooksService,
1012
} from "../common/declarations";
@@ -68,6 +70,7 @@ export class PrepareController
6870
private $platformController: IPlatformController,
6971
public $hooksService: IHooksService,
7072
private $fs: IFileSystem,
73+
private $errors: IErrors,
7174
private $logger: ILogger,
7275
private $options: IOptions,
7376
private $mobileHelper: Mobile.IMobileHelper,
@@ -155,6 +158,7 @@ export class PrepareController
155158
projectData,
156159
);
157160

161+
this.ensureRuntimeMeetsBundlerMinimum(prepareData.platform, projectData);
158162
await this.trackRuntimeVersion(prepareData.platform, projectData);
159163

160164
this.$logger.info("Preparing project...");
@@ -640,6 +644,49 @@ export class PrepareController
640644
});
641645
}
642646

647+
/**
648+
* Fails when the installed runtime is older than the minimum the installed
649+
* bundler declares for `platform`. Versions compare on major.minor.patch
650+
* only, so prerelease and local builds of the minimum's release satisfy it.
651+
*/
652+
private ensureRuntimeMeetsBundlerMinimum(
653+
platform: string,
654+
projectData: IProjectData,
655+
): void {
656+
const requirement = this.$bundlerCompilerService.getViteMinRuntimeVersion(
657+
platform,
658+
projectData,
659+
);
660+
if (!requirement) {
661+
return;
662+
}
663+
664+
const runtime = this.$projectDataService.getRuntimePackage(
665+
projectData.projectDir,
666+
platform as SupportedPlatform,
667+
);
668+
const installed = semver.coerce(runtime?.version);
669+
const minimum = semver.coerce(requirement.minVersion);
670+
if (!installed || !minimum) {
671+
this.$logger.trace(
672+
`Skipping the ${requirement.bundlerPackageName} runtime check for ${platform}: cannot compare runtime version "${runtime?.version}" with minimum "${requirement.minVersion}".`,
673+
);
674+
return;
675+
}
676+
if (semver.gte(installed, minimum)) {
677+
return;
678+
}
679+
680+
const bundler = requirement.bundlerVersion
681+
? `${requirement.bundlerPackageName} ${requirement.bundlerVersion}`
682+
: requirement.bundlerPackageName;
683+
this.$errors.fail(
684+
`${bundler} requires ${runtime.name} ${minimum.version} or newer for ${platform.toLowerCase()}, ` +
685+
`but the project has ${runtime.name} ${runtime.version} installed. ` +
686+
`Update the runtime with "npm install --save-dev ${runtime.name}@^${minimum.version}" and run the command again.`,
687+
);
688+
}
689+
643690
private isFileWatcherPaused() {
644691
return this.pausedFileWatch;
645692
}

‎lib/services/bundler/bundler-compiler-service.ts‎

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,27 @@ export class BundlerCompilerService
823823
);
824824
}
825825

826+
public getViteMinRuntimeVersion(
827+
platform: string,
828+
projectData: IProjectData,
829+
): IBundlerRuntimeRequirement | null {
830+
if (this.getBundler() !== "vite") {
831+
return null;
832+
}
833+
const packageJSON = this.readBundlerPackageJSON(projectData);
834+
const minVersion =
835+
packageJSON?.nativescriptCli?.minRuntimeVersion?.[platform.toLowerCase()];
836+
if (typeof minVersion !== "string") {
837+
return null;
838+
}
839+
return {
840+
bundlerPackageName: this.getBundlerPackageName(),
841+
bundlerVersion:
842+
typeof packageJSON.version === "string" ? packageJSON.version : null,
843+
minVersion,
844+
};
845+
}
846+
826847
/**
827848
* The platforms the installed `@nativescript/vite` lists under
828849
* `nativescriptCli.<capability>` in its package.json.
@@ -831,19 +852,20 @@ export class BundlerCompilerService
831852
capability: "singleProcessDev" | "devServerFile",
832853
projectData: IProjectData,
833854
): string[] {
855+
const platforms =
856+
this.readBundlerPackageJSON(projectData)?.nativescriptCli?.[capability];
857+
return Array.isArray(platforms) ? platforms : [];
858+
}
859+
860+
private readBundlerPackageJSON(projectData: IProjectData): any {
834861
try {
835862
const packageJSONPath = resolvePackageJSONPath(
836863
this.getBundlerPackageName(),
837864
{ paths: [projectData.projectDir] },
838865
);
839-
if (!packageJSONPath) {
840-
return [];
841-
}
842-
const platforms =
843-
this.$fs.readJson(packageJSONPath)?.nativescriptCli?.[capability];
844-
return Array.isArray(platforms) ? platforms : [];
866+
return packageJSONPath ? this.$fs.readJson(packageJSONPath) : null;
845867
} catch {
846-
return [];
868+
return null;
847869
}
848870
}
849871

‎lib/services/bundler/bundler.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,23 @@ declare global {
3636
* `platform`, instead of baking one origin into the bundle.
3737
*/
3838
usesDevServerFile(platform: string, projectData: IProjectData): boolean;
39+
/**
40+
* The minimum runtime version the installed `@nativescript/vite`
41+
* declares for `platform` under `nativescriptCli.minRuntimeVersion`;
42+
* null when it declares none or the bundler is not vite.
43+
*/
44+
getViteMinRuntimeVersion(
45+
platform: string,
46+
projectData: IProjectData,
47+
): IBundlerRuntimeRequirement | null;
48+
}
49+
50+
interface IBundlerRuntimeRequirement {
51+
bundlerPackageName: string;
52+
/** Null when the bundler's package.json has no version. */
53+
bundlerVersion: string | null;
54+
/** As declared; may not be valid semver. */
55+
minVersion: string;
3956
}
4057

4158
/**

‎test/controllers/prepare-controller.ts‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ function createTestInjector(data: { hasNativeChanges: boolean }): IInjector {
4747
compileWithoutWatch: async () => {
4848
isCompileWithoutWatchCalled = true;
4949
},
50+
getViteMinRuntimeVersion: (): IBundlerRuntimeRequirement => null,
5051
});
5152

5253
injector.register("mobileHelper", MobileHelper);
@@ -256,4 +257,82 @@ describe("prepareController", () => {
256257
});
257258
});
258259
});
260+
261+
describe("bundler runtime minimum", () => {
262+
const prepareWith = async (
263+
requirement: IBundlerRuntimeRequirement,
264+
runtimeVersion: string,
265+
) => {
266+
const injector = createTestInjector({ hasNativeChanges: false });
267+
injector.resolve("bundlerCompilerService").getViteMinRuntimeVersion =
268+
() => requirement;
269+
injector.resolve("projectDataService").getRuntimePackage = () => ({
270+
name: "@nativescript/ios",
271+
version: runtimeVersion,
272+
});
273+
const prepareController: PrepareController =
274+
injector.resolve("prepareController");
275+
await prepareController.prepare({
276+
...prepareData,
277+
watch: false,
278+
platform: "ios",
279+
});
280+
};
281+
const vite = (minVersion: string): IBundlerRuntimeRequirement => ({
282+
bundlerPackageName: "@nativescript/vite",
283+
bundlerVersion: "2.0.0",
284+
minVersion,
285+
});
286+
287+
it("prepares when the bundler declares no minimum for the platform", async () => {
288+
await prepareWith(null, "8.0.0");
289+
290+
assert.isTrue(isCompileWithoutWatchCalled);
291+
});
292+
293+
it("stops before bundling when the runtime is older", async () => {
294+
await assert.isRejected(
295+
prepareWith(vite("9.2.0"), "9.1.3"),
296+
'@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.',
297+
);
298+
299+
assert.isFalse(isCompileWithoutWatchCalled);
300+
assert.isFalse(isNativePrepareCalled);
301+
});
302+
303+
it("rejects a prerelease of an older version", async () => {
304+
await assert.isRejected(
305+
prepareWith(vite("9.2.0"), "9.1.3-next.2026-12-01-100"),
306+
"requires @nativescript/ios 9.2.0 or newer",
307+
);
308+
});
309+
310+
it("accepts prerelease and PR builds of the minimum's version", async () => {
311+
await prepareWith(vite("9.2.0"), "9.2.0-next.2026-12-10-123");
312+
assert.isTrue(isCompileWithoutWatchCalled);
313+
314+
isCompileWithoutWatchCalled = false;
315+
await prepareWith(vite("9.2.0"), "9.2.0-pr.625-1a2b3c4");
316+
assert.isTrue(isCompileWithoutWatchCalled);
317+
});
318+
319+
it("accepts a newer runtime", async () => {
320+
await prepareWith(vite("9.2.0"), "9.3.1");
321+
322+
assert.isTrue(isCompileWithoutWatchCalled);
323+
});
324+
325+
it("skips the check when a version cannot be compared", async () => {
326+
await prepareWith(vite("9.2.0"), null);
327+
assert.isTrue(isCompileWithoutWatchCalled);
328+
329+
isCompileWithoutWatchCalled = false;
330+
await prepareWith(vite("9.2.0"), "next");
331+
assert.isTrue(isCompileWithoutWatchCalled);
332+
333+
isCompileWithoutWatchCalled = false;
334+
await prepareWith(vite("latest"), "9.1.0");
335+
assert.isTrue(isCompileWithoutWatchCalled);
336+
});
337+
});
259338
});

‎test/services/bundler/bundler-compiler-service.ts‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,87 @@ describe("BundlerCompilerService", () => {
11111111
});
11121112
});
11131113

1114+
describe("getViteMinRuntimeVersion", () => {
1115+
let projectDir: string;
1116+
1117+
const writeVitePackage = (pkg: object) => {
1118+
const dir = path.join(
1119+
projectDir,
1120+
"node_modules",
1121+
"@nativescript",
1122+
"vite",
1123+
);
1124+
fs.mkdirSync(dir, { recursive: true });
1125+
fs.writeFileSync(path.join(dir, "package.json"), JSON.stringify(pkg));
1126+
};
1127+
const minRuntime = (platform: string) =>
1128+
bundlerCompilerService.getViteMinRuntimeVersion(platform, <any>{
1129+
projectDir,
1130+
});
1131+
1132+
beforeEach(() => {
1133+
projectDir = fs.mkdtempSync(path.join(os.tmpdir(), "ns-vite-mrv-"));
1134+
(<any>bundlerCompilerService).getBundler = () => "vite";
1135+
testInjector.resolve("fs").readJson = (file: string) =>
1136+
JSON.parse(fs.readFileSync(file, "utf8"));
1137+
});
1138+
1139+
afterEach(() => {
1140+
fs.rmSync(projectDir, { recursive: true, force: true });
1141+
});
1142+
1143+
it("is null for a package without minRuntimeVersion", () => {
1144+
writeVitePackage({
1145+
name: "@nativescript/vite",
1146+
version: "2.0.0",
1147+
nativescriptCli: { singleProcessDev: ["ios"] },
1148+
});
1149+
1150+
assert.isNull(minRuntime("ios"));
1151+
});
1152+
1153+
it("is null for a platform minRuntimeVersion does not list", () => {
1154+
writeVitePackage({
1155+
name: "@nativescript/vite",
1156+
version: "2.0.0",
1157+
nativescriptCli: { minRuntimeVersion: { ios: "9.2.0" } },
1158+
});
1159+
1160+
assert.isNull(minRuntime("android"));
1161+
});
1162+
1163+
it("reads the platform's minimum with the package's version", () => {
1164+
writeVitePackage({
1165+
name: "@nativescript/vite",
1166+
version: "2.0.0",
1167+
nativescriptCli: {
1168+
minRuntimeVersion: { ios: "9.2.0", visionos: "9.3.0" },
1169+
},
1170+
});
1171+
1172+
assert.deepEqual(minRuntime("iOS"), {
1173+
bundlerPackageName: "@nativescript/vite",
1174+
bundlerVersion: "2.0.0",
1175+
minVersion: "9.2.0",
1176+
});
1177+
assert.equal(minRuntime("visionos").minVersion, "9.3.0");
1178+
});
1179+
1180+
it("is null when no @nativescript/vite is installed", () => {
1181+
assert.isNull(minRuntime("ios"));
1182+
});
1183+
1184+
it("never applies to webpack", () => {
1185+
writeVitePackage({
1186+
name: "@nativescript/vite",
1187+
nativescriptCli: { minRuntimeVersion: { ios: "9.2.0" } },
1188+
});
1189+
(<any>bundlerCompilerService).getBundler = () => "webpack";
1190+
1191+
assert.isNull(minRuntime("ios"));
1192+
});
1193+
});
1194+
11141195
describe("stopBundlerCompiler", () => {
11151196
const platformData = <any>{
11161197
platformNameLowerCase: "ios",

0 commit comments

Comments
 (0)