vike 0.4.159 → 0.4.160-commit-30d535e

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 (125) hide show
  1. package/dist/cjs/node/plugin/index.js +20 -1
  2. package/dist/cjs/node/plugin/plugins/autoFullBuild.js +2 -2
  3. package/dist/cjs/node/plugin/plugins/buildConfig.js +9 -0
  4. package/dist/cjs/node/plugin/plugins/commonConfig.js +1 -1
  5. package/dist/cjs/node/plugin/plugins/config/stemUtils.js +1 -1
  6. package/dist/cjs/node/plugin/plugins/devConfig/index.js +1 -1
  7. package/dist/cjs/node/plugin/plugins/importBuild/index.js +1 -1
  8. package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/getConfigFileExport.js +18 -0
  9. package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/crawlPlusFiles.js +1 -5
  10. package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/filesystemRouting.js +28 -46
  11. package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/loadFileAtConfigTime.js +129 -0
  12. package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/resolveFilePath.js +33 -0
  13. package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/resolveImportPath.js +139 -0
  14. package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/{replaceImportStatements.js → getVikeConfig/transformFileImports.js} +44 -13
  15. package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/{transpileAndExecuteFile.js → getVikeConfig/transpileAndExecuteFile.js} +105 -78
  16. package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.js +201 -382
  17. package/dist/cjs/node/plugin/plugins/previewConfig.js +1 -1
  18. package/dist/cjs/node/plugin/shared/getHttpRequestAsyncStore.js +1 -1
  19. package/dist/cjs/node/plugin/shared/loggerNotProd/log.js +3 -3
  20. package/dist/cjs/node/plugin/shared/loggerNotProd.js +1 -1
  21. package/dist/cjs/node/plugin/utils.js +1 -2
  22. package/dist/cjs/node/prerender/runPrerender.js +12 -5
  23. package/dist/cjs/node/runtime/globalContext.js +7 -3
  24. package/dist/cjs/node/runtime/html/injectAssets/getHtmlTags.js +2 -2
  25. package/dist/cjs/node/runtime/html/renderHtml.js +1 -1
  26. package/dist/cjs/node/runtime/html/stream.js +2 -2
  27. package/dist/cjs/node/runtime/renderPage/executeOnRenderHtmlHook.js +2 -2
  28. package/dist/cjs/node/runtime/renderPage/getHttpResponseBody.js +1 -1
  29. package/dist/cjs/node/runtime/renderPage/loadUserFilesServerSide.js +1 -1
  30. package/dist/cjs/node/runtime/renderPage/{logHintForCjsEsmError.js → logErrorHint.js} +81 -43
  31. package/dist/cjs/node/runtime/renderPage/loggerProd.js +3 -3
  32. package/dist/cjs/node/runtime/utils.js +1 -1
  33. package/dist/cjs/shared/page-configs/assertPlusFileExport.js +44 -0
  34. package/dist/cjs/shared/page-configs/serialize/parseConfigValuesImported.js +54 -27
  35. package/dist/cjs/shared/route/abort.js +1 -1
  36. package/dist/cjs/shared/route/executeGuardHook.js +3 -2
  37. package/dist/cjs/shared/utils.js +0 -1
  38. package/dist/cjs/utils/assert.js +5 -6
  39. package/dist/cjs/utils/assertIsNotProductionRuntime.js +35 -17
  40. package/dist/cjs/utils/{findUserPackageJsonPath.js → findFile.js} +11 -8
  41. package/dist/cjs/utils/nodeEnv.js +33 -1
  42. package/dist/cjs/utils/objectKeys.js +19 -3
  43. package/dist/cjs/utils/projectInfo.js +2 -4
  44. package/dist/cjs/utils/sorter.js +62 -1
  45. package/dist/esm/node/plugin/index.js +21 -2
  46. package/dist/esm/node/plugin/plugins/autoFullBuild.js +2 -2
  47. package/dist/esm/node/plugin/plugins/buildConfig.js +10 -1
  48. package/dist/esm/node/plugin/plugins/commonConfig.js +2 -2
  49. package/dist/esm/node/plugin/plugins/config/stemUtils.js +2 -2
  50. package/dist/esm/node/plugin/plugins/devConfig/index.js +2 -2
  51. package/dist/esm/node/plugin/plugins/importBuild/index.js +2 -2
  52. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getConfigFileExport.d.ts +2 -0
  53. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getConfigFileExport.js +12 -0
  54. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/configDefinitionsBuiltIn.d.ts +8 -1
  55. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/crawlPlusFiles.d.ts +0 -1
  56. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/crawlPlusFiles.js +1 -5
  57. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/filesystemRouting.d.ts +39 -12
  58. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/filesystemRouting.js +29 -47
  59. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/loadFileAtConfigTime.d.ts +21 -0
  60. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/loadFileAtConfigTime.js +123 -0
  61. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/resolveFilePath.d.ts +5 -0
  62. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/resolveFilePath.js +27 -0
  63. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/resolveImportPath.d.ts +12 -0
  64. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/resolveImportPath.js +133 -0
  65. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/{replaceImportStatements.d.ts → getVikeConfig/transformFileImports.d.ts} +5 -5
  66. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/{replaceImportStatements.js → getVikeConfig/transformFileImports.js} +43 -12
  67. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/{transpileAndExecuteFile.d.ts → getVikeConfig/transpileAndExecuteFile.d.ts} +3 -3
  68. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/{transpileAndExecuteFile.js → getVikeConfig/transpileAndExecuteFile.js} +105 -78
  69. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.d.ts +16 -1
  70. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.js +199 -380
  71. package/dist/esm/node/plugin/plugins/previewConfig.js +2 -2
  72. package/dist/esm/node/plugin/shared/getHttpRequestAsyncStore.js +1 -1
  73. package/dist/esm/node/plugin/shared/loggerNotProd/log.js +3 -3
  74. package/dist/esm/node/plugin/shared/loggerNotProd.js +1 -1
  75. package/dist/esm/node/plugin/utils.d.ts +1 -2
  76. package/dist/esm/node/plugin/utils.js +1 -2
  77. package/dist/esm/node/prerender/runPrerender.js +13 -6
  78. package/dist/esm/node/runtime/globalContext.js +8 -4
  79. package/dist/esm/node/runtime/html/injectAssets/getHtmlTags.js +2 -2
  80. package/dist/esm/node/runtime/html/renderHtml.js +1 -1
  81. package/dist/esm/node/runtime/html/stream.js +2 -2
  82. package/dist/esm/node/runtime/renderPage/executeOnRenderHtmlHook.js +2 -2
  83. package/dist/esm/node/runtime/renderPage/getHttpResponseBody.js +1 -1
  84. package/dist/esm/node/runtime/renderPage/loadUserFilesServerSide.js +1 -1
  85. package/dist/esm/node/runtime/renderPage/logErrorHint.d.ts +8 -0
  86. package/dist/esm/node/runtime/renderPage/{logHintForCjsEsmError.js → logErrorHint.js} +80 -42
  87. package/dist/esm/node/runtime/renderPage/loggerProd.js +3 -3
  88. package/dist/esm/node/runtime/utils.d.ts +1 -1
  89. package/dist/esm/node/runtime/utils.js +1 -1
  90. package/dist/esm/shared/page-configs/Config.d.ts +1 -1
  91. package/dist/esm/shared/page-configs/PageConfig.d.ts +6 -0
  92. package/dist/esm/shared/page-configs/assertPlusFileExport.d.ts +2 -0
  93. package/dist/esm/shared/page-configs/assertPlusFileExport.js +38 -0
  94. package/dist/esm/shared/page-configs/serialize/parseConfigValuesImported.js +54 -27
  95. package/dist/esm/shared/route/abort.js +2 -2
  96. package/dist/esm/shared/route/executeGuardHook.js +3 -2
  97. package/dist/esm/shared/utils.d.ts +0 -1
  98. package/dist/esm/shared/utils.js +0 -1
  99. package/dist/esm/utils/assert.js +5 -6
  100. package/dist/esm/utils/assertIsNotProductionRuntime.d.ts +8 -6
  101. package/dist/esm/utils/assertIsNotProductionRuntime.js +35 -17
  102. package/dist/esm/utils/debug.d.ts +1 -1
  103. package/dist/esm/utils/findFile.d.ts +3 -0
  104. package/dist/esm/utils/findFile.js +21 -0
  105. package/dist/esm/utils/nodeEnv.d.ts +6 -0
  106. package/dist/esm/utils/nodeEnv.js +29 -0
  107. package/dist/esm/utils/objectKeys.d.ts +10 -1
  108. package/dist/esm/utils/objectKeys.js +20 -3
  109. package/dist/esm/utils/projectInfo.d.ts +2 -8
  110. package/dist/esm/utils/projectInfo.js +2 -4
  111. package/dist/esm/utils/sorter.d.ts +59 -0
  112. package/dist/esm/utils/sorter.js +61 -0
  113. package/package.json +1 -1
  114. package/dist/cjs/shared/page-configs/assertExports.js +0 -67
  115. package/dist/cjs/utils/objectEntries.js +0 -8
  116. package/dist/esm/node/runtime/renderPage/logHintForCjsEsmError.d.ts +0 -13
  117. package/dist/esm/shared/page-configs/assertExports.d.ts +0 -6
  118. package/dist/esm/shared/page-configs/assertExports.js +0 -61
  119. package/dist/esm/utils/findUserPackageJsonPath.d.ts +0 -2
  120. package/dist/esm/utils/findUserPackageJsonPath.js +0 -18
  121. package/dist/esm/utils/objectEntries.d.ts +0 -4
  122. package/dist/esm/utils/objectEntries.js +0 -5
  123. /package/dist/cjs/node/runtime/renderPage/{logHintForCjsEsmError → logErrorHint}/errors.js +0 -0
  124. /package/dist/esm/node/runtime/renderPage/{logHintForCjsEsmError → logErrorHint}/errors.d.ts +0 -0
  125. /package/dist/esm/node/runtime/renderPage/{logHintForCjsEsmError → logErrorHint}/errors.js +0 -0
@@ -1,42 +1,69 @@
1
1
  export { parseConfigValuesImported };
2
2
  import { assert } from '../../utils.js';
3
- import { assertExportsOfValueFile } from '../assertExports.js';
3
+ import { assertPlusFileExport } from '../assertPlusFileExport.js';
4
4
  function parseConfigValuesImported(configValuesImported) {
5
- const configValues = {};
6
- const addConfigValue = (configName, value, importPath, exportName) => {
7
- configValues[configName] = {
8
- value,
9
- definedAt: {
10
- // importPath cannot be relative to the current file, since the current file is a virtual file
11
- filePathToShowToUser: importPath,
12
- fileExportPathToShowToUser: [configName, 'default'].includes(exportName)
13
- ? []
14
- : // Side-effect config
15
- [exportName]
16
- }
17
- };
18
- assertIsNotNull(value, configName, importPath);
19
- };
20
- configValuesImported.forEach((configValueLoaded) => {
5
+ const configValuesUnmerged = {};
6
+ configValuesImported
7
+ .filter((c) => c.configName !== 'client')
8
+ .forEach((configValueLoaded) => {
21
9
  if (configValueLoaded.isValueFile) {
22
10
  const { exportValues, importPath, configName } = configValueLoaded;
23
- if (configName !== 'client') {
24
- assertExportsOfValueFile(exportValues, importPath, configName);
25
- }
11
+ assertPlusFileExport(exportValues, importPath, configName);
26
12
  Object.entries(exportValues).forEach(([exportName, exportValue]) => {
27
13
  const isSideExport = exportName !== 'default'; // .md files may have "side-exports" such as `export { frontmatter }`
28
14
  const configName = isSideExport ? exportName : configValueLoaded.configName;
29
- if (isSideExport && configName in configValues) {
30
- // We can't avoid side-export conflicts upstream. (Because we cannot know about side-exports upstream at build-time.)
31
- // Side-exports have the lowest priority.
32
- return;
33
- }
34
- addConfigValue(configName, exportValue, importPath, exportName);
15
+ configValuesUnmerged[configName] ?? (configValuesUnmerged[configName] = []);
16
+ configValuesUnmerged[configName].push({
17
+ value: exportValue,
18
+ importPath,
19
+ exportName,
20
+ isSideExport
21
+ });
35
22
  });
36
23
  }
37
24
  else {
38
25
  const { configName, importPath, exportValue, exportName } = configValueLoaded;
39
- addConfigValue(configName, exportValue, importPath, exportName);
26
+ configValuesUnmerged[configName] ?? (configValuesUnmerged[configName] = []);
27
+ configValuesUnmerged[configName].push({
28
+ value: exportValue,
29
+ importPath,
30
+ exportName,
31
+ isSideExport: false
32
+ });
33
+ }
34
+ });
35
+ const configValues = {};
36
+ Object.entries(configValuesUnmerged).forEach(([configName, values]) => {
37
+ const valuesWithoutSideExports = values.filter((v) => !v.isSideExport);
38
+ const isCumulative = valuesWithoutSideExports.length > 1;
39
+ const noSideExports = valuesWithoutSideExports.length === values.length;
40
+ if (isCumulative) {
41
+ // Vike currently doesn't support side exports for cumulative configs
42
+ assert(noSideExports);
43
+ // TODO: implement
44
+ assert(false);
45
+ }
46
+ else {
47
+ const val = valuesWithoutSideExports[0] ??
48
+ // We can't avoid side-export conflicts upstream. (We cannot know about side-exports at build-time.)
49
+ // Side-exports have lower precedence.
50
+ values[0];
51
+ assert(val);
52
+ const { value, importPath, exportName } = val;
53
+ configValues[configName] = {
54
+ value,
55
+ definedAt: {
56
+ // importPath cannot be relative to the current file, since the current file is a virtual file
57
+ filePathToShowToUser: importPath,
58
+ fileExportPathToShowToUser: [configName, 'default'].includes(exportName)
59
+ ? []
60
+ : [
61
+ // Side-export
62
+ exportName
63
+ ]
64
+ }
65
+ };
66
+ assertIsNotNull(value, configName, importPath);
40
67
  }
41
68
  });
42
69
  return configValues;
@@ -7,7 +7,7 @@ export { logAbortErrorHandled };
7
7
  export { getPageContextFromAllRewrites };
8
8
  export { AbortRender };
9
9
  export { assertNoInfiniteAbortLoop };
10
- import { assert, assertInfo, assertUsage, assertWarning, checkType, hasProp, isUriWithProtocol, isUserHookError, joinEnglish, objectAssign, projectInfo, truncateString } from './utils.js';
10
+ import { assert, assertInfo, assertUsage, assertWarning, checkType, hasProp, isUriWithProtocol, isUserHookError, joinEnglish, objectAssign, truncateString } from './utils.js';
11
11
  import pc from '@brillout/picocolors';
12
12
  /**
13
13
  * Abort the rendering of the current page, and redirect the user to another URL instead.
@@ -132,7 +132,7 @@ function logAbortErrorHandled(err, isProduction, pageContext) {
132
132
  }
133
133
  function assertStatusCode(statusCode, expected, caller) {
134
134
  const expectedEnglish = joinEnglish(expected.map((s) => s.toString()), 'or');
135
- assertWarning(expected.includes(statusCode), `Unepexected status code ${statusCode} passed to ${caller}(), we recommend ${expectedEnglish} instead. (Or reach out at ${projectInfo.githubRepository}/issues/1008 if you believe ${statusCode} should be added.)`, { onlyOnce: true });
135
+ assertWarning(expected.includes(statusCode), `Unepexected status code ${statusCode} passed to ${caller}(), we recommend ${expectedEnglish} instead. (Or reach out at https://github.com/vikejs/vike/issues/1008 if you believe ${statusCode} should be added.)`, { onlyOnce: true });
136
136
  }
137
137
  function getPageContextFromAllRewrites(pageContextsFromRewrite) {
138
138
  assertNoInfiniteLoop(pageContextsFromRewrite);
@@ -1,6 +1,7 @@
1
1
  export { executeGuardHook };
2
2
  import { getHook, getHookTimeoutDefault } from '../hooks/getHook.js';
3
3
  import { assert, assertUsage, executeHook, isCallable } from './utils.js';
4
+ const errIntro = 'The guard() hook defined by';
4
5
  async function executeGuardHook(pageContext, prepareForUserConsumption) {
5
6
  let hook;
6
7
  if (pageContext._pageFilesAll.length > 0) {
@@ -20,7 +21,7 @@ async function executeGuardHook(pageContext, prepareForUserConsumption) {
20
21
  if (res)
21
22
  pageContextForUserConsumption = res;
22
23
  const hookResult = await executeHook(() => guard(pageContextForUserConsumption), hook);
23
- assertUsage(hookResult === undefined, `The guard() hook of ${hook.hookFilePath} returns a value, but guard() doesn't accept any return value`);
24
+ assertUsage(hookResult === undefined, `${errIntro} ${hook.hookFilePath} returns a value, but guard() shouldn't return any value`);
24
25
  }
25
26
  function findPageGuard(pageId, pageFilesAll) {
26
27
  const pageRouteFile = pageFilesAll.find((p) => p.pageId === pageId && p.fileType === '.page.route');
@@ -33,6 +34,6 @@ function findPageGuard(pageId, pageFilesAll) {
33
34
  return null;
34
35
  const hookFilePath = filePath;
35
36
  const hookTimeout = getHookTimeoutDefault('guard');
36
- assertUsage(isCallable(hookFn), `guard() defined by ${hookFilePath} should be a function`);
37
+ assertUsage(isCallable(hookFn), `${errIntro} ${hookFilePath} should be a function`);
37
38
  return { hookFn, hookName: 'guard', hookFilePath, hookTimeout };
38
39
  }
@@ -18,6 +18,5 @@ export * from '../utils/projectInfo.js';
18
18
  export * from '../utils/isPropertyGetter.js';
19
19
  export * from '../utils/isPromise.js';
20
20
  export * from '../utils/checkType.js';
21
- export * from '../utils/objectEntries.js';
22
21
  export * from '../utils/getValuePrintable.js';
23
22
  export * from '../utils/escapeRegex.js';
@@ -22,6 +22,5 @@ export * from '../utils/projectInfo.js';
22
22
  export * from '../utils/isPropertyGetter.js';
23
23
  export * from '../utils/isPromise.js';
24
24
  export * from '../utils/checkType.js';
25
- export * from '../utils/objectEntries.js';
26
25
  export * from '../utils/getValuePrintable.js';
27
26
  export * from '../utils/escapeRegex.js';
@@ -25,8 +25,8 @@ const globalObject = getGlobalObject('utils/assert.ts', {
25
25
  },
26
26
  showStackTraceList: new WeakSet()
27
27
  });
28
- const projectTag = `[${projectInfo.npmPackageName}]`;
29
- const projectTagWithVersion = `[${projectInfo.npmPackageName}@${projectInfo.projectVersion}]`;
28
+ const projectTag = `[vike]`;
29
+ const projectTagWithVersion = `[vike@${projectInfo.projectVersion}]`;
30
30
  const numberOfStackTraceLinesToRemove = 2;
31
31
  function assert(condition, debugInfo) {
32
32
  if (condition)
@@ -36,12 +36,11 @@ function assert(condition, debugInfo) {
36
36
  return null;
37
37
  }
38
38
  const debugInfoSerialized = typeof debugInfo === 'string' ? debugInfo : JSON.stringify(debugInfo);
39
- return pc.dim(`Debug info (for ${projectInfo.projectName} maintainers; you can ignore this): ${debugInfoSerialized}`);
39
+ return pc.dim(`Debug info (for Vike maintainers; you can ignore this): ${debugInfoSerialized}`);
40
40
  })();
41
- const link = `${projectInfo.githubRepository}/issues/new`;
42
41
  let errMsg = [
43
- `You stumbled upon a bug in ${projectInfo.projectName}'s source code.`,
44
- `Go to ${pc.blue(link)} and copy-paste this error. A maintainer will fix the bug (usually under 24 hours).`,
42
+ `You stumbled upon a Vike bug.`,
43
+ `Go to ${pc.blue('https://github.com/vikejs/vike/issues/new')} and copy-paste this error. A maintainer will fix the bug (usually under 24 hours).`,
45
44
  debugStr
46
45
  ]
47
46
  .filter(Boolean)
@@ -1,10 +1,12 @@
1
1
  export { assertIsNotProductionRuntime };
2
- export { markEnvAsDev };
3
- export { markEnvAsPreview };
4
- export { markEnvAsVite };
2
+ export { markEnvAsViteDev };
3
+ export { markEnvAsVitePreview };
4
+ export { markEnvAsVikePluginLoaded };
5
5
  export { assertEnv };
6
+ export { vikeVitePluginLoadedInProductionError };
7
+ declare const vikeVitePluginLoadedInProductionError = "Loading Vike's Vite plugin (the vike/plugin module) is prohibited in production.";
6
8
  declare function assertIsNotProductionRuntime(): void | undefined;
7
- declare function markEnvAsDev(): void | undefined;
8
- declare function markEnvAsPreview(): void | undefined;
9
- declare function markEnvAsVite(): void;
9
+ declare function markEnvAsViteDev(): void | undefined;
10
+ declare function markEnvAsVitePreview(): void | undefined;
11
+ declare function markEnvAsVikePluginLoaded(): void;
10
12
  declare function assertEnv(): void | undefined;
@@ -1,41 +1,59 @@
1
1
  // Mechanism to ensure code isn't loaded by production runtime
2
2
  export { assertIsNotProductionRuntime };
3
- export { markEnvAsDev };
4
- export { markEnvAsPreview };
5
- export { markEnvAsVite };
3
+ export { markEnvAsViteDev };
4
+ export { markEnvAsVitePreview };
5
+ export { markEnvAsVikePluginLoaded };
6
6
  export { assertEnv };
7
- import { assert } from './assert.js';
7
+ export { vikeVitePluginLoadedInProductionError };
8
+ import { assert, assertUsage } from './assert.js';
8
9
  import { assertIsNotBrowser } from './assertIsNotBrowser.js';
10
+ import { createDebugger } from './debug.js';
9
11
  import { getGlobalObject } from './getGlobalObject.js';
10
12
  import { isVitest } from './isVitest.js';
11
13
  assertIsNotBrowser();
14
+ const debug = createDebugger('vike:setup');
15
+ const vikeVitePluginLoadedInProductionError = `Loading Vike's Vite plugin (the vike/plugin module) is prohibited in production.`;
12
16
  const env = getGlobalObject('utils/assertIsNotProductionRuntime.ts', {});
13
- // Called by *.ts that want to ensure that they aren't loaded by the server runtime in production
17
+ // Called by Vike modules that want to ensure that they aren't loaded by the server runtime in production
14
18
  function assertIsNotProductionRuntime() {
15
- env.shouldBeVite = true;
19
+ if (debug.isEnabled)
20
+ debug('assertIsNotProductionRuntime()', new Error().stack);
21
+ env.shouldNotBeProduction = true;
16
22
  }
17
23
  // Called by Vite hook configureServer()
18
- function markEnvAsDev() {
19
- env.isDev = true;
24
+ function markEnvAsViteDev() {
25
+ if (debug.isEnabled)
26
+ debug('markEnvAsViteDev()', new Error().stack);
27
+ env.isViteDev = true;
20
28
  }
21
29
  // Called by Vite hook configurePreviewServer()
22
- function markEnvAsPreview() {
23
- env.isPreview = true;
30
+ function markEnvAsVitePreview() {
31
+ if (debug.isEnabled)
32
+ debug('markEnvAsVitePreview()', new Error().stack);
33
+ env.isVitePreview = true;
24
34
  }
25
35
  // Called by ../node/plugin/index.ts
26
- function markEnvAsVite() {
27
- env.isVite = true;
36
+ function markEnvAsVikePluginLoaded() {
37
+ if (debug.isEnabled)
38
+ debug('markEnvAsVikePluginLoaded()', new Error().stack);
39
+ env.isVikePluginLoaded = true;
28
40
  }
29
41
  // Called by ../node/runtime/index.ts
30
42
  function assertEnv() {
43
+ if (debug.isEnabled)
44
+ debug('assertEnv()', new Error().stack);
31
45
  if (isVitest())
32
46
  return;
33
- if (env.isDev || env.isPreview) {
34
- assert(env.isVite);
35
- assert(env.shouldBeVite);
47
+ const isProduction = !env.isViteDev && !env.isVitePreview;
48
+ if (isProduction) {
49
+ // Seems to be the only reliable way to assert that the user doesn't load Vike's Vite plugin in production. (The other assert() that uses process.env.NODE_ENV doesn't work if the user sets the process.env.NODE_ENV value later.)
50
+ assertUsage(!env.isVikePluginLoaded, vikeVitePluginLoadedInProductionError);
51
+ // This assert() is the main goal of this file: it ensures assertIsNotProductionRuntime()
52
+ assert(!env.shouldNotBeProduction);
36
53
  }
37
54
  else {
38
- assert(!env.isVite);
39
- assert(!env.shouldBeVite);
55
+ // This assert() is obious and boring
56
+ assert(env.shouldNotBeProduction);
57
+ assert(env.isVikePluginLoaded);
40
58
  }
41
59
  }
@@ -1,7 +1,7 @@
1
1
  export { createDebugger };
2
2
  export { isDebugEnabled };
3
3
  export type { Debug };
4
- type Flag = 'vike:routing' | 'vike:error' | 'vike:stream' | 'vike:log' | 'vike:virtual-files' | 'vike:outDir' | 'vike:extractExportNames' | 'vike:extractAssets' | 'vike:glob' | 'vike:pageFiles' | 'vike:stem';
4
+ type Flag = 'vike:routing' | 'vike:error' | 'vike:stream' | 'vike:log' | 'vike:virtual-files' | 'vike:outDir' | 'vike:extractExportNames' | 'vike:extractAssets' | 'vike:glob' | 'vike:pageFiles' | 'vike:stem' | 'vike:setup';
5
5
  type Debug = ReturnType<typeof createDebugger>;
6
6
  type Options = {
7
7
  serialization?: {
@@ -0,0 +1,3 @@
1
+ export { findFile };
2
+ type Filename = 'package.json' | 'vike.config.js' | 'vike.config.ts';
3
+ declare function findFile(arg: Filename | Filename[], cwd: string): null | string;
@@ -0,0 +1,21 @@
1
+ export { findFile };
2
+ import path from 'path';
3
+ import fs from 'fs';
4
+ function findFile(arg, cwd) {
5
+ const filenames = Array.isArray(arg) ? arg : [arg];
6
+ let dir = cwd;
7
+ while (true) {
8
+ for (const filename of filenames) {
9
+ const configFilePath = path.join(dir, `./${filename}`);
10
+ if (fs.existsSync(configFilePath)) {
11
+ // return toPosixPath(configFilePath)
12
+ return configFilePath;
13
+ }
14
+ }
15
+ const dirPrevious = dir;
16
+ dir = path.dirname(dir);
17
+ if (dir === dirPrevious) {
18
+ return null;
19
+ }
20
+ }
21
+ }
@@ -1,4 +1,10 @@
1
1
  export { getNodeEnv };
2
2
  export { setNodeEnvToProduction };
3
+ export { isNodeEnvDev };
4
+ export { getNodeEnvDesc };
5
+ export { assertNodeEnvIsNotDev };
3
6
  declare function getNodeEnv(): null | undefined | string;
4
7
  declare function setNodeEnvToProduction(): void | undefined;
8
+ declare function isNodeEnvDev(): boolean;
9
+ declare function getNodeEnvDesc(): `environment is set to be a ${string} environment by process.env.NODE_ENV === ${string}`;
10
+ declare function assertNodeEnvIsNotDev(operation: 'building' | 'pre-rendering'): void;
@@ -1,5 +1,12 @@
1
1
  export { getNodeEnv };
2
2
  export { setNodeEnvToProduction };
3
+ export { isNodeEnvDev };
4
+ export { getNodeEnvDesc };
5
+ export { assertNodeEnvIsNotDev };
6
+ import pc from '@brillout/picocolors';
7
+ import { assertIsNotBrowser } from './assertIsNotBrowser.js';
8
+ import { assertUsage } from './assert.js';
9
+ assertIsNotBrowser();
3
10
  function getNodeEnv() {
4
11
  if (typeof process === 'undefined')
5
12
  return null;
@@ -11,3 +18,25 @@ function setNodeEnvToProduction() {
11
18
  const { env } = proc;
12
19
  env.NODE_ENV = 'production';
13
20
  }
21
+ function isNodeEnvDev() {
22
+ const nodeEnv = getNodeEnv();
23
+ if (!nodeEnv)
24
+ return true;
25
+ if (['development', 'dev'].includes(nodeEnv))
26
+ return true;
27
+ // That's quite aggressive, let's see if some user complains
28
+ return false;
29
+ }
30
+ function getNodeEnvDesc() {
31
+ const nodeEnv = getNodeEnv();
32
+ const isDev = isNodeEnvDev();
33
+ const nodeEnvDesc = `environment is set to be a ${(isDev ? 'development' : 'production')} environment by ${pc.cyan(`process.env.NODE_ENV === ${JSON.stringify(nodeEnv)}`)}`;
34
+ return nodeEnvDesc;
35
+ }
36
+ function assertNodeEnvIsNotDev(operation) {
37
+ const isDev = isNodeEnvDev();
38
+ if (!isDev)
39
+ return;
40
+ const nodeEnvDesc = getNodeEnvDesc();
41
+ assertUsage(false, `The ${nodeEnvDesc} which is forbidden upon ${operation}, see https://vike.dev/NODE_ENV`);
42
+ }
@@ -1,2 +1,11 @@
1
+ export { objectEntries };
2
+ export { objectFromEntries };
3
+ export { objectKeys };
4
+ type ValueOf<T> = T[keyof T];
5
+ type Entries<T> = [keyof T, ValueOf<T>][];
6
+ /** Same as Object.entries() but with type inference */
7
+ declare function objectEntries<T extends object>(obj: T): Entries<T>;
8
+ /** Same as Object.fromEntries() but with type inference */
9
+ declare function objectFromEntries<T extends [PropertyKey, unknown][]>(arr: T): Record<T[number][0], T[number][1]>;
1
10
  /** Same as Object.keys() but with type inference */
2
- export declare function objectKeys<T extends Record<string, unknown>>(obj: T): Array<keyof T>;
11
+ declare function objectKeys<T extends object>(obj: T): (keyof T)[];
@@ -1,6 +1,23 @@
1
- /** Same as Object.keys() but with type inference */
2
- export function objectKeys(obj) {
3
- return Object.keys(obj);
1
+ export { objectEntries };
2
+ export { objectFromEntries };
3
+ export { objectKeys };
4
+ // https://stackoverflow.com/questions/60141960/typescript-key-value-relation-preserving-object-entries-type/75337277#75337277
5
+ /** Same as Object.entries() but with type inference */
6
+ function objectEntries(obj) {
7
+ return Object.entries(obj);
8
+ }
9
+ /** Same as Object.fromEntries() but with type inference */
10
+ function objectFromEntries(arr) {
11
+ return Object.fromEntries(arr);
4
12
  }
5
13
  // https://stackoverflow.com/questions/52856496/typescript-object-keys-return-string
6
14
  // https://github.com/sindresorhus/ts-extras/blob/main/source/object-keys.ts
15
+ /** Same as Object.keys() but with type inference */
16
+ function objectKeys(obj) {
17
+ return Object.keys(obj);
18
+ }
19
+ /*
20
+ function objectKeys2<T extends Record<string, unknown>>(obj: T): Array<keyof T> {
21
+ return Object.keys(obj)
22
+ }
23
+ */
@@ -1,13 +1,7 @@
1
1
  export { projectInfo };
2
- export type { ProjectTag };
3
2
  export { PROJECT_VERSION };
4
- declare const PROJECT_VERSION: "0.4.159";
5
- type PackageName = typeof projectInfo.npmPackageName;
6
- type ProjectVersion = typeof projectInfo.projectVersion;
7
- type ProjectTag = `[${PackageName}]` | `[${PackageName}@${ProjectVersion}]`;
3
+ declare const PROJECT_VERSION: "0.4.160-commit-30d535e";
8
4
  declare const projectInfo: {
9
5
  projectName: "Vike";
10
- projectVersion: "0.4.159";
11
- npmPackageName: "vike";
12
- githubRepository: "https://github.com/vikejs/vike";
6
+ projectVersion: "0.4.160-commit-30d535e";
13
7
  };
@@ -1,12 +1,10 @@
1
1
  export { projectInfo };
2
2
  export { PROJECT_VERSION };
3
3
  import { onProjectInfo } from './assertSingleInstance.js';
4
- const PROJECT_VERSION = '0.4.159';
4
+ const PROJECT_VERSION = '0.4.160-commit-30d535e';
5
5
  const projectInfo = {
6
6
  projectName: 'Vike',
7
- projectVersion: PROJECT_VERSION,
8
- npmPackageName: 'vike',
9
- githubRepository: 'https://github.com/vikejs/vike'
7
+ projectVersion: PROJECT_VERSION
10
8
  };
11
9
  // Trick: since `utils/asserts.ts` depends on this file (`utils/projectInfo.ts`), we can have confidence that this file is always instantiated. So that we don't have to initialize this code snippet at every possible entry. (There are a *lot* of entries: `client/router/`, `client/`, `node/`, `node/plugin/`, `node/cli`, etc.)
12
10
  onProjectInfo(projectInfo.projectVersion);
@@ -2,7 +2,66 @@ export { higherFirst };
2
2
  export { lowerFirst };
3
3
  export { makeFirst };
4
4
  export { makeLast };
5
+ export { reverse };
6
+ /**
7
+ * ```js
8
+ * let arr = [
9
+ * { price: 10 },
10
+ * { price: 1000 },
11
+ * { price: 100 }
12
+ * ]
13
+ * arr = arr.sort(higherFirst(el => el.price))
14
+ * isEqual(arr, [
15
+ * { price: 1000 },
16
+ * { price: 100 },
17
+ * { price: 10 }
18
+ * ])
19
+ * ```
20
+ */
5
21
  declare function higherFirst<T>(getValue: (element: T) => number): (element1: T, element2: T) => 0 | 1 | -1;
22
+ /**
23
+ * ```js
24
+ * let arr = [
25
+ * { price: 10 },
26
+ * { price: 1000 },
27
+ * { price: 100 }
28
+ * ]
29
+ * arr = arr.sort(lowerFirst(el => el.price))
30
+ * isEqual(arr, [
31
+ * { price: 10 },
32
+ * { price: 100 },
33
+ * { price: 1000 }
34
+ * ])
35
+ * ```
36
+ */
6
37
  declare function lowerFirst<T>(getValue: (element: T) => number): (element1: T, element2: T) => 0 | 1 | -1;
38
+ /**
39
+ * ```js
40
+ * let arr = [
41
+ * { name: 'iphone', isRocket: false },
42
+ * { name: 'starship', isRocket: true }
43
+ * ]
44
+ * arr = arr.sort(makeFirst(el => el.isRocket))
45
+ * isEqual(arr, [
46
+ * { name: 'starship', isRocket: true },
47
+ * { name: 'iphone', isRocket: false }
48
+ * ])
49
+ * ```
50
+ */
7
51
  declare function makeFirst<T>(getValue: (element: T) => boolean | null): (element1: T, element2: T) => 0 | 1 | -1;
52
+ /**
53
+ * ```js
54
+ * let arr = [
55
+ * { name: 'starship', isRocket: true },
56
+ * { name: 'iphone', isRocket: false }
57
+ * ]
58
+ * arr = arr.sort(makeLast(el => el.isRocket))
59
+ * isEqual(arr, [
60
+ * { name: 'iphone', isRocket: false },
61
+ * { name: 'starship', isRocket: true }
62
+ * ])
63
+ * ```
64
+ */
8
65
  declare function makeLast<T>(getValue: (element: T) => boolean | null): (element1: T, element2: T) => 0 | 1 | -1;
66
+ /** Reverse order result. */
67
+ declare function reverse(sortKey: 0 | 1 | -1): 0 | 1 | -1;
@@ -3,9 +3,25 @@ export { higherFirst };
3
3
  export { lowerFirst };
4
4
  export { makeFirst };
5
5
  export { makeLast };
6
+ export { reverse };
6
7
  // -1 => element1 first (i.e. `indexOf(element1) < indexOf(element2)`)
7
8
  // +1 => element2 first (i.e. `indexOf(element2) < indexOf(element1)`)
8
9
  // 0 => keep original order of element1 and element2
10
+ /**
11
+ * ```js
12
+ * let arr = [
13
+ * { price: 10 },
14
+ * { price: 1000 },
15
+ * { price: 100 }
16
+ * ]
17
+ * arr = arr.sort(higherFirst(el => el.price))
18
+ * isEqual(arr, [
19
+ * { price: 1000 },
20
+ * { price: 100 },
21
+ * { price: 10 }
22
+ * ])
23
+ * ```
24
+ */
9
25
  function higherFirst(getValue) {
10
26
  return (element1, element2) => {
11
27
  const val1 = getValue(element1);
@@ -16,6 +32,21 @@ function higherFirst(getValue) {
16
32
  return val1 > val2 ? -1 : 1;
17
33
  };
18
34
  }
35
+ /**
36
+ * ```js
37
+ * let arr = [
38
+ * { price: 10 },
39
+ * { price: 1000 },
40
+ * { price: 100 }
41
+ * ]
42
+ * arr = arr.sort(lowerFirst(el => el.price))
43
+ * isEqual(arr, [
44
+ * { price: 10 },
45
+ * { price: 100 },
46
+ * { price: 1000 }
47
+ * ])
48
+ * ```
49
+ */
19
50
  function lowerFirst(getValue) {
20
51
  return (element1, element2) => {
21
52
  const val1 = getValue(element1);
@@ -26,6 +57,19 @@ function lowerFirst(getValue) {
26
57
  return val1 < val2 ? -1 : 1;
27
58
  };
28
59
  }
60
+ /**
61
+ * ```js
62
+ * let arr = [
63
+ * { name: 'iphone', isRocket: false },
64
+ * { name: 'starship', isRocket: true }
65
+ * ]
66
+ * arr = arr.sort(makeFirst(el => el.isRocket))
67
+ * isEqual(arr, [
68
+ * { name: 'starship', isRocket: true },
69
+ * { name: 'iphone', isRocket: false }
70
+ * ])
71
+ * ```
72
+ */
29
73
  function makeFirst(getValue) {
30
74
  return (element1, element2) => {
31
75
  const val1 = getValue(element1);
@@ -44,6 +88,19 @@ function makeFirst(getValue) {
44
88
  assert(false);
45
89
  };
46
90
  }
91
+ /**
92
+ * ```js
93
+ * let arr = [
94
+ * { name: 'starship', isRocket: true },
95
+ * { name: 'iphone', isRocket: false }
96
+ * ]
97
+ * arr = arr.sort(makeLast(el => el.isRocket))
98
+ * isEqual(arr, [
99
+ * { name: 'iphone', isRocket: false },
100
+ * { name: 'starship', isRocket: true }
101
+ * ])
102
+ * ```
103
+ */
47
104
  function makeLast(getValue) {
48
105
  return makeFirst((element) => {
49
106
  const val = getValue(element);
@@ -55,3 +112,7 @@ function makeLast(getValue) {
55
112
  }
56
113
  });
57
114
  }
115
+ /** Reverse order result. */
116
+ function reverse(sortKey) {
117
+ return (-1 * sortKey);
118
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vike",
3
- "version": "0.4.159",
3
+ "version": "0.4.160-commit-30d535e",
4
4
  "scripts": {
5
5
  "dev": "tsc --watch",
6
6
  "build": "rimraf dist/ && pnpm run build:esm && pnpm run build:cjs",