vike 0.4.219 → 0.4.220-commit-a9f46b8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/dist/cjs/node/api/build.js +23 -58
  2. package/dist/cjs/node/api/context.js +6 -8
  3. package/dist/cjs/node/api/prepareViteApiCall.js +6 -7
  4. package/dist/cjs/node/cli/context.js +16 -0
  5. package/dist/cjs/node/cli/entry.js +2 -0
  6. package/dist/cjs/node/cli/utils.js +1 -0
  7. package/dist/cjs/node/plugin/plugins/autoFullBuild.js +32 -19
  8. package/dist/cjs/node/plugin/plugins/baseUrls.js +1 -1
  9. package/dist/cjs/node/plugin/plugins/commonConfig.js +10 -8
  10. package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/getVirtualFilePageConfigs.js +3 -2
  11. package/dist/cjs/node/prerender/{isPrerenderAutoRunEnabled.js → context.js} +9 -1
  12. package/dist/cjs/node/prerender/runPrerender.js +55 -33
  13. package/dist/cjs/node/prerender/utils.js +1 -0
  14. package/dist/cjs/node/runtime/globalContext.js +2 -22
  15. package/dist/cjs/node/runtime/page-files/setup.js +1 -1
  16. package/dist/cjs/node/runtime/utils.js +1 -0
  17. package/dist/cjs/shared/page-configs/loadConfigValues.js +5 -1
  18. package/dist/cjs/utils/PROJECT_VERSION.js +1 -1
  19. package/dist/cjs/utils/assertSetup.js +15 -1
  20. package/dist/cjs/utils/catchInfiniteLoop.js +34 -0
  21. package/dist/cjs/utils/isDev.js +2 -0
  22. package/dist/cjs/utils/makePublicCopy.js +32 -0
  23. package/dist/esm/client/client-routing-runtime/history.d.ts +3 -1
  24. package/dist/esm/client/client-routing-runtime/history.js +23 -18
  25. package/dist/esm/client/client-routing-runtime/index.d.ts +0 -1
  26. package/dist/esm/client/client-routing-runtime/index.js +0 -1
  27. package/dist/esm/client/client-routing-runtime/initClientRouter.js +2 -2
  28. package/dist/esm/client/client-routing-runtime/initOnLinkClick.js +3 -4
  29. package/dist/esm/client/client-routing-runtime/initOnPopState.d.ts +0 -10
  30. package/dist/esm/client/client-routing-runtime/initOnPopState.js +50 -62
  31. package/dist/esm/client/client-routing-runtime/renderPageClientSide.js +15 -15
  32. package/dist/esm/client/client-routing-runtime/scrollRestoration.d.ts +4 -6
  33. package/dist/esm/client/client-routing-runtime/scrollRestoration.js +17 -12
  34. package/dist/esm/client/client-routing-runtime/setScrollPosition.d.ts +1 -1
  35. package/dist/esm/client/client-routing-runtime/setScrollPosition.js +29 -5
  36. package/dist/esm/client/client-routing-runtime/utils.d.ts +1 -0
  37. package/dist/esm/client/client-routing-runtime/utils.js +1 -0
  38. package/dist/esm/client/shared/normalizeClientSideUrl.js +2 -3
  39. package/dist/esm/node/api/build.d.ts +1 -6
  40. package/dist/esm/node/api/build.js +20 -25
  41. package/dist/esm/node/api/context.d.ts +4 -4
  42. package/dist/esm/node/api/context.js +6 -9
  43. package/dist/esm/node/api/prepareViteApiCall.d.ts +0 -1
  44. package/dist/esm/node/api/prepareViteApiCall.js +7 -8
  45. package/dist/esm/node/cli/context.d.ts +5 -0
  46. package/dist/esm/node/cli/context.js +14 -0
  47. package/dist/esm/node/cli/entry.js +2 -0
  48. package/dist/esm/node/cli/parseCli.d.ts +3 -1
  49. package/dist/esm/node/cli/utils.d.ts +1 -0
  50. package/dist/esm/node/cli/utils.js +1 -0
  51. package/dist/esm/node/plugin/plugins/autoFullBuild.js +31 -18
  52. package/dist/esm/node/plugin/plugins/baseUrls.js +1 -1
  53. package/dist/esm/node/plugin/plugins/commonConfig.d.ts +8 -2
  54. package/dist/esm/node/plugin/plugins/commonConfig.js +8 -6
  55. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVirtualFilePageConfigs.js +3 -2
  56. package/dist/esm/node/prerender/{isPrerenderAutoRunEnabled.d.ts → context.d.ts} +4 -0
  57. package/dist/esm/node/prerender/{isPrerenderAutoRunEnabled.js → context.js} +9 -1
  58. package/dist/esm/node/prerender/runPrerender.d.ts +42 -1
  59. package/dist/esm/node/prerender/runPrerender.js +56 -34
  60. package/dist/esm/node/prerender/utils.d.ts +1 -0
  61. package/dist/esm/node/prerender/utils.js +1 -0
  62. package/dist/esm/node/runtime/globalContext.js +3 -23
  63. package/dist/esm/node/runtime/page-files/setup.js +1 -1
  64. package/dist/esm/node/runtime/utils.d.ts +1 -0
  65. package/dist/esm/node/runtime/utils.js +1 -0
  66. package/dist/esm/shared/page-configs/PageConfig.d.ts +6 -3
  67. package/dist/esm/shared/page-configs/loadConfigValues.js +6 -2
  68. package/dist/esm/utils/PROJECT_VERSION.d.ts +1 -1
  69. package/dist/esm/utils/PROJECT_VERSION.js +1 -1
  70. package/dist/esm/utils/assertSetup.js +15 -1
  71. package/dist/esm/utils/catchInfiniteLoop.d.ts +2 -0
  72. package/dist/esm/utils/catchInfiniteLoop.js +32 -0
  73. package/dist/esm/utils/isDev.js +2 -0
  74. package/dist/esm/utils/makePublicCopy.d.ts +3 -0
  75. package/dist/esm/utils/makePublicCopy.js +30 -0
  76. package/dist/esm/utils/projectInfo.d.ts +1 -1
  77. package/package.json +1 -1
@@ -2,8 +2,12 @@ export { setScrollPosition };
2
2
  export { autoSaveScrollPosition };
3
3
  export { scrollToHashOrTop };
4
4
  import { assert, onPageHide, sleep, throttle } from './utils.js';
5
- import { saveScrollPosition } from './history.js';
6
- function setScrollPosition(scrollTarget) {
5
+ import { replaceHistoryStateOriginal, saveScrollPosition } from './history.js';
6
+ function setScrollPosition(scrollTarget, url) {
7
+ if (!scrollTarget && url && hasTextFragment(url)) {
8
+ scrollToTextFragment(url);
9
+ return;
10
+ }
7
11
  if (scrollTarget && 'x' in scrollTarget) {
8
12
  setScroll(scrollTarget);
9
13
  return;
@@ -14,6 +18,21 @@ function setScrollPosition(scrollTarget) {
14
18
  const hash = getUrlHash();
15
19
  scrollToHashOrTop(hash);
16
20
  }
21
+ // https://github.com/vikejs/vike/issues/2114
22
+ // https://github.com/WICG/scroll-to-text-fragment/issues/261
23
+ function scrollToTextFragment(url) {
24
+ const stateOriginal = window.history.state;
25
+ replaceHistoryStateOriginal(null, url);
26
+ // We need `history.state===null` before location.replace() so that our 'popstate' handling is correct
27
+ assert(window.history.state === null);
28
+ // - Chrome's location.replace() keeps the current state (`history.state===stateOriginal`)
29
+ // - Firefox's location.replace() replaces the current state with `null` (`history.state===null`)
30
+ window.location.replace(url);
31
+ replaceHistoryStateOriginal(stateOriginal, url);
32
+ }
33
+ function hasTextFragment(url) {
34
+ return url.includes('#') && url.includes(':~:text');
35
+ }
17
36
  // Replicates the browser's native behavior
18
37
  function scrollToHashOrTop(hash) {
19
38
  if (!hash) {
@@ -45,9 +64,14 @@ function scrollToTop() {
45
64
  * - Let's remove it and see if users complain?
46
65
  */
47
66
  function setScroll(scrollPosition) {
48
- const scroll = () => window.scrollTo(scrollPosition.x, scrollPosition.y);
49
- const done = () => window.scrollX === scrollPosition.x && window.scrollY === scrollPosition.y;
50
- // In principle, this `done()` call should force the repaint to be finished. But that doesn't seem to be the case with `Firefox 97.0.1`.
67
+ const scroll = () => {
68
+ // `window.scrollTo()` respects the CSS `scroll-behavior: smooth` property
69
+ window.scrollTo(scrollPosition.x, scrollPosition.y);
70
+ };
71
+ const done = () => {
72
+ return window.scrollX === scrollPosition.x && window.scrollY === scrollPosition.y;
73
+ };
74
+ // In principle, this `done()` call should force the repaint to be finished, but that doesn't seem to be the case with `Firefox 97.0.1`.
51
75
  if (done())
52
76
  return;
53
77
  scroll();
@@ -21,3 +21,4 @@ export * from '../../utils/onPageVisibilityChange.js';
21
21
  export * from '../../utils/augmentType.js';
22
22
  export * from '../../utils/PROJECT_VERSION.js';
23
23
  export * from '../../utils/genPromise.js';
24
+ export * from '../../utils/catchInfiniteLoop.js';
@@ -25,3 +25,4 @@ export * from '../../utils/onPageVisibilityChange.js';
25
25
  export * from '../../utils/augmentType.js';
26
26
  export * from '../../utils/PROJECT_VERSION.js';
27
27
  export * from '../../utils/genPromise.js';
28
+ export * from '../../utils/catchInfiniteLoop.js';
@@ -2,9 +2,8 @@ export { normalizeClientSideUrl };
2
2
  import { assert, parseUrl } from './utils.js';
3
3
  /** Resolves relative URLs */
4
4
  function normalizeClientSideUrl(url, options) {
5
- // This function doesn't work for `url === '#some-hash'` because `searchOriginal` is `null` even if window.location.href has a search string.
6
- // - Thus the resolved absolute URL would be missing the search string.
7
- // - It makes sense that `parseUrl()` returns `searchOriginal === null` since there isn't any search string in `url`.
5
+ // This function doesn't work for `url === '#some-hash'` because `searchOriginal` will be missing: if window.location.href has a search string then it's going to be missing in the returned `urlCurrent` value because `parseUrl(url)` returns `searchOriginal: null` since there isn't any search string in `url`.
6
+ // - Maybe `const { searchOriginal } = parseUrl(window.location.href)` can be a fix. (Let's check how `normalizeClientSideUrl()` is being used.)
8
7
  assert(!url.startsWith('#'));
9
8
  const { searchOriginal, hashOriginal, pathname } = parseUrl(url, '/');
10
9
  let urlCurrent = `${pathname}${searchOriginal || ''}`;
@@ -1,13 +1,8 @@
1
1
  export { build };
2
- import { type Rollup } from 'vite';
3
2
  import type { APIOptions } from './types.js';
4
- type RollupOutput = Rollup.RollupOutput | Rollup.RollupOutput[] | Rollup.RollupWatcher;
5
3
  /**
6
4
  * Programmatically trigger `$ vike build`
7
5
  *
8
6
  * https://vike.dev/api#build
9
7
  */
10
- declare function build(options?: APIOptions): Promise<{
11
- rollupOutputClient: RollupOutput;
12
- rollupOutputServer: RollupOutput;
13
- }>;
8
+ declare function build(options?: APIOptions): Promise<{}>;
@@ -1,37 +1,32 @@
1
1
  export { build };
2
2
  import { prepareViteApiCall } from './prepareViteApiCall.js';
3
3
  import { build as buildVite } from 'vite';
4
- import { isPrerenderAutoRunEnabled } from '../prerender/isPrerenderAutoRunEnabled.js';
4
+ import assert from 'assert';
5
+ import { isVikeCli } from '../cli/context.js';
6
+ import { isPrerendering } from '../prerender/context.js';
5
7
  /**
6
8
  * Programmatically trigger `$ vike build`
7
9
  *
8
10
  * https://vike.dev/api#build
9
11
  */
10
12
  async function build(options = {}) {
11
- const { viteConfigEnhanced, vikeConfigGlobal } = await prepareViteApiCall(options.viteConfig, 'build');
12
- // Build client-side
13
- const outputClient = await buildVite(viteConfigEnhanced);
14
- // Build server-side
15
- const outputServer = await buildVite(setSSR(viteConfigEnhanced));
16
- // Pre-render
17
- if (isPrerenderAutoRunEnabled(vikeConfigGlobal)) {
18
- const { runPrerenderFromAutoRun } = await import('../prerender/runPrerender.js');
19
- await runPrerenderFromAutoRun(viteConfigEnhanced);
20
- }
13
+ const { viteConfigEnhanced } = await prepareViteApiCall(options.viteConfig, 'build');
14
+ // Pass it to autoFullBuild()
15
+ if (viteConfigEnhanced)
16
+ viteConfigEnhanced._viteConfigEnhanced = viteConfigEnhanced;
17
+ // 1. Build client-side
18
+ // 2. Build server-side
19
+ // > See: https://github.com/vikejs/vike/blob/c6c7533a56b3a16fc43ed644fc5c10c02d0ff375/vike/node/plugin/plugins/autoFullBuild.ts#L90
20
+ // 3. Pre-render (if enabled)
21
+ // > See: https://github.com/vikejs/vike/blob/c6c7533a56b3a16fc43ed644fc5c10c02d0ff375/vike/node/plugin/plugins/autoFullBuild.ts#L98
22
+ // > We purposely don't start the pre-rendering in this `build()` function but in a Rollup hook instead.
23
+ // > Rationale: https://github.com/vikejs/vike/issues/2123
24
+ await buildVite(viteConfigEnhanced);
25
+ // When using the Vike CLI with pre-rendering the process is forcefully exited at the end of the buildVite() call above
26
+ assert(!(isVikeCli() && isPrerendering()));
21
27
  return {
22
- /* We don't return `viteConfig` because `viteConfigEnhanced` is `InlineConfig` not `ResolvedConfig`
23
- viteConfig: viteConfigEnhanced,
24
- */
25
- rollupOutputClient: outputClient,
26
- rollupOutputServer: outputServer
27
- };
28
- }
29
- function setSSR(viteConfig) {
30
- return {
31
- ...viteConfig,
32
- build: {
33
- ...viteConfig?.build,
34
- ssr: true
35
- }
28
+ /* We don't return `viteConfig` because `viteConfigEnhanced` is `InlineConfig` not `ResolvedConfig`
29
+ viteConfig: viteConfigEnhanced,
30
+ */
36
31
  };
37
32
  }
@@ -1,7 +1,7 @@
1
- export { setOperation };
2
- export { clearOperation };
3
1
  export { isVikeCliOrApi };
2
+ export { setContextApiOperation };
3
+ export { clearContextApiOperation };
4
4
  import type { Operation } from './types.js';
5
5
  declare function isVikeCliOrApi(): boolean;
6
- declare function setOperation(operation: Operation): void;
7
- declare function clearOperation(): void;
6
+ declare function setContextApiOperation(operation: Operation): void;
7
+ declare function clearContextApiOperation(): void;
@@ -1,12 +1,9 @@
1
- // export { getOperation }
2
- export { setOperation };
3
- export { clearOperation };
4
1
  export { isVikeCliOrApi };
2
+ export { setContextApiOperation };
3
+ export { clearContextApiOperation };
5
4
  import { assert, getGlobalObject } from './utils.js';
6
- const globalObject = getGlobalObject('context.ts', {
7
- apiOperation: undefined
8
- });
9
- function getOperation() {
5
+ const globalObject = getGlobalObject('api/context.ts', {});
6
+ function getApiOperation() {
10
7
  assert(globalObject.apiOperation);
11
8
  return globalObject.apiOperation;
12
9
  }
@@ -14,10 +11,10 @@ function isVikeCliOrApi() {
14
11
  // The CLI uses the API
15
12
  return !!globalObject.apiOperation;
16
13
  }
17
- function setOperation(operation) {
14
+ function setContextApiOperation(operation) {
18
15
  assert(!globalObject.apiOperation);
19
16
  globalObject.apiOperation = operation;
20
17
  }
21
- function clearOperation() {
18
+ function clearContextApiOperation() {
22
19
  globalObject.apiOperation = undefined;
23
20
  }
@@ -6,7 +6,6 @@ import type { InlineConfig, ResolvedConfig } from 'vite';
6
6
  import type { Operation } from './types.js';
7
7
  declare function prepareViteApiCall(viteConfig: InlineConfig | undefined, operation: Operation): Promise<{
8
8
  viteConfigEnhanced: InlineConfig | undefined;
9
- vikeConfigGlobal: import("../plugin/plugins/importUserCode/v1-design/getVikeConfig.js").VikeConfigGlobal;
10
9
  }>;
11
10
  declare function getViteRoot(operation: 'build' | 'dev' | 'preview' | 'prerender'): Promise<string>;
12
11
  declare function normalizeViteRoot(root: string): string;
@@ -4,7 +4,7 @@ export { assertViteRoot };
4
4
  export { normalizeViteRoot };
5
5
  // TODO: enable Vike extensions to add Vite plugins
6
6
  import { loadConfigFromFile, mergeConfig, resolveConfig } from 'vite';
7
- import { clearOperation, setOperation } from './context.js';
7
+ import { clearContextApiOperation, setContextApiOperation } from './context.js';
8
8
  import { getVikeConfig2 } from '../plugin/plugins/importUserCode/v1-design/getVikeConfig.js';
9
9
  import path from 'path';
10
10
  import { assert, assertUsage, getGlobalObject, isObject, toPosixPath } from './utils.js';
@@ -13,31 +13,30 @@ import { clearGlobalContext } from '../runtime/globalContext.js';
13
13
  const globalObject = getGlobalObject('prepareViteApiCall.ts', {});
14
14
  async function prepareViteApiCall(viteConfig, operation) {
15
15
  clear();
16
- setOperation(operation);
16
+ setContextApiOperation(operation);
17
17
  return enhanceViteConfig(viteConfig, operation);
18
18
  }
19
19
  // For subsequent API calls, e.g. calling prerender() after build()
20
20
  function clear() {
21
- clearOperation();
21
+ clearContextApiOperation();
22
22
  clearGlobalContext();
23
23
  }
24
24
  async function enhanceViteConfig(viteConfig, operation) {
25
25
  const viteInfo = await getInfoFromVite(viteConfig, operation);
26
26
  await assertViteRoot2(viteInfo.root, viteInfo.viteConfigEnhanced, operation);
27
27
  const vikeConfig = await getVikeConfig2(viteInfo.root, operation === 'dev', viteInfo.vikeVitePluginOptions);
28
- const viteConfigEnhanced = addViteSettingsSetByUser(viteInfo.viteConfigEnhanced, vikeConfig);
28
+ const viteConfigEnhanced = addViteSettingsSetByVikeConfig(viteInfo.viteConfigEnhanced, vikeConfig);
29
29
  return {
30
- viteConfigEnhanced,
31
- vikeConfigGlobal: vikeConfig.vikeConfigGlobal
30
+ viteConfigEnhanced
32
31
  };
33
32
  }
34
- function addViteSettingsSetByUser(viteConfigEnhanced, vikeConfig) {
33
+ function addViteSettingsSetByVikeConfig(viteConfigEnhanced, vikeConfig) {
35
34
  const viteConfigs = vikeConfig.global.from.configsCumulative.vite;
36
35
  if (!viteConfigs)
37
36
  return viteConfigEnhanced;
38
37
  viteConfigs.values.forEach((v) => {
39
38
  assertUsage(isObject(v.value), `${v.definedAt} should be an object`);
40
- viteConfigEnhanced = mergeConfig(v.value, viteConfigEnhanced ?? {});
39
+ viteConfigEnhanced = mergeConfig(viteConfigEnhanced ?? {}, v.value);
41
40
  });
42
41
  return viteConfigEnhanced;
43
42
  }
@@ -0,0 +1,5 @@
1
+ export { isVikeCli };
2
+ export { setContextCliCommand };
3
+ import type { Command } from './parseCli.js';
4
+ declare function isVikeCli(): boolean;
5
+ declare function setContextCliCommand(command: Command): void;
@@ -0,0 +1,14 @@
1
+ export { isVikeCli };
2
+ export { setContextCliCommand };
3
+ import { assert, getGlobalObject } from './utils.js';
4
+ const globalObject = getGlobalObject('cli/context.ts', {});
5
+ function getCliCommand() {
6
+ return globalObject.cliCommand;
7
+ }
8
+ function isVikeCli() {
9
+ return !!globalObject.cliCommand;
10
+ }
11
+ function setContextCliCommand(command) {
12
+ assert(!globalObject.cliCommand);
13
+ globalObject.cliCommand = command;
14
+ }
@@ -2,9 +2,11 @@ import { projectInfo } from './utils.js';
2
2
  import { dev, build, preview } from '../api/index.js';
3
3
  import pc from '@brillout/picocolors';
4
4
  import { parseCli } from './parseCli.js';
5
+ import { setContextCliCommand } from './context.js';
5
6
  cli();
6
7
  async function cli() {
7
8
  const { command } = parseCli();
9
+ setContextCliCommand(command);
8
10
  if (command === 'dev') {
9
11
  await cmdDev();
10
12
  }
@@ -1,4 +1,6 @@
1
1
  export { parseCli };
2
+ export type { Command };
3
+ type Command = 'dev' | 'build' | 'preview' | 'prerender';
2
4
  declare function parseCli(): {
3
- command: "build" | "dev" | "preview" | "prerender";
5
+ command: Command;
4
6
  };
@@ -1,3 +1,4 @@
1
1
  export * from '../../utils/assert.js';
2
2
  export * from '../../utils/projectInfo.js';
3
3
  export * from '../../utils/includes.js';
4
+ export * from '../../utils/getGlobalObject.js';
@@ -5,3 +5,4 @@ onLoad();
5
5
  export * from '../../utils/assert.js';
6
6
  export * from '../../utils/projectInfo.js';
7
7
  export * from '../../utils/includes.js';
8
+ export * from '../../utils/getGlobalObject.js';
@@ -3,12 +3,14 @@ export { autoFullBuild };
3
3
  import { build } from 'vite';
4
4
  import { assertWarning } from '../utils.js';
5
5
  import { runPrerenderFromAutoRun, runPrerender_forceExit } from '../../prerender/runPrerender.js';
6
- import { isPrerenderAutoRunEnabled } from '../../prerender/isPrerenderAutoRunEnabled.js';
6
+ import { isPrerenderAutoRunEnabled } from '../../prerender/context.js';
7
7
  import { isViteCliCall, getViteConfigFromCli } from '../shared/isViteCliCall.js';
8
8
  import pc from '@brillout/picocolors';
9
9
  import { logErrorHint } from '../../runtime/renderPage/logErrorHint.js';
10
10
  import { manifestTempFile } from './buildConfig.js';
11
11
  import { getVikeConfig } from './importUserCode/v1-design/getVikeConfig.js';
12
+ import { isVikeCliOrApi } from '../../api/context.js';
13
+ import { isVikeCli } from '../../cli/context.js';
12
14
  let forceExit = false;
13
15
  function autoFullBuild() {
14
16
  let config;
@@ -69,22 +71,22 @@ async function triggerFullBuild(config, vikeConfigGlobal, bundle) {
69
71
  if (!bundle[manifestTempFile])
70
72
  return;
71
73
  const configFromCli = !isViteCliCall() ? null : getViteConfigFromCli();
72
- const configInline = {
73
- ...configFromCli,
74
- configFile: configFromCli?.configFile || config.configFile,
75
- root: config.root,
76
- build: {
77
- ...configFromCli?.build
78
- }
79
- };
80
- try {
81
- await build({
82
- ...configInline,
74
+ let configInline;
75
+ if (config._viteConfigEnhanced) {
76
+ configInline = config._viteConfigEnhanced;
77
+ }
78
+ else {
79
+ configInline = {
80
+ ...configFromCli,
81
+ configFile: configFromCli?.configFile || config.configFile,
82
+ root: config.root,
83
83
  build: {
84
- ...configInline.build,
85
- ssr: true
84
+ ...configFromCli?.build
86
85
  }
87
- });
86
+ };
87
+ }
88
+ try {
89
+ await build(setSSR(configInline));
88
90
  }
89
91
  catch (err) {
90
92
  console.error(err);
@@ -92,10 +94,20 @@ async function triggerFullBuild(config, vikeConfigGlobal, bundle) {
92
94
  process.exit(1);
93
95
  }
94
96
  if (isPrerenderAutoRunEnabled(vikeConfigGlobal)) {
95
- await runPrerenderFromAutoRun(configInline);
96
- forceExit = true;
97
+ const { prerenderContextPublic } = await runPrerenderFromAutoRun(configInline);
98
+ config.vike.prerenderContext = prerenderContextPublic;
99
+ forceExit = isVikeCli() || isViteCliCall();
97
100
  }
98
101
  }
102
+ function setSSR(configInline) {
103
+ return {
104
+ ...configInline,
105
+ build: {
106
+ ...configInline.build,
107
+ ssr: true
108
+ }
109
+ };
110
+ }
99
111
  function abortViteBuildSsr(vikeConfigGlobal) {
100
112
  if (vikeConfigGlobal.disableAutoFullBuild !== true && isViteCliCall() && getViteConfigFromCli()?.build.ssr) {
101
113
  assertWarning(false, `The CLI call ${pc.cyan('$ vite build --ssr')} is superfluous since ${pc.cyan('$ vite build')} also builds the server-side. If you want two separate build steps then use https://vike.dev/disableAutoFullBuild or use Vite's ${pc.cyan('build()')} API.`, { onlyOnce: true });
@@ -105,7 +117,8 @@ function abortViteBuildSsr(vikeConfigGlobal) {
105
117
  function isDisabled(vikeConfigGlobal) {
106
118
  const { disableAutoFullBuild } = vikeConfigGlobal;
107
119
  if (disableAutoFullBuild === null || disableAutoFullBuild === 'prerender') {
108
- return !isViteCliCall();
120
+ const isViteApi = !isViteCliCall() && !isVikeCliOrApi();
121
+ return isViteApi;
109
122
  }
110
123
  else {
111
124
  return disableAutoFullBuild;
@@ -11,7 +11,7 @@ function baseUrls() {
11
11
  const isDev = config._isDev;
12
12
  assert(typeof isDev === 'boolean');
13
13
  const baseViteOriginal = config.base ?? '/__UNSET__'; // '/__UNSET__' because Vite resolves `_baseViteOriginal: null` to `undefined`
14
- basesResolved = resolveBase(baseViteOriginal, config.vike.vikeConfigGlobal.baseServer, config.vike.vikeConfigGlobal.baseAssets);
14
+ basesResolved = resolveBase(baseViteOriginal, config._vikeConfigGlobal.baseServer, config._vikeConfigGlobal.baseAssets);
15
15
  // We cannot define these in configResolved() because Vite picks up the env variables before any configResolved() hook is called
16
16
  process.env.BASE_SERVER = basesResolved.baseServer;
17
17
  process.env.BASE_ASSETS = basesResolved.baseAssets;
@@ -1,12 +1,18 @@
1
1
  export { commonConfig };
2
- import { type Plugin } from 'vite';
2
+ import { type InlineConfig, type Plugin } from 'vite';
3
3
  import { type VikeConfigObject } from './importUserCode/v1-design/getVikeConfig.js';
4
+ import type { PrerenderContextPublic } from '../../prerender/runPrerender.js';
4
5
  declare module 'vite' {
5
6
  interface UserConfig {
6
7
  _isDev?: boolean;
7
8
  _vikeVitePluginOptions?: unknown;
8
9
  _root?: string;
9
- vike?: VikeConfigObject;
10
+ _viteConfigEnhanced?: InlineConfig;
11
+ vike?: {
12
+ global: VikeConfigObject['global'];
13
+ prerenderContext?: PrerenderContextPublic;
14
+ };
15
+ _vikeConfigGlobal?: VikeConfigObject['vikeConfigGlobal'];
10
16
  }
11
17
  }
12
18
  declare function commonConfig(vikeVitePluginOptions: unknown): Plugin[];
@@ -11,7 +11,7 @@ import { isViteCliCall } from '../shared/isViteCliCall.js';
11
11
  import { isVikeCliOrApi } from '../../api/context.js';
12
12
  import { getVikeConfig2 } from './importUserCode/v1-design/getVikeConfig.js';
13
13
  import { assertViteRoot, getViteRoot, normalizeViteRoot } from '../../api/prepareViteApiCall.js';
14
- import { temp_disablePrerenderAutoRun } from '../../prerender/isPrerenderAutoRunEnabled.js';
14
+ import { temp_disablePrerenderAutoRun } from '../../prerender/context.js';
15
15
  const pluginName = 'vike:commonConfig';
16
16
  function commonConfig(vikeVitePluginOptions) {
17
17
  return [
@@ -30,9 +30,11 @@ function commonConfig(vikeVitePluginOptions) {
30
30
  _isDev: isDev,
31
31
  _root: root,
32
32
  _vikeVitePluginOptions: vikeVitePluginOptions,
33
- vike: vikeConfig,
34
- // TODO/v1-release: remove
35
- configVikePromise: Promise.resolve(vikeConfig.vikeConfigGlobal)
33
+ vike: { global: vikeConfig.global },
34
+ // TODO/now: remove
35
+ _vikeConfigGlobal: vikeConfig.vikeConfigGlobal,
36
+ // TODO/v1-release: remove https://github.com/vikejs/vike/issues/2122
37
+ configVikePromise: Promise.resolve({ prerender: !!vikeConfig.vikeConfigGlobal.prerender })
36
38
  };
37
39
  }
38
40
  }
@@ -75,7 +77,7 @@ function commonConfig(vikeVitePluginOptions) {
75
77
  // VITE_CONFIG
76
78
  const configFromEnvVar = getEnvVarObject('VITE_CONFIG');
77
79
  if (configFromEnvVar)
78
- configFromVike = mergeConfig(configFromEnvVar, configFromVike);
80
+ configFromVike = mergeConfig(configFromVike, configFromEnvVar);
79
81
  return configFromVike;
80
82
  }
81
83
  }
@@ -142,7 +144,7 @@ function assertVikeCliOrApi(config) {
142
144
  onlyOnce: true
143
145
  });
144
146
  }
145
- // TODO/v1-release: remove
147
+ // TODO/v1-release: remove https://github.com/vikejs/vike/issues/2122
146
148
  function temp_supportOldInterface(config) {
147
149
  if (!('vitePluginSsr' in config))
148
150
  return;
@@ -26,12 +26,13 @@ function getCodePageConfigsSerialized(pageConfigs, isForClientSide, isClientRout
26
26
  const lines = [];
27
27
  pageConfigs.forEach((pageConfig) => {
28
28
  const { pageId, routeFilesystem, isErrorPage } = pageConfig;
29
- const virtualFileIdPageConfigValuesAll = getVirtualFileIdPageConfigValuesAll(pageId, isForClientSide);
30
29
  lines.push(` {`);
31
30
  lines.push(` pageId: ${JSON.stringify(pageId)},`);
32
31
  lines.push(` isErrorPage: ${JSON.stringify(isErrorPage)},`);
33
32
  lines.push(` routeFilesystem: ${JSON.stringify(routeFilesystem)},`);
34
- lines.push(` loadConfigValuesAll: () => import(${JSON.stringify(virtualFileIdPageConfigValuesAll)}),`);
33
+ const virtualFileId = JSON.stringify(getVirtualFileIdPageConfigValuesAll(pageId, isForClientSide));
34
+ const load = `() => ({ moduleId: ${virtualFileId}, moduleExports: import(${virtualFileId}) })`;
35
+ lines.push(` loadConfigValuesAll: ${load},`);
35
36
  lines.push(` configValuesSerialized: {`);
36
37
  lines.push(...serializeConfigValues(pageConfig, importStatements, (configEnv) => isRuntimeEnvMatch(configEnv, { isForClientSide, isClientRouting, isDev }), ' ', true));
37
38
  lines.push(` },`);
@@ -1,5 +1,9 @@
1
1
  export { isPrerenderAutoRunEnabled };
2
2
  export { temp_disablePrerenderAutoRun };
3
+ export { isPrerendering };
4
+ export { setContextIsPrerendering };
3
5
  import type { VikeConfigGlobal } from '../plugin/plugins/importUserCode/v1-design/getVikeConfig.js';
4
6
  declare function isPrerenderAutoRunEnabled(vikeConfigGlobal: VikeConfigGlobal): boolean;
5
7
  declare function temp_disablePrerenderAutoRun(): void;
8
+ declare function isPrerendering(): boolean;
9
+ declare function setContextIsPrerendering(): void;
@@ -1,7 +1,9 @@
1
1
  export { isPrerenderAutoRunEnabled };
2
2
  export { temp_disablePrerenderAutoRun };
3
+ export { isPrerendering };
4
+ export { setContextIsPrerendering };
3
5
  import { getGlobalObject } from '../../utils/getGlobalObject.js';
4
- const globalObject = getGlobalObject('isPrerenderAutoRunEnabled.ts', {});
6
+ const globalObject = getGlobalObject('prerender/context.ts', {});
5
7
  function isPrerenderAutoRunEnabled(vikeConfigGlobal) {
6
8
  return (vikeConfigGlobal.prerender &&
7
9
  !vikeConfigGlobal.prerender.disableAutoRun &&
@@ -12,3 +14,9 @@ function isPrerenderAutoRunEnabled(vikeConfigGlobal) {
12
14
  function temp_disablePrerenderAutoRun() {
13
15
  globalObject.isDisabled = true;
14
16
  }
17
+ function isPrerendering() {
18
+ return !!globalObject.isPrerendering;
19
+ }
20
+ function setContextIsPrerendering() {
21
+ globalObject.isPrerendering = true;
22
+ }
@@ -3,8 +3,47 @@ export { runPrerenderFromCLIPrerenderCommand };
3
3
  export { runPrerenderFromAutoRun };
4
4
  export { runPrerender_forceExit };
5
5
  export type { PrerenderOptions };
6
+ export type { PrerenderContextPublic };
7
+ import { PageContextInitEnhanced } from '../runtime/renderPage/renderPageAlreadyRouted.js';
6
8
  import type { InlineConfig, ResolvedConfig } from 'vite';
9
+ import { PageContextUrlInternal } from '../../shared/getPageContextUrlComputed.js';
7
10
  import type { APIOptions } from '../api/types.js';
11
+ type ProvidedByHook = null | {
12
+ hookFilePath: string;
13
+ hookName: 'onBeforePrerenderStart' | 'prerender';
14
+ };
15
+ type TransformerHook = {
16
+ hookFilePath: string;
17
+ hookName: 'onPrerenderStart' | 'onBeforePrerender';
18
+ };
19
+ type PageContextPrerendered = {
20
+ urlOriginal: string;
21
+ _providedByHook?: ProvidedByHook;
22
+ };
23
+ type PrerenderedPageContexts = Record<string, PageContextPrerendered>;
24
+ type PrerenderContext = {
25
+ pageContexts: PageContext[];
26
+ pageContextInit: Record<string, unknown> | null;
27
+ noExtraDir: boolean;
28
+ prerenderedPageContexts: PrerenderedPageContexts;
29
+ output: Output;
30
+ };
31
+ type Output = {
32
+ filePath: string;
33
+ fileType: FileType;
34
+ fileContent: string;
35
+ pageContext: PageContextPrerendered;
36
+ }[];
37
+ type FileType = 'HTML' | 'JSON';
38
+ type PrerenderContextPublic = Pick<PrerenderContext, 'pageContexts'>;
39
+ type PageContext = PageContextInitEnhanced & {
40
+ _urlRewrite: null;
41
+ _urlHandler: null;
42
+ _urlOriginalBeforeHook?: string;
43
+ _urlOriginalModifiedByHook?: TransformerHook;
44
+ _providedByHook: ProvidedByHook;
45
+ _pageContextAlreadyProvidedByOnPrerenderHook?: true;
46
+ } & PageContextUrlInternal;
8
47
  type PrerenderOptions = APIOptions & {
9
48
  /** Initial `pageContext` values */
10
49
  pageContextInit?: Record<string, unknown>;
@@ -29,5 +68,7 @@ declare function runPrerenderFromAPI(options?: PrerenderOptions): Promise<{
29
68
  viteConfig: ResolvedConfig;
30
69
  }>;
31
70
  declare function runPrerenderFromCLIPrerenderCommand(): Promise<void>;
32
- declare function runPrerenderFromAutoRun(viteConfig: InlineConfig | undefined): Promise<void>;
71
+ declare function runPrerenderFromAutoRun(viteConfig: InlineConfig | undefined): Promise<{
72
+ prerenderContextPublic: PrerenderContextPublic;
73
+ }>;
33
74
  declare function runPrerender_forceExit(): void;