sanity
Version:
Sanity is a real-time content infrastructure with a scalable, hosted backend featuring a Graph Oriented Query Language (GROQ), asset pipelines and fast edge caches
119 lines (116 loc) • 5.98 kB
JavaScript
;
var path = require("node:path"), telemetry = require("@sanity/telemetry"), chalk = require("chalk"), logSymbols = require("log-symbols"), rimraf = require("rimraf"), semver = require("semver"), previewServer = require("./previewServer.js");
require("./runtime.js");
var moduleFormatUtils = require("./moduleFormatUtils.js"), shouldAutoUpdate = require("./shouldAutoUpdate.js"), timing = require("./timing.js");
function _interopDefaultCompat(e) {
return e && typeof e == "object" && "default" in e ? e : { default: e };
}
var path__default = /* @__PURE__ */ _interopDefaultCompat(path), chalk__default = /* @__PURE__ */ _interopDefaultCompat(chalk), semver__default = /* @__PURE__ */ _interopDefaultCompat(semver);
const BuildTrace = telemetry.defineTrace({
name: "App Build Completed",
version: 0,
description: "An App build completed"
});
async function buildSanityApp(args, context, overrides) {
const timer = timing.getTimer(), {
output,
prompt,
workDir,
cliConfig,
telemetry: telemetry$1 = telemetry.noopLogger
} = context, flags = {
minify: !0,
stats: !1,
"source-maps": !1,
...args.extOptions
}, unattendedMode = !!(flags.yes || flags.y), defaultOutputDir = path__default.default.resolve(path__default.default.join(workDir, "dist")), outputDir = path__default.default.resolve(args.argsWithoutOptions[0] || defaultOutputDir), autoUpdatesEnabled = shouldAutoUpdate.shouldAutoUpdate({
flags,
cliConfig
}), installedSdkVersion = await shouldAutoUpdate.readModuleVersion(context.workDir, "@sanity/sdk-react"), installedSanityVersion = await shouldAutoUpdate.readModuleVersion(context.workDir, "sanity");
if (!installedSdkVersion)
throw new Error("Failed to find installed @sanity/sdk-react version");
const cleanSDKVersion = semver__default.default.parse(installedSanityVersion)?.version, cleanSanityVersion = semver__default.default.parse(installedSanityVersion)?.version;
if (autoUpdatesEnabled && !cleanSDKVersion)
throw new Error(`Failed to parse installed SDK version: ${installedSdkVersion}`);
const sdkVersion = encodeURIComponent(`^${cleanSDKVersion}`), sanityVersion = cleanSanityVersion && encodeURIComponent(`^${cleanSanityVersion}`), autoUpdatesImports = shouldAutoUpdate.getAppAutoUpdateImportMap({
sdkVersion,
sanityVersion
});
if (autoUpdatesEnabled) {
output.print(`${logSymbols.info} Building with auto-updates enabled`);
const result = await shouldAutoUpdate.compareDependencyVersions(autoUpdatesImports, workDir);
if (result?.length && !unattendedMode && !await prompt.single({
type: "confirm",
message: chalk__default.default.yellow(`The following local package versions are different from the versions currently served at runtime.
When using auto updates, we recommend that you test locally with the same versions before deploying.
${result.map((mod) => ` - ${mod.pkg} (local version: ${mod.installed}, runtime version: ${mod.remote})`).join(`
`)}
Continue anyway?`),
default: !1
}))
return process.exit(0);
}
const envVarKeys = getSanityEnvVars();
envVarKeys.length > 0 && (output.print(`
Including the following environment variables as part of the JavaScript bundle:`), envVarKeys.forEach((key) => output.print(`- ${key}`)), output.print(""));
let shouldClean = !0;
outputDir !== defaultOutputDir && !unattendedMode && (shouldClean = await prompt.single({
type: "confirm",
message: `Do you want to delete the existing directory (${outputDir}) first?`,
default: !0
}));
let basePath = "/";
const envBasePath = process.env.SANITY_APP_BASEPATH, configBasePath = cliConfig?.project?.basePath;
overrides?.basePath ? basePath = overrides.basePath : envBasePath ? basePath = envBasePath : configBasePath && (basePath = configBasePath), envBasePath && configBasePath && output.warn(`Overriding configured base path (${configBasePath}) with value from environment variable (${envBasePath})`);
let spin;
if (shouldClean) {
timer.start("cleanOutputFolder"), spin = output.spinner("Clean output folder").start(), await rimraf.rimraf(outputDir);
const cleanDuration = timer.end("cleanOutputFolder");
spin.text = `Clean output folder (${cleanDuration.toFixed()}ms)`, spin.succeed();
}
spin = output.spinner("Build Sanity application").start();
const trace = telemetry$1.trace(BuildTrace);
trace.start();
let importMap;
autoUpdatesEnabled && (importMap = {
imports: {
...await moduleFormatUtils.buildVendorDependencies({
cwd: workDir,
outputDir,
basePath
}),
...autoUpdatesImports
}
});
try {
timer.start("bundleStudio");
const bundle = await previewServer.buildStaticFiles({
cwd: workDir,
outputDir,
basePath,
sourceMap: !!flags["source-maps"],
minify: !!flags.minify,
vite: cliConfig && "vite" in cliConfig ? cliConfig.vite : void 0,
importMap,
reactCompiler: cliConfig && "reactCompiler" in cliConfig ? cliConfig.reactCompiler : void 0,
entry: cliConfig && "app" in cliConfig ? cliConfig.app?.entry : void 0,
isApp: !0
});
trace.log({
outputSize: bundle.chunks.flatMap((chunk) => chunk.modules.flatMap((mod) => mod.renderedLength)).reduce((sum, n) => sum + n, 0)
});
const buildDuration = timer.end("bundleStudio");
spin.text = `Build Sanity application (${buildDuration.toFixed()}ms)`, spin.succeed(), trace.complete(), flags.stats && (output.print(`
Largest module files:`), output.print(moduleFormatUtils.formatModuleSizes(moduleFormatUtils.sortModulesBySize(bundle.chunks).slice(0, 15))));
} catch (err) {
throw spin.fail(), trace.error(err), err;
}
return {
didCompile: !0
};
}
function getSanityEnvVars(env = process.env) {
return Object.keys(env).filter((key) => key.toUpperCase().startsWith("SANITY_APP_"));
}
exports.default = buildSanityApp;
//# sourceMappingURL=buildAction.js.map