@nuxt/cli
Version:
Nuxt CLI
236 lines (235 loc) • 9.21 kB
JavaScript
import { h as logger } from "./startup-clock-SyYej6uN.mjs";
import { t as NUXT_PACKAGES } from "./nuxt-packages-DI1WwngW.mjs";
import { i as getProjectPkgJSON, n as getPkgJSON, r as getPkgVersion, t as getNuxtPkgVersion } from "./pkg-D70J0kr6.mjs";
import { t as findPnpmWorkspaceYaml } from "./workspace-9wVsabPJ.mjs";
import { n as getServerBuilderName } from "./server-build-DwN4T0qt.mjs";
import { dirname } from "node:path";
import { styleText } from "node:util";
import { coerce, compare } from "verkit";
// src/utils/nitro.ts
/**
* Names Nitro is installed under, newest naming first, so a manifest that
* declares more than one resolves to the current name.
*
* Nightly releases are aliased onto these names (for example
* `"nitropack": "npm:nitropack-nightly@latest"`), so they are not separate keys.
*/
const NITRO_PKGS = ["nitro", "nitropack"];
/**
* The package that declares Nitro in Nuxt versions where `nuxt` itself does
* not. Only consulted when the installed Nuxt declares it as a dependency, so
* a copy installed outside the Nuxt dependency chain cannot shadow the Nitro
* version Nuxt actually uses.
*/
const NITRO_SERVER_PKG = "@nuxt/nitro-server";
/** The Nitro package that provides each Nitro major. */
const NITRO_MAJOR_PKGS = {
2: "nitropack",
3: "nitro"
};
/** The first Nuxt minor to resolve `_nitroMajor`. */
const NITRO_MAJOR_NUXT_VERSION = "4.7.0";
/** The first Nitro release whose dev server proxies HTTP/2 requests to its worker. */
const HTTP2_NITRO_VERSION = "2.13.3";
/**
* The Nitro packages to look for.
*
* Without a `nitroMajor`, the package the installed Nuxt depends on comes first,
* with the plain names kept as later candidates so Nitro is still found when no
* owning manifest can be read (`exports` withholding `package.json`, or Nitro
* installed without Nuxt).
*/
function getNitroCandidates(cwd, nitroMajor) {
const declared = getNuxtNitroDependency(cwd);
if (nitroMajor) {
const name = NITRO_MAJOR_PKGS[nitroMajor];
const chain = declared?.name === name ? [declared] : [];
const plain = [{ name }];
return manifestDeclaresPkg(getProjectPkgJSON(cwd), name) ? [...plain, ...chain] : [...chain, ...plain];
}
const candidates = NITRO_PKGS.map((name) => ({ name }));
return declared ? [declared, ...candidates] : candidates;
}
/** Whether a manifest declares `name` in its dependencies or devDependencies. */
function manifestDeclaresPkg(pkg, name) {
const { dependencies, devDependencies } = pkg ?? {};
return name in (dependencies ?? {}) || name in (devDependencies ?? {});
}
/**
* The Nitro major selected by the project: the project's own dependencies
* first (`nitro` winning when both are listed), then the workspace root when
* it declares exactly one, or `undefined` when neither does.
*/
function getProjectNitroMajor(cwd) {
const major = getNitroMajorFromManifest(getProjectPkgJSON(cwd));
if (major) return major;
const workspaceYaml = findPnpmWorkspaceYaml(cwd);
const workspaceDir = workspaceYaml && dirname(workspaceYaml);
if (!workspaceDir || workspaceDir === cwd) return;
const { dependencies, devDependencies } = getProjectPkgJSON(workspaceDir) ?? {};
const deps = {
...dependencies,
...devDependencies
};
const hasNitro = "nitro" in deps;
if (hasNitro === "nitropack" in deps) return;
return hasNitro ? 3 : 2;
}
/** The Nitro major a manifest's `dependencies` and `devDependencies` select. */
function getNitroMajorFromManifest(pkg) {
const { dependencies, devDependencies } = pkg ?? {};
const deps = {
...dependencies,
...devDependencies
};
if ("nitro" in deps) return 3;
if ("nitropack" in deps) return 2;
}
/** An internal `_nitroMajor` option, allowing only the values Nuxt resolves. */
function readNitroMajor(config) {
const { _nitroMajor } = config ?? {};
return _nitroMajor === 2 || _nitroMajor === 3 ? _nitroMajor : void 0;
}
/** Whether the installed Nuxt resolves the `_nitroMajor` option. */
function supportsNitroMajor(cwd) {
const version = coerce(getNuxtPkgVersion(cwd));
return !!version && compare(version, NITRO_MAJOR_NUXT_VERSION) >= 0;
}
/**
* The Nitro major to report, from an explicit value or the project's selection
* once the installed Nuxt resolves the `_nitroMajor` option.
*/
function resolveNitroMajor(cwd, fromNuxt) {
if (fromNuxt !== void 0) return fromNuxt;
return supportsNitroMajor(cwd) ? getProjectNitroMajor(cwd) : void 0;
}
/**
* The Nitro package the installed Nuxt depends on, directly or through
* `@nuxt/nitro-server`, or `undefined` when no owning manifest can be read.
*/
function getNuxtNitroDependency(cwd) {
const readManifest = (name, via) => getPkgJSON(cwd, name, {
via,
strict: true
});
for (const owner of NUXT_PACKAGES) {
const manifest = readManifest(owner);
if (!manifest) continue;
const name = findNitroPkgName(manifest);
if (name) return {
name,
via: [owner]
};
if (manifest.dependencies?.[NITRO_SERVER_PKG]) {
const server = findNitroPkgName(readManifest(NITRO_SERVER_PKG, [owner]));
if (server) return {
name: server,
via: [owner, NITRO_SERVER_PKG]
};
}
}
}
/** The name of the Nitro package a manifest depends on. */
function findNitroPkgName(manifest) {
const deps = manifest?.dependencies;
return deps && NITRO_PKGS.find((name) => name in deps);
}
/** The version of the first candidate installed in the project. */
function getInstalledVersion(cwd, candidates) {
for (const { name, via } of candidates) {
const version = getPkgVersion(cwd, name, via && {
via,
strict: true
});
if (version) return version;
}
return "";
}
/**
* The version of Nitro the installed Nuxt depends on, or of the package
* providing `nitroMajor` when it is given.
*/
function getNitroVersion(cwd, nitroMajor) {
return getInstalledVersion(cwd, getNitroCandidates(cwd, nitroMajor));
}
/**
* Whether the project's Nitro can sit behind an HTTP/2 dev server, which is
* assumed when no Nitro version can be read.
*/
function supportsHttp2(cwd) {
const version = coerce(getNitroVersion(cwd, resolveNitroMajor(cwd)));
return !version || compare(version, HTTP2_NITRO_VERSION) >= 0;
}
/**
* The version of Nitro the installed Nuxt depends on, falling back to the
* version `getDepVersion` reports when no Nitro package can be resolved from
* the project (for example when it is only declared in a catalog).
*/
async function resolveNitroVersion(cwd, getDepVersion, nitroMajor) {
const candidates = getNitroCandidates(cwd, nitroMajor);
const version = getInstalledVersion(cwd, candidates);
if (version) return version;
for (const { name } of candidates) {
const declared = await getDepVersion(name);
if (declared) return declared;
}
}
// src/utils/banner.ts
function getBuilder(cwd, builder) {
switch (builder) {
case "rspack":
case "@nuxt/rspack-builder": return {
name: "Rspack",
version: getPkgVersion(cwd, "@rspack/core", { via: ["@nuxt/rspack-builder"] })
};
case "webpack":
case "@nuxt/webpack-builder": return {
name: "Webpack",
version: getPkgVersion(cwd, "webpack", { via: ["@nuxt/webpack-builder"] })
};
default: {
const pkgJSON = getPkgJSON(cwd, "vite", { via: ["nuxt", "@nuxt/vite-builder"] });
const isRolldown = pkgJSON?.name.includes("rolldown");
const isVitePlus = pkgJSON?.name === "@voidzero-dev/vite-plus-core";
return {
name: isRolldown ? "Rolldown-Vite" : "Vite",
version: (isVitePlus ? pkgJSON?.bundledVersions?.vite : pkgJSON?.version) || "unknown",
provider: isVitePlus ? {
name: "Vite+",
version: pkgJSON?.version || "unknown"
} : void 0
};
}
}
}
function showBanner(nuxt) {
logger.info(describeStack(nuxt.options.rootDir, {
nuxtVersion: nuxt._version,
builder: nuxt.options.builder,
nitroMajor: readNitroMajor(nuxt.options),
withNitro: getServerBuilderName(nuxt) === "Nitro"
}));
}
/** The versions of the framework packages in use, for the banner and the dev UI. */
function resolveStackVersions(cwd, options = {}) {
return {
nuxt: options.nuxtVersion || getNuxtPkgVersion(cwd),
nitro: getNitroVersion(cwd, options.nitroMajor),
builder: getBuilder(cwd, options.builder),
vue: getPkgVersion(cwd, "vue", { via: ["nuxt"] }) || null
};
}
/** The `Nuxt x (with Nitro y, Vite z and Vue w)` line shown at startup. */
function describeStack(cwd, options = {}) {
const { nuxt: nuxtVersion, nitro, builder, vue: vueVersion } = resolveStackVersions(cwd, options);
const nitroVersion = options.withNitro === false ? void 0 : nitro;
const builderPart = `${builder.name} ${styleText("bold", builder.version)}${builder.provider ? ` via ${builder.provider.name} ${styleText("bold", builder.provider.version)}` : ""}`;
const parts = [
nitroVersion ? `Nitro ${styleText("bold", nitroVersion)}` : null,
builderPart,
vueVersion ? `Vue ${styleText("bold", vueVersion)}` : null
].filter((part) => !!part);
const detail = parts.length > 1 ? `${parts.slice(0, -1).join(", ")} and ${parts.at(-1)}` : parts[0];
return styleText("green", `Nuxt ${styleText("bold", nuxtVersion)}`) + styleText("gray", ` (with ${detail})`);
}
export { resolveNitroMajor as a, readNitroMajor as i, resolveStackVersions as n, resolveNitroVersion as o, showBanner as r, supportsHttp2 as s, getBuilder as t };