vike 0.4.240-commit-8ce2cbd → 0.4.240-commit-309067c

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.
@@ -668,6 +668,7 @@ if (import.meta.env.DEV && import.meta.hot)
668
668
  function logError(err) {
669
669
  if (isObject(err) &&
670
670
  // Set by vike-react
671
+ // https://github.com/vikejs/vike-react/blob/195a208c6b77e7f34496e1f637278a36c60fbe07/packages/vike-react/src/integration/onRenderClient.tsx#L109
671
672
  err.isAlreadyLogged) {
672
673
  return;
673
674
  }
@@ -5,7 +5,7 @@ import { parseJson5 } from '../vite/shared/getEnvVarObject.js';
5
5
  const commands = [
6
6
  { name: 'dev', desc: 'Start development server' },
7
7
  { name: 'build', desc: 'Build for production' },
8
- { name: 'preview', desc: 'Start preview server using production build (only works for SSG apps)' },
8
+ { name: 'preview', desc: 'Start preview server using production build' },
9
9
  { name: 'prerender', desc: 'Pre-render pages (only needed when prerender.disableAutoRun is true)' },
10
10
  ];
11
11
  function parseCli() {
@@ -690,6 +690,7 @@ function preparePrerenderContextForPublicUsage(prerenderContext) {
690
690
  });
691
691
  return prerenderContext.pageContexts;
692
692
  },
693
+ configurable: true,
693
694
  });
694
695
  }
695
696
  // Required because of https://vike.dev/i18n#pre-rendering
@@ -556,7 +556,8 @@ function isProdOptional() {
556
556
  const yes5 = globalObject.isProductionAccordingToUser === true;
557
557
  // vite-plugin-vercel
558
558
  const yes6 = globalObject.isProductionAccordingToPhotonVercel === true;
559
- const yes = yes1 || yes2 || yes3 || yes4 || yes5 || yes6;
559
+ const yes7 = globalThis.__VIKE__IS_DEV === false;
560
+ const yes = yes1 || yes2 || yes3 || yes4 || yes5 || yes6 || yes7;
560
561
  const no1 = !!globalObject.viteDevServer;
561
562
  // Vike CLI & Vike API
562
563
  const no2 = vikeApiOperation === 'dev';
@@ -566,8 +567,9 @@ function isProdOptional() {
566
567
  const no4 = globalObject.isProductionAccordingToUser === false;
567
568
  // @cloudflare/vite-plugin
568
569
  const no5 = isNonRunnableDev();
569
- const no = no1 || no2 || no3 || no4 || no5;
570
- const debug = { yes1, yes2, yes3, yes4, yes5, yes6, no1, no2, no3, no4, no5 };
570
+ const no6 = globalThis.__VIKE__IS_DEV === true;
571
+ const no = no1 || no2 || no3 || no4 || no5 || no6;
572
+ const debug = { yes1, yes2, yes3, yes4, yes5, yes6, yes7, no1, no2, no3, no4, no5, no6 };
571
573
  assert(typeof yes === 'boolean', debug);
572
574
  assert(typeof no === 'boolean', debug);
573
575
  if (yes) {
@@ -310,7 +310,6 @@ async function handleAssetsManifest_getBuildConfig() {
310
310
  async function handleAssetsManifest(config, viteEnv, options, bundle) {
311
311
  const isSsrEnv = isViteServerSide_onlySsrEnv(config, viteEnv);
312
312
  if (isSsrEnv) {
313
- assert(!globalObject.assetsJsonFilePath);
314
313
  const outDirs = getOutDirs(config, viteEnv);
315
314
  globalObject.assetsJsonFilePath = path.posix.join(outDirs.outDirRoot, 'assets.json');
316
315
  await writeAssetsManifestFile(globalObject.assetsJsonFilePath, config);
@@ -131,10 +131,16 @@ function assertVikeCliOrApi(config) {
131
131
  assert(!isVitest());
132
132
  return;
133
133
  }
134
+ /* This warning is always shown: Vitest loads Vite *before* any Vike JavaScript API can be invoked.
134
135
  if (isVitest()) {
135
- assertWarning(false, `Unexpected Vitest setup: you seem to be using Vitest together with Vike's Vite plugin but without using Vike's JavaScript API which is unexpected, see ${pc.underline('https://vike.dev/vitest')}`, { onlyOnce: true });
136
- return;
136
+ assertWarning(
137
+ false,
138
+ `Unexpected Vitest setup: you seem to be using Vitest together with Vike's Vite plugin but without using Vike's JavaScript API which is unexpected, see ${pc.underline('https://vike.dev/vitest')}`,
139
+ { onlyOnce: true },
140
+ )
141
+ return
137
142
  }
143
+ */
138
144
  if (config.server.middlewareMode) {
139
145
  assertWarning(false, `${pc.cyan('vite.createServer()')} is deprecated ${pc.underline('https://vike.dev/migration/cli#api')}`, {
140
146
  onlyOnce: true,
@@ -61,8 +61,10 @@ function pluginReplaceConstantsEnvVars() {
61
61
  const { magicString, getMagicStringResult } = getMagicString(code, id);
62
62
  // Get regex operations
63
63
  const replacements = Object.entries(envVarsAll)
64
- // Skip env vars that start with [`config.envPrefix`](https://vite.dev/config/shared-options.html#envprefix) => they are already handled by Vite
64
+ // Skip env vars that start with [`config.envPrefix`](https://vite.dev/config/shared-options.html#envprefix) they are already handled by Vite
65
65
  .filter(([envName]) => !envPrefix.some((prefix) => envName.startsWith(prefix)))
66
+ // Skip constants like import.meta.env.DEV which are already handled by Vite
67
+ .filter(([envName]) => !['DEV', 'PROD', 'SSR', 'MODE', 'BASE_URL'].includes(envName))
66
68
  .map(([envName, envVal]) => {
67
69
  const envStatement = `import.meta.env.${envName}`;
68
70
  const envStatementRegExpStr = escapeRegex(envStatement) + '\\b';
@@ -1,17 +1,10 @@
1
1
  export { pluginReplaceConstantsGlobalThis };
2
2
  import type { Plugin } from 'vite';
3
3
  declare global {
4
- /** Like `import.meta.env.DEV` but works inside `node_modules/` (even if package is `ssr.external`). The value `undefined` is to be interpreted as `false`. */
5
- var __VIKE__IS_DEV: boolean | undefined;
6
- /** Like `import.meta.env.SSR` but works inside `node_modules/` (even if package is `ssr.external`). The value `undefined` is to be interpreted as `false`. */
7
- var __VIKE__IS_CLIENT: boolean | undefined;
8
- /**
9
- * Whether a debug flag is enabled (either the global flag `DEBUG=vike` or a specific flag `DEBUG=vike:some-flag`).
10
- *
11
- * WARNING: must be used ONLY on the client-side. (The value is always `undefined` on the server-side.)
12
- *
13
- * In isomorhpic code, use `globalThis.__VIKE__IS_CLIENT` to make sure it's only used on the client-side.
14
- */
15
- var __VIKE__IS_DEBUG: boolean | undefined;
4
+ /** Like `import.meta.env.DEV` but works for `node_modules/` packages with `ssr.external` */
5
+ var __VIKE__IS_DEV: boolean;
6
+ /** Like `import.meta.env.SSR` but works for `node_modules/` packages with `ssr.external` */
7
+ var __VIKE__IS_CLIENT: boolean;
8
+ var __VIKE__IS_DEBUG: boolean;
16
9
  }
17
10
  declare function pluginReplaceConstantsGlobalThis(): Plugin[];
@@ -1,52 +1,77 @@
1
1
  export { pluginReplaceConstantsGlobalThis };
2
- import { assert, isDebug } from '../utils.js';
3
- // === Explanation: globalThis.__VIKE__IS_DEV
4
- // If client-side => always noExternal => globalThis.__VIKE__IS_DEV is set by the `define` config below.
5
- // If server-side:
6
- // If ssr.noExternal => globalThis.__VIKE__IS_DEV is set by the `define` config below.
7
- // If `ssr.external`:
8
- // If not RunnableDevEnvironment (e.g. `@cloudflare/vite-plugin`) => always ssr.noExternal => globalThis.__VIKE__IS_DEV is set by the `define` config below.
9
- // If RunnableDevEnvironment (the default setup):
10
- // If dev/preview/pre-rendering => Vite is loaded, and server and Vite run inside the same process (because RunnableDevEnvironment) => globalThis.__VIKE__IS_DEV is set by the assignment below.
11
- // If production => Vite isn't loaded => globalThis.__VIKE__IS_DEV is `undefined` (it's never set) => value `undefined` is to be interpreted as `false`.
12
- // === Explanation: globalThis.__VIKE__IS_CLIENT
13
- // If client-side => always noExternal => globalThis.__VIKE__IS_CLIENT is set to `true` by the `define` config below.
14
- // If server-side => globalThis.__VIKE__IS_CLIENT is either `false` or `undefined` (the value `undefined` is to be interpreted as `false`).
2
+ import { assert, isDebug, addVirtualFileIdPrefix, escapeRegex } from '../utils.js';
3
+ import { isViteServerSide_applyToEnvironment, isViteServerSide_configEnvironment, isViteServerSide_extraSafe, } from '../shared/isViteServerSide.js';
4
+ const isDebugVal = isDebug();
15
5
  globalThis.__VIKE__IS_CLIENT = false;
6
+ globalThis.__VIKE__IS_DEBUG = isDebugVal;
7
+ const VIRTUAL_FILE_ID = 'virtual:vike:server:globalThis-constants';
8
+ const filterRolldown = {
9
+ id: {
10
+ include: new RegExp(escapeRegex(VIRTUAL_FILE_ID)),
11
+ },
12
+ };
13
+ const filterFunction = (id) => id === VIRTUAL_FILE_ID || id === addVirtualFileIdPrefix(VIRTUAL_FILE_ID);
16
14
  function pluginReplaceConstantsGlobalThis() {
15
+ let config;
16
+ let isDev;
17
17
  return [
18
18
  {
19
- name: 'vike:pluginReplaceConstantsGlobalThis',
19
+ name: 'vike:pluginReplaceConstantsGlobalThis:define',
20
20
  config: {
21
21
  handler(config) {
22
- const isDev = config._isDev;
23
- assert(typeof isDev === 'boolean');
22
+ assert(typeof config._isDev === 'boolean');
23
+ isDev = config._isDev;
24
24
  globalThis.__VIKE__IS_DEV = isDev;
25
25
  return {
26
26
  define: {
27
27
  'globalThis.__VIKE__IS_DEV': JSON.stringify(isDev),
28
+ 'globalThis.__VIKE__IS_DEBUG': JSON.stringify(isDebugVal),
28
29
  },
29
30
  };
30
31
  },
31
32
  },
32
33
  configEnvironment: {
33
34
  handler(name, config) {
34
- const consumer = config.consumer ?? (name === 'client' ? 'client' : 'server');
35
- const isClientSide = consumer === 'client';
36
- const defineIsDebug = !isClientSide
37
- ? {}
38
- : {
39
- // We purposely only define it on the client-side, because we cannot know the value in server-side ssr.external production.
40
- 'globalThis.__VIKE__IS_DEBUG': JSON.stringify(isDebug()),
41
- };
35
+ const isClientSide = !isViteServerSide_configEnvironment(name, config);
42
36
  return {
43
37
  define: {
44
38
  'globalThis.__VIKE__IS_CLIENT': JSON.stringify(isClientSide),
45
- ...defineIsDebug,
46
39
  },
47
40
  };
48
41
  },
49
42
  },
43
+ configResolved(config_) {
44
+ config = config_;
45
+ },
46
+ },
47
+ {
48
+ name: 'vike:pluginReplaceConstantsGlobalThis:virtual-file',
49
+ // We only need the virtual file for the server-side (for node_modules/ packages with ssr.external) — the `define` values above always apply to the client-side.
50
+ applyToEnvironment(env) {
51
+ return isViteServerSide_applyToEnvironment(env);
52
+ },
53
+ resolveId: {
54
+ filter: filterRolldown,
55
+ handler(id) {
56
+ assert(filterFunction(id));
57
+ assert(id === VIRTUAL_FILE_ID);
58
+ return addVirtualFileIdPrefix(id);
59
+ },
60
+ },
61
+ load: {
62
+ filter: filterRolldown,
63
+ handler(id, options) {
64
+ assert(filterFunction(id));
65
+ assert(isViteServerSide_extraSafe(config, this.environment, options));
66
+ assert(typeof isDev === 'boolean');
67
+ const code = [
68
+ `globalThis.__VIKE__IS_DEV = ${JSON.stringify(isDev)};`,
69
+ `globalThis.__VIKE__IS_CLIENT = false;`,
70
+ `globalThis.__VIKE__IS_DEBUG = ${JSON.stringify(isDebugVal)};`,
71
+ ].join('\n');
72
+ return code;
73
+ },
74
+ },
50
75
  },
51
76
  ];
52
77
  }
@@ -12,6 +12,9 @@ function getCode(pageConfigs, pageConfigGlobal, isForClientSide, isDev, id, isCl
12
12
  const lines = [];
13
13
  const importStatements = [];
14
14
  const filesEnv = new Map();
15
+ if (!isForClientSide) {
16
+ importStatements.push("import 'virtual:vike:server:globalThis-constants';");
17
+ }
15
18
  lines.push('export const pageConfigsSerialized = [');
16
19
  lines.push(getCodePageConfigsSerialized(pageConfigs, isForClientSide, isClientRouting, isDev, importStatements, filesEnv));
17
20
  lines.push('];');
@@ -22,7 +25,10 @@ function getCode(pageConfigs, pageConfigGlobal, isForClientSide, isDev, id, isCl
22
25
  // https://vite.dev/guide/api-environment-frameworks.html
23
26
  lines.push('if (import.meta.hot) import.meta.hot.accept();');
24
27
  }
25
- const code = [...importStatements, ...lines].join('\n');
28
+ let code = [...importStatements, ...lines].join('\n');
29
+ if (!isForClientSide) {
30
+ code = "import 'virtual:vike:server:globalThis-constants';\n" + code;
31
+ }
26
32
  debug(id, isForClientSide ? 'CLIENT-SIDE' : 'SERVER-SIDE', code);
27
33
  return code;
28
34
  }
@@ -23,6 +23,9 @@ async function getCode(config, isForClientSide, isClientRouting, isDev, id) {
23
23
  const isBuild = command === 'build';
24
24
  assert(isDev === !isBuild);
25
25
  let content = '';
26
+ if (!isForClientSide) {
27
+ content += "import 'virtual:vike:server:globalThis-constants';\n";
28
+ }
26
29
  {
27
30
  const globRoots = getGlobRoots(config);
28
31
  debugGlob('Glob roots: ', globRoots);
@@ -1,18 +1,21 @@
1
1
  export { isViteServerSide };
2
- export { isViteClientSide };
3
2
  export { isViteServerSide_viteEnvOptional };
4
3
  export { isViteServerSide_onlySsrEnv };
5
4
  export { isViteServerSide_extraSafe };
5
+ export { isViteServerSide_applyToEnvironment };
6
+ export { isViteServerSide_configEnvironment };
6
7
  export type { ViteEnv };
7
- import type { Environment, EnvironmentOptions, ResolvedConfig, UserConfig } from 'vite';
8
+ import type { Environment, EnvironmentOptions, ResolvedConfig, UserConfig, Plugin } from 'vite';
8
9
  type ViteEnv = {
9
10
  name?: string;
10
11
  config: EnvironmentOptions | Environment['config'];
11
12
  };
12
13
  declare function isViteServerSide(configGlobal: ResolvedConfig | UserConfig, viteEnv: ViteEnv): boolean;
13
14
  declare function isViteServerSide_viteEnvOptional(configGlobal: ResolvedConfig | UserConfig, viteEnv?: ViteEnv | undefined): boolean;
14
- declare function isViteClientSide(configGlobal: ResolvedConfig, viteEnv: ViteEnv): boolean;
15
15
  declare function isViteServerSide_onlySsrEnv(configGlobal: ResolvedConfig, viteEnv: ViteEnv): boolean;
16
16
  declare function isViteServerSide_extraSafe(config: ResolvedConfig, viteEnv: ViteEnv, options: {
17
17
  ssr?: boolean;
18
18
  } | undefined): boolean;
19
+ type PartialEnvironment = Parameters<NonNullable<Plugin['applyToEnvironment']>>[0];
20
+ declare function isViteServerSide_applyToEnvironment(env: PartialEnvironment): boolean;
21
+ declare function isViteServerSide_configEnvironment(name: string, config: EnvironmentOptions): boolean;
@@ -1,8 +1,9 @@
1
1
  export { isViteServerSide };
2
- export { isViteClientSide };
3
2
  export { isViteServerSide_viteEnvOptional };
4
3
  export { isViteServerSide_onlySsrEnv };
5
4
  export { isViteServerSide_extraSafe };
5
+ export { isViteServerSide_applyToEnvironment };
6
+ export { isViteServerSide_configEnvironment };
6
7
  import { assert } from '../../../utils/assert.js';
7
8
  function isViteServerSide_impl(configGlobal, viteEnv) {
8
9
  assert(!('consumer' in configGlobal)); // make sure configGlobal isn't viteEnv.config
@@ -42,9 +43,6 @@ function isViteServerSide(configGlobal, viteEnv) {
42
43
  function isViteServerSide_viteEnvOptional(configGlobal, viteEnv) {
43
44
  return isViteServerSide_impl(configGlobal, viteEnv);
44
45
  }
45
- function isViteClientSide(configGlobal, viteEnv) {
46
- return !isViteServerSide(configGlobal, viteEnv);
47
- }
48
46
  // Only `ssr` env: for example don't include `vercel_edge` nor `vercel_node`.
49
47
  function isViteServerSide_onlySsrEnv(configGlobal, viteEnv) {
50
48
  return viteEnv.name ? viteEnv.name === 'ssr' : isViteServerSide(configGlobal, viteEnv);
@@ -64,3 +62,14 @@ function isViteServerSide_extraSafe(config, viteEnv, options) {
64
62
  assert(options.ssr === isServerSide, debug);
65
63
  return isServerSide;
66
64
  }
65
+ function isViteServerSide_applyToEnvironment(env) {
66
+ const { consumer } = env.config;
67
+ return isViteServerSide_consumer(consumer);
68
+ }
69
+ function isViteServerSide_configEnvironment(name, config) {
70
+ const consumer = config.consumer ?? (name === 'client' ? 'client' : 'server');
71
+ return isViteServerSide_consumer(consumer);
72
+ }
73
+ function isViteServerSide_consumer(consumer) {
74
+ return consumer !== 'client';
75
+ }
@@ -1 +1 @@
1
- export declare const PROJECT_VERSION: "0.4.240-commit-8ce2cbd";
1
+ export declare const PROJECT_VERSION: "0.4.240-commit-309067c";
@@ -1,2 +1,2 @@
1
1
  // Automatically updated by @brillout/release-me
2
- export const PROJECT_VERSION = '0.4.240-commit-8ce2cbd';
2
+ export const PROJECT_VERSION = '0.4.240-commit-309067c';
@@ -1,5 +1,6 @@
1
1
  export function isVikeReactApp() {
2
2
  const g = globalThis;
3
- // Set by vike-react https://github.com/vikejs/vike-react/blob/23e92434424f10e7e742b6bf587edee5aa8832df/packages/vike-react/src/renderer/onRenderHtml.tsx#L75
3
+ // Set by vike-react
4
+ // https://github.com/vikejs/vike-react/blob/23e92434424f10e7e742b6bf587edee5aa8832df/packages/vike-react/src/renderer/onRenderHtml.tsx#L75
4
5
  return !!g._isVikeReactApp;
5
6
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vike",
3
- "version": "0.4.240-commit-8ce2cbd",
3
+ "version": "0.4.240-commit-309067c",
4
4
  "repository": "https://github.com/vikejs/vike",
5
5
  "exports": {
6
6
  "./server": {