vike 0.4.224 → 0.4.225-commit-2b7971f

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 (163) hide show
  1. package/dist/cjs/client/shared/getPageContextProxyForUser.js +3 -66
  2. package/dist/cjs/node/api/build.js +1 -1
  3. package/dist/cjs/node/api/context.js +4 -4
  4. package/dist/cjs/node/api/dev.js +1 -1
  5. package/dist/cjs/node/api/prepareViteApiCall.js +25 -14
  6. package/dist/cjs/node/api/prerender.js +1 -1
  7. package/dist/cjs/node/api/preview.js +1 -1
  8. package/dist/cjs/node/cli/context.js +5 -4
  9. package/dist/cjs/node/cli/entry.js +3 -3
  10. package/dist/cjs/node/cli/parseCli.js +43 -15
  11. package/dist/cjs/node/cli/utils.js +1 -1
  12. package/dist/cjs/node/plugin/plugins/build/pluginBuildEntry.js +1 -1
  13. package/dist/cjs/node/plugin/plugins/commonConfig.js +16 -11
  14. package/dist/cjs/node/plugin/plugins/devConfig/determineOptimizeDeps.js +6 -4
  15. package/dist/cjs/node/plugin/plugins/devConfig/index.js +1 -1
  16. package/dist/cjs/node/plugin/plugins/importUserCode/index.js +14 -10
  17. package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/configDefinitionsBuiltIn.js +10 -2
  18. package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/transpileAndExecuteFile.js +11 -12
  19. package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.js +139 -84
  20. package/dist/cjs/node/plugin/plugins/previewConfig.js +12 -7
  21. package/dist/cjs/node/plugin/plugins/setGlobalContext.js +10 -1
  22. package/dist/cjs/node/plugin/shared/addSsrMiddleware.js +5 -1
  23. package/dist/cjs/node/plugin/shared/getEnvVarObject.js +9 -8
  24. package/dist/cjs/node/plugin/shared/loggerNotProd/log.js +1 -1
  25. package/dist/cjs/node/prerender/context.js +1 -1
  26. package/dist/cjs/node/prerender/resolvePrerenderConfig.js +33 -18
  27. package/dist/cjs/node/prerender/runPrerender.js +12 -18
  28. package/dist/cjs/node/prerender/utils.js +1 -1
  29. package/dist/cjs/node/runtime/globalContext.js +9 -5
  30. package/dist/cjs/node/runtime/html/injectAssets/getViteDevScript.js +6 -3
  31. package/dist/cjs/node/runtime/html/serializePageContextClientSide.js +67 -14
  32. package/dist/cjs/node/runtime/html/stream.js +7 -0
  33. package/dist/cjs/node/runtime/renderPage/createHttpResponse/assertNoInfiniteHttpRedirect.js +14 -5
  34. package/dist/cjs/node/runtime/renderPage/createHttpResponse.js +2 -4
  35. package/dist/cjs/node/runtime/renderPage/logErrorHint.js +6 -1
  36. package/dist/cjs/node/runtime/renderPage/preparePageContextForUserConsumptionServerSide.js +3 -0
  37. package/dist/cjs/node/runtime/renderPage/renderPageAlreadyRouted.js +18 -12
  38. package/dist/cjs/node/runtime/renderPage.js +19 -38
  39. package/dist/cjs/node/runtime/utils.js +0 -1
  40. package/dist/cjs/node/runtime-dev/createDevMiddleware.js +10 -8
  41. package/dist/cjs/node/shared/assertV1Design.js +2 -1
  42. package/dist/cjs/node/shared/utils.js +0 -1
  43. package/dist/cjs/shared/NOT_SERIALIZABLE.js +5 -0
  44. package/dist/cjs/shared/page-configs/getConfigDefinedAt.js +19 -2
  45. package/dist/cjs/shared/page-configs/getConfigValueBuildTime.js +8 -5
  46. package/dist/cjs/shared/page-configs/helpers.js +1 -1
  47. package/dist/cjs/shared/page-configs/serialize/parsePageConfigs.js +2 -5
  48. package/dist/cjs/shared/page-configs/serialize/serializeConfigValues.js +9 -5
  49. package/dist/cjs/shared/route/loadPageRoutes.js +2 -2
  50. package/dist/cjs/utils/PROJECT_VERSION.js +1 -1
  51. package/dist/cjs/utils/assert.js +12 -5
  52. package/dist/cjs/utils/assertSingleInstance.js +11 -17
  53. package/dist/cjs/utils/debug.js +2 -1
  54. package/dist/cjs/utils/getRandomId.js +1 -1
  55. package/dist/cjs/utils/normalizeHeaders.js +1 -1
  56. package/dist/cjs/utils/objectAssign.js +7 -2
  57. package/dist/cjs/utils/parseUrl-extras.js +1 -0
  58. package/dist/esm/client/client-routing-runtime/createPageContext.d.ts +3 -1
  59. package/dist/esm/client/client-routing-runtime/createPageContext.js +4 -3
  60. package/dist/esm/client/client-routing-runtime/renderPageClientSide.js +1 -1
  61. package/dist/esm/client/server-routing-runtime/getPageContext.d.ts +6 -3
  62. package/dist/esm/client/server-routing-runtime/getPageContext.js +6 -3
  63. package/dist/esm/client/shared/getPageContextProxyForUser.d.ts +1 -11
  64. package/dist/esm/client/shared/getPageContextProxyForUser.js +4 -67
  65. package/dist/esm/client/shared/preparePageContextForUserConsumptionClientSide.d.ts +2 -2
  66. package/dist/esm/node/api/build.js +1 -1
  67. package/dist/esm/node/api/context.d.ts +8 -2
  68. package/dist/esm/node/api/context.js +4 -4
  69. package/dist/esm/node/api/dev.js +1 -1
  70. package/dist/esm/node/api/prepareViteApiCall.d.ts +2 -2
  71. package/dist/esm/node/api/prepareViteApiCall.js +25 -14
  72. package/dist/esm/node/api/prerender.js +1 -1
  73. package/dist/esm/node/api/preview.js +1 -1
  74. package/dist/esm/node/api/types.d.ts +7 -0
  75. package/dist/esm/node/cli/context.d.ts +4 -2
  76. package/dist/esm/node/cli/context.js +5 -4
  77. package/dist/esm/node/cli/entry.js +4 -4
  78. package/dist/esm/node/cli/parseCli.d.ts +3 -0
  79. package/dist/esm/node/cli/parseCli.js +44 -16
  80. package/dist/esm/node/cli/utils.d.ts +1 -1
  81. package/dist/esm/node/cli/utils.js +1 -1
  82. package/dist/esm/node/plugin/plugins/build/pluginBuildEntry.js +2 -2
  83. package/dist/esm/node/plugin/plugins/commonConfig.d.ts +1 -0
  84. package/dist/esm/node/plugin/plugins/commonConfig.js +16 -11
  85. package/dist/esm/node/plugin/plugins/devConfig/determineOptimizeDeps.js +6 -4
  86. package/dist/esm/node/plugin/plugins/devConfig/index.js +1 -1
  87. package/dist/esm/node/plugin/plugins/importUserCode/index.js +15 -11
  88. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/configDefinitionsBuiltIn.d.ts +3 -1
  89. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/configDefinitionsBuiltIn.js +10 -2
  90. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/transpileAndExecuteFile.d.ts +5 -2
  91. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/transpileAndExecuteFile.js +11 -12
  92. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.d.ts +3 -2
  93. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.js +141 -85
  94. package/dist/esm/node/plugin/plugins/previewConfig.js +12 -7
  95. package/dist/esm/node/plugin/plugins/setGlobalContext.js +11 -2
  96. package/dist/esm/node/plugin/shared/addSsrMiddleware.d.ts +1 -1
  97. package/dist/esm/node/plugin/shared/addSsrMiddleware.js +5 -1
  98. package/dist/esm/node/plugin/shared/getEnvVarObject.d.ts +3 -1
  99. package/dist/esm/node/plugin/shared/getEnvVarObject.js +9 -8
  100. package/dist/esm/node/plugin/shared/loggerNotProd/log.js +2 -2
  101. package/dist/esm/node/prerender/context.js +1 -1
  102. package/dist/esm/node/prerender/resolvePrerenderConfig.d.ts +2 -1
  103. package/dist/esm/node/prerender/resolvePrerenderConfig.js +34 -19
  104. package/dist/esm/node/prerender/runPrerender.js +13 -19
  105. package/dist/esm/node/prerender/utils.d.ts +1 -1
  106. package/dist/esm/node/prerender/utils.js +1 -1
  107. package/dist/esm/node/runtime/globalContext.d.ts +1 -1
  108. package/dist/esm/node/runtime/globalContext.js +10 -6
  109. package/dist/esm/node/runtime/html/injectAssets/getViteDevScript.js +6 -3
  110. package/dist/esm/node/runtime/html/serializePageContextClientSide.d.ts +2 -0
  111. package/dist/esm/node/runtime/html/serializePageContextClientSide.js +69 -15
  112. package/dist/esm/node/runtime/html/stream.js +7 -0
  113. package/dist/esm/node/runtime/index-deprecated.d.ts +1 -3
  114. package/dist/esm/node/runtime/renderPage/createHttpResponse/assertNoInfiniteHttpRedirect.d.ts +3 -1
  115. package/dist/esm/node/runtime/renderPage/createHttpResponse/assertNoInfiniteHttpRedirect.js +15 -6
  116. package/dist/esm/node/runtime/renderPage/createHttpResponse.d.ts +3 -1
  117. package/dist/esm/node/runtime/renderPage/createHttpResponse.js +2 -4
  118. package/dist/esm/node/runtime/renderPage/logErrorHint.js +6 -1
  119. package/dist/esm/node/runtime/renderPage/preparePageContextForUserConsumptionServerSide.js +3 -0
  120. package/dist/esm/node/runtime/renderPage/renderPageAlreadyRouted.d.ts +32 -6
  121. package/dist/esm/node/runtime/renderPage/renderPageAlreadyRouted.js +18 -12
  122. package/dist/esm/node/runtime/renderPage.js +21 -40
  123. package/dist/esm/node/runtime/utils.d.ts +0 -1
  124. package/dist/esm/node/runtime/utils.js +0 -1
  125. package/dist/esm/node/runtime-dev/createDevMiddleware.js +10 -8
  126. package/dist/esm/node/shared/assertV1Design.js +2 -1
  127. package/dist/esm/node/shared/utils.d.ts +0 -1
  128. package/dist/esm/node/shared/utils.js +0 -1
  129. package/dist/esm/shared/NOT_SERIALIZABLE.d.ts +1 -0
  130. package/dist/esm/shared/NOT_SERIALIZABLE.js +2 -0
  131. package/dist/esm/shared/page-configs/Config.d.ts +23 -3
  132. package/dist/esm/shared/page-configs/PageConfig.d.ts +17 -6
  133. package/dist/esm/shared/page-configs/getConfigDefinedAt.d.ts +5 -3
  134. package/dist/esm/shared/page-configs/getConfigDefinedAt.js +20 -3
  135. package/dist/esm/shared/page-configs/getConfigValueBuildTime.js +8 -5
  136. package/dist/esm/shared/page-configs/helpers.js +1 -1
  137. package/dist/esm/shared/page-configs/serialize/parsePageConfigs.d.ts +1 -1
  138. package/dist/esm/shared/page-configs/serialize/parsePageConfigs.js +2 -5
  139. package/dist/esm/shared/page-configs/serialize/serializeConfigValues.d.ts +3 -3
  140. package/dist/esm/shared/page-configs/serialize/serializeConfigValues.js +9 -5
  141. package/dist/esm/shared/route/abort.d.ts +2 -2
  142. package/dist/esm/shared/route/loadPageRoutes.js +2 -2
  143. package/dist/esm/shared/types.d.ts +39 -3
  144. package/dist/esm/types/index.d.ts +2 -0
  145. package/dist/esm/utils/PROJECT_VERSION.d.ts +1 -1
  146. package/dist/esm/utils/PROJECT_VERSION.js +1 -1
  147. package/dist/esm/utils/assert.d.ts +2 -1
  148. package/dist/esm/utils/assert.js +12 -5
  149. package/dist/esm/utils/assertSingleInstance.js +11 -17
  150. package/dist/esm/utils/debug.js +2 -1
  151. package/dist/esm/utils/getRandomId.d.ts +1 -1
  152. package/dist/esm/utils/getRandomId.js +1 -1
  153. package/dist/esm/utils/normalizeHeaders.js +1 -1
  154. package/dist/esm/utils/objectAssign.d.ts +1 -1
  155. package/dist/esm/utils/objectAssign.js +7 -2
  156. package/dist/esm/utils/parseUrl-extras.js +1 -0
  157. package/package.json +6 -3
  158. package/dist/cjs/shared/notSerializable.js +0 -5
  159. package/dist/cjs/utils/projectInfo.js +0 -8
  160. package/dist/esm/shared/notSerializable.d.ts +0 -1
  161. package/dist/esm/shared/notSerializable.js +0 -2
  162. package/dist/esm/utils/projectInfo.d.ts +0 -4
  163. package/dist/esm/utils/projectInfo.js +0 -5
@@ -9,13 +9,16 @@ import * as virtualFileExports from 'virtual:vike:importUserCode:client:server-r
9
9
  const { pageFilesAll, pageConfigs, pageConfigGlobal } = getPageConfigsRuntime(virtualFileExports);
10
10
  const urlFirst = getCurrentUrl({ withoutHash: true });
11
11
  async function getPageContext() {
12
- const pageContext = getPageContextSerializedInHtml();
13
- objectAssign(pageContext, {
12
+ const pageContext = {
13
+ _isPageContextObject: true,
14
+ isPrerendering: false,
15
+ isClientSide: true,
14
16
  isHydration: true,
15
17
  isBackwardNavigation: null,
16
18
  _hasPageContextFromServer: true,
17
19
  _hasPageContextFromClient: false
18
- });
20
+ };
21
+ objectAssign(pageContext, getPageContextSerializedInHtml());
19
22
  objectAssign(pageContext, await loadPageUserFiles(pageContext.pageId));
20
23
  assertPristineUrl();
21
24
  return pageContext;
@@ -1,12 +1,2 @@
1
1
  export { getPageContextProxyForUser };
2
- export { PageContextForPassToClientWarning };
3
- type PageContextForPassToClientWarning = {
4
- _hasPageContextFromServer: boolean;
5
- _hasPageContextFromClient: boolean;
6
- };
7
- /**
8
- * Throw error when pageContext value isn't:
9
- * - serializable, or
10
- * - defined.
11
- */
12
- declare function getPageContextProxyForUser<PageContext extends Record<string, unknown> & PageContextForPassToClientWarning>(pageContext: PageContext): PageContext;
2
+ declare function getPageContextProxyForUser<PageContext extends Record<string, unknown>>(pageContext: PageContext): PageContext;
@@ -1,77 +1,14 @@
1
1
  export { getPageContextProxyForUser };
2
- import { assert, assertUsage, assertWarning, getGlobalObject, getPropAccessNotation } from '../server-routing-runtime/utils.js';
3
- import { notSerializable } from '../../shared/notSerializable.js';
4
- const globalObject = getGlobalObject('shared/getPageContextProxyForUser.ts', {});
5
- /**
6
- * Throw error when pageContext value isn't:
7
- * - serializable, or
8
- * - defined.
9
- */
2
+ import { assertUsage, getPropAccessNotation } from '../server-routing-runtime/utils.js';
3
+ import { NOT_SERIALIZABLE } from '../../shared/NOT_SERIALIZABLE.js';
4
+ // Throw error when pageContext value isn't serializable
10
5
  function getPageContextProxyForUser(pageContext) {
11
- assert([true, false].includes(pageContext._hasPageContextFromServer));
12
- assert([true, false].includes(pageContext._hasPageContextFromClient));
13
6
  return new Proxy(pageContext, {
14
7
  get(_, prop) {
15
8
  const val = pageContext[prop];
16
9
  const propName = getPropAccessNotation(prop);
17
- assertUsage(val !== notSerializable, `Can't access pageContext${propName} on the client side. Because it can't be serialized, see server logs.`);
18
- passToClientHint(pageContext, prop, propName);
10
+ assertUsage(val !== NOT_SERIALIZABLE, `Can't access pageContext${propName} on the client side. Because it can't be serialized, see server logs.`);
19
11
  return val;
20
12
  }
21
13
  });
22
14
  }
23
- function passToClientHint(pageContext, prop, propName) {
24
- if (handleVueReactivity(prop))
25
- return;
26
- // `prop in pageContext` is the trick we use to know the passToClient value on the client-side, as we set a value to all passToClient props, even `undefined` ones:
27
- // ```html
28
- // <script id="vike_pageContext" type="application/json">{"pageProps":"!undefined"}</script>
29
- // ```
30
- if (prop in pageContext)
31
- return;
32
- if (isWhitelisted(prop))
33
- return;
34
- // The trick described above (`prop in pageContext`) doesn't work if Vike doesn't fetch any pageContext from the server.
35
- // - There would still be some value to show a warning, but it isn't worth it because of the confusion that the first recommendation (adding `prop` to `passToClient`) wouldn't actually remove the warning, and only the second recommendation (using `prop in pageContext` instead of `pageContext[prop]`) would work.
36
- if (!pageContext._hasPageContextFromServer)
37
- return;
38
- const errMsg = `pageContext${propName} isn't defined on the client-side, see https://vike.dev/passToClient#error`;
39
- if (
40
- // TODO/next-major-release always make it an error.
41
- // - Remove pageContext._hasPageContextFromClient logic (IIRC this is its only use case).
42
- pageContext._hasPageContextFromClient) {
43
- assertWarning(false, errMsg, { onlyOnce: false, showStackTrace: true });
44
- }
45
- else {
46
- assertUsage(false, errMsg);
47
- }
48
- }
49
- const WHITELIST = [
50
- 'then',
51
- // Vue calls toJSON()
52
- 'toJSON'
53
- ];
54
- function isWhitelisted(prop) {
55
- if (WHITELIST.includes(prop))
56
- return true;
57
- if (typeof prop === 'symbol')
58
- return true; // Vue tries to access some symbols
59
- if (typeof prop !== 'string')
60
- return true;
61
- if (prop.startsWith('__v_'))
62
- return true; // Vue internals upon `reactive(pageContext)`
63
- return false;
64
- }
65
- // Handle Vue's reactivity.
66
- // When changing a reactive object:
67
- // - Vue tries to read its old value first. This triggers a `assertIsDefined()` failure if e.g. `pageContextReactive.routeParams = pageContextNew.routeParams` and `pageContextReactive` has no `routeParams`.
68
- // - Vue seems to read __v_raw before reading the property.
69
- function handleVueReactivity(prop) {
70
- if (globalObject.prev === prop || globalObject.prev === '__v_raw')
71
- return true;
72
- globalObject.prev = prop;
73
- window.setTimeout(() => {
74
- globalObject.prev = undefined;
75
- }, 0);
76
- return false;
77
- }
@@ -2,9 +2,9 @@ export { preparePageContextForUserConsumptionClientSide };
2
2
  export type { PageContextForUserConsumptionClientSide };
3
3
  import type { PageConfigUserFriendlyOld } from '../../shared/getPageFiles.js';
4
4
  import type { PageConfigRuntime } from '../../shared/page-configs/PageConfig.js';
5
- import { PageContextForPassToClientWarning } from './getPageContextProxyForUser.js';
6
- type PageContextForUserConsumptionClientSide = PageConfigUserFriendlyOld & PageContextForPassToClientWarning & {
5
+ type PageContextForUserConsumptionClientSide = PageConfigUserFriendlyOld & {
7
6
  pageId: string;
7
+ _hasPageContextFromServer: boolean;
8
8
  _pageConfigs: PageConfigRuntime[];
9
9
  };
10
10
  declare function preparePageContextForUserConsumptionClientSide<T extends PageContextForUserConsumptionClientSide>(pageContext: T, isClientRouting: boolean): T & {
@@ -11,7 +11,7 @@ import { assertVersion } from './utils.js';
11
11
  * https://vike.dev/api#build
12
12
  */
13
13
  async function build(options = {}) {
14
- const { viteConfigEnhanced, vikeConfig } = await prepareViteApiCall(options.viteConfig, 'build');
14
+ const { viteConfigEnhanced, vikeConfig } = await prepareViteApiCall(options, 'build');
15
15
  // Pass it to pluginAutoFullBuild()
16
16
  if (viteConfigEnhanced)
17
17
  viteConfigEnhanced._viteConfigEnhanced = viteConfigEnhanced;
@@ -1,7 +1,13 @@
1
1
  export { isVikeCliOrApi };
2
2
  export { setContextApiOperation };
3
3
  export { clearContextApiOperation };
4
- import type { Operation } from './types.js';
4
+ export { getApiOperation };
5
+ import type { APIOptions, Operation } from './types.js';
6
+ type ApiOperation = {
7
+ operation: Operation;
8
+ options: APIOptions;
9
+ };
10
+ declare function getApiOperation(): ApiOperation | null;
5
11
  declare function isVikeCliOrApi(): boolean;
6
- declare function setContextApiOperation(operation: Operation): void;
12
+ declare function setContextApiOperation(operation: Operation, options: APIOptions): void;
7
13
  declare function clearContextApiOperation(): void;
@@ -1,19 +1,19 @@
1
1
  export { isVikeCliOrApi };
2
2
  export { setContextApiOperation };
3
3
  export { clearContextApiOperation };
4
+ export { getApiOperation };
4
5
  import { assert, getGlobalObject } from './utils.js';
5
6
  const globalObject = getGlobalObject('api/context.ts', {});
6
7
  function getApiOperation() {
7
- assert(globalObject.apiOperation);
8
- return globalObject.apiOperation;
8
+ return globalObject.apiOperation ?? null;
9
9
  }
10
10
  function isVikeCliOrApi() {
11
11
  // The CLI uses the API
12
12
  return !!globalObject.apiOperation;
13
13
  }
14
- function setContextApiOperation(operation) {
14
+ function setContextApiOperation(operation, options) {
15
15
  assert(!globalObject.apiOperation);
16
- globalObject.apiOperation = operation;
16
+ globalObject.apiOperation = { operation, options };
17
17
  }
18
18
  function clearContextApiOperation() {
19
19
  globalObject.apiOperation = undefined;
@@ -7,7 +7,7 @@ import { createServer } from 'vite';
7
7
  * https://vike.dev/api#dev
8
8
  */
9
9
  async function dev(options = {}) {
10
- const { viteConfigEnhanced } = await prepareViteApiCall(options.viteConfig, 'dev');
10
+ const { viteConfigEnhanced } = await prepareViteApiCall(options, 'dev');
11
11
  const server = await createServer(viteConfigEnhanced);
12
12
  return {
13
13
  viteServer: server,
@@ -3,9 +3,9 @@ export { getViteRoot };
3
3
  export { assertViteRoot };
4
4
  export { normalizeViteRoot };
5
5
  import type { InlineConfig, ResolvedConfig } from 'vite';
6
- import type { Operation } from './types.js';
6
+ import type { APIOptions, Operation } from './types.js';
7
7
  import { type VikeConfigObject } from '../plugin/plugins/importUserCode/v1-design/getVikeConfig.js';
8
- declare function prepareViteApiCall(viteConfigFromOptions: InlineConfig | undefined, operation: Operation): Promise<{
8
+ declare function prepareViteApiCall(options: APIOptions, operation: Operation): Promise<{
9
9
  vikeConfig: VikeConfigObject;
10
10
  viteConfigEnhanced: InlineConfig | undefined;
11
11
  }>;
@@ -9,10 +9,12 @@ import path from 'path';
9
9
  import { assert, assertUsage, getGlobalObject, isObject, toPosixPath } from './utils.js';
10
10
  import pc from '@brillout/picocolors';
11
11
  import { clearGlobalContext } from '../runtime/globalContext.js';
12
+ import { getEnvVarObject } from '../plugin/shared/getEnvVarObject.js';
12
13
  const globalObject = getGlobalObject('api/prepareViteApiCall.ts', {});
13
- async function prepareViteApiCall(viteConfigFromOptions, operation) {
14
+ async function prepareViteApiCall(options, operation) {
14
15
  clear();
15
- setContextApiOperation(operation);
16
+ setContextApiOperation(operation, options);
17
+ const viteConfigFromOptions = options.viteConfig;
16
18
  return enhanceViteConfig(viteConfigFromOptions, operation);
17
19
  }
18
20
  // For subsequent API calls, e.g. calling prerender() after build()
@@ -21,7 +23,7 @@ function clear() {
21
23
  clearGlobalContext();
22
24
  }
23
25
  async function enhanceViteConfig(viteConfigFromOptions, operation) {
24
- const viteInfo = await getInfoFromVite(viteConfigFromOptions, operation);
26
+ const viteInfo = await getViteInfo(viteConfigFromOptions, operation);
25
27
  await assertViteRoot2(viteInfo.root, viteInfo.viteConfigEnhanced, operation);
26
28
  const vikeConfig = await getVikeConfig2(viteInfo.root, operation === 'dev', viteInfo.vikeVitePluginOptions);
27
29
  const viteConfigEnhanced = addViteSettingsSetByVikeConfig(viteInfo.viteConfigEnhanced, vikeConfig);
@@ -43,21 +45,30 @@ function addViteSettingsSetByVikeConfig(viteConfigEnhanced, vikeConfig) {
43
45
  }
44
46
  async function getViteRoot(operation) {
45
47
  if (!globalObject.root)
46
- await getInfoFromVite(undefined, operation);
48
+ await getViteInfo(undefined, operation);
47
49
  assert(globalObject.root);
48
50
  return globalObject.root;
49
51
  }
50
- async function getInfoFromVite(viteConfigFromOptions, operation) {
51
- const viteConfigFromUserViteFile = await loadViteConfigFile(viteConfigFromOptions, operation);
52
- const root = normalizeViteRoot(
53
- // `viteConfigFromOptions.root` before `viteConfigFromUserViteFile.root` replicates Vite's precedence:
52
+ async function getViteInfo(viteConfigFromOptions, operation) {
53
+ let viteConfigEnhanced = viteConfigFromOptions;
54
+ // Precedence:
55
+ // - viteConfigFromUserEnvVar (highest precendence)
56
+ // - viteConfigFromOptions
57
+ // - viteConfigFromUserViteFile (lowest precendence)
58
+ const viteConfigFromUserEnvVar = getEnvVarObject('VITE_CONFIG');
59
+ if (viteConfigFromUserEnvVar)
60
+ viteConfigEnhanced = mergeConfig(viteConfigEnhanced ?? {}, viteConfigFromUserEnvVar);
61
+ const viteConfigFromUserViteFile = await loadViteConfigFile(viteConfigEnhanced, operation);
62
+ // Correct precedence, replicates Vite:
54
63
  // https://github.com/vitejs/vite/blob/4f5845a3182fc950eb9cd76d7161698383113b18/packages/vite/src/node/config.ts#L1001
55
- viteConfigFromOptions?.root ?? viteConfigFromUserViteFile?.root ?? process.cwd());
64
+ const viteConfigResolved = mergeConfig(viteConfigFromUserViteFile ?? {}, viteConfigEnhanced ?? {});
65
+ const root = normalizeViteRoot(viteConfigResolved.root ?? process.cwd());
56
66
  globalObject.root = root;
67
+ // - Find options `vike(options)` set in vite.config.js
68
+ // - TODO/next-major: remove
69
+ // - Add Vike's Vite plugin if missing
57
70
  let vikeVitePluginOptions;
58
- let viteConfigEnhanced = viteConfigFromOptions;
59
- // If Vike's Vite plugin is found in both viteConfigFromOptions and viteConfigFromUserViteFile then Vike will later throw an error
60
- const found = findVikeVitePlugin(viteConfigFromOptions) || findVikeVitePlugin(viteConfigFromUserViteFile);
71
+ const found = findVikeVitePlugin(viteConfigResolved);
61
72
  if (found) {
62
73
  vikeVitePluginOptions = found.vikeVitePluginOptions;
63
74
  }
@@ -66,8 +77,8 @@ async function getInfoFromVite(viteConfigFromOptions, operation) {
66
77
  // Using a dynamic import because the script calling the Vike API may not live in the same place as vite.config.js, thus vike/plugin may resolved to two different node_modules/vike directories.
67
78
  const { plugin: vikePlugin } = await import('../plugin/index.js');
68
79
  viteConfigEnhanced = {
69
- ...viteConfigFromOptions,
70
- plugins: [...(viteConfigFromOptions?.plugins ?? []), vikePlugin()]
80
+ ...viteConfigEnhanced,
81
+ plugins: [...(viteConfigEnhanced?.plugins ?? []), vikePlugin()]
71
82
  };
72
83
  const res = findVikeVitePlugin(viteConfigEnhanced);
73
84
  assert(res);
@@ -8,7 +8,7 @@ import { prepareViteApiCall } from './prepareViteApiCall.js';
8
8
  * https://vike.dev/api#prerender
9
9
  */
10
10
  async function prerender(options = {}) {
11
- const { viteConfigEnhanced } = await prepareViteApiCall(options.viteConfig, 'prerender');
11
+ const { viteConfigEnhanced } = await prepareViteApiCall(options, 'prerender');
12
12
  options.viteConfig = viteConfigEnhanced;
13
13
  const { viteConfig } = await runPrerenderFromAPI(options);
14
14
  return {
@@ -7,7 +7,7 @@ import { preview as previewVite } from 'vite';
7
7
  * https://vike.dev/api#preview
8
8
  */
9
9
  async function preview(options = {}) {
10
- const { viteConfigEnhanced } = await prepareViteApiCall(options.viteConfig, 'preview');
10
+ const { viteConfigEnhanced } = await prepareViteApiCall(options, 'preview');
11
11
  const server = await previewVite(viteConfigEnhanced);
12
12
  return {
13
13
  viteServer: server,
@@ -1,6 +1,7 @@
1
1
  export type { APIOptions };
2
2
  export type { Operation };
3
3
  import type { InlineConfig } from 'vite';
4
+ import type { Config } from '../../shared/page-configs/Config.js';
4
5
  type APIOptions = {
5
6
  /**
6
7
  * Vite config.
@@ -8,5 +9,11 @@ type APIOptions = {
8
9
  * https://vike.dev/api
9
10
  */
10
11
  viteConfig?: InlineConfig;
12
+ /**
13
+ * Vike config.
14
+ *
15
+ * https://vike.dev/api
16
+ */
17
+ vikeConfig?: Config;
11
18
  };
12
19
  type Operation = 'build' | 'dev' | 'preview' | 'prerender';
@@ -1,5 +1,7 @@
1
1
  export { isVikeCli };
2
2
  export { setContextCliCommand };
3
- import type { Command } from './parseCli.js';
3
+ export { getCliOptions };
4
+ import type { CliOptions, Command } from './parseCli.js';
5
+ declare function getCliOptions(): CliOptions | null;
4
6
  declare function isVikeCli(): boolean;
5
- declare function setContextCliCommand(command: Command): void;
7
+ declare function setContextCliCommand(command: Command, cliOptions: CliOptions): void;
@@ -1,14 +1,15 @@
1
1
  export { isVikeCli };
2
2
  export { setContextCliCommand };
3
+ export { getCliOptions };
3
4
  import { assert, getGlobalObject } from './utils.js';
4
5
  const globalObject = getGlobalObject('cli/context.ts', {});
5
- function getCliCommand() {
6
- return globalObject.cliCommand;
6
+ function getCliOptions() {
7
+ return globalObject.cliCommand?.cliOptions ?? null;
7
8
  }
8
9
  function isVikeCli() {
9
10
  return !!globalObject.cliCommand;
10
11
  }
11
- function setContextCliCommand(command) {
12
+ function setContextCliCommand(command, cliOptions) {
12
13
  assert(!globalObject.cliCommand);
13
- globalObject.cliCommand = command;
14
+ globalObject.cliCommand = { command, cliOptions };
14
15
  }
@@ -1,12 +1,12 @@
1
- import { projectInfo } from './utils.js';
1
+ import { PROJECT_VERSION } 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
5
  import { setContextCliCommand } from './context.js';
6
6
  cli();
7
7
  async function cli() {
8
- const { command } = parseCli();
9
- setContextCliCommand(command);
8
+ const { command, cliOptions } = parseCli();
9
+ setContextCliCommand(command, cliOptions);
10
10
  if (command === 'dev') {
11
11
  await cmdDev();
12
12
  }
@@ -28,7 +28,7 @@ async function cmdDev() {
28
28
  const info = viteServer.config.logger.info;
29
29
  const startupDurationString = pc.dim(`ready in ${pc.reset(pc.bold(String(Math.ceil(performance.now() - startTime))))} ms`);
30
30
  const hasExistingLogs = process.stdout.bytesWritten > 0 || process.stderr.bytesWritten > 0;
31
- info(` ${pc.yellow(`${pc.bold(projectInfo.projectName)} v${projectInfo.projectVersion}`)} ${startupDurationString}\n`, {
31
+ info(` ${pc.yellow(`${pc.bold('Vike')} v${PROJECT_VERSION}`)} ${startupDurationString}\n`, {
32
32
  clear: !hasExistingLogs
33
33
  });
34
34
  viteServer.printUrls();
@@ -1,6 +1,9 @@
1
1
  export { parseCli };
2
2
  export type { Command };
3
+ export type { CliOptions };
4
+ type CliOptions = Record<string, unknown>;
3
5
  type Command = 'dev' | 'build' | 'preview' | 'prerender';
4
6
  declare function parseCli(): {
5
7
  command: Command;
8
+ cliOptions: CliOptions;
6
9
  };
@@ -1,6 +1,7 @@
1
1
  export { parseCli };
2
2
  import pc from '@brillout/picocolors';
3
- import { projectInfo, includes } from './utils.js';
3
+ import { assert, includes, PROJECT_VERSION } from './utils.js';
4
+ import { parseJson5 } from '../plugin/shared/getEnvVarObject.js';
4
5
  const commands = [
5
6
  { name: 'dev', desc: 'Start development server' },
6
7
  { name: 'build', desc: 'Build for production' },
@@ -8,32 +9,59 @@ const commands = [
8
9
  { name: 'prerender', desc: 'Pre-render pages (only needed when partial.disableAutoRun is true)' }
9
10
  ];
10
11
  function parseCli() {
11
- const command = (() => {
12
- const firstArg = process.argv[2];
13
- if (!firstArg) {
14
- showHelp();
15
- }
16
- showHelpOrVersion(firstArg);
17
- if (includes(commands.map((c) => c.name), firstArg))
18
- return firstArg;
19
- wrongUsage(`Unknown command ${pc.bold(firstArg)}`);
20
- })();
12
+ const command = getCommand();
13
+ const cliOptions = getCliOptions();
14
+ return { command, cliOptions };
15
+ }
16
+ function getCommand() {
17
+ const firstArg = process.argv[2];
18
+ if (includes(commands.map((c) => c.name), firstArg)) {
19
+ return firstArg;
20
+ }
21
+ if (!firstArg)
22
+ showHelp();
23
+ showHelpOrVersion(firstArg);
24
+ wrongUsage(`Unknown command ${pc.bold(firstArg)}`);
25
+ }
26
+ function getCliOptions() {
27
+ let cliOptions = {};
28
+ let configNameCurrent;
29
+ const commitIfDefinedWithoutValue = () => {
30
+ if (configNameCurrent)
31
+ commit(true);
32
+ };
33
+ const commit = (val) => {
34
+ assert(configNameCurrent);
35
+ cliOptions[configNameCurrent] = val;
36
+ configNameCurrent = undefined;
37
+ };
21
38
  for (const arg of process.argv.slice(3)) {
22
39
  showHelpOrVersion(arg);
23
- wrongUsage(`Unknown option ${pc.bold(arg)}`);
40
+ if (arg.startsWith('--')) {
41
+ commitIfDefinedWithoutValue();
42
+ configNameCurrent = arg.slice('--'.length);
43
+ }
44
+ else {
45
+ if (!configNameCurrent)
46
+ wrongUsage(`Unknown option ${pc.bold(arg)}`);
47
+ commit(parseJson5(arg, `CLI option --${configNameCurrent}`));
48
+ }
24
49
  }
25
- return { command };
50
+ commitIfDefinedWithoutValue();
51
+ return cliOptions;
26
52
  }
27
53
  function showHelp() {
28
54
  const TAB = ' '.repeat(3);
29
55
  const nameMaxLength = Math.max(...commands.map((c) => c.name.length));
30
56
  console.log([
31
- `vike@${projectInfo.projectVersion}`,
57
+ `vike@${PROJECT_VERSION}`,
32
58
  '',
33
59
  'Usage:',
34
60
  ...[...commands, { name: '-v', desc: "Print Vike's installed version" }].map((c) => ` ${pc.dim('$')} ${pc.bold(`vike ${c.name}`)}${' '.repeat(nameMaxLength - c.name.length)}${TAB}${pc.dim(`# ${c.desc}`)}`),
35
61
  '',
36
- `More infos at ${pc.underline('https://vike.dev/cli')}`
62
+ `Vike settings can be passed over the ${pc.cyan('VIKE_CONFIG')} environment variable or as ${pc.cyan('CLI options')} such as --host.`,
63
+ `Vite settings can be passed over the ${pc.cyan('VITE_CONFIG')} environment variable.`,
64
+ `See ${pc.underline('https://vike.dev/cli')} for more information.`
37
65
  ].join('\n'));
38
66
  process.exit(1);
39
67
  }
@@ -46,7 +74,7 @@ function showHelpOrVersion(arg) {
46
74
  }
47
75
  }
48
76
  function showVersion() {
49
- console.log(projectInfo.projectVersion);
77
+ console.log(PROJECT_VERSION);
50
78
  process.exit(1);
51
79
  }
52
80
  function wrongUsage(msg) {
@@ -1,4 +1,4 @@
1
1
  export * from '../../utils/assert.js';
2
- export * from '../../utils/projectInfo.js';
2
+ export * from '../../utils/PROJECT_VERSION.js';
3
3
  export * from '../../utils/includes.js';
4
4
  export * from '../../utils/getGlobalObject.js';
@@ -3,6 +3,6 @@
3
3
  import { onLoad } from './onLoad.js';
4
4
  onLoad();
5
5
  export * from '../../utils/assert.js';
6
- export * from '../../utils/projectInfo.js';
6
+ export * from '../../utils/PROJECT_VERSION.js';
7
7
  export * from '../../utils/includes.js';
8
8
  export * from '../../utils/getGlobalObject.js';
@@ -2,7 +2,7 @@ export { pluginBuildEntry };
2
2
  export { set_macro_ASSETS_MANIFEST };
3
3
  import { serverProductionEntryPlugin } from '@brillout/vite-plugin-server-entry/plugin';
4
4
  import { virtualFileIdImportUserCodeServer } from '../../../shared/virtual-files/virtualFileImportUserCode.js';
5
- import { assert, projectInfo, toPosixPath } from '../../utils.js';
5
+ import { assert, PROJECT_VERSION, toPosixPath } from '../../utils.js';
6
6
  import fs from 'fs/promises';
7
7
  import path from 'path';
8
8
  import { createRequire } from 'module';
@@ -35,7 +35,7 @@ function pluginBuildEntry() {
35
35
  function getServerProductionEntryCode(config) {
36
36
  const importPath = getImportPath(config);
37
37
  const buildInfo = {
38
- versionAtBuildTime: projectInfo.projectVersion,
38
+ versionAtBuildTime: PROJECT_VERSION,
39
39
  usesClientRouter: isUsingClientRouter(), // TODO/v1-release: remove
40
40
  viteConfigRuntime: getViteConfigRuntime(config)
41
41
  };
@@ -1,5 +1,6 @@
1
1
  export { commonConfig };
2
2
  export { getVikeConfigPublic };
3
+ export type { VikeConfigPublic };
3
4
  import { type InlineConfig, type Plugin, type ResolvedConfig, type UserConfig } from 'vite';
4
5
  import { type VikeConfigObject } from './importUserCode/v1-design/getVikeConfig.js';
5
6
  import type { PrerenderContextPublic } from '../../prerender/runPrerender.js';
@@ -1,13 +1,11 @@
1
1
  export { commonConfig };
2
2
  export { getVikeConfigPublic };
3
- import { mergeConfig } from 'vite';
4
3
  import { assert, assertUsage, assertWarning, findPackageJson, hasProp, isDevCheck, isDocker, isObject } from '../utils.js';
5
4
  import { assertRollupInput } from './build/pluginBuildConfig.js';
6
5
  import { installRequireShim_setUserRootDir } from '@brillout/require-shim';
7
6
  import pc from '@brillout/picocolors';
8
7
  import path from 'path';
9
8
  import { assertResolveAlias } from './commonConfig/assertResolveAlias.js';
10
- import { getEnvVarObject } from '../shared/getEnvVarObject.js';
11
9
  import { isViteCliCall } from '../shared/isViteCliCall.js';
12
10
  import { isVikeCliOrApi } from '../../api/context.js';
13
11
  import { getVikeConfig2 } from './importUserCode/v1-design/getVikeConfig.js';
@@ -39,7 +37,7 @@ function commonConfig(vikeVitePluginOptions) {
39
37
  },
40
38
  // TODO/v1-release: remove https://github.com/vikejs/vike/issues/2122
41
39
  configVikePromise: Promise.resolve({
42
- prerender: resolvePrerenderConfigGlobal(vikeConfig).isEnabled
40
+ prerender: resolvePrerenderConfigGlobal(vikeConfig).isPrerenderingEnabled
43
41
  })
44
42
  };
45
43
  }
@@ -73,17 +71,24 @@ function commonConfig(vikeVitePluginOptions) {
73
71
  config: {
74
72
  order: 'post',
75
73
  handler(configFromUser) {
76
- // Change default port
77
74
  let configFromVike = { server: {}, preview: {} };
78
- setDefault('port', 3000, configFromUser, configFromVike);
79
- // Set `--host` for Docker/Podman
80
- if (isDocker()) {
75
+ const vike = getVikeConfigPublic(configFromUser);
76
+ if (vike.config.port !== undefined) {
77
+ // https://vike.dev/port
78
+ setDefault('port', vike.config.port, configFromUser, configFromVike);
79
+ }
80
+ else {
81
+ // Change Vite's default port
82
+ setDefault('port', 3000, configFromUser, configFromVike);
83
+ }
84
+ if (vike.config.host) {
85
+ // https://vike.dev/host
86
+ setDefault('host', vike.config.host, configFromUser, configFromVike);
87
+ }
88
+ else if (isDocker()) {
89
+ // Set `--host` for Docker/Podman
81
90
  setDefault('host', true, configFromUser, configFromVike);
82
91
  }
83
- // VITE_CONFIG
84
- const configFromEnvVar = getEnvVarObject('VITE_CONFIG');
85
- if (configFromEnvVar)
86
- configFromVike = mergeConfig(configFromVike, configFromEnvVar);
87
92
  return configFromVike;
88
93
  }
89
94
  }
@@ -50,16 +50,18 @@ async function getPageDeps(config, pageConfigs) {
50
50
  .forEach((configValueSource) => {
51
51
  if (!configValueSource.valueIsLoadedWithImport && !configValueSource.valueIsFilePath)
52
52
  return;
53
- const { definedAtFilePath, configEnv } = configValueSource;
53
+ const { definedAt, configEnv } = configValueSource;
54
54
  if (!configEnv.client)
55
55
  return;
56
- if (definedAtFilePath.filePathAbsoluteUserRootDir !== null) {
56
+ if (definedAt.definedBy)
57
+ return;
58
+ if (definedAt.filePathAbsoluteUserRootDir !== null) {
57
59
  // Vite expects entries to be filesystem absolute paths (surprisingly so).
58
- addEntry(definedAtFilePath.filePathAbsoluteFilesystem);
60
+ addEntry(definedAt.filePathAbsoluteFilesystem);
59
61
  }
60
62
  else {
61
63
  // Adding definedAtFilePath.filePathAbsoluteFilesystem doesn't work for npm packages, I guess because of Vite's config.server.fs.allow
62
- addInclude(definedAtFilePath.importPathAbsolute);
64
+ addInclude(definedAt.importPathAbsolute);
63
65
  }
64
66
  });
65
67
  });
@@ -79,7 +79,7 @@ function devConfig() {
79
79
  if (config.server.middlewareMode || hasHonoViteDevServer)
80
80
  return;
81
81
  return () => {
82
- addSsrMiddleware(server.middlewares, config, false);
82
+ addSsrMiddleware(server.middlewares, config, false, null);
83
83
  };
84
84
  }
85
85
  },