vike 0.4.225-commit-37a36a5 → 0.4.225-commit-b8fc36e
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.
- package/dist/cjs/client/shared/getPageContextProxyForUser.js +3 -66
- package/dist/cjs/node/api/build.js +7 -7
- package/dist/cjs/node/api/dev.js +2 -2
- package/dist/cjs/node/api/prepareViteApiCall.js +52 -29
- package/dist/cjs/node/api/prerender.js +2 -2
- package/dist/cjs/node/api/preview.js +2 -2
- package/dist/cjs/node/api/utils.js +1 -0
- package/dist/cjs/node/cli/parseCli.js +10 -4
- package/dist/cjs/node/plugin/plugins/build/pluginAutoFullBuild.js +2 -2
- package/dist/cjs/node/plugin/plugins/commonConfig.js +0 -6
- package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/configDefinitionsBuiltIn.js +4 -0
- package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.js +20 -7
- package/dist/cjs/node/plugin/shared/getEnvVarObject.js +7 -0
- package/dist/cjs/node/prerender/resolvePrerenderConfig.js +3 -3
- package/dist/cjs/node/prerender/runPrerender.js +8 -12
- package/dist/cjs/node/runtime/html/serializePageContextClientSide.js +67 -14
- package/dist/cjs/node/runtime/renderPage/preparePageContextForUserConsumptionServerSide.js +3 -0
- package/dist/cjs/node/runtime/renderPage/renderPageAlreadyRouted.js +18 -12
- package/dist/cjs/node/runtime/renderPage.js +13 -28
- package/dist/cjs/node/runtime-dev/createDevMiddleware.js +2 -2
- package/dist/cjs/shared/NOT_SERIALIZABLE.js +5 -0
- package/dist/cjs/utils/PROJECT_VERSION.js +1 -1
- package/dist/cjs/utils/assert.js +1 -1
- package/dist/cjs/utils/objectAssign.js +7 -2
- package/dist/cjs/utils/pick.js +12 -0
- package/dist/esm/client/client-routing-runtime/createPageContext.d.ts +2 -0
- package/dist/esm/client/client-routing-runtime/createPageContext.js +2 -1
- package/dist/esm/client/client-routing-runtime/renderPageClientSide.js +1 -1
- package/dist/esm/client/server-routing-runtime/getPageContext.d.ts +6 -3
- package/dist/esm/client/server-routing-runtime/getPageContext.js +6 -3
- package/dist/esm/client/shared/getPageContextProxyForUser.d.ts +1 -11
- package/dist/esm/client/shared/getPageContextProxyForUser.js +4 -67
- package/dist/esm/client/shared/preparePageContextForUserConsumptionClientSide.d.ts +2 -2
- package/dist/esm/node/api/build.js +7 -7
- package/dist/esm/node/api/dev.js +2 -2
- package/dist/esm/node/api/prepareViteApiCall.d.ts +1 -1
- package/dist/esm/node/api/prepareViteApiCall.js +54 -31
- package/dist/esm/node/api/prerender.js +2 -2
- package/dist/esm/node/api/preview.js +2 -2
- package/dist/esm/node/api/utils.d.ts +1 -0
- package/dist/esm/node/api/utils.js +1 -0
- package/dist/esm/node/cli/parseCli.js +10 -4
- package/dist/esm/node/plugin/plugins/build/pluginAutoFullBuild.js +2 -2
- package/dist/esm/node/plugin/plugins/commonConfig.d.ts +1 -1
- package/dist/esm/node/plugin/plugins/commonConfig.js +0 -6
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/configDefinitionsBuiltIn.js +4 -0
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.d.ts +8 -0
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.js +20 -7
- package/dist/esm/node/plugin/shared/getEnvVarObject.js +7 -0
- package/dist/esm/node/prerender/resolvePrerenderConfig.js +3 -3
- package/dist/esm/node/prerender/runPrerender.js +8 -12
- package/dist/esm/node/runtime/html/serializePageContextClientSide.d.ts +2 -0
- package/dist/esm/node/runtime/html/serializePageContextClientSide.js +69 -15
- package/dist/esm/node/runtime/renderPage/preparePageContextForUserConsumptionServerSide.js +3 -0
- package/dist/esm/node/runtime/renderPage/renderPageAlreadyRouted.d.ts +27 -1
- package/dist/esm/node/runtime/renderPage/renderPageAlreadyRouted.js +18 -12
- package/dist/esm/node/runtime/renderPage.js +14 -29
- package/dist/esm/node/runtime-dev/createDevMiddleware.js +2 -2
- package/dist/esm/shared/NOT_SERIALIZABLE.d.ts +1 -0
- package/dist/esm/shared/NOT_SERIALIZABLE.js +2 -0
- package/dist/esm/shared/page-configs/Config.d.ts +11 -3
- package/dist/esm/shared/page-configs/PageConfig.d.ts +1 -1
- package/dist/esm/shared/route/abort.d.ts +2 -2
- package/dist/esm/shared/types.d.ts +34 -2
- package/dist/esm/utils/PROJECT_VERSION.d.ts +1 -1
- package/dist/esm/utils/PROJECT_VERSION.js +1 -1
- package/dist/esm/utils/assert.js +1 -1
- package/dist/esm/utils/objectAssign.d.ts +1 -1
- package/dist/esm/utils/objectAssign.js +7 -2
- package/dist/esm/utils/pick.d.ts +1 -0
- package/dist/esm/utils/pick.js +9 -0
- package/package.json +1 -1
- package/dist/cjs/shared/notSerializable.js +0 -5
- package/dist/esm/shared/notSerializable.d.ts +0 -1
- package/dist/esm/shared/notSerializable.js +0 -2
|
@@ -4,61 +4,84 @@ export { assertViteRoot };
|
|
|
4
4
|
export { normalizeViteRoot };
|
|
5
5
|
import { loadConfigFromFile, mergeConfig, resolveConfig } from 'vite';
|
|
6
6
|
import { clearContextApiOperation, setContextApiOperation } from './context.js';
|
|
7
|
-
import { getVikeConfig2 } from '../plugin/plugins/importUserCode/v1-design/getVikeConfig.js';
|
|
7
|
+
import { getVikeConfig2, getVikeConfigFromCliOrEnv } from '../plugin/plugins/importUserCode/v1-design/getVikeConfig.js';
|
|
8
8
|
import path from 'path';
|
|
9
|
-
import { assert, assertUsage, getGlobalObject, isObject, toPosixPath } from './utils.js';
|
|
9
|
+
import { assert, assertUsage, getGlobalObject, isObject, pick, 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
14
|
async function prepareViteApiCall(options, operation) {
|
|
14
15
|
clear();
|
|
15
16
|
setContextApiOperation(operation, options);
|
|
16
|
-
const
|
|
17
|
-
return
|
|
17
|
+
const viteConfigFromUserApiOptions = options.viteConfig;
|
|
18
|
+
return resolveConfigs(viteConfigFromUserApiOptions, operation);
|
|
18
19
|
}
|
|
19
20
|
// For subsequent API calls, e.g. calling prerender() after build()
|
|
20
21
|
function clear() {
|
|
21
22
|
clearContextApiOperation();
|
|
22
23
|
clearGlobalContext();
|
|
23
24
|
}
|
|
24
|
-
async function
|
|
25
|
-
const viteInfo = await
|
|
26
|
-
await assertViteRoot2(viteInfo.root, viteInfo.
|
|
25
|
+
async function resolveConfigs(viteConfigFromUserApiOptions, operation) {
|
|
26
|
+
const viteInfo = await getViteInfo(viteConfigFromUserApiOptions, operation);
|
|
27
|
+
await assertViteRoot2(viteInfo.root, viteInfo.viteConfigFromUserEnhanced, operation);
|
|
27
28
|
const vikeConfig = await getVikeConfig2(viteInfo.root, operation === 'dev', viteInfo.vikeVitePluginOptions);
|
|
28
|
-
const
|
|
29
|
+
const viteConfigFromUserEnhanced = applyVikeViteConfig(viteInfo.viteConfigFromUserEnhanced, vikeConfig);
|
|
29
30
|
return {
|
|
30
31
|
vikeConfig,
|
|
31
|
-
|
|
32
|
+
viteConfigFromUserEnhanced
|
|
32
33
|
};
|
|
33
34
|
}
|
|
34
|
-
|
|
35
|
+
// Apply +vite
|
|
36
|
+
// - For example, Vike extensions adding Vite plugins
|
|
37
|
+
function applyVikeViteConfig(viteConfigFromUserEnhanced, vikeConfig) {
|
|
35
38
|
const viteConfigs = vikeConfig.global._from.configsCumulative.vite;
|
|
36
39
|
if (!viteConfigs)
|
|
37
|
-
return
|
|
40
|
+
return viteConfigFromUserEnhanced;
|
|
38
41
|
viteConfigs.values.forEach((v) => {
|
|
39
42
|
assertUsage(isObject(v.value), `${v.definedAt} should be an object`);
|
|
40
|
-
|
|
43
|
+
viteConfigFromUserEnhanced = mergeConfig(viteConfigFromUserEnhanced ?? {}, v.value);
|
|
41
44
|
assertUsage(!findVikeVitePlugin(v.value), "Using the +vite setting to add Vike's Vite plugin is forbidden");
|
|
42
45
|
});
|
|
43
|
-
return
|
|
46
|
+
return viteConfigFromUserEnhanced;
|
|
44
47
|
}
|
|
45
48
|
async function getViteRoot(operation) {
|
|
46
49
|
if (!globalObject.root)
|
|
47
|
-
await
|
|
50
|
+
await getViteInfo(undefined, operation);
|
|
48
51
|
assert(globalObject.root);
|
|
49
52
|
return globalObject.root;
|
|
50
53
|
}
|
|
51
|
-
async function
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
//
|
|
54
|
+
async function getViteInfo(viteConfigFromUserApiOptions, operation) {
|
|
55
|
+
let viteConfigFromUserEnhanced = viteConfigFromUserApiOptions;
|
|
56
|
+
// Precedence:
|
|
57
|
+
// 1) viteConfigFromUserEnvVar (highest precendence)
|
|
58
|
+
// 2) viteConfigFromUserVikeConfig
|
|
59
|
+
// 2) viteConfigFromUserApiOptions
|
|
60
|
+
// 3) viteConfigFromUserViteFile (lowest precendence)
|
|
61
|
+
// Resolve Vike's +mode setting
|
|
62
|
+
{
|
|
63
|
+
const viteConfigFromUserVikeConfig = pick(getVikeConfigFromCliOrEnv().vikeConfigFromCliOrEnv, ['mode']);
|
|
64
|
+
if (Object.keys(viteConfigFromUserVikeConfig).length > 0) {
|
|
65
|
+
viteConfigFromUserEnhanced = mergeConfig(viteConfigFromUserEnhanced ?? {}, viteConfigFromUserVikeConfig);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
// Resolve VITE_CONFIG
|
|
69
|
+
const viteConfigFromUserEnvVar = getEnvVarObject('VITE_CONFIG');
|
|
70
|
+
if (viteConfigFromUserEnvVar) {
|
|
71
|
+
viteConfigFromUserEnhanced = mergeConfig(viteConfigFromUserEnhanced ?? {}, viteConfigFromUserEnvVar);
|
|
72
|
+
}
|
|
73
|
+
// Resolve vite.config.js
|
|
74
|
+
const viteConfigFromUserViteFile = await loadViteConfigFile(viteConfigFromUserEnhanced, operation);
|
|
75
|
+
// Correct precedence, replicates Vite:
|
|
55
76
|
// https://github.com/vitejs/vite/blob/4f5845a3182fc950eb9cd76d7161698383113b18/packages/vite/src/node/config.ts#L1001
|
|
56
|
-
|
|
77
|
+
const viteConfigResolved = mergeConfig(viteConfigFromUserViteFile ?? {}, viteConfigFromUserEnhanced ?? {});
|
|
78
|
+
const root = normalizeViteRoot(viteConfigResolved.root ?? process.cwd());
|
|
57
79
|
globalObject.root = root;
|
|
80
|
+
// - Find options `vike(options)` set in vite.config.js
|
|
81
|
+
// - TODO/next-major: remove
|
|
82
|
+
// - Add Vike's Vite plugin if missing
|
|
58
83
|
let vikeVitePluginOptions;
|
|
59
|
-
|
|
60
|
-
// If Vike's Vite plugin is found in both viteConfigFromOptions and viteConfigFromUserViteFile then Vike will later throw an error
|
|
61
|
-
const found = findVikeVitePlugin(viteConfigFromOptions) || findVikeVitePlugin(viteConfigFromUserViteFile);
|
|
84
|
+
const found = findVikeVitePlugin(viteConfigResolved);
|
|
62
85
|
if (found) {
|
|
63
86
|
vikeVitePluginOptions = found.vikeVitePluginOptions;
|
|
64
87
|
}
|
|
@@ -66,16 +89,16 @@ async function getInfoFromVite(viteConfigFromOptions, operation) {
|
|
|
66
89
|
// Add Vike to plugins if not present.
|
|
67
90
|
// 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.
|
|
68
91
|
const { plugin: vikePlugin } = await import('../plugin/index.js');
|
|
69
|
-
|
|
70
|
-
...
|
|
71
|
-
plugins: [...(
|
|
92
|
+
viteConfigFromUserEnhanced = {
|
|
93
|
+
...viteConfigFromUserEnhanced,
|
|
94
|
+
plugins: [...(viteConfigFromUserEnhanced?.plugins ?? []), vikePlugin()]
|
|
72
95
|
};
|
|
73
|
-
const res = findVikeVitePlugin(
|
|
96
|
+
const res = findVikeVitePlugin(viteConfigFromUserEnhanced);
|
|
74
97
|
assert(res);
|
|
75
98
|
vikeVitePluginOptions = res.vikeVitePluginOptions;
|
|
76
99
|
}
|
|
77
100
|
assert(vikeVitePluginOptions);
|
|
78
|
-
return { root, vikeVitePluginOptions,
|
|
101
|
+
return { root, vikeVitePluginOptions, viteConfigFromUserEnhanced };
|
|
79
102
|
}
|
|
80
103
|
function findVikeVitePlugin(viteConfig) {
|
|
81
104
|
let vikeVitePluginOptions;
|
|
@@ -93,8 +116,8 @@ function findVikeVitePlugin(viteConfig) {
|
|
|
93
116
|
return { vikeVitePluginOptions };
|
|
94
117
|
}
|
|
95
118
|
// Copied from https://github.com/vitejs/vite/blob/4f5845a3182fc950eb9cd76d7161698383113b18/packages/vite/src/node/config.ts#L961-L1005
|
|
96
|
-
async function loadViteConfigFile(
|
|
97
|
-
const [inlineConfig, command, defaultMode, _defaultNodeEnv, isPreview] = getResolveConfigArgs(
|
|
119
|
+
async function loadViteConfigFile(viteConfigFromUserApiOptions, operation) {
|
|
120
|
+
const [inlineConfig, command, defaultMode, _defaultNodeEnv, isPreview] = getResolveConfigArgs(viteConfigFromUserApiOptions, operation);
|
|
98
121
|
let config = inlineConfig;
|
|
99
122
|
let mode = inlineConfig.mode || defaultMode;
|
|
100
123
|
const configEnv = {
|
|
@@ -126,8 +149,8 @@ function normalizeViteRoot(root) {
|
|
|
126
149
|
path.resolve(root));
|
|
127
150
|
}
|
|
128
151
|
const errMsg = `A Vite plugin is modifying Vite's setting ${pc.cyan('root')} which is forbidden`;
|
|
129
|
-
async function assertViteRoot2(root,
|
|
130
|
-
const args = getResolveConfigArgs(
|
|
152
|
+
async function assertViteRoot2(root, viteConfigFromUserEnhanced, operation) {
|
|
153
|
+
const args = getResolveConfigArgs(viteConfigFromUserEnhanced, operation);
|
|
131
154
|
// We can eventually this resolveConfig() call (along with removing the whole assertViteRoot2() function which is redundant with the assertViteRoot() function) so that Vike doesn't make any resolveConfig() (except for pre-rendering which is required). But let's keep it for now, just to see whether calling resolveConfig() can be problematic.
|
|
132
155
|
const viteConfigResolved = await resolveConfig(...args);
|
|
133
156
|
assertUsage(normalizeViteRoot(viteConfigResolved.root) === normalizeViteRoot(root), errMsg);
|
|
@@ -8,8 +8,8 @@ import { prepareViteApiCall } from './prepareViteApiCall.js';
|
|
|
8
8
|
* https://vike.dev/api#prerender
|
|
9
9
|
*/
|
|
10
10
|
async function prerender(options = {}) {
|
|
11
|
-
const {
|
|
12
|
-
options.viteConfig =
|
|
11
|
+
const { viteConfigFromUserEnhanced } = await prepareViteApiCall(options, 'prerender');
|
|
12
|
+
options.viteConfig = viteConfigFromUserEnhanced;
|
|
13
13
|
const { viteConfig } = await runPrerenderFromAPI(options);
|
|
14
14
|
return {
|
|
15
15
|
viteConfig
|
|
@@ -7,8 +7,8 @@ import { preview as previewVite } from 'vite';
|
|
|
7
7
|
* https://vike.dev/api#preview
|
|
8
8
|
*/
|
|
9
9
|
async function preview(options = {}) {
|
|
10
|
-
const {
|
|
11
|
-
const server = await previewVite(
|
|
10
|
+
const { viteConfigFromUserEnhanced } = await prepareViteApiCall(options, 'preview');
|
|
11
|
+
const server = await previewVite(viteConfigFromUserEnhanced);
|
|
12
12
|
return {
|
|
13
13
|
viteServer: server,
|
|
14
14
|
viteConfig: server.config
|
|
@@ -6,7 +6,7 @@ const commands = [
|
|
|
6
6
|
{ name: 'dev', desc: 'Start development server' },
|
|
7
7
|
{ name: 'build', desc: 'Build for production' },
|
|
8
8
|
{ name: 'preview', desc: 'Start preview server using production build (only works for SSG apps)' },
|
|
9
|
-
{ name: 'prerender', desc: 'Pre-render pages (only needed when
|
|
9
|
+
{ name: 'prerender', desc: 'Pre-render pages (only needed when prerender.disableAutoRun is true)' }
|
|
10
10
|
];
|
|
11
11
|
function parseCli() {
|
|
12
12
|
const command = getCommand();
|
|
@@ -57,10 +57,16 @@ function showHelp() {
|
|
|
57
57
|
`vike@${PROJECT_VERSION}`,
|
|
58
58
|
'',
|
|
59
59
|
'Usage:',
|
|
60
|
-
...[...commands, { name: '-v', desc: "Print Vike's installed version" }].map((c) => ` ${pc.dim('$')} ${pc.
|
|
60
|
+
...[...commands, { name: '-v', desc: "Print Vike's installed version" }].map((c) => ` ${pc.dim('$')} vike ${c.name.startsWith('-') ? pc.cyan(`${c.name}`) : pc.bold(`${c.name}`)}${' '.repeat(nameMaxLength - c.name.length)}${TAB}${pc.dim(`# ${c.desc}`)}`),
|
|
61
61
|
'',
|
|
62
|
-
|
|
63
|
-
`
|
|
62
|
+
'Common CLI options:',
|
|
63
|
+
[`vike dev ${pc.cyan('--host')}`, `vike dev ${pc.cyan('--port')} 80`, `vike build ${pc.cyan('--mode')} staging`]
|
|
64
|
+
.map((o) => ` ${pc.dim('$')} ${o}`)
|
|
65
|
+
.join('\n'),
|
|
66
|
+
'',
|
|
67
|
+
`More Vike settings can be passed over the ${pc.cyan('VIKE_CONFIG')} environment variable or as ${pc.cyan('CLI options')}.`,
|
|
68
|
+
`More Vite settings can be passed over the ${pc.cyan('VITE_CONFIG')} environment variable.`,
|
|
69
|
+
``,
|
|
64
70
|
`See ${pc.underline('https://vike.dev/cli')} for more information.`
|
|
65
71
|
].join('\n'));
|
|
66
72
|
process.exit(1);
|
|
@@ -124,8 +124,8 @@ function isPrerenderForceExit() {
|
|
|
124
124
|
}
|
|
125
125
|
function getFullBuildInlineConfig(config) {
|
|
126
126
|
const configFromCli = !isViteCliCall() ? null : getViteConfigFromCli();
|
|
127
|
-
if (config.
|
|
128
|
-
return config.
|
|
127
|
+
if (config._viteConfigFromUserEnhanced) {
|
|
128
|
+
return config._viteConfigFromUserEnhanced;
|
|
129
129
|
}
|
|
130
130
|
else {
|
|
131
131
|
return {
|
|
@@ -10,7 +10,7 @@ declare module 'vite' {
|
|
|
10
10
|
_vikeVitePluginOptions?: unknown;
|
|
11
11
|
_root?: string;
|
|
12
12
|
_baseViteOriginal?: string;
|
|
13
|
-
|
|
13
|
+
_viteConfigFromUserEnhanced?: InlineConfig;
|
|
14
14
|
_vike?: VikeConfigPublic;
|
|
15
15
|
_vikeConfigObject?: VikeConfigObject;
|
|
16
16
|
}
|
|
@@ -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';
|
|
@@ -91,10 +89,6 @@ function commonConfig(vikeVitePluginOptions) {
|
|
|
91
89
|
// Set `--host` for Docker/Podman
|
|
92
90
|
setDefault('host', true, configFromUser, configFromVike);
|
|
93
91
|
}
|
|
94
|
-
// VITE_CONFIG
|
|
95
|
-
const configFromEnvVar = getEnvVarObject('VITE_CONFIG');
|
|
96
|
-
if (configFromEnvVar)
|
|
97
|
-
configFromVike = mergeConfig(configFromVike, configFromEnvVar);
|
|
98
92
|
return configFromVike;
|
|
99
93
|
}
|
|
100
94
|
}
|
|
@@ -5,6 +5,7 @@ export { reloadVikeConfig };
|
|
|
5
5
|
export { isV1Design };
|
|
6
6
|
export { getConfVal };
|
|
7
7
|
export { getConfigDefinitionOptional };
|
|
8
|
+
export { getVikeConfigFromCliOrEnv };
|
|
8
9
|
export { isOverriden };
|
|
9
10
|
export type { VikeConfigObject };
|
|
10
11
|
import type { PageConfigGlobalBuildTime, ConfigValueSource, PageConfigBuildTime } from '../../../../../shared/page-configs/PageConfig.js';
|
|
@@ -26,6 +27,13 @@ declare function getVikeConfig(config: ResolvedConfig, { doNotRestartViteOnError
|
|
|
26
27
|
declare function getVikeConfig2(userRootDir: string, isDev: boolean, vikeVitePluginOptions: unknown): Promise<VikeConfigObject>;
|
|
27
28
|
declare function getVikeConfigOptional(): Promise<null | VikeConfigObject>;
|
|
28
29
|
declare function isV1Design(config: ResolvedConfig | UserConfig): boolean;
|
|
30
|
+
declare function getVikeConfigFromCliOrEnv(): {
|
|
31
|
+
vikeConfigFromCliOrEnv: {
|
|
32
|
+
[x: string]: unknown;
|
|
33
|
+
};
|
|
34
|
+
configFromCliOptions: import("../../../../cli/parseCli.js").CliOptions | null;
|
|
35
|
+
configFromEnvVar: Record<string, unknown> | null;
|
|
36
|
+
};
|
|
29
37
|
declare function getConfigDefinitionOptional(configDefinitions: ConfigDefinitions, configName: string): ConfigDefinitionInternal | null;
|
|
30
38
|
declare function getConfVal(plusFile: PlusFile, configName: string): null | {
|
|
31
39
|
value: unknown;
|
|
@@ -5,6 +5,7 @@ export { reloadVikeConfig };
|
|
|
5
5
|
export { isV1Design };
|
|
6
6
|
export { getConfVal };
|
|
7
7
|
export { getConfigDefinitionOptional };
|
|
8
|
+
export { getVikeConfigFromCliOrEnv };
|
|
8
9
|
export { isOverriden };
|
|
9
10
|
import { assertPosixPath, assert, isObject, assertUsage, assertWarning, objectEntries, hasProp, includes, assertIsNotProductionRuntime, getMostSimilar, joinEnglish, assertKeys, objectKeys, objectFromEntries, unique, isCallable, makeFirst, lowerFirst, makeLast, assertIsSingleModuleInstance } from '../../../utils.js';
|
|
10
11
|
import { configDefinitionsBuiltIn } from './getVikeConfig/configDefinitionsBuiltIn.js';
|
|
@@ -305,7 +306,7 @@ function assertGlobalConfigLocation(configName, sources, plusFilesAll, configDef
|
|
|
305
306
|
: 'to a value that is global';
|
|
306
307
|
const what = isConditionallyGlobal ? 'global values' : pc.cyan(configName);
|
|
307
308
|
const errEnd = configFilePathsGlobal.length > 0
|
|
308
|
-
? `define ${what} at a global config file such as ${joinEnglish(configFilePathsGlobal, 'or')} instead`
|
|
309
|
+
? `define ${what} at a global config file such as ${joinEnglish(configFilePathsGlobal.map(pc.bold), 'or')} instead`
|
|
309
310
|
: `create a global config file (e.g. /pages/+config.js) and define ${what} there instead`;
|
|
310
311
|
// When updating this error message => also update error message at https://vike.dev/warning/global-config
|
|
311
312
|
const errMsg = `${errBeg} ${errMid}: ${errEnd} (https://vike.dev/warning/global-config).`;
|
|
@@ -390,15 +391,14 @@ function setCliAndApiOptions(pageConfigGlobal, configDefinitionsResolved) {
|
|
|
390
391
|
if (apiOperation?.options.vikeConfig) {
|
|
391
392
|
addSources(apiOperation.options.vikeConfig, { definedBy: 'api', operation: apiOperation.operation }, false);
|
|
392
393
|
}
|
|
394
|
+
const { configFromCliOptions, configFromEnvVar } = getVikeConfigFromCliOrEnv();
|
|
393
395
|
// Vike CLI options
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
addSources(cliOptions, { definedBy: 'cli' }, true);
|
|
396
|
+
if (configFromCliOptions) {
|
|
397
|
+
addSources(configFromCliOptions, { definedBy: 'cli' }, true);
|
|
397
398
|
}
|
|
398
399
|
// VIKE_CONFIG [highest precedence]
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
addSources(configFromEnv, { definedBy: 'env' }, false);
|
|
400
|
+
if (configFromEnvVar) {
|
|
401
|
+
addSources(configFromEnvVar, { definedBy: 'env' }, false);
|
|
402
402
|
}
|
|
403
403
|
return;
|
|
404
404
|
function addSources(configValues, definedBy, exitOnError) {
|
|
@@ -411,6 +411,19 @@ function setCliAndApiOptions(pageConfigGlobal, configDefinitionsResolved) {
|
|
|
411
411
|
});
|
|
412
412
|
}
|
|
413
413
|
}
|
|
414
|
+
function getVikeConfigFromCliOrEnv() {
|
|
415
|
+
const configFromCliOptions = getCliOptions();
|
|
416
|
+
const configFromEnvVar = getEnvVarObject('VIKE_CONFIG');
|
|
417
|
+
const vikeConfigFromCliOrEnv = {
|
|
418
|
+
...configFromCliOptions, // Lower precedence
|
|
419
|
+
...configFromEnvVar // Higher precedence
|
|
420
|
+
};
|
|
421
|
+
return {
|
|
422
|
+
vikeConfigFromCliOrEnv,
|
|
423
|
+
configFromCliOptions,
|
|
424
|
+
configFromEnvVar
|
|
425
|
+
};
|
|
426
|
+
}
|
|
414
427
|
function getSourceNonConfigFile(configName, value, definedAt) {
|
|
415
428
|
assert(includes(objectKeys(configDefinitionsBuiltIn), configName));
|
|
416
429
|
const configDef = configDefinitionsBuiltIn[configName];
|
|
@@ -18,8 +18,15 @@ function parseJson5(valueStr, what) {
|
|
|
18
18
|
value = JSON5.parse(valueStr);
|
|
19
19
|
}
|
|
20
20
|
catch (err) {
|
|
21
|
+
if (isNotJavaScriptLike(valueStr)) {
|
|
22
|
+
// Interpret as string
|
|
23
|
+
return valueStr;
|
|
24
|
+
}
|
|
21
25
|
console.error(err);
|
|
22
26
|
assertUsage(false, `Cannot parse ${pc.cyan(what)} (see error above) because it's set to the following which isn't a valid JSON5 string: ${pc.bold(valueStr)}`);
|
|
23
27
|
}
|
|
24
28
|
return value;
|
|
25
29
|
}
|
|
30
|
+
function isNotJavaScriptLike(valueStr) {
|
|
31
|
+
return ![':', ',', '{', '}', '(', ')'].some((c) => valueStr.includes(c));
|
|
32
|
+
}
|
|
@@ -15,8 +15,8 @@ function resolvePrerenderConfigGlobal(vikeConfig) {
|
|
|
15
15
|
};
|
|
16
16
|
let defaultLocalValue = false;
|
|
17
17
|
{
|
|
18
|
-
const valueFirst = prerenderConfigs.filter((p) => !isObject(p) || p.
|
|
19
|
-
if (valueFirst === true || (isObject(valueFirst) && (valueFirst.
|
|
18
|
+
const valueFirst = prerenderConfigs.filter((p) => !isObject(p) || p.enable !== null)[0];
|
|
19
|
+
if (valueFirst === true || (isObject(valueFirst) && (valueFirst.enable ?? defaultValueForObject))) {
|
|
20
20
|
defaultLocalValue = true;
|
|
21
21
|
}
|
|
22
22
|
}
|
|
@@ -47,7 +47,7 @@ function resolvePrerenderConfigLocal(pageConfig) {
|
|
|
47
47
|
const values = configValue.value;
|
|
48
48
|
assert(isArray(values));
|
|
49
49
|
const value = values[0];
|
|
50
|
-
//
|
|
50
|
+
// If it's set to an object in a local config then Vike considers it a global config and it's skipped from local inheritance, thus we can assume the value to be a boolean.
|
|
51
51
|
assert(typeof value === 'boolean');
|
|
52
52
|
assert(isArray(configValue.definedAtData));
|
|
53
53
|
const prerenderConfigLocal = { value };
|
|
@@ -38,8 +38,8 @@ async function runPrerenderFromAPI(options = {}) {
|
|
|
38
38
|
}
|
|
39
39
|
async function runPrerenderFromCLIPrerenderCommand() {
|
|
40
40
|
try {
|
|
41
|
-
const {
|
|
42
|
-
await runPrerender({ viteConfig:
|
|
41
|
+
const { viteConfigFromUserEnhanced } = await prepareViteApiCall({}, 'prerender');
|
|
42
|
+
await runPrerender({ viteConfig: viteConfigFromUserEnhanced }, '$ vike prerender');
|
|
43
43
|
}
|
|
44
44
|
catch (err) {
|
|
45
45
|
console.error(err);
|
|
@@ -304,20 +304,16 @@ async function handlePagesWithStaticRoutes(prerenderContext, globalContext, doNo
|
|
|
304
304
|
})));
|
|
305
305
|
}
|
|
306
306
|
async function createPageContext(urlOriginal, prerenderContext, globalContext) {
|
|
307
|
-
const
|
|
307
|
+
const pageContextInit = { urlOriginal };
|
|
308
|
+
objectAssign(pageContextInit, prerenderContext.pageContextInit);
|
|
309
|
+
const pageContext = await getPageContextInitEnhanced(pageContextInit, globalContext, true, {});
|
|
310
|
+
assert(pageContext.isPrerendering === true);
|
|
311
|
+
objectAssign(pageContext, {
|
|
308
312
|
_urlHandler: null,
|
|
309
313
|
_urlRewrite: null,
|
|
310
314
|
_noExtraDir: prerenderContext.noExtraDir,
|
|
311
315
|
_prerenderContext: prerenderContext
|
|
312
|
-
};
|
|
313
|
-
const pageContextInit = {
|
|
314
|
-
urlOriginal
|
|
315
|
-
};
|
|
316
|
-
objectAssign(pageContextInit, prerenderContext.pageContextInit);
|
|
317
|
-
{
|
|
318
|
-
const pageContextInitEnhanced = await getPageContextInitEnhanced(pageContextInit, globalContext);
|
|
319
|
-
objectAssign(pageContext, pageContextInitEnhanced);
|
|
320
|
-
}
|
|
316
|
+
});
|
|
321
317
|
return pageContext;
|
|
322
318
|
}
|
|
323
319
|
async function callOnPrerenderStartHook(prerenderContext, globalContext) {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export { serializePageContextClientSide };
|
|
2
2
|
export { serializePageContextAbort };
|
|
3
3
|
export type { PageContextSerialization };
|
|
4
|
+
export { getPropKeys };
|
|
4
5
|
import type { PageConfigRuntime } from '../../../shared/page-configs/PageConfig.js';
|
|
5
6
|
import type { UrlRedirect } from '../../../shared/route/abort.js';
|
|
6
7
|
type PageContextSerialization = {
|
|
@@ -20,3 +21,4 @@ declare function serializePageContextAbort(pageContext: Record<string, unknown>
|
|
|
20
21
|
} | {
|
|
21
22
|
abortStatusCode: number;
|
|
22
23
|
})): string;
|
|
24
|
+
declare function getPropKeys(prop: string): string[];
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
export { serializePageContextClientSide };
|
|
2
2
|
export { serializePageContextAbort };
|
|
3
|
+
// For ./serializePageContextClientSide.spec.ts
|
|
4
|
+
export { getPropKeys };
|
|
3
5
|
import { stringify, isJsonSerializerError } from '@brillout/json-serializer/stringify';
|
|
4
|
-
import { assert, assertUsage, assertWarning, getPropAccessNotation, hasProp, unique } from '../utils.js';
|
|
6
|
+
import { assert, assertUsage, assertWarning, getPropAccessNotation, hasProp, isObject, unique } from '../utils.js';
|
|
5
7
|
import { isErrorPage } from '../../../shared/error-page.js';
|
|
6
8
|
import { addIs404ToPageProps } from '../../../shared/addIs404ToPageProps.js';
|
|
7
9
|
import pc from '@brillout/picocolors';
|
|
8
|
-
import {
|
|
10
|
+
import { NOT_SERIALIZABLE } from '../../../shared/NOT_SERIALIZABLE.js';
|
|
9
11
|
import { pageContextInitIsPassedToClient } from '../../../shared/misc/pageContextInitIsPassedToClient.js';
|
|
10
12
|
import { isServerSideError } from '../../../shared/misc/isServerSideError.js';
|
|
11
13
|
const PASS_TO_CLIENT = [
|
|
@@ -25,12 +27,8 @@ const PASS_TO_CLIENT = [
|
|
|
25
27
|
const PASS_TO_CLIENT_ERROR_PAGE = ['pageProps', 'is404', isServerSideError];
|
|
26
28
|
function serializePageContextClientSide(pageContext) {
|
|
27
29
|
const passToClient = getPassToClient(pageContext);
|
|
28
|
-
const pageContextClient =
|
|
29
|
-
passToClient.
|
|
30
|
-
// We set non-existing props to `undefined`, in order to pass the list of passToClient values to the client-side
|
|
31
|
-
pageContextClient[prop] = pageContext[prop];
|
|
32
|
-
});
|
|
33
|
-
if (Object.keys(pageContext._pageContextInit).some((p) => passToClient.includes(p))) {
|
|
30
|
+
const pageContextClient = applyPassToClient(passToClient, pageContext);
|
|
31
|
+
if (passToClient.some((prop) => getPropVal(pageContext._pageContextInit, prop))) {
|
|
34
32
|
pageContextClient[pageContextInitIsPassedToClient] = true;
|
|
35
33
|
}
|
|
36
34
|
let pageContextSerialized;
|
|
@@ -42,14 +40,15 @@ function serializePageContextClientSide(pageContext) {
|
|
|
42
40
|
let hasWarned = false;
|
|
43
41
|
const propsNonSerializable = [];
|
|
44
42
|
passToClient.forEach((prop) => {
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
43
|
+
const res = getPropVal(pageContext, prop);
|
|
44
|
+
if (!res)
|
|
45
|
+
return;
|
|
46
|
+
const { value } = res;
|
|
47
|
+
const varName = `pageContext${getPropKeys(prop).map(getPropAccessNotation).join('')}`;
|
|
48
48
|
try {
|
|
49
|
-
serialize(
|
|
49
|
+
serialize(value, varName);
|
|
50
50
|
}
|
|
51
51
|
catch (err) {
|
|
52
|
-
hasWarned = true;
|
|
53
52
|
propsNonSerializable.push(prop);
|
|
54
53
|
// useConfig() wrong usage
|
|
55
54
|
if (prop === '_configFromHook') {
|
|
@@ -62,7 +61,7 @@ function serializePageContextClientSide(pageContext) {
|
|
|
62
61
|
// Non-serializable pageContext set by the user
|
|
63
62
|
let msg = [
|
|
64
63
|
`${h(varName)} can't be serialized and, therefore, can't be passed to the client side.`,
|
|
65
|
-
`Make sure ${h(varName)} is serializable, or remove ${h(
|
|
64
|
+
`Make sure ${h(varName)} is serializable, or remove ${h(JSON.stringify(prop))} from ${h('passToClient')}.`
|
|
66
65
|
].join(' ');
|
|
67
66
|
if (isJsonSerializerError(err)) {
|
|
68
67
|
msg = `${msg} Serialization error: ${err.messageCore}.`;
|
|
@@ -75,11 +74,12 @@ function serializePageContextClientSide(pageContext) {
|
|
|
75
74
|
}
|
|
76
75
|
// We warn (instead of throwing an error) since Vike's client runtime throws an error (with `assertUsage()`) if the user's client code tries to access the property that cannot be serialized
|
|
77
76
|
assertWarning(false, msg, { onlyOnce: false });
|
|
77
|
+
hasWarned = true;
|
|
78
78
|
}
|
|
79
79
|
});
|
|
80
80
|
assert(hasWarned);
|
|
81
81
|
propsNonSerializable.forEach((prop) => {
|
|
82
|
-
pageContextClient[prop] =
|
|
82
|
+
pageContextClient[getPropKeys(prop)[0]] = NOT_SERIALIZABLE;
|
|
83
83
|
});
|
|
84
84
|
try {
|
|
85
85
|
pageContextSerialized = serialize(pageContextClient);
|
|
@@ -138,3 +138,57 @@ function serializePageContextAbort(pageContext) {
|
|
|
138
138
|
}
|
|
139
139
|
return serialize(pageContext);
|
|
140
140
|
}
|
|
141
|
+
function applyPassToClient(passToClient, pageContext) {
|
|
142
|
+
const pageContextClient = {};
|
|
143
|
+
passToClient.forEach((prop) => {
|
|
144
|
+
// Get value from pageContext
|
|
145
|
+
const res = getPropVal(pageContext, prop);
|
|
146
|
+
if (!res)
|
|
147
|
+
return;
|
|
148
|
+
const { value } = res;
|
|
149
|
+
// Set value to pageContextClient
|
|
150
|
+
setPropVal(pageContextClient, prop, value);
|
|
151
|
+
});
|
|
152
|
+
return pageContextClient;
|
|
153
|
+
}
|
|
154
|
+
// Get a nested property from an object using a dot-separated path such as 'user.id'
|
|
155
|
+
function getPropVal(obj, prop) {
|
|
156
|
+
const keys = getPropKeys(prop);
|
|
157
|
+
let value = obj;
|
|
158
|
+
for (const key of keys) {
|
|
159
|
+
if (isObject(value) && key in value) {
|
|
160
|
+
value = value[key];
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
return null; // Property or intermediate property doesn't exist
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return { value };
|
|
167
|
+
}
|
|
168
|
+
// Set a nested property in an object using a dot-separated path such as 'user.id'
|
|
169
|
+
function setPropVal(obj, prop, val) {
|
|
170
|
+
const keys = getPropKeys(prop);
|
|
171
|
+
let currentObj = obj;
|
|
172
|
+
// Creating intermediate objects if necessary
|
|
173
|
+
for (let i = 0; i <= keys.length - 2; i++) {
|
|
174
|
+
const key = keys[i];
|
|
175
|
+
if (!(key in currentObj)) {
|
|
176
|
+
// Create intermediate object
|
|
177
|
+
currentObj[key] = {};
|
|
178
|
+
}
|
|
179
|
+
if (!isObject(currentObj[key])) {
|
|
180
|
+
// Skip value upon data structure conflict
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
currentObj = currentObj[key];
|
|
184
|
+
}
|
|
185
|
+
// Set the final key to the value
|
|
186
|
+
const finalKey = keys[keys.length - 1];
|
|
187
|
+
currentObj[finalKey] = val;
|
|
188
|
+
}
|
|
189
|
+
function getPropKeys(prop) {
|
|
190
|
+
// Like `prop.split('.')` but with added support for `\` escaping, see serializePageContextClientSide.spec.ts
|
|
191
|
+
return prop
|
|
192
|
+
.split(/(?<!\\)\./) // Split on unescaped dots
|
|
193
|
+
.map((key) => key.replace(/\\\./g, '.')); // Replace escaped dots with literal dots
|
|
194
|
+
}
|
|
@@ -7,5 +7,8 @@ function preparePageContextForUserConsumptionServerSide(pageContext) {
|
|
|
7
7
|
assert(isPlainObject(pageContext.routeParams));
|
|
8
8
|
assert('Page' in pageContext);
|
|
9
9
|
assert(typeof pageContext.isClientSideNavigation === 'boolean');
|
|
10
|
+
assert(pageContext._isPageContextObject);
|
|
11
|
+
assert(pageContext.isClientSide === false);
|
|
12
|
+
assert(typeof pageContext.isPrerendering === 'boolean');
|
|
10
13
|
preparePageContextForUserConsumption(pageContext);
|
|
11
14
|
}
|