UNPKG

vike

Version:

The Framework *You* Control - Next.js & Nuxt alternative for unprecedented flexibility and dependability.

88 lines (87 loc) 3.67 kB
import './assertEnvApi.js'; export { dev }; // TO-DO/eventually: remove if it doesn't end up being used export { startupLog }; import { prepareViteApiCall } from './prepareViteApiCall.js'; import { createServer } from 'vite'; import { assert } from '../../utils/assert.js'; import { assertIsNotProductionRuntime } from '../../utils/assertSetup.js'; import { colorVike } from '../../utils/colorsClient.js'; import { colorVite } from '../../utils/colorsServer.js'; import { PROJECT_VERSION } from '../../utils/PROJECT_VERSION.js'; import pc from '@brillout/picocolors'; import { processStartupLog } from '../vite/shared/loggerVite.js'; assertIsNotProductionRuntime(); /** * Programmatically trigger `$ vike dev` * * https://vike.dev/api#dev */ async function dev(options = {}) { const { viteConfigFromUserResolved } = await prepareViteApiCall(options, 'dev'); const server = await createServer(viteConfigFromUserResolved); const viteServer = server; const viteConfig = server.config; const viteVersion = viteConfig._viteVersionResolved; assert(viteVersion); if (viteServer.httpServer) await viteServer.listen(); if (options.startupLog) { if (viteServer.resolvedUrls) { startupLog(viteServer.resolvedUrls, viteServer); } else { // TO-DO/eventually: remove if it doesn't end up being used ; viteConfig.server.startupLog = (resolvedUrls) => startupLog(resolvedUrls, viteServer); } } return { viteServer, viteConfig, viteVersion, }; } const startTime = performance.now(); async function startupLog(resolvedUrls, viteServer) { const viteConfig = viteServer.config; const viteVersion = viteConfig._viteVersionResolved; assert(viteVersion); const startupDurationString = pc.dim(`ready in ${pc.reset(pc.bold(String(Math.ceil(performance.now() - startTime))))} ms`); const sep = pc.dim('·'); const firstLine = `\n ${colorVike('Vike')} ${pc.yellow(`v${PROJECT_VERSION}`)} ${sep} ${colorVite('Vite')} ${pc.cyan(`v${viteVersion}`)} ${sep} ${startupDurationString}\n`; const ret = processStartupLog(firstLine, viteConfig); console.log(ret.firstLine); const { isCompact } = ret; // We don't call viteServer.printUrls() because Vite throws an error if `resolvedUrls` is missing: // https://github.com/vitejs/vite/blob/df5a30d2690a2ebc4824a79becdcef30538dc602/packages/vite/src/node/server/index.ts#L745 printServerUrls(resolvedUrls, viteConfig.server.host); viteServer.bindCLIShortcuts({ print: true }); if (!isCompact) console.log(); } // Copied & adapted from Vite // https://github.com/vitejs/vite/blob/df5a30d2690a2ebc4824a79becdcef30538dc602/packages/vite/src/node/logger.ts#L168-L188 function printServerUrls(urls, optionsHost) { // [Begin] interop const colors = pc; const info = (msg) => console.log(msg); // [End] interop const colorUrl = (url) => colors.underline(removeTrailingSlash(url)); for (const url of urls.local) { info(` ${colors.green('➜')} ${colors.bold('Local')}: ${colorUrl(url)}`); } for (const url of urls.network) { info(` ${colors.green('➜')} ${colors.bold('Network')}: ${colorUrl(url)}`); } if (urls.network.length === 0 && optionsHost === undefined) { info(colors.dim(` ${colors.green('➜')} ${colors.bold('Network')}: use `) + colors.bold('--host') + colors.dim(' to expose')); } } function removeTrailingSlash(url) { if (url.endsWith('/')) return url.slice(0, -1); // remove trailing slash return url; }