vike 0.4.259-commit-a909f04 → 0.4.259-commit-dcc116b

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.
@@ -5,7 +5,7 @@ export { getViteRoot };
5
5
  export { assertViteRoot };
6
6
  export { normalizeViteRoot };
7
7
  import { loadConfigFromFile, mergeConfig, resolveConfig } from 'vite';
8
- import { getVikeConfigInternal, getVikeConfigFromCliOrEnv, setVikeConfigContext, isVikeConfigContextSet, } from '../vite/shared/resolveVikeConfigInternal.js';
8
+ import { getVikeConfigInternal, getVikeConfigFromCliOrEnv, setVikeConfigContext, isVikeConfigContextSet, EARLY_SETTINGS, } from '../vite/shared/resolveVikeConfigInternal.js';
9
9
  import path from 'node:path';
10
10
  import { assert, assertUsage, assertWarning } from '../../utils/assert.js';
11
11
  import { getGlobalObject } from '../../utils/getGlobalObject.js';
@@ -53,34 +53,38 @@ async function getViteRoot(viteContext) {
53
53
  assert(globalObject.root);
54
54
  return globalObject.root;
55
55
  }
56
+ // TODO rename to resolveViteConfig
57
+ // TODO rename_all viteConfigFromUser viteConfigFrom
58
+ // TODO rename_all vikeConfigFromUser vikeConfigFrom
56
59
  async function getViteInfo(viteContext) {
57
- const { viteConfigFromUserVikeApiOptions } = getVikeApiContext();
58
- let viteConfigFromUserResolved = clone(viteConfigFromUserVikeApiOptions ?? {});
59
60
  // Precedence:
60
61
  // 1. (highest precedence) | viteConfigFromUserEnvVar | VITE_CONFIG
61
- // 2. | viteConfigFromUserVikeSettings | VIKE_CONFIG & Vike CLI options — only `+mode` & `+root`
62
+ // 2. | viteConfigFromVikeCliOrEnv | VIKE_CONFIG & Vike CLI options — `+mode` & `+root`
62
63
  // 3. | viteConfigFromUserViteCli | Vite CLI args — `[root]` & `-c/--config`
63
- // 4. | viteConfigFromUserVikeApiOptions | Vike API options
64
+ // 4. | viteConfigFromUserVikeApiOptions | Vike API options — `viteConfig`, and `+mode` & `+root` from `vikeConfig`
64
65
  // 5. (lowest precedence) | viteConfigFromUserViteConfigFile | vite.config.js
65
- // Resolve Vite CLI args (when invoked via Vite's CLI rather than Vike's API).
66
- // Without this, Vike loads vite.config.js blind to `vite [root]` / `-c <file>` and
67
- // ends up with the wrong root when those Vite CLI args are used.
66
+ let viteConfigFromUserResolved = {};
67
+ // Vike API args
68
+ const { viteConfigFromUserVikeApiOptions, vikeConfigFromUserVikeApiOptions } = getVikeApiContext();
69
+ // - Resolve `viteConfig` set over Vike's API
70
+ viteConfigFromUserResolved = merge(viteConfigFromUserResolved, viteConfigFromUserVikeApiOptions ?? {});
71
+ // - Resolve +mode/+root set over Vike's API
72
+ {
73
+ const viteConfigFromVikeApi2 = pick(vikeConfigFromUserVikeApiOptions ?? {}, EARLY_SETTINGS);
74
+ viteConfigFromUserResolved = merge(viteConfigFromUserResolved ?? {}, viteConfigFromVikeApi2);
75
+ }
76
+ // Vite CLI args (when invoked via Vite's CLI rather than Vike's API).
77
+ // - Without this, Vike loads vite.config.js blind to `vite [root]` / `-c <file>` and ends up with the wrong root when those Vite CLI args are used.
68
78
  const viteConfigFromUserViteCli = getViteCliArgs();
69
79
  if (viteConfigFromUserViteCli) {
70
80
  viteConfigFromUserResolved = merge(viteConfigFromUserResolved ?? {}, viteConfigFromUserViteCli);
71
81
  }
72
- // Resolve Vike's +mode and +root settings.
73
- // These alias Vite settings that need to be resolved early: +root determines where Vike looks for
74
- // +config.js files (so it can't be defined inside +config.js itself), and +mode affects which
75
- // vite.config.js environment is loaded. That's why — unlike +host/+port/+force which are applied
76
- // later from the resolved Vike config — they're read here from Vike's CLI options & VIKE_CONFIG only.
82
+ // Vike's CLI and VIKE_CONFIG
83
+ // - Resolve +mode/+root set over Vike's CLI or VIKE_CONFIG
77
84
  {
78
- const viteConfigFromUserVikeSettings = pick(getVikeConfigFromCliOrEnv().vikeConfigFromCliOrEnv, [
79
- 'mode',
80
- 'root',
81
- ]);
82
- if (Object.keys(viteConfigFromUserVikeSettings).length > 0) {
83
- viteConfigFromUserResolved = merge(viteConfigFromUserResolved ?? {}, viteConfigFromUserVikeSettings);
85
+ const viteConfigFromVikeCliOrEnv = pick(getVikeConfigFromCliOrEnv().vikeConfigFromCliOrEnv, EARLY_SETTINGS);
86
+ if (Object.keys(viteConfigFromVikeCliOrEnv).length > 0) {
87
+ viteConfigFromUserResolved = merge(viteConfigFromUserResolved ?? {}, viteConfigFromVikeCliOrEnv);
84
88
  }
85
89
  }
86
90
  // Resolve VITE_CONFIG
@@ -92,7 +96,7 @@ async function getViteInfo(viteContext) {
92
96
  globalObject.isOnlyResolvingUserConfig = true;
93
97
  const viteConfigFromUserViteConfigFile = await loadViteConfigFile(viteConfigFromUserResolved, viteContext);
94
98
  globalObject.isOnlyResolvingUserConfig = false;
95
- // Correct precedence, replicates Vite:
99
+ // Lowest precedence. The `merge()` applies the correct precedence and replicates Vite:
96
100
  // https://github.com/vitejs/vite/blob/4f5845a3182fc950eb9cd76d7161698383113b18/packages/vite/src/node/config.ts#L1001
97
101
  const viteConfigResolved = merge(viteConfigFromUserViteConfigFile ?? {}, viteConfigFromUserResolved ?? {});
98
102
  const root = normalizeViteRoot(viteConfigResolved.root ?? process.cwd());
@@ -129,9 +133,6 @@ async function getViteInfo(viteContext) {
129
133
  function merge(c1, c2) {
130
134
  return mergeConfig(c1, c2);
131
135
  }
132
- function clone(c) {
133
- return mergeConfig({}, c);
134
- }
135
136
  function findVikeVitePlugin(viteConfig) {
136
137
  let vikeVitePluginOptions;
137
138
  let vikeVitePuginFound = false;
@@ -205,11 +206,12 @@ function getViteContextWithOperation(operation) {
205
206
  function getVikeApiContext() {
206
207
  const vikeApiOperation = getVikeApiOperation();
207
208
  if (!vikeApiOperation)
208
- return { viteConfigFromUserVikeApiOptions: null, viteContext: null };
209
+ return { viteConfigFromUserVikeApiOptions: null, vikeConfigFromUserVikeApiOptions: null, viteContext: null };
209
210
  const { options, operation } = vikeApiOperation;
210
211
  const viteConfigFromUserVikeApiOptions = options.viteConfig;
212
+ const vikeConfigFromUserVikeApiOptions = options.vikeConfig;
211
213
  const viteContext = getViteContextWithOperation(operation);
212
- return { viteConfigFromUserVikeApiOptions, viteContext };
214
+ return { viteConfigFromUserVikeApiOptions, vikeConfigFromUserVikeApiOptions, viteContext };
213
215
  }
214
216
  function resolveViteContext(inlineConfig = {}, viteContext) {
215
217
  const isBuild = viteContext === 'build';
@@ -78,10 +78,10 @@ function showHelp() {
78
78
  'Common CLI options:',
79
79
  [
80
80
  `vike dev ${pc.cyan('--host')} ${TAB}${pc.dim('# Make server available over LAN and public addresses')}`,
81
- `vike dev ${pc.cyan('--port')} 80 ${TAB}${pc.dim('# Change the server port')}`,
82
- `vike build ${pc.cyan('--mode')} staging${TAB}${pc.dim('# Set the mode to run in')}`,
81
+ `vike dev ${pc.cyan('--port')} 80 ${TAB}${pc.dim('# Set server port')}`,
82
+ `vike build ${pc.cyan('--mode')} staging${TAB}${pc.dim('# Set mode (e.g. development, production, staging)')}`,
83
83
  `vike dev ${pc.cyan('--force')} ${TAB}${pc.dim("# Disable Vite's cache")}`,
84
- `vike dev ${pc.cyan('--root')} src ${TAB}${pc.dim('# Set the project root directory')}`,
84
+ `vike dev ${pc.cyan('--root')} src ${TAB}${pc.dim('# Set project root directory')}`,
85
85
  ]
86
86
  .map((o) => ` ${pc.dim('$')} ${o}`)
87
87
  .join('\n'),
@@ -198,7 +198,9 @@ const metaBuiltIn = {
198
198
  mode: {
199
199
  env: { config: true },
200
200
  global: true,
201
+ /* +mode can't be set in +config.js => a +config.js change never affects it — no Vite restart needed
201
202
  vite: true,
203
+ */
202
204
  },
203
205
  force: {
204
206
  env: { config: true },
@@ -208,7 +210,9 @@ const metaBuiltIn = {
208
210
  root: {
209
211
  env: { config: true },
210
212
  global: true,
213
+ /* +root can't be set in +config.js => a +config.js change never affects it — no Vite restart needed
211
214
  vite: true,
215
+ */
212
216
  },
213
217
  csp: {
214
218
  env: { server: true },
@@ -9,6 +9,7 @@ export { isV1Design };
9
9
  export { getConfVal };
10
10
  export { getConfigDefinitionOptional };
11
11
  export { getVikeConfigFromCliOrEnv };
12
+ export { EARLY_SETTINGS };
12
13
  export type { VikeConfigInternal };
13
14
  export type { PageConfigBuildTimeBeforeComputed };
14
15
  import type { PageConfigGlobalBuildTime, ConfigEnv, PageConfigBuildTime, DefinedAtFilePath } from '../../../types/PageConfig.js';
@@ -62,6 +63,7 @@ declare function getVikeConfigFromCliOrEnv(): {
62
63
  configFromCliOptions: import("../../cli/parseCli.js").CliOptions | null;
63
64
  configFromEnvVar: Record<string, unknown> | null;
64
65
  };
66
+ declare const EARLY_SETTINGS: readonly ["root", "mode"];
65
67
  type PageConfigBuildTimeBeforeComputed = Omit<PageConfigBuildTime, 'configValuesComputed'>;
66
68
  declare function getConfigDefinitionOptional(configDefinitions: ConfigDefinitionsInternal, configName: string): {
67
69
  env: ConfigEnv;
@@ -10,6 +10,7 @@ export { isV1Design };
10
10
  export { getConfVal };
11
11
  export { getConfigDefinitionOptional };
12
12
  export { getVikeConfigFromCliOrEnv };
13
+ export { EARLY_SETTINGS };
13
14
  import { deepEqual } from '../../../utils/deepEqual.js';
14
15
  import { assertKeys } from '../../../utils/assertKeys.js';
15
16
  import { assertIsNotProductionRuntime } from '../../../utils/assertSetup.js';
@@ -220,6 +221,7 @@ async function resolveVikeConfigInternal(userRootDir, vikeVitePluginOptions, esb
220
221
  // Backwards compatibility for vike(options) in vite.config.js
221
222
  temp_interopVikeVitePlugin(pageConfigGlobal, vikeVitePluginOptions, userRootDir);
222
223
  setCliAndApiOptions(pageConfigGlobal, pageConfigs, configDefinitionsResolved);
224
+ warnEarlySettingsInConfigFile(pageConfigGlobal);
223
225
  const globalConfigPublic = resolveGlobalConfig(pageConfigGlobal, pageConfigs);
224
226
  const prerenderContext = await resolvePrerenderContext({
225
227
  config: globalConfigPublic.config,
@@ -536,6 +538,30 @@ function getSourceNonConfigFile(configName, value, definedAt, configDefinitionsG
536
538
  };
537
539
  return source;
538
540
  }
541
+ // Settings that must be resolved early before Vike crawls +config.js files:
542
+ // - They can't be defined inside +config.js files.
543
+ // - They must be set early (via Vike's CLI/API options or VIKE_CONFIG), and read early before Vike's main config resolution.
544
+ const EARLY_SETTINGS = [
545
+ // +root determines where Vike looks for +config.js files (so it can't be defined inside +config.js itself)
546
+ 'root',
547
+ // +mode affects which vite.config.js environment is loaded
548
+ 'mode',
549
+ ];
550
+ function warnEarlySettingsInConfigFile(pageConfigGlobal) {
551
+ for (const configName of EARLY_SETTINGS) {
552
+ const sources = pageConfigGlobal.configValueSources[configName];
553
+ if (!sources)
554
+ continue;
555
+ for (const source of sources) {
556
+ if (!source.plusFile) {
557
+ // `plusFile === null` => source comes from Vike CLI argument, Vike API option, or VIKE_CONFIG
558
+ continue;
559
+ }
560
+ const configDefinedAt = getConfigDefinedAt('Config', configName, source.definedAt);
561
+ assertWarning(false, `${configDefinedAt} has no effect: it can't be set from a ${pc.cyan('+')} file — see ${pc.underline(`https://vike.dev/${configName}`)}`, { onlyOnce: true });
562
+ }
563
+ }
564
+ }
539
565
  function sortConfigValueSources(configValueSources, locationIdPage) {
540
566
  Object.entries(configValueSources).forEach(([configName, sources]) => {
541
567
  sources
@@ -492,10 +492,6 @@ type ConfigBuiltIn = {
492
492
  /**
493
493
  * The root directory of your project.
494
494
  *
495
- * Can be an absolute path, or a path relative to the current working directory.
496
- *
497
- * @default process.cwd()
498
- *
499
495
  * https://vike.dev/root
500
496
  */
501
497
  root?: string;
@@ -1 +1 @@
1
- export declare const PROJECT_VERSION: "0.4.259-commit-a909f04";
1
+ export declare const PROJECT_VERSION: "0.4.259-commit-dcc116b";
@@ -1,2 +1,2 @@
1
1
  // Automatically updated by @brillout/release-me
2
- export const PROJECT_VERSION = '0.4.259-commit-a909f04';
2
+ export const PROJECT_VERSION = '0.4.259-commit-dcc116b';
@@ -1 +1 @@
1
- export declare function pick<Obj extends object, Keys extends keyof Obj>(obj: Obj, keys: Keys[]): Pick<Obj, Keys>;
1
+ export declare function pick<Obj extends object, Keys extends keyof Obj>(obj: Obj, keys: readonly Keys[]): Pick<Obj, Keys>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vike",
3
- "version": "0.4.259-commit-a909f04",
3
+ "version": "0.4.259-commit-dcc116b",
4
4
  "repository": "https://github.com/vikejs/vike",
5
5
  "exports": {
6
6
  "./server": {