UNPKG

vike

Version:

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

191 lines (187 loc) 8.75 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getVirtualFileEntry = getVirtualFileEntry; const utils_js_1 = require("../../utils.js"); const virtualFileEntry_js_1 = require("../../../shared/virtualFiles/virtualFileEntry.js"); const vite_1 = require("vite"); const fileTypes_js_1 = require("../../../../shared/getPageFiles/fileTypes.js"); const node_path_1 = __importDefault(require("node:path")); const getVirtualFilePageConfigsEager_js_1 = require("./getVirtualFilePageConfigsEager.js"); const resolveVikeConfigInternal_js_1 = require("../../shared/resolveVikeConfigInternal.js"); const getOutDirs_js_1 = require("../../shared/getOutDirs.js"); const isViteServerBuild_js_1 = require("../../shared/isViteServerBuild.js"); const getPageAssets_js_1 = require("../../../runtime/renderPage/getPageAssets.js"); async function getVirtualFileEntry(id, options, config, isDev) { const idParsed = (0, virtualFileEntry_js_1.isVirtualFileIdEntry)(id); (0, utils_js_1.assert)(idParsed); const { isForClientSide, isClientRouting } = idParsed; (0, utils_js_1.assert)(isForClientSide === !(0, isViteServerBuild_js_1.isViteServerBuild_options)(options)); const code = await getCode(config, isForClientSide, isClientRouting, isDev, id); return code; } async function getCode(config, isForClientSide, isClientRouting, isDev, id) { const { command } = config; (0, utils_js_1.assert)(command === 'serve' || command === 'build'); const isBuild = command === 'build'; (0, utils_js_1.assert)(isDev === !isBuild); let content = ''; { const globRoots = getGlobRoots(config); (0, utils_js_1.debugGlob)('Glob roots: ', globRoots); content += await generateGlobImports(globRoots, isBuild, isForClientSide, isClientRouting, isDev, id); } (0, utils_js_1.debugGlob)(`Glob imports for ${isForClientSide ? 'client' : 'server'}:\n`, content); return content; } function determineInjection({ fileType, isForClientSide, isClientRouting, isPrerendering, isBuild, }) { if (!isForClientSide) { return { includeImport: fileType === '.page.server' || fileType === '.page' || fileType === '.page.route', includeExportNames: isPrerendering && isBuild ? fileType === '.page.client' || fileType === '.page.server' || fileType === '.page' // We extensively use `PageFile['exportNames']` while pre-rendering, in order to avoid loading page files unnecessarily, and therefore reducing memory usage. : fileType === '.page.client', }; } else { const includeImport = fileType === '.page.client' || fileType === '.css' || fileType === '.page'; if (!isClientRouting) { return { includeImport, includeExportNames: false, }; } else { return { includeImport: includeImport || fileType === '.page.route', includeExportNames: fileType === '.page.client' || fileType === '.page.server' || fileType === '.page', }; } } } async function generateGlobImports(globRoots, isBuild, isForClientSide, isClientRouting, isDev, id) { let fileContent = `// Generated by Vike export const pageFilesLazy = {}; export const pageFilesEager = {}; export const pageFilesExportNamesLazy = {}; export const pageFilesExportNamesEager = {}; export const pageFilesList = []; export const neverLoaded = {}; ${await (0, getVirtualFilePageConfigsEager_js_1.getVirtualFilePageConfigsEager)(isForClientSide, isDev, id, isClientRouting)} `; // We still use import.meta.glob() when using th V1 design in order to not break the V1 design deprecation warning const isV1Design = (0, resolveVikeConfigInternal_js_1.isV1Design)(); const vikeConfig = await (0, resolveVikeConfigInternal_js_1.getVikeConfigInternal)(); // Old design => no + files => only to enable pre-rendering is setting `vike({prerender})` in vite.config.js const isPrerendering = !!vikeConfig.config.prerender; fileTypes_js_1.fileTypes .filter((fileType) => fileType !== '.css') .forEach((fileType) => { const { includeImport, includeExportNames } = determineInjection({ fileType, isForClientSide, isClientRouting, isPrerendering, isBuild, }); if (includeImport) { fileContent += getGlobs(globRoots, isBuild, fileType, null, isV1Design); } if (includeExportNames) { fileContent += getGlobs(globRoots, isBuild, fileType, 'extractExportNames', isV1Design); } }); const includeAssetsImportedByServer = (0, getPageAssets_js_1.resolveIncludeAssetsImportedByServer)(vikeConfig.config); if (includeAssetsImportedByServer && isForClientSide) { fileContent += getGlobs(globRoots, isBuild, '.page.server', 'extractAssets', isV1Design); } return fileContent; } function getGlobs(globRoots, isBuild, fileType, query, isV1Design) { const isEager = isBuild && (query === 'extractExportNames' || fileType === '.page.route'); let pageFilesVar; if (query === 'extractExportNames') { if (!isEager) { pageFilesVar = 'pageFilesExportNamesLazy'; } else { pageFilesVar = 'pageFilesExportNamesEager'; } } else if (query === 'extractAssets') { (0, utils_js_1.assert)(!isEager); pageFilesVar = 'neverLoaded'; } else if (!query) { if (!isEager) { pageFilesVar = 'pageFilesLazy'; } else { // Used for `.page.route.js` files pageFilesVar = 'pageFilesEager'; } } else { (0, utils_js_1.assert)(false); } const varNameSuffix = (fileType === '.page' && 'Isomorph') || (fileType === '.page.client' && 'Client') || (fileType === '.page.server' && 'Server') || (fileType === '.page.route' && 'Route'); (0, utils_js_1.assert)(varNameSuffix); const varName = `${pageFilesVar}${varNameSuffix}`; const varNameLocals = []; return [ ...globRoots.map((globRoot, i) => { const varNameLocal = `${varName}${i + 1}`; varNameLocals.push(varNameLocal); const globIncludePath = `'${getGlobPath(globRoot.includeDir, fileType)}'`; const globExcludePath = globRoot.excludeDir ? `'!${getGlobPath(globRoot.excludeDir, fileType)}'` : null; const globOptions = { eager: isEager }; if (query) { const isNewViteInterface = (0, utils_js_1.isVersionOrAbove)(vite_1.version, '5.1.0'); if (isNewViteInterface && // When used for the old design, the new syntax breaks Vike's CI (surprinsigly so). I couldn't reproduce locally (I didn't dig much). isV1Design) { globOptions.query = `?${query}`; } else { globOptions.as = query; const msg = [ "Update to the new V1 design to get rid of Vite's warning:", 'The glob option "as" has been deprecated in favour of "query".', 'See https://vike.dev/migration/v1-design for how to migrate.', ].join(' '); (0, utils_js_1.assertWarning)(!isNewViteInterface, msg, { onlyOnce: true }); } } const globPaths = globExcludePath ? `[${globIncludePath}, ${globExcludePath}]` : `[${globIncludePath}]`; const globLine = `const ${varNameLocal} = import.meta.glob(${globPaths}, ${JSON.stringify(globOptions)});`; return globLine; }), `const ${varName} = {${varNameLocals.map((varNameLocal) => `...${varNameLocal}`).join(',')}};`, `${pageFilesVar}['${fileType}'] = ${varName};`, '', ].join('\n'); } function getGlobRoots(config) { const globRoots = [ { includeDir: '/', excludeDir: node_path_1.default.posix.relative(config.root, (0, getOutDirs_js_1.getOutDirs)(config).outDirRoot), }, ]; return globRoots; } function getGlobPath(globRootDir, fileType) { (0, utils_js_1.assertPosixPath)(globRootDir); let globPath = [...globRootDir.split('/'), '**', `*${fileType}.${utils_js_1.scriptFileExtensionPattern}`] .filter(Boolean) .join('/'); if (!globPath.startsWith('/')) { globPath = '/' + globPath; } return globPath; }