From 5e6537752587093cc93554d19854ca0359bedc66 Mon Sep 17 00:00:00 2001 From: Romuald Brillout Date: Tue, 30 Jan 2024 20:14:52 +0100 Subject: [PATCH 1/8] feat: vike.config.js --- docs/pages/base-url/+Page.mdx | 22 +-- docs/pages/disableAutoFullBuild/+Page.mdx | 10 +- .../includeAssetsImportedByServer/+Page.mdx | 10 +- docs/pages/pre-rendering/+Page.mdx | 12 +- .../prerender-programmatic.page.server.mdx | 95 ++++++++++++ docs/pages/prerender/+Page.mdx | 42 ++---- docs/pages/redirects/+Page.mdx | 26 ++-- docs/pages/url-normalization/+Page.mdx | 20 +-- examples/react-full/renderer/+config.ts | 11 +- examples/react-full/vike.config.ts | 14 ++ examples/react-full/vite.config.ts | 8 +- test-e2e.config.mjs | 4 + vike/node/plugin/plugins/config/index.ts | 18 ++- .../plugin/plugins/importUserCode/index.ts | 4 +- .../importUserCode/v1-design/getVikeConfig.ts | 138 +++++++++++------- .../getVikeConfig/loadFileAtConfigTime.ts | 25 +++- vike/shared/ConfigVike.ts | 61 ++------ vike/shared/page-configs/Config.ts | 115 ++++++++++++++- vike/types/index.ts | 1 + vike/utils/findFile.ts | 1 + 20 files changed, 418 insertions(+), 219 deletions(-) create mode 100644 docs/pages/prerender-programmatic.page.server.mdx create mode 100644 examples/react-full/vike.config.ts diff --git a/docs/pages/base-url/+Page.mdx b/docs/pages/base-url/+Page.mdx index 4f5879c721..67ca9d7ab3 100644 --- a/docs/pages/base-url/+Page.mdx +++ b/docs/pages/base-url/+Page.mdx @@ -50,16 +50,10 @@ We can change the Base URL *only* of our assets by using the `baseAssets` config > The most common use case for using `baseAssets` is when deploying assets to a CDN. ```js -// vite.config.js - -import vike from 'vike/plugin' +// vike.config.js export default { - plugins: [ - vike({ - baseAssets: 'https://cdn.example.org/my-website-assets/' - }) - ] + baseAssets: 'https://cdn.example.org/my-website-assets/' } ``` @@ -106,17 +100,11 @@ We can do both: - Change the Base URL of our server using the `baseServer` configuration. ```js -// vite.config.js - -import vike from 'vike/plugin' +// vike.config.js export default { - plugins: [ - vike({ - baseAssets: 'https://cdn.example.org/my-website-assets/', - baseServer: '/some-base/' - }) - ] + baseAssets: 'https://cdn.example.org/my-website-assets/', + baseServer: '/some-base/' } ``` diff --git a/docs/pages/disableAutoFullBuild/+Page.mdx b/docs/pages/disableAutoFullBuild/+Page.mdx index d3b6714a2b..e26444d1a9 100644 --- a/docs/pages/disableAutoFullBuild/+Page.mdx +++ b/docs/pages/disableAutoFullBuild/+Page.mdx @@ -3,16 +3,10 @@ import { Link } from '@brillout/docpress' Set to `true` to disable the automatic chaining of all build steps. ```js -// vite.config.js - -import vike from 'vike/plugin' +// vike.config.js export default { - plugins: [ - vike({ - disableAutoFullBuild: true - }) - ] + disableAutoFullBuild: true } ``` diff --git a/docs/pages/includeAssetsImportedByServer/+Page.mdx b/docs/pages/includeAssetsImportedByServer/+Page.mdx index b49405cc51..42b413a812 100644 --- a/docs/pages/includeAssetsImportedByServer/+Page.mdx +++ b/docs/pages/includeAssetsImportedByServer/+Page.mdx @@ -7,15 +7,9 @@ When `includeAssetsImportedByServer` is `true`, then static assets (CSS, images, It's `true` by default and, in principle, there isn't any reason to disable it. You can however disable it: ```js -// vite.config.js - -import vike from 'vike/plugin' +// vike.config.js export default { - plugins: [ - vike({ - includeAssetsImportedByServer: false - }) - ] + includeAssetsImportedByServer: false } ``` diff --git a/docs/pages/pre-rendering/+Page.mdx b/docs/pages/pre-rendering/+Page.mdx index 1de76ce2d5..f52950d931 100644 --- a/docs/pages/pre-rendering/+Page.mdx +++ b/docs/pages/pre-rendering/+Page.mdx @@ -34,14 +34,10 @@ Pre-rendering can be used for websites with content that changes only occasional To opt into pre-rendering: ```js -// vite.config.js - -import vike from 'vike/plugin' +// vike.config.js export default { - plugins: [ - vike({ prerender: true }) - ] + prerender: true } ``` @@ -60,13 +56,13 @@ If you pre-render all your pages, then you can use Vite's CLI instead of a serve You can programmatically invoke the pre-rendering process, see . React Example: - - (see setting prerender option to `true`) + - (see setting prerender option to `true`) - - - (see Vite CLI usage) Vue Example: - - (see setting prerender option to `true`) + - (see setting prerender option to `true`) - - - (see Vite CLI usage) diff --git a/docs/pages/prerender-programmatic.page.server.mdx b/docs/pages/prerender-programmatic.page.server.mdx new file mode 100644 index 0000000000..9a1e5d26fe --- /dev/null +++ b/docs/pages/prerender-programmatic.page.server.mdx @@ -0,0 +1,95 @@ +import { Warning, Link } from '@brillout/docpress' + +> See for an overview of what pre-rendering is and how to enable it. + +Pre-rendering is automatically initiated when running `$ vite build`. (If .) + +Instead, we can programmatically initiate pre-rendering: + +```js +// vike.config.js + +export default { + prerender: { + // Stop `$ vite build` from initiating pre-rendering + disableAutoRun: true + } +} +``` + +```js +// my-custom-build-script.js + +import { prerender } from 'vike/prerender' + +// Without options +prerender() + +// All options are optional +prerender({ + pageContextInit, + viteConfig, + onPagePrerender +}) +``` + + +## `pageContextInit` + +The initial value of each page's `pageContext` object. + +```js +prerender({ + pageContextInit: { + someData: 42, + // ... + } +}) +``` + +## `viteConfig` + +The Vite config. + +It's optional: if omitted, Vite automatically loads our `vite.config.js`. + +We recommend to either omit this option or set it to `prerender({ viteConfig: { root }})`: the `vite.config.js` file living at `root` will be loaded. + +Alternatively you can: + - Set: + ```js + prerender({ + viteConfig: { + configFile: require.resolve('./path/to/vite.config.js') + } + }) + ``` + - Not load any `vite.config.js` file and, instead, use `prerender({ viteConfig: { configFile: false, ...myViteConfig }})` to programmatically define the entire Vite config. + +You can also load a `vite.config.js` file while overriding parts of the Vite config. + +See [Vite > JavaScript API > `InlineConfig`](https://vitejs.dev/guide/api-javascript.html#inlineconfig). + + +## `onPagePrerender()` + +DON'T use this option without having contacted a Vike maintainer: this functionality may be completely changed/removed at any time if you don't. + +This feature is experimental and may be changed in any minor version update. + +The `onPagePrerender` option allows us to override where the HTML is written. + +```js +prerender({ + // If we provide `onPagePrerender` then Vike won't write the HTML to the filesystem + onPagePrerender(pageContext) { + // TODO: write the HTML to the filesystem. + // ... + } +}) +``` + + +## More configs + +For more configurations, see the global config at . diff --git a/docs/pages/prerender/+Page.mdx b/docs/pages/prerender/+Page.mdx index 5afdab0fcd..630e2d2304 100644 --- a/docs/pages/prerender/+Page.mdx +++ b/docs/pages/prerender/+Page.mdx @@ -6,24 +6,18 @@ import { TextType } from '../settings/TextType' By default, pre-rendering is disabled. To enable it: ```js -// vite.config.js - -import vike from 'vike/plugin' +// vike.config.js export default { - plugins: [ - vike({ - // Use default settings: - prerender: true, - // Or set settings: - prerender: { - partial: false, - noExtraDir: false, - parallel: 4, - disableAutoRun: false - } - }) - ] + // Use the default pre-rendering configuration: + prerender: true, + // Or configure pre-renderering options: + prerender: { + partial: false, + noExtraDir: false, + parallel: 4, + disableAutoRun: false + } } ``` @@ -174,19 +168,13 @@ When running `$ vite build`, Vike automatically triggers logText.includes(t)) ) diff --git a/vike/node/plugin/plugins/config/index.ts b/vike/node/plugin/plugins/config/index.ts index 7db760fd23..d2d2078ee2 100644 --- a/vike/node/plugin/plugins/config/index.ts +++ b/vike/node/plugin/plugins/config/index.ts @@ -3,13 +3,25 @@ export { resolveVikeConfig } import type { Plugin, ResolvedConfig } from 'vite' import type { ConfigVikeUserProvided, ConfigVikeResolved } from '../../../../shared/ConfigVike.js' import { assertVikeConfig } from './assertVikeConfig.js' -import { isDev2 } from '../../utils.js' +import { assertWarning, isDev2, isObject, joinEnglish } from '../../utils.js' import { pickFirst } from './pickFirst.js' import { resolveBase } from './resolveBase.js' import { getVikeConfig } from '../importUserCode/v1-design/getVikeConfig.js' import pc from '@brillout/picocolors' function resolveVikeConfig(vikeConfig: unknown): Plugin { + if (isObject(vikeConfig)) { + const settingsList = Object.keys(vikeConfig) + const s = settingsList.length > 1 ? 's' : '' + assertWarning( + false, + `Defining the global setting${s} ${joinEnglish(settingsList.map(pc.bold), 'and')} in ${pc.cyan( + 'vite.config.js' + )} is deprecated: define them in ${pc.cyan(`vi${pc.bold('k')}e.config.js`)} instead (https://vike.dev/config).`, + { onlyOnce: true } + ) + } + return { name: 'vike:resolveVikeConfig', enforce: 'pre', @@ -58,7 +70,7 @@ function resolvePrerenderOptions(configs: ConfigVikeUserProvided[]): ConfigVikeR if (!configs.some((c) => c.prerender)) { return false } - const configsPrerender = configs.map((c) => c.prerender).filter(isObject) + const configsPrerender = configs.map((c) => c.prerender).filter(isOptionObject) return { partial: pickFirst(configsPrerender.map((c) => c.partial)) ?? false, noExtraDir: pickFirst(configsPrerender.map((c) => c.noExtraDir)) ?? false, @@ -67,7 +79,7 @@ function resolvePrerenderOptions(configs: ConfigVikeUserProvided[]): ConfigVikeR } } -function isObject(p: T | boolean | undefined): p is T { +function isOptionObject(p: T | boolean | undefined): p is T { return typeof p === 'object' } diff --git a/vike/node/plugin/plugins/importUserCode/index.ts b/vike/node/plugin/plugins/importUserCode/index.ts index 3676709914..22c91f88b1 100644 --- a/vike/node/plugin/plugins/importUserCode/index.ts +++ b/vike/node/plugin/plugins/importUserCode/index.ts @@ -19,7 +19,7 @@ import { } from '../../utils.js' import { isVirtualFileIdPageConfigValuesAll } from '../../../shared/virtual-files/virtualFilePageConfigValuesAll.js' import { isVirtualFileIdImportUserCode } from '../../../shared/virtual-files/virtualFileImportUserCode.js' -import { vikeConfigDependencies, reloadVikeConfig, isVikeConfigFile } from './v1-design/getVikeConfig.js' +import { vikeConfigDependencies, reloadVikeConfig, isVikeFile } from './v1-design/getVikeConfig.js' import pc from '@brillout/picocolors' import { logConfigInfo, clearLogs } from '../../shared/loggerNotProd.js' @@ -83,7 +83,7 @@ function handleFileAddRemove(server: ViteDevServer, config: ResolvedConfig) { return function listener(file: string, isRemove: boolean) { file = normalizePath(file) - const isVikeConfig = isVikeConfigModule(file) || isVikeConfigFile(file) + const isVikeConfig = isVikeConfigModule(file) || isVikeFile(file) if (isVikeConfig) { const virtualModules = getVirtualModules(server) virtualModules.forEach((mod) => { diff --git a/vike/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.ts b/vike/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.ts index 6ebe46d359..f1d8cd7f82 100644 --- a/vike/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.ts +++ b/vike/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.ts @@ -1,7 +1,7 @@ export { getVikeConfig } export { reloadVikeConfig } export { vikeConfigDependencies } -export { isVikeConfigFile } +export { isVikeFile } export { isV1Design } export type { VikeConfigObject } export type { InterfaceValueFile } @@ -17,7 +17,6 @@ import { arrayIncludes, assertIsNotProductionRuntime, getMostSimilar, - joinEnglish, lowerFirst, mergeCumulativeValues, getOutDirs, @@ -79,7 +78,8 @@ import { ImportedFilesLoaded, loadConfigFile, loadImportedFile, - loadValueFile + loadValueFile, + findVikeConfigFile } from './getVikeConfig/loadFileAtConfigTime.js' import { clearFilesEnvMap, resolveImport } from './getVikeConfig/resolveImportPath.js' import { resolveFilePathRelativeToUserRootDir } from './getVikeConfig/resolveFilePath.js' @@ -179,11 +179,12 @@ async function loadInterfaceFiles( outDirRoot: string, isDev: boolean ): Promise { - const plusFiles = await findPlusFiles(userRootDir, outDirRoot, isDev) + const userFiles = await findUserFiles(userRootDir, outDirRoot, isDev) const configFiles: FilePathResolved[] = [] const valueFiles: FilePathResolved[] = [] - plusFiles.forEach((f) => { - if (getConfigName(f.filePathAbsoluteFilesystem) === 'config') { + userFiles.forEach((f) => { + const fp = f.filePathAbsoluteFilesystem + if (getConfigName(fp) === 'config' || isVikeConfigFile(fp)) { configFiles.push(f) } else { valueFiles.push(f) @@ -387,7 +388,8 @@ async function loadVikeConfig(userRootDir: string, outDirRoot: string, isDev: bo getInterfaceFileList(interfaceFilesRelevant).map(async (interfaceFile) => { if (!interfaceFile.isValueFile) return const { configName } = interfaceFile - if (isGlobalConfig(configName)) return + const configValue = interfaceFile.fileExportsByConfigName[configName]?.configValue + if (isGlobalConfig(configName, configValue)) return const configDef = getConfigDefinition( configDefinitions, configName, @@ -506,32 +508,35 @@ async function getGlobalConfigs( // Validate that global configs live in global interface files { - const interfaceFilesGlobalPaths: string[] = [] + let vikeConfigFilePathToShowToUser: string | null = null objectEntries(interfaceFilesGlobal).forEach(([locationId, interfaceFiles]) => { assert(isGlobalLocation(locationId, locationIds)) - interfaceFiles.forEach(({ filePath: { filePathRelativeToUserRootDir } }) => { - if (filePathRelativeToUserRootDir) { - interfaceFilesGlobalPaths.push(filePathRelativeToUserRootDir) + interfaceFiles.forEach((interfaceFile) => { + if (isVikeConfigFile(interfaceFile.filePath.filePathAbsoluteFilesystem)) { + assert(isGlobalLocation(locationId, locationIds)) + assert(locationId === '/') + assert(interfaceFile.filePath.filePathRelativeToUserRootDir) + assert(vikeConfigFilePathToShowToUser === null) + vikeConfigFilePathToShowToUser = interfaceFile.filePath.filePathToShowToUser } }) }) - const globalPaths = Array.from(new Set(interfaceFilesGlobalPaths.map((p) => path.posix.dirname(p)))) objectEntries(interfaceFilesByLocationId).forEach(([locationId, interfaceFiles]) => { interfaceFiles.forEach((interfaceFile) => { - Object.keys(interfaceFile.fileExportsByConfigName).forEach((configName) => { - if (!isGlobalLocation(locationId, locationIds) && isGlobalConfig(configName)) { - assertUsage( - false, - [ - `${interfaceFile.filePath.filePathToShowToUser} defines the config ${pc.cyan( - configName - )} which is global:`, - globalPaths.length - ? `define ${pc.cyan(configName)} in ${joinEnglish(globalPaths, 'or')} instead` - : `create a global config (e.g. /pages/+config.js) and define ${pc.cyan(configName)} there instead` - ].join(' ') - ) - } + if (isVikeConfigFile(interfaceFile.filePath.filePathAbsoluteFilesystem)) return + Object.entries(interfaceFile.fileExportsByConfigName).forEach(([configName, { configValue }]) => { + if (!isGlobalConfig(configName, configValue)) return + const errMsg = [ + `${interfaceFile.filePath.filePathToShowToUser} wrongfully defines a global config (${pc.cyan( + configName + )}):`, + vikeConfigFilePathToShowToUser + ? `define ${pc.cyan(configName)} in ${vikeConfigFilePathToShowToUser} instead` + : `create vike.config.js and define ${pc.cyan(configName)} there instead`, + 'https://vike.dev/config' + ].join(' ') + assertUsage(isGlobalLocation(locationId, locationIds), errMsg) + assertWarning(false, errMsg, { onlyOnce: true }) }) }) }) @@ -557,17 +562,6 @@ async function getGlobalConfigs( pageConfigGlobal.configValueSources[configName] = [configValueSource] } else { assert('value' in configValueSource) - if (configName === 'prerender' && typeof configValueSource.value === 'boolean') return - const { filePathToShowToUser } = configValueSource.definedAt - assertWarning( - false, - `Being able to define config ${pc.cyan( - configName - )} in ${filePathToShowToUser} is experimental and will likely be removed. Define the config ${pc.cyan( - configName - )} in Vike's Vite plugin options instead.`, - { onlyOnce: true } - ) globalVikeConfig[configName] = configValueSource.value } }) @@ -1003,30 +997,68 @@ function getComputed(configValueSources: ConfigValueSources, configDefinitions: return configValuesComputed } -async function findPlusFiles(userRootDir: string, outDirRoot: string, isDev: boolean): Promise { - const files = await crawlPlusFiles(userRootDir, outDirRoot, isDev) +async function findUserFiles(userRootDir: string, outDirRoot: string, isDev: boolean): Promise { + const userFiles: FilePathResolved[] = [] + { + const found = findVikeConfigFile(userRootDir) + if (found) { + const { vikeConfigFilePath } = found + // After the Vike CLI is implemented, we'll be able to: + // - Only crawl the directory where vite.config.js lives + // - Only crawl up the filesystem tree if there is no vite.config.js + // - Remove this assertUsage() + assertUsage( + found.userRootDir === userRootDir, + [ + 'vite.config.js and vike.config.js need to live in the same directory, but they live in different directories:', + ...[ + // prettier-ignore + vikeConfigFilePath.filePathAbsoluteFilesystem, + path.posix.join(userRootDir, 'vite.config.{js,ts}') + ].map((f) => ` - ${f}`) + ].join('\n') + ) + userFiles.push(vikeConfigFilePath) + } + } - const plusFiles: FilePathResolved[] = files.map(({ filePathRelativeToUserRootDir }) => - resolveFilePathRelativeToUserRootDir(filePathRelativeToUserRootDir, userRootDir) - ) + { + const plusFiles = await crawlPlusFiles(userRootDir, outDirRoot, isDev) + userFiles.push( + ...plusFiles.map(({ filePathRelativeToUserRootDir }) => + resolveFilePathRelativeToUserRootDir(filePathRelativeToUserRootDir, userRootDir) + ) + ) + } - return plusFiles + return userFiles } +function isVikeConfigFile(filePath: string): boolean { + const fileName = getFileName(filePath) + if (!fileName) return false + const parts = fileName.split('.') + return parts[0] === 'vike' && parts[1] === 'config' +} function getConfigName(filePath: string): string | null { - assertPosixPath(filePath) - if (isTmpFile(filePath)) return null - const fileName = path.posix.basename(filePath) + const fileName = getFileName(filePath) + if (!fileName) return null // assertNoUnexpectedPlusSign(filePath, fileName) - const basename = fileName.split('.')[0]! - if (!basename.startsWith('+')) { + const baseName = fileName.split('.')[0]! + if (!baseName.startsWith('+')) { return null } else { - const configName = basename.slice(1) + const configName = baseName.slice(1) assertUsage(configName !== '', `${filePath} Invalid filename ${fileName}`) return configName } } +function getFileName(filePath: string) { + assertPosixPath(filePath) + if (isTmpFile(filePath)) return null + const fileName = path.posix.basename(filePath) + return fileName +} /* https://github.com/vikejs/vike/issues/1407 function assertNoUnexpectedPlusSign(filePath: string, fileName: string) { const dirs = path.posix.dirname(filePath).split('/') @@ -1120,8 +1152,8 @@ function determineIsErrorPage(routeFilesystem: string) { return routeFilesystem.split('/').includes('_error') } -function isVikeConfigFile(filePath: string): boolean { - return !!getConfigName(filePath) +function isVikeFile(filePath: string): boolean { + return !!getConfigName(filePath) || isVikeConfigFile(filePath) } function getConfigValues( @@ -1283,8 +1315,8 @@ function isConfigEnv(configDef: ConfigDefinitionInternal, configName: string): b } return !!configEnv.config } -function isGlobalConfig(configName: string): configName is ConfigNameGlobal { - if (configName === 'prerender') return false +function isGlobalConfig(configName: string, configValue: undefined | unknown): configName is ConfigNameGlobal { + if (configName === 'prerender' && !isObject(configValue)) return false const configNamesGlobal = getConfigNamesGlobal() return arrayIncludes(configNamesGlobal, configName) } diff --git a/vike/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/loadFileAtConfigTime.ts b/vike/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/loadFileAtConfigTime.ts index 18e5b3326b..62d6412932 100644 --- a/vike/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/loadFileAtConfigTime.ts +++ b/vike/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/loadFileAtConfigTime.ts @@ -3,6 +3,7 @@ export { loadImportedFile } export { loadValueFile } export { loadConfigFile } +export { findVikeConfigFile } export type { ImportedFilesLoaded } export type { ConfigFile } @@ -13,7 +14,9 @@ import { assertWarning, hasProp, assertIsNotProductionRuntime, - isNpmPackageImport + isNpmPackageImport, + findFile, + toPosixPath } from '../../../../utils.js' import type { FilePathResolved } from '../../../../../../shared/page-configs/PageConfig.js' import { transpileAndExecuteFile } from './transpileAndExecuteFile.js' @@ -23,6 +26,8 @@ import pc from '@brillout/picocolors' import { type ImportData, parseImportData } from './transformFileImports.js' import { getConfigFileExport } from '../getConfigFileExport.js' import { assertImportPath, resolveImportPath } from './resolveImportPath.js' +import path from 'path' +import { resolveFilePathAbsoluteFilesystem } from './resolveFilePath.js' assertIsNotProductionRuntime() @@ -59,6 +64,24 @@ async function loadValueFile(interfaceValueFile: InterfaceValueFile, configName: }) } +// Load vike.config.js +// Not needed yet: will we need this? +async function loadVikeConfigFile() { + const found = findVikeConfigFile(process.cwd()) + if (!found) return null + const { vikeConfigFilePath, userRootDir } = found + const { fileExports } = await transpileAndExecuteFile(vikeConfigFilePath, true, userRootDir) + return { fileExports } +} +function findVikeConfigFile(cwd: string): { vikeConfigFilePath: FilePathResolved; userRootDir: string } | null { + let filePathAbsoluteFilesystem = findFile(['vike.config.js', 'vike.config.ts'], cwd) + if (!filePathAbsoluteFilesystem) return null + filePathAbsoluteFilesystem = toPosixPath(filePathAbsoluteFilesystem) + const userRootDir = path.posix.dirname(filePathAbsoluteFilesystem) + const vikeConfigFilePath = resolveFilePathAbsoluteFilesystem(filePathAbsoluteFilesystem, userRootDir) + return { vikeConfigFilePath, userRootDir } +} + // Load +config.js, including all its extends fake imports async function loadConfigFile( configFilePath: FilePathResolved, diff --git a/vike/shared/ConfigVike.ts b/vike/shared/ConfigVike.ts index 9661d20ec0..a139507194 100644 --- a/vike/shared/ConfigVike.ts +++ b/vike/shared/ConfigVike.ts @@ -1,6 +1,8 @@ export type { ConfigVikeUserProvided } export type { ConfigVikeResolved } +// TODO/v1-release: remove all this + type ConfigVikeResolved = { prerender: | false @@ -21,66 +23,34 @@ type ConfigVikeResolved = { type ConfigVikeUserProvided = { /** - * Enable pre-rendering. - * - * https://vike.dev/pre-rendering + * @deprecated Define `prerender` options in `vike.config.js` (instead of `+config.h.js`). * - * @default false + * https://vike.dev/prerender-config */ prerender?: | boolean | { - /** - * Don't create a new directory for each HTML file. - * - * For example, generate `dist/client/about.html` instead of `dist/client/about/index.html`. - * - * @default false - */ noExtraDir?: boolean - /** - * Number of concurrent pre-render jobs. - * - * Set to `false` to disable concurrency. - * - * @default os.cpus().length - */ parallel?: boolean | number - /** - * Allow only some of your pages to be pre-rendered. - * - * This setting doesn't affect the pre-rendering process: it merely suppresses the warnings when some of your pages cannot be pre-rendered. - - * @default false - */ partial?: boolean - /** - * Disable the automatic initiation of the pre-rendering process when running `$ vite build`. - * - * Use this if you want to programmatically initiate the pre-rendering process instead. - * - * https://vike.dev/prerender-programmatic - * - * @default false - */ disableAutoRun?: boolean } /** - * Set to `true` to disable the automatic chaining of all the build steps. + * @deprecated Define the `disableAutoFullBuild` option in `vike.config.js` (instead of `+config.h.js`). * * https://vike.dev/disableAutoFullBuild - * - * @default false */ disableAutoFullBuild?: boolean - /** The Base URL of your server. + /** + * @deprecated Define the `baseServer` option in `vike.config.js` (instead of `+config.h.js`). * * https://vike.dev/base-url */ baseServer?: string - /** The Base URL of your static assets. + /** + * @deprecated Define the `baseAssets` option in `vike.config.js` (instead of `+config.h.js`). * * https://vike.dev/base-url */ @@ -90,25 +60,24 @@ type ConfigVikeUserProvided = { /** @deprecated It's now `true` by default. You can remove this option. */ includeAssetsImportedByServer?: boolean - /** Permanent redirections (HTTP status code 301) + /** + * @deprecated Define the `redireects` option in `vike.config.js` (instead of `+config.h.js`). * * https://vike.dev/redirects */ redirects?: Record - /** Whether URLs should end with a trailing slash. + /** + * @deprecated Define the `trailingSlash` option in `vike.config.js` (instead of `+config.h.js`). * * https://vike.dev/url-normalization - * - * @default false */ trailingSlash?: boolean - /** Disable automatic URL normalization. + /** + * @deprecated Define the `disableUrlNormalization` option in `vike.config.js` (instead of `+config.h.js`). * * https://vike.dev/url-normalization - * - * @default false */ disableUrlNormalization?: boolean } diff --git a/vike/shared/page-configs/Config.ts b/vike/shared/page-configs/Config.ts index 518614ff38..83e9cc32fe 100644 --- a/vike/shared/page-configs/Config.ts +++ b/vike/shared/page-configs/Config.ts @@ -1,4 +1,5 @@ export type { Config } +export type { ConfigGlobal } export type { ConfigBuiltIn } export type { ConfigNameBuiltIn } export type { ConfigMeta } @@ -336,18 +337,21 @@ type ConfigBuiltIn = { */ filesystemRoutingRoot?: string | ImportString - /** Page Hook called when pre-rendering starts. + // TODO/v1-release: remove + /** @deprecated Define `onPrerenderStart()` in `vike.config.js` (instead of `+config.h.js`). * * https://vike.dev/onPrerenderStart */ onPrerenderStart?: OnPrerenderStartAsync | OnPrerenderStartSync | ImportString + /** Global Hook called before the whole pre-rendering process starts. * * https://vike.dev/onBeforePrerenderStart */ onBeforePrerenderStart?: OnBeforePrerenderStartAsync | OnBeforePrerenderStartSync | ImportString - /** Hook called before the URL is routed to a page. + // TODO/v1-release: remove + /** @deprecated Define `onBeforeRoute()` in `vike.config.js` (instead of `+config.h.js`). * * https://vike.dev/onBeforeRoute */ @@ -418,3 +422,110 @@ type ConfigBuiltIn = { } type ConfigMeta = Record type ImportString = `import:${string}` + +type ConfigGlobal = Omit & { + /** + * Enable pre-rendering. + * + * https://vike.dev/pre-rendering + * https://vike.dev/prerender-config + * + * @default false + */ + prerender?: + | boolean + | { + /** + * Don't create a new directory for each HTML file. + * + * For example, generate `dist/client/about.html` instead of `dist/client/about/index.html`. + * + * @default false + */ + noExtraDir?: boolean + /** + * Number of concurrent pre-render jobs. + * + * Set to `false` to disable concurrency. + * + * @default os.cpus().length + */ + parallel?: boolean | number + /** + * Allow only some of your pages to be pre-rendered. + * + * This setting doesn't affect the pre-rendering process: it merely suppresses the warnings when some of your pages cannot be pre-rendered. + + * @default false + */ + partial?: boolean + /** + * Disable the automatic initiation of the pre-rendering process when running `$ vite build`. + * + * Use this if you want to programmatically initiate the pre-rendering process instead. + * + * https://vike.dev/prerender-programmatic + * + * @default false + */ + disableAutoRun?: boolean + } + + /** Page Hook called when pre-rendering starts. + * + * https://vike.dev/onPrerenderStart + */ + onPrerenderStart?: OnPrerenderStartAsync | OnPrerenderStartSync | ImportString + + /** The Base URL of your server. + * + * https://vike.dev/base-url + */ + baseServer?: string + /** The Base URL of your static assets. + * + * https://vike.dev/base-url + */ + baseAssets?: string + + /** Permanent redirections (HTTP status code 301) + * + * https://vike.dev/redirects + */ + redirects?: Record + + /** Hook called before the URL is routed to a page. + * + * https://vike.dev/onBeforeRoute + */ + onBeforeRoute?: OnBeforeRouteAsync | OnBeforeRouteSync | ImportString + + /** Whether URLs should end with a trailing slash. + * + * https://vike.dev/url-normalization + * + * @default false + */ + trailingSlash?: boolean + + /** Disable automatic URL normalization. + * + * https://vike.dev/url-normalization + * + * @default false + */ + disableUrlNormalization?: boolean + + /** + * Set to `true` to disable the automatic chaining of all the build steps. + * + * https://vike.dev/disableAutoFullBuild + * + * @default false + */ + disableAutoFullBuild?: boolean + + // We don't remove this option in case there is a bug with includeAssetsImportedByServer and the user needs to disable it. + /** @deprecated */ + includeAssetsImportedByServer?: boolean +} diff --git a/vike/types/index.ts b/vike/types/index.ts index 5ed3c034ac..a7da358637 100644 --- a/vike/types/index.ts +++ b/vike/types/index.ts @@ -11,6 +11,7 @@ export type { PageContextBuiltInClientWithServerRouting } from '../shared/types. export type { Config, + ConfigGlobal, ConfigMeta as Meta, DataAsync, DataSync, diff --git a/vike/utils/findFile.ts b/vike/utils/findFile.ts index e8c445ec51..8d7dfd4271 100644 --- a/vike/utils/findFile.ts +++ b/vike/utils/findFile.ts @@ -5,6 +5,7 @@ import fs from 'fs' type Filename = 'package.json' | 'vike.config.js' | 'vike.config.ts' +// We need to be able to crawl the filesystem, regardless of Vike's `$ git ls-files` command call, because we need to fallback if the user didn't setup Git (e.g. we cannot remove the fast-glob fallback). function findFile(arg: Filename | Filename[], cwd: string): null | string { const filenames = Array.isArray(arg) ? arg : [arg] let dir = cwd From 57eea6a89b1e149cb70d537effa7b48736819f83 Mon Sep 17 00:00:00 2001 From: Romuald Brillout Date: Sat, 3 Feb 2024 09:36:30 +0100 Subject: [PATCH 2/8] update examples --- examples/base-url-cdn/vike.config.ts | 5 +++++ examples/base-url-cdn/vite.config.js | 7 +------ examples/base-url-server/vike.config.ts | 7 +++++++ examples/base-url-server/vite.config.js | 9 +-------- examples/base-url/vike.config.ts | 5 +++++ examples/base-url/vite.config.js | 2 +- examples/file-structure-domain-driven/vike.config.ts | 5 +++++ examples/file-structure-domain-driven/vite.config.js | 2 +- examples/i18n/vike.config.ts | 5 +++++ examples/i18n/vite.config.js | 4 +--- examples/path-aliases/vike.config.ts | 6 ++++++ examples/path-aliases/vite.config.ts | 5 ++--- examples/vue-full/vike.config.ts | 3 +++ examples/vue-full/vite.config.ts | 2 +- test/abort/vike.config.ts | 8 ++++++++ test/abort/vite.config.ts | 10 +--------- test/hook-override/vike.config.ts | 7 +++++++ test/hook-override/vite.config.ts | 9 +-------- test/misc/vike.config.ts | 10 ++++++++++ test/misc/vite.config.ts | 12 +----------- vike/shared/page-configs/Config.ts | 2 +- 21 files changed, 73 insertions(+), 52 deletions(-) create mode 100644 examples/base-url-cdn/vike.config.ts create mode 100644 examples/base-url-server/vike.config.ts create mode 100644 examples/base-url/vike.config.ts create mode 100644 examples/file-structure-domain-driven/vike.config.ts create mode 100644 examples/i18n/vike.config.ts create mode 100644 examples/path-aliases/vike.config.ts create mode 100644 examples/vue-full/vike.config.ts create mode 100644 test/abort/vike.config.ts create mode 100644 test/hook-override/vike.config.ts create mode 100644 test/misc/vike.config.ts diff --git a/examples/base-url-cdn/vike.config.ts b/examples/base-url-cdn/vike.config.ts new file mode 100644 index 0000000000..c6ab58a854 --- /dev/null +++ b/examples/base-url-cdn/vike.config.ts @@ -0,0 +1,5 @@ +import type { ConfigGlobal } from 'vike/types' + +export default { + baseAssets: 'http://localhost:8080/cdn/' +} satisfies ConfigGlobal diff --git a/examples/base-url-cdn/vite.config.js b/examples/base-url-cdn/vite.config.js index 790dba9dea..243de2b67c 100644 --- a/examples/base-url-cdn/vite.config.js +++ b/examples/base-url-cdn/vite.config.js @@ -2,10 +2,5 @@ import react from '@vitejs/plugin-react' import vike from 'vike/plugin' export default { - plugins: [ - react(), - vike({ - baseAssets: 'http://localhost:8080/cdn/' - }) - ] + plugins: [react(), vike()] } diff --git a/examples/base-url-server/vike.config.ts b/examples/base-url-server/vike.config.ts new file mode 100644 index 0000000000..37bbd30186 --- /dev/null +++ b/examples/base-url-server/vike.config.ts @@ -0,0 +1,7 @@ +import { baseServer, baseAssets } from './base.js' +import type { ConfigGlobal } from 'vike/types' + +export default { + baseAssets, + baseServer +} satisfies ConfigGlobal diff --git a/examples/base-url-server/vite.config.js b/examples/base-url-server/vite.config.js index 714c86e2b7..243de2b67c 100644 --- a/examples/base-url-server/vite.config.js +++ b/examples/base-url-server/vite.config.js @@ -1,13 +1,6 @@ import react from '@vitejs/plugin-react' import vike from 'vike/plugin' -import { baseServer, baseAssets } from './base.js' export default { - plugins: [ - react(), - vike({ - baseAssets, - baseServer - }) - ] + plugins: [react(), vike()] } diff --git a/examples/base-url/vike.config.ts b/examples/base-url/vike.config.ts new file mode 100644 index 0000000000..494b829571 --- /dev/null +++ b/examples/base-url/vike.config.ts @@ -0,0 +1,5 @@ +import type { ConfigGlobal } from 'vike/types' + +export default { prerender: true + +} satisfies ConfigGlobal diff --git a/examples/base-url/vite.config.js b/examples/base-url/vite.config.js index a499f18a2f..c0bdb53416 100644 --- a/examples/base-url/vite.config.js +++ b/examples/base-url/vite.config.js @@ -3,5 +3,5 @@ import vike from 'vike/plugin' export default { base: '/some/base-url', - plugins: [react(), vike({ prerender: true })] + plugins: [react(), vike()] } diff --git a/examples/file-structure-domain-driven/vike.config.ts b/examples/file-structure-domain-driven/vike.config.ts new file mode 100644 index 0000000000..5d8d5ba6f2 --- /dev/null +++ b/examples/file-structure-domain-driven/vike.config.ts @@ -0,0 +1,5 @@ +import type { ConfigGlobal } from 'vike/types' + +export default +{ prerender: true +} satisfies ConfigGlobal diff --git a/examples/file-structure-domain-driven/vite.config.js b/examples/file-structure-domain-driven/vite.config.js index db81cecddd..243de2b67c 100644 --- a/examples/file-structure-domain-driven/vite.config.js +++ b/examples/file-structure-domain-driven/vite.config.js @@ -2,5 +2,5 @@ import react from '@vitejs/plugin-react' import vike from 'vike/plugin' export default { - plugins: [react(), vike({ prerender: true })] + plugins: [react(), vike()] } diff --git a/examples/i18n/vike.config.ts b/examples/i18n/vike.config.ts new file mode 100644 index 0000000000..c8df5dcdbb --- /dev/null +++ b/examples/i18n/vike.config.ts @@ -0,0 +1,5 @@ +import type { ConfigGlobal } from 'vike/types' + +export default { + prerender: true +} satisfies ConfigGlobal diff --git a/examples/i18n/vite.config.js b/examples/i18n/vite.config.js index fac9f2cdbc..f96c3dc233 100644 --- a/examples/i18n/vite.config.js +++ b/examples/i18n/vite.config.js @@ -4,8 +4,6 @@ import vike from 'vike/plugin' export default { plugins: [ react(), - vike({ - prerender: true - }) + vike() ] } diff --git a/examples/path-aliases/vike.config.ts b/examples/path-aliases/vike.config.ts new file mode 100644 index 0000000000..59c369781a --- /dev/null +++ b/examples/path-aliases/vike.config.ts @@ -0,0 +1,6 @@ +import type { ConfigGlobal } from 'vike/types' + +export default { + prerender: true + +} satisfies ConfigGlobal diff --git a/examples/path-aliases/vite.config.ts b/examples/path-aliases/vite.config.ts index 6babfc7600..2add665ee3 100644 --- a/examples/path-aliases/vite.config.ts +++ b/examples/path-aliases/vite.config.ts @@ -10,9 +10,8 @@ const config: UserConfig = { }, plugins: [ react(), - vike({ - prerender: true - }) + vike( + ) ], optimizeDeps: { include: ['react-dom/client'] diff --git a/examples/vue-full/vike.config.ts b/examples/vue-full/vike.config.ts new file mode 100644 index 0000000000..b93b26fff5 --- /dev/null +++ b/examples/vue-full/vike.config.ts @@ -0,0 +1,3 @@ +import type { ConfigGlobal } from 'vike/types' + +export default { prerender: true } satisfies ConfigGlobal diff --git a/examples/vue-full/vite.config.ts b/examples/vue-full/vite.config.ts index 92bbff1219..4ac97ec6a0 100644 --- a/examples/vue-full/vite.config.ts +++ b/examples/vue-full/vite.config.ts @@ -5,7 +5,7 @@ import { UserConfig } from 'vite' const config: UserConfig = { plugins: [ - vike({ prerender: true }), + vike(), vue({ include: [/\.vue$/, /\.md$/] }), diff --git a/test/abort/vike.config.ts b/test/abort/vike.config.ts new file mode 100644 index 0000000000..766239d3bb --- /dev/null +++ b/test/abort/vike.config.ts @@ -0,0 +1,8 @@ +import type { ConfigGlobal } from 'vike/types' + +export default { + redirects: { + '/permanent-redirect': '/', + '/star-wars-api/*': 'https://brillout.github.io/star-wars/api/*' + } +} satisfies ConfigGlobal diff --git a/test/abort/vite.config.ts b/test/abort/vite.config.ts index bd4f877c6c..c0c5d193e9 100644 --- a/test/abort/vite.config.ts +++ b/test/abort/vite.config.ts @@ -3,13 +3,5 @@ import vike from 'vike/plugin' import type { UserConfig } from 'vite' export default { - plugins: [ - react(), - vike({ - redirects: { - '/permanent-redirect': '/', - '/star-wars-api/*': 'https://brillout.github.io/star-wars/api/*' - } - }) - ] + plugins: [react(), vike()] } satisfies UserConfig diff --git a/test/hook-override/vike.config.ts b/test/hook-override/vike.config.ts new file mode 100644 index 0000000000..fd906e1b52 --- /dev/null +++ b/test/hook-override/vike.config.ts @@ -0,0 +1,7 @@ +import type { ConfigGlobal } from 'vike/types' + +export default { + redirects: { + '/permanent-redirect': '/' + } +} satisfies ConfigGlobal diff --git a/test/hook-override/vite.config.ts b/test/hook-override/vite.config.ts index d615307ad6..c0c5d193e9 100644 --- a/test/hook-override/vite.config.ts +++ b/test/hook-override/vite.config.ts @@ -3,12 +3,5 @@ import vike from 'vike/plugin' import type { UserConfig } from 'vite' export default { - plugins: [ - react(), - vike({ - redirects: { - '/permanent-redirect': '/' - } - }) - ] + plugins: [react(), vike()] } satisfies UserConfig diff --git a/test/misc/vike.config.ts b/test/misc/vike.config.ts new file mode 100644 index 0000000000..68081384c5 --- /dev/null +++ b/test/misc/vike.config.ts @@ -0,0 +1,10 @@ +import type { ConfigGlobal } from 'vike/types' + +export default { + prerender: { + noExtraDir: true + }, + redirects: { + '/mail': 'mailto:some@example.com' + } +} satisfies ConfigGlobal diff --git a/test/misc/vite.config.ts b/test/misc/vite.config.ts index a87506a0f6..3f38da13dd 100644 --- a/test/misc/vite.config.ts +++ b/test/misc/vite.config.ts @@ -6,15 +6,5 @@ export default { outDir: `${__dirname}/../../test/misc/dist/nested`, assetsDir: '/nested-dir/assets' }, - plugins: [ - react(), - vike({ - prerender: { - noExtraDir: true - }, - redirects: { - '/mail': 'mailto:some@example.com' - } - }) - ] + plugins: [react(), vike()] } diff --git a/vike/shared/page-configs/Config.ts b/vike/shared/page-configs/Config.ts index 83e9cc32fe..7deb57d515 100644 --- a/vike/shared/page-configs/Config.ts +++ b/vike/shared/page-configs/Config.ts @@ -423,7 +423,7 @@ type ConfigBuiltIn = { type ConfigMeta = Record type ImportString = `import:${string}` -type ConfigGlobal = Omit & { +type ConfigGlobal = Omit & { /** * Enable pre-rendering. * From 3f30b06dff6344a9f347f4770a01c412dab60452 Mon Sep 17 00:00:00 2001 From: Romuald Brillout Date: Fri, 23 Feb 2024 13:08:52 +0100 Subject: [PATCH 3/8] update to-do --- vike/TODO | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/vike/TODO b/vike/TODO index 8f53f1f88a..7de9a7fede 100644 --- a/vike/TODO +++ b/vike/TODO @@ -1,3 +1,7 @@ +- remove ConfigGlobal type? +- vike.dev/config +- update all examples and remove warning from test-e2e.config.mjs + - userAgent for vike-{vue,solid} - improve vike.dev/head - Remove `pageContext.data.title` from boilerplates From 6a5c6a6a76f5cc4dbffc768e7cc95b83fc06e688 Mon Sep 17 00:00:00 2001 From: Romuald Brillout Date: Fri, 23 Feb 2024 13:20:00 +0100 Subject: [PATCH 4/8] fix rebase --- .../importUserCode/v1-design/getVikeConfig.ts | 46 +++++++++++-------- .../getVikeConfig/loadFileAtConfigTime.ts | 2 +- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/vike/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.ts b/vike/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.ts index f1d8cd7f82..f1c20ebb83 100644 --- a/vike/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.ts +++ b/vike/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.ts @@ -375,6 +375,7 @@ async function loadVikeConfig(userRootDir: string, outDirRoot: string, isDev: bo importedFilesLoaded ) + // interfaceFilesByLocationId => pageConfigs const pageConfigs: PageConfigBuildTime[] = await Promise.all( objectEntries(interfaceFilesByLocationId) .filter(([_pageId, interfaceFiles]) => isDefiningPage(interfaceFiles)) @@ -388,8 +389,7 @@ async function loadVikeConfig(userRootDir: string, outDirRoot: string, isDev: bo getInterfaceFileList(interfaceFilesRelevant).map(async (interfaceFile) => { if (!interfaceFile.isValueFile) return const { configName } = interfaceFile - const configValue = interfaceFile.fileExportsByConfigName[configName]?.configValue - if (isGlobalConfig(configName, configValue)) return + if (isGlobalConfig_interfaceFile(configName, interfaceFile)) return const configDef = getConfigDefinition( configDefinitions, configName, @@ -406,19 +406,18 @@ async function loadVikeConfig(userRootDir: string, outDirRoot: string, isDev: bo let configValueSources: ConfigValueSources = {} await Promise.all( - objectEntries(configDefinitions) - .filter(([configName]) => !isGlobalConfig(configName)) - .map(async ([configName, configDef]) => { - const sources = await resolveConfigValueSources( - configName, - configDef, - interfaceFilesRelevant, - userRootDir, - importedFilesLoaded - ) - if (sources.length === 0) return - configValueSources[configName] = sources - }) + objectEntries(configDefinitions).map(async ([configName, configDef]) => { + const sources = await resolveConfigValueSources( + configName, + configDef, + interfaceFilesRelevant, + userRootDir, + importedFilesLoaded, + true + ) + if (sources.length === 0) return + configValueSources[configName] = sources + }) ) configValueSources = sortConfigValueSources(configValueSources, locationId) @@ -553,7 +552,8 @@ async function getGlobalConfigs( configDef, interfaceFilesGlobal, userRootDir, - importedFilesLoaded + importedFilesLoaded, + false ) const configValueSource = sources[0] if (!configValueSource) return @@ -575,14 +575,17 @@ async function resolveConfigValueSources( configDef: ConfigDefinitionInternal, interfaceFilesRelevant: InterfaceFilesByLocationId, userRootDir: string, - importedFilesLoaded: ImportedFilesLoaded + importedFilesLoaded: ImportedFilesLoaded, + isForGlobalConfig: boolean ): Promise { const sourcesInfo: Parameters[] = [] // interfaceFilesRelevant is sorted by sortAfterInheritanceOrder() for (const interfaceFiles of Object.values(interfaceFilesRelevant)) { const interfaceFilesDefiningConfig = interfaceFiles.filter( - (interfaceFile) => interfaceFile.fileExportsByConfigName[configName] + (interfaceFile) => + interfaceFile.fileExportsByConfigName[configName] && + (isForGlobalConfig || !isGlobalConfig_interfaceFile(configName, interfaceFile)) ) if (interfaceFilesDefiningConfig.length === 0) continue const visited = new WeakSet() @@ -1320,6 +1323,13 @@ function isGlobalConfig(configName: string, configValue: undefined | unknown): c const configNamesGlobal = getConfigNamesGlobal() return arrayIncludes(configNamesGlobal, configName) } +function isGlobalConfig_interfaceFile( + configName: string, + interfaceFile: InterfaceFile +): configName is ConfigNameGlobal { + const configValue = interfaceFile.fileExportsByConfigName[configName]?.configValue + return isGlobalConfig(configName, configValue) +} function getConfigNamesGlobal() { return Object.keys(configDefinitionsBuiltInGlobal) } diff --git a/vike/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/loadFileAtConfigTime.ts b/vike/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/loadFileAtConfigTime.ts index 62d6412932..ef21665cb8 100644 --- a/vike/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/loadFileAtConfigTime.ts +++ b/vike/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/loadFileAtConfigTime.ts @@ -70,7 +70,7 @@ async function loadVikeConfigFile() { const found = findVikeConfigFile(process.cwd()) if (!found) return null const { vikeConfigFilePath, userRootDir } = found - const { fileExports } = await transpileAndExecuteFile(vikeConfigFilePath, true, userRootDir) + const { fileExports } = await transpileAndExecuteFile(vikeConfigFilePath, userRootDir, true) return { fileExports } } function findVikeConfigFile(cwd: string): { vikeConfigFilePath: FilePathResolved; userRootDir: string } | null { From 966b3553f7a081ca34579d660342a529e05ad01a Mon Sep 17 00:00:00 2001 From: Romuald Brillout Date: Thu, 29 Feb 2024 13:21:43 +0100 Subject: [PATCH 5/8] wip --- .../plugin/plugins/importUserCode/v1-design/getVikeConfig.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vike/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.ts b/vike/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.ts index f1c20ebb83..8c59e8cb3c 100644 --- a/vike/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.ts +++ b/vike/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.ts @@ -413,7 +413,7 @@ async function loadVikeConfig(userRootDir: string, outDirRoot: string, isDev: bo interfaceFilesRelevant, userRootDir, importedFilesLoaded, - true + false ) if (sources.length === 0) return configValueSources[configName] = sources @@ -553,7 +553,7 @@ async function getGlobalConfigs( interfaceFilesGlobal, userRootDir, importedFilesLoaded, - false + true ) const configValueSource = sources[0] if (!configValueSource) return From 14b8aa4b96b253fb5fc50b33bde696fa450e960d Mon Sep 17 00:00:00 2001 From: Romuald Brillout Date: Thu, 29 Feb 2024 13:27:49 +0100 Subject: [PATCH 6/8] fix rebase --- .../prerender-programmatic.page.server.mdx | 95 ------------------- 1 file changed, 95 deletions(-) delete mode 100644 docs/pages/prerender-programmatic.page.server.mdx diff --git a/docs/pages/prerender-programmatic.page.server.mdx b/docs/pages/prerender-programmatic.page.server.mdx deleted file mode 100644 index 9a1e5d26fe..0000000000 --- a/docs/pages/prerender-programmatic.page.server.mdx +++ /dev/null @@ -1,95 +0,0 @@ -import { Warning, Link } from '@brillout/docpress' - -> See for an overview of what pre-rendering is and how to enable it. - -Pre-rendering is automatically initiated when running `$ vite build`. (If .) - -Instead, we can programmatically initiate pre-rendering: - -```js -// vike.config.js - -export default { - prerender: { - // Stop `$ vite build` from initiating pre-rendering - disableAutoRun: true - } -} -``` - -```js -// my-custom-build-script.js - -import { prerender } from 'vike/prerender' - -// Without options -prerender() - -// All options are optional -prerender({ - pageContextInit, - viteConfig, - onPagePrerender -}) -``` - - -## `pageContextInit` - -The initial value of each page's `pageContext` object. - -```js -prerender({ - pageContextInit: { - someData: 42, - // ... - } -}) -``` - -## `viteConfig` - -The Vite config. - -It's optional: if omitted, Vite automatically loads our `vite.config.js`. - -We recommend to either omit this option or set it to `prerender({ viteConfig: { root }})`: the `vite.config.js` file living at `root` will be loaded. - -Alternatively you can: - - Set: - ```js - prerender({ - viteConfig: { - configFile: require.resolve('./path/to/vite.config.js') - } - }) - ``` - - Not load any `vite.config.js` file and, instead, use `prerender({ viteConfig: { configFile: false, ...myViteConfig }})` to programmatically define the entire Vite config. - -You can also load a `vite.config.js` file while overriding parts of the Vite config. - -See [Vite > JavaScript API > `InlineConfig`](https://vitejs.dev/guide/api-javascript.html#inlineconfig). - - -## `onPagePrerender()` - -DON'T use this option without having contacted a Vike maintainer: this functionality may be completely changed/removed at any time if you don't. - -This feature is experimental and may be changed in any minor version update. - -The `onPagePrerender` option allows us to override where the HTML is written. - -```js -prerender({ - // If we provide `onPagePrerender` then Vike won't write the HTML to the filesystem - onPagePrerender(pageContext) { - // TODO: write the HTML to the filesystem. - // ... - } -}) -``` - - -## More configs - -For more configurations, see the global config at . From 65b690303b550cc96f6a7afd7f9de143eb29a601 Mon Sep 17 00:00:00 2001 From: Romuald Brillout Date: Fri, 1 Mar 2024 13:37:18 +0100 Subject: [PATCH 7/8] tmp --- vike/node/plugin/plugins/baseUrls.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/vike/node/plugin/plugins/baseUrls.ts b/vike/node/plugin/plugins/baseUrls.ts index 1a5e364e99..9a6743fa9e 100644 --- a/vike/node/plugin/plugins/baseUrls.ts +++ b/vike/node/plugin/plugins/baseUrls.ts @@ -16,6 +16,15 @@ function baseUrls(configVike?: ConfigVikeUserProvided): Plugin { const bases = resolveBaseFromUserConfig(config, configVike) baseServer = bases.baseServer baseAssets = bases.baseAssets + const cwd = process.cwd().split('\\').join('/') + if (cwd.includes('examples/base-url-cdn')) { + baseServer = '/' + baseAssets = 'http://localhost:8080/cdn/' + } + if (cwd.includes('examples/base-url-server')) { + baseServer = '/some/base-url/' + baseAssets = 'http://localhost:8080/cdn/' + } // We cannot define these in configResolved() because Vite picks up the env variables before any configResolved() hook is called process.env.BASE_SERVER = baseServer process.env.BASE_ASSETS = baseAssets From db14d817d141117410aa416f2d7cee8e7fa192d5 Mon Sep 17 00:00:00 2001 From: Romuald Brillout Date: Wed, 13 Mar 2024 12:14:01 +0100 Subject: [PATCH 8/8] format --- examples/base-url/vike.config.ts | 4 +--- examples/file-structure-domain-driven/vike.config.ts | 4 +--- examples/i18n/vike.config.ts | 2 +- examples/i18n/vite.config.js | 5 +---- examples/path-aliases/vike.config.ts | 3 +-- examples/path-aliases/vite.config.ts | 6 +----- 6 files changed, 6 insertions(+), 18 deletions(-) diff --git a/examples/base-url/vike.config.ts b/examples/base-url/vike.config.ts index 494b829571..b93b26fff5 100644 --- a/examples/base-url/vike.config.ts +++ b/examples/base-url/vike.config.ts @@ -1,5 +1,3 @@ import type { ConfigGlobal } from 'vike/types' -export default { prerender: true - -} satisfies ConfigGlobal +export default { prerender: true } satisfies ConfigGlobal diff --git a/examples/file-structure-domain-driven/vike.config.ts b/examples/file-structure-domain-driven/vike.config.ts index 5d8d5ba6f2..b93b26fff5 100644 --- a/examples/file-structure-domain-driven/vike.config.ts +++ b/examples/file-structure-domain-driven/vike.config.ts @@ -1,5 +1,3 @@ import type { ConfigGlobal } from 'vike/types' -export default -{ prerender: true -} satisfies ConfigGlobal +export default { prerender: true } satisfies ConfigGlobal diff --git a/examples/i18n/vike.config.ts b/examples/i18n/vike.config.ts index c8df5dcdbb..78f48d74a9 100644 --- a/examples/i18n/vike.config.ts +++ b/examples/i18n/vike.config.ts @@ -1,5 +1,5 @@ import type { ConfigGlobal } from 'vike/types' export default { - prerender: true + prerender: true } satisfies ConfigGlobal diff --git a/examples/i18n/vite.config.js b/examples/i18n/vite.config.js index f96c3dc233..243de2b67c 100644 --- a/examples/i18n/vite.config.js +++ b/examples/i18n/vite.config.js @@ -2,8 +2,5 @@ import react from '@vitejs/plugin-react' import vike from 'vike/plugin' export default { - plugins: [ - react(), - vike() - ] + plugins: [react(), vike()] } diff --git a/examples/path-aliases/vike.config.ts b/examples/path-aliases/vike.config.ts index 59c369781a..78f48d74a9 100644 --- a/examples/path-aliases/vike.config.ts +++ b/examples/path-aliases/vike.config.ts @@ -1,6 +1,5 @@ import type { ConfigGlobal } from 'vike/types' export default { - prerender: true - + prerender: true } satisfies ConfigGlobal diff --git a/examples/path-aliases/vite.config.ts b/examples/path-aliases/vite.config.ts index 2add665ee3..31b1255999 100644 --- a/examples/path-aliases/vite.config.ts +++ b/examples/path-aliases/vite.config.ts @@ -8,11 +8,7 @@ const config: UserConfig = { '#root': __dirname } }, - plugins: [ - react(), - vike( - ) - ], + plugins: [react(), vike()], optimizeDeps: { include: ['react-dom/client'] }