vike 0.4.159 → 0.4.160-commit-a1101c1

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 (72) hide show
  1. package/dist/cjs/node/plugin/index.js +20 -1
  2. package/dist/cjs/node/plugin/plugins/devConfig/index.js +1 -1
  3. package/dist/cjs/node/plugin/plugins/importBuild/index.js +1 -1
  4. package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/getConfigFileExport.js +18 -0
  5. package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/loadFileConfigEnv.js +129 -0
  6. package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/resolveImportPath.js +139 -0
  7. package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/{replaceImportStatements.js → getVikeConfig/transformImports.js} +44 -13
  8. package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/{transpileAndExecuteFile.js → getVikeConfig/transpileAndExecuteFile.js} +110 -77
  9. package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.js +58 -279
  10. package/dist/cjs/node/plugin/plugins/previewConfig.js +1 -1
  11. package/dist/cjs/node/plugin/shared/getHttpRequestAsyncStore.js +1 -1
  12. package/dist/cjs/node/plugin/shared/loggerNotProd/log.js +3 -3
  13. package/dist/cjs/node/plugin/shared/loggerNotProd.js +1 -1
  14. package/dist/cjs/node/runtime/globalContext.js +4 -3
  15. package/dist/cjs/node/runtime/html/injectAssets/getHtmlTags.js +1 -1
  16. package/dist/cjs/node/runtime/html/renderHtml.js +1 -1
  17. package/dist/cjs/node/runtime/html/stream.js +2 -2
  18. package/dist/cjs/node/runtime/renderPage/executeOnRenderHtmlHook.js +2 -2
  19. package/dist/cjs/node/runtime/renderPage/getHttpResponseBody.js +1 -1
  20. package/dist/cjs/node/runtime/renderPage/loadUserFilesServerSide.js +1 -1
  21. package/dist/cjs/shared/page-configs/assertPlusFileExport.js +44 -0
  22. package/dist/cjs/shared/page-configs/serialize/parseConfigValuesImported.js +2 -2
  23. package/dist/cjs/shared/route/abort.js +1 -1
  24. package/dist/cjs/utils/assert.js +5 -6
  25. package/dist/cjs/utils/assertIsNotProductionRuntime.js +35 -17
  26. package/dist/cjs/utils/nodeEnv.js +11 -1
  27. package/dist/cjs/utils/projectInfo.js +2 -4
  28. package/dist/esm/node/plugin/index.js +21 -2
  29. package/dist/esm/node/plugin/plugins/devConfig/index.js +2 -2
  30. package/dist/esm/node/plugin/plugins/importBuild/index.js +2 -2
  31. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getConfigFileExport.d.ts +2 -0
  32. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getConfigFileExport.js +12 -0
  33. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/configDefinitionsBuiltIn.d.ts +6 -1
  34. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/loadFileConfigEnv.d.ts +21 -0
  35. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/loadFileConfigEnv.js +123 -0
  36. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/resolveImportPath.d.ts +12 -0
  37. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/resolveImportPath.js +133 -0
  38. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/{replaceImportStatements.d.ts → getVikeConfig/transformImports.d.ts} +5 -5
  39. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/{replaceImportStatements.js → getVikeConfig/transformImports.js} +43 -12
  40. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/{transpileAndExecuteFile.d.ts → getVikeConfig/transpileAndExecuteFile.d.ts} +3 -3
  41. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/{transpileAndExecuteFile.js → getVikeConfig/transpileAndExecuteFile.js} +110 -77
  42. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.d.ts +14 -1
  43. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.js +55 -276
  44. package/dist/esm/node/plugin/plugins/previewConfig.js +2 -2
  45. package/dist/esm/node/plugin/shared/getHttpRequestAsyncStore.js +1 -1
  46. package/dist/esm/node/plugin/shared/loggerNotProd/log.js +3 -3
  47. package/dist/esm/node/plugin/shared/loggerNotProd.js +1 -1
  48. package/dist/esm/node/runtime/globalContext.js +5 -4
  49. package/dist/esm/node/runtime/html/injectAssets/getHtmlTags.js +1 -1
  50. package/dist/esm/node/runtime/html/renderHtml.js +1 -1
  51. package/dist/esm/node/runtime/html/stream.js +2 -2
  52. package/dist/esm/node/runtime/renderPage/executeOnRenderHtmlHook.js +2 -2
  53. package/dist/esm/node/runtime/renderPage/getHttpResponseBody.js +1 -1
  54. package/dist/esm/node/runtime/renderPage/loadUserFilesServerSide.js +1 -1
  55. package/dist/esm/shared/page-configs/Config.d.ts +1 -1
  56. package/dist/esm/shared/page-configs/PageConfig.d.ts +4 -0
  57. package/dist/esm/shared/page-configs/assertPlusFileExport.d.ts +2 -0
  58. package/dist/esm/shared/page-configs/assertPlusFileExport.js +38 -0
  59. package/dist/esm/shared/page-configs/serialize/parseConfigValuesImported.js +2 -2
  60. package/dist/esm/shared/route/abort.js +2 -2
  61. package/dist/esm/utils/assert.js +5 -6
  62. package/dist/esm/utils/assertIsNotProductionRuntime.d.ts +8 -6
  63. package/dist/esm/utils/assertIsNotProductionRuntime.js +35 -17
  64. package/dist/esm/utils/debug.d.ts +1 -1
  65. package/dist/esm/utils/nodeEnv.d.ts +2 -0
  66. package/dist/esm/utils/nodeEnv.js +10 -0
  67. package/dist/esm/utils/projectInfo.d.ts +2 -8
  68. package/dist/esm/utils/projectInfo.js +2 -4
  69. package/package.json +1 -1
  70. package/dist/cjs/shared/page-configs/assertExports.js +0 -67
  71. package/dist/esm/shared/page-configs/assertExports.d.ts +0 -6
  72. package/dist/esm/shared/page-configs/assertExports.js +0 -61
@@ -1,4 +1,4 @@
1
- export { replaceImportStatements };
1
+ export { transformImports };
2
2
  export { parseImportData };
3
3
  export { isImportData };
4
4
  export type { FileImport };
@@ -8,12 +8,12 @@ type FileImport = {
8
8
  importString: string;
9
9
  importLocalName: string;
10
10
  };
11
- declare function replaceImportStatements(code: string, filePathToShowToUser: string): {
12
- noImportStatement: true;
11
+ declare function transformImports(code: string, filePathToShowToUser2: string, skipWarnings?: true): {
12
+ noTransformation: true;
13
13
  } | {
14
- noImportStatement: false;
14
+ noTransformation: false;
15
15
  code: string;
16
- fileImports: FileImport[];
16
+ fileImportsTransformed: FileImport[];
17
17
  };
18
18
  /**
19
19
  * Data Structure holding info about import statement:
@@ -1,21 +1,50 @@
1
- export { replaceImportStatements };
1
+ export { transformImports };
2
2
  export { parseImportData };
3
3
  export { isImportData };
4
4
  // Playground: https://github.com/brillout/acorn-playground
5
+ // Import attributes support: https://github.com/acornjs/acorn/issues/983
6
+ // - Isn't stage 4 yet: https://github.com/tc39/proposal-import-attributes
5
7
  import { parse } from 'acorn';
6
- import { assert, assertUsage, assertWarning, styleFileRE } from '../../../utils.js';
8
+ import { assert, assertUsage, assertWarning, styleFileRE } from '../../../../utils.js';
7
9
  import pc from '@brillout/picocolors';
8
- function replaceImportStatements(code, filePathToShowToUser) {
10
+ function transformImports(code, filePathToShowToUser2,
11
+ // For ./transformImports.spec.ts
12
+ skipWarnings) {
9
13
  const spliceOperations = [];
10
- const fileImports = [];
14
+ const fileImportsTransformed = [];
15
+ // Performance trick
16
+ if (!code.includes('import'))
17
+ return { noTransformation: true };
11
18
  const imports = getImports(code);
12
19
  if (imports.length === 0)
13
- return { noImportStatement: true };
20
+ return { noTransformation: true };
14
21
  imports.forEach((node) => {
15
22
  if (node.type !== 'ImportDeclaration')
16
23
  return;
17
24
  const importPath = node.source.value;
18
25
  assert(typeof importPath === 'string');
26
+ // - This doesn't work. To make it work we would need to run esbuild twice: esbuild for TypeScript to JavaScript => transformImports() => esbuild for bundling.
27
+ // - Or we use an esbuild plugin to apply transformImports(). Maybe we can completely skip the need for acorn?
28
+ // - ?real breaks TypeScript, and TypeScript isn't working on supporting query params: https://github.com/microsoft/TypeScript/issues/10988#issuecomment-867135453
29
+ // - Import attributes would be the best.
30
+ // - But it only works with Node.js >=21: https://nodejs.org/api/esm.html#import-attributes
31
+ // - But it's probably ok to tell users "to use real imports you need Node.js 21 or above".
32
+ // - It works well with TypeScript: it doesn't complain upon `with { type: 'unknown-to-typescript' }` and go-to-definition & types are preserved: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-3.html#import-attributes
33
+ // - Esbuid seems to support it: https://esbuild.github.io/plugins/#on-load-arguments:~:text=This%20contains%20a%20map%20of%20the%20import%20attributes%20that
34
+ // - acorn supports it over an acorn plugin: https://github.com/acornjs/acorn/issues/983
35
+ // - Maybe we can use an esbuild plugin instead of acorn to apply transformImports()?
36
+ // - Using a magic comment `// @vike-real-import` is tricky:
37
+ // - Esbuild removes comments: https://github.com/evanw/esbuild/issues/1439#issuecomment-877656182
38
+ // - Using source maps to track these magic comments is brittle (source maps can easily break)
39
+ if (importPath.endsWith('?real')) {
40
+ const { start, end } = node.source;
41
+ spliceOperations.push({
42
+ start,
43
+ end,
44
+ replacement: importPath.slice(0, -1 * '?real'.length)
45
+ });
46
+ return;
47
+ }
19
48
  const { start, end } = node;
20
49
  const importStatementCode = code.slice(start, end);
21
50
  // No variable imported
@@ -29,13 +58,15 @@ function replaceImportStatements(code, filePathToShowToUser) {
29
58
  quote = pc.bold(pc.red(quote));
30
59
  }
31
60
  const errMsg = [
32
- `As explained in https://vike.dev/header-file the following import in ${filePathToShowToUser} has no effect:`,
61
+ `As explained in https://vike.dev/header-file the following import in ${filePathToShowToUser2} has no effect:`,
33
62
  quote
34
63
  ].join('\n');
35
- if (!isWarning) {
36
- assertUsage(false, errMsg);
64
+ if (!skipWarnings) {
65
+ if (!isWarning) {
66
+ assertUsage(false, errMsg);
67
+ }
68
+ assertWarning(false, errMsg, { onlyOnce: true });
37
69
  }
38
- assertWarning(false, errMsg, { onlyOnce: true });
39
70
  }
40
71
  let replacement = '';
41
72
  node.specifiers.forEach((specifier) => {
@@ -57,7 +88,7 @@ function replaceImportStatements(code, filePathToShowToUser) {
57
88
  })();
58
89
  const importString = serializeImportData({ importPath, exportName, importStringWasGenerated: true });
59
90
  replacement += `const ${importLocalName} = '${importString}';`;
60
- fileImports.push({
91
+ fileImportsTransformed.push({
61
92
  importStatementCode,
62
93
  importString,
63
94
  importLocalName
@@ -70,7 +101,7 @@ function replaceImportStatements(code, filePathToShowToUser) {
70
101
  });
71
102
  });
72
103
  const codeMod = spliceMany(code, spliceOperations);
73
- return { code: codeMod, fileImports, noImportStatement: false };
104
+ return { code: codeMod, fileImportsTransformed, noTransformation: false };
74
105
  }
75
106
  function getImports(code) {
76
107
  const { body } = parse(code, {
@@ -137,7 +168,7 @@ function spliceMany(str, operations) {
137
168
  .join('');
138
169
  endPrev = end;
139
170
  });
140
- strMod += str.slice(endPrev, str.length - 1);
171
+ strMod += str.slice(endPrev, str.length);
141
172
  return strMod;
142
173
  }
143
174
  function indent(str) {
@@ -1,10 +1,10 @@
1
1
  export { transpileAndExecuteFile };
2
2
  export { getConfigBuildErrorFormatted };
3
- export { getConfigExecutionErrorIntroMsg as getConfigExecutionErrorIntroMsg };
3
+ export { getConfigExecutionErrorIntroMsg };
4
4
  export { isTmpFile };
5
5
  import 'source-map-support/register.js';
6
- import type { FilePathResolved } from '../../../../../shared/page-configs/PageConfig.js';
7
- declare function transpileAndExecuteFile(filePath: FilePathResolved, isValueFile: boolean, userRootDir: string): Promise<{
6
+ import type { FilePathResolved } from '../../../../../../shared/page-configs/PageConfig.js';
7
+ declare function transpileAndExecuteFile(filePath: FilePathResolved, isValueFile: boolean, userRootDir: string, isConfigOfExtension?: boolean): Promise<{
8
8
  fileExports: Record<string, unknown>;
9
9
  }>;
10
10
  declare function getConfigBuildErrorFormatted(err: unknown): null | string;
@@ -1,63 +1,79 @@
1
1
  export { transpileAndExecuteFile };
2
2
  export { getConfigBuildErrorFormatted };
3
- export { getConfigExecutionErrorIntroMsg as getConfigExecutionErrorIntroMsg };
3
+ export { getConfigExecutionErrorIntroMsg };
4
4
  export { isTmpFile };
5
5
  import { build, formatMessages } from 'esbuild';
6
6
  import fs from 'fs';
7
7
  import path from 'path';
8
8
  import pc from '@brillout/picocolors';
9
9
  import { import_ } from '@brillout/import';
10
- import { assertPosixPath, getRandomId, assertIsNotProductionRuntime, assert, unique, assertWarning, isObject, toPosixPath } from '../../../utils.js';
11
- import { isImportData, replaceImportStatements } from './replaceImportStatements.js';
12
- import { vikeConfigDependencies } from './getVikeConfig.js';
10
+ import { assertPosixPath, getRandomId, assertIsNotProductionRuntime, assert, unique, assertWarning, isObject, toPosixPath } from '../../../../utils.js';
11
+ import { isImportData, transformImports } from './transformImports.js';
12
+ import { vikeConfigDependencies } from '../getVikeConfig.js';
13
13
  import 'source-map-support/register.js';
14
- import { assertExportsOfConfigFile } from '../../../../../shared/page-configs/assertExports.js';
14
+ import { getConfigFileExport } from '../getConfigFileExport.js';
15
15
  assertIsNotProductionRuntime();
16
- async function transpileAndExecuteFile(filePath, isValueFile, userRootDir) {
17
- const { code, fileImports } = await transpileFile(filePath, isValueFile, userRootDir);
18
- const { fileExports } = await executeFile(filePath, code, fileImports, isValueFile);
19
- return { fileExports };
16
+ async function transpileAndExecuteFile(filePath, isValueFile, userRootDir, isConfigOfExtension = false) {
17
+ if (isConfigOfExtension) {
18
+ const fileExports = await executeFile(filePath.filePathAbsoluteFilesystem, filePath);
19
+ if (isHeaderFile(filePath.filePathAbsoluteFilesystem)) {
20
+ const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
21
+ assertWarning(false, `${filePathToShowToUser2} is a JavaScript header file (.h.js), but JavaScript header files don't apply to the config files of extensions`, { onlyOnce: true });
22
+ }
23
+ return { fileExports };
24
+ }
25
+ else {
26
+ const { code, fileImportsTransformed } = await transpileFile(filePath, isValueFile, userRootDir);
27
+ const fileExports = await executeTranspiledFile(filePath, code, fileImportsTransformed, isValueFile);
28
+ return { fileExports };
29
+ }
20
30
  }
21
31
  async function transpileFile(filePath, isValueFile, userRootDir) {
22
32
  const { filePathAbsoluteFilesystem } = filePath;
33
+ const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
23
34
  assertPosixPath(filePathAbsoluteFilesystem);
24
35
  vikeConfigDependencies.add(filePathAbsoluteFilesystem);
25
- let code = await transpileWithEsbuild(filePath, isValueFile, userRootDir);
26
- let fileImports = null;
27
- {
28
- const res = transpileImports(code, filePath, isValueFile);
36
+ const importsAreTransformed = !isValueFile;
37
+ let code = await transpileWithEsbuild(filePath, userRootDir, importsAreTransformed, isValueFile);
38
+ let fileImportsTransformed = null;
39
+ if (importsAreTransformed) {
40
+ const res = transformImports_(code, filePath);
29
41
  if (res) {
30
42
  code = res.code;
31
- fileImports = res.fileImports;
43
+ fileImportsTransformed = res.fileImportsTransformed;
32
44
  }
33
45
  }
34
- return { code, fileImports };
35
- }
36
- function transpileImports(codeOriginal, filePath, isValueFile) {
37
- // Do we need to remove the imports?
38
- const { filePathAbsoluteFilesystem, filePathToShowToUser } = filePath;
39
- assertPosixPath(filePathAbsoluteFilesystem);
40
- const isHeader = isHeaderFile(filePathAbsoluteFilesystem);
41
- const isPageConfigFile = !isValueFile;
42
- if (!isHeader && !isPageConfigFile) {
43
- return null;
46
+ else {
47
+ if (isHeaderFile(filePathAbsoluteFilesystem)) {
48
+ if (isValueFile) {
49
+ assertWarning(false, `${filePathToShowToUser2} is a JavaScript header file (.h.js), but JavaScript header files only apply to +config.h.js, see https://vike.dev/header-file`, { onlyOnce: true });
50
+ }
51
+ else {
52
+ assert(false);
53
+ }
54
+ }
44
55
  }
45
- assertWarning(isPageConfigFile, `${filePathToShowToUser} is a JavaScript header file (.h.js), but JavaScript header files should only be used for +config.h.js, see https://vike.dev/header-file`, { onlyOnce: true });
46
- // Remove the imports
47
- const res = replaceImportStatements(codeOriginal, filePathToShowToUser);
48
- if (res.noImportStatement) {
56
+ return { code, fileImportsTransformed };
57
+ }
58
+ function transformImports_(codeOriginal, filePath) {
59
+ const { filePathAbsoluteFilesystem } = filePath;
60
+ const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
61
+ // Replace import statements with import strings
62
+ const res = transformImports(codeOriginal, filePathToShowToUser2);
63
+ if (res.noTransformation) {
49
64
  return null;
50
65
  }
51
- const { code, fileImports } = res;
52
- if (!isHeader) {
53
- const filePathCorrect = appendHeaderFileExtension(filePathToShowToUser);
54
- assertWarning(false, `Rename ${filePathToShowToUser} to ${filePathCorrect}, see https://vike.dev/header-file`, {
66
+ const { code, fileImportsTransformed } = res;
67
+ if (!isHeaderFile(filePathAbsoluteFilesystem)) {
68
+ const filePathCorrect = appendHeaderFileExtension(filePathToShowToUser2);
69
+ assertWarning(false, `Rename ${filePathToShowToUser2} to ${filePathCorrect}, see https://vike.dev/header-file`, {
55
70
  onlyOnce: true
56
71
  });
57
72
  }
58
- return { code, fileImports };
73
+ return { code, fileImportsTransformed };
59
74
  }
60
- async function transpileWithEsbuild(filePath, bundle, userRootDir) {
75
+ async function transpileWithEsbuild(filePath, userRootDir, importsAreTransformed, isValueFile) {
76
+ const isConfigFile = !isValueFile;
61
77
  const entryFilePath = filePath.filePathAbsoluteFilesystem;
62
78
  const entryFileDir = path.posix.dirname(entryFilePath);
63
79
  const options = {
@@ -73,22 +89,26 @@ async function transpileWithEsbuild(filePath, bundle, userRootDir) {
73
89
  'NEVER_EMITTED.js'),
74
90
  logLevel: 'silent',
75
91
  format: 'esm',
76
- bundle,
92
+ absWorkingDir: userRootDir,
93
+ // Disable tree-shaking to avoid dead-code elimination, so that unused imports aren't removed.
94
+ // Esbuild still sometimes removes unused imports because of TypeScript: https://github.com/evanw/esbuild/issues/3034
95
+ treeShaking: false,
77
96
  minify: false,
78
- metafile: bundle,
79
- absWorkingDir: userRootDir
97
+ metafile: isConfigFile,
98
+ // We cannot bundle imports that are meant to be transformed
99
+ bundle: !importsAreTransformed
80
100
  };
81
- if (bundle) {
82
- options.bundle = true;
101
+ // Track dependencies
102
+ if (isConfigFile) {
83
103
  options.packages = 'external';
84
104
  options.plugins = [
85
105
  {
86
- name: 'vike:import-hook',
106
+ name: 'vike:dependency-tracker',
87
107
  setup(b) {
88
108
  b.onLoad({ filter: /./ }, (args) => {
109
+ // We collect the dependency `args.path` in case the bulid fails (upon build error => error is thrown => no metafile)
89
110
  let { path } = args;
90
111
  path = toPosixPath(path);
91
- // We collect the dependency args.path in case it fails to build (upon build error => error is thrown => no metafile)
92
112
  vikeConfigDependencies.add(path);
93
113
  return undefined;
94
114
  });
@@ -105,11 +125,6 @@ async function transpileWithEsbuild(filePath, bundle, userRootDir) {
105
125
  }
106
126
  ];
107
127
  }
108
- else {
109
- // Avoid dead-code elimination to ensure unused imports aren't removed.
110
- // Esbuild still sometimes removes unused imports because of TypeScript: https://github.com/evanw/esbuild/issues/3034
111
- options.treeShaking = false;
112
- }
113
128
  let result;
114
129
  try {
115
130
  result = await build(options);
@@ -118,7 +133,8 @@ async function transpileWithEsbuild(filePath, bundle, userRootDir) {
118
133
  await formatBuildErr(err, filePath);
119
134
  throw err;
120
135
  }
121
- if (bundle) {
136
+ // Track dependencies
137
+ if (isConfigFile) {
122
138
  assert(result.metafile);
123
139
  Object.keys(result.metafile.inputs).forEach((filePathRelative) => {
124
140
  filePathRelative = toPosixPath(filePathRelative);
@@ -131,7 +147,7 @@ async function transpileWithEsbuild(filePath, bundle, userRootDir) {
131
147
  assert(typeof code === 'string');
132
148
  return code;
133
149
  }
134
- async function executeFile(filePath, code, fileImports, isValueFile) {
150
+ async function executeTranspiledFile(filePath, code, fileImportsTransformed, isValueFile) {
135
151
  const { filePathAbsoluteFilesystem, filePathRelativeToUserRootDir } = filePath;
136
152
  // Alternative to using a temporary file: https://github.com/vitejs/vite/pull/13269
137
153
  // - But seems to break source maps, so I don't think it's worth it
@@ -140,28 +156,35 @@ async function executeFile(filePath, code, fileImports, isValueFile) {
140
156
  const clean = () => fs.unlinkSync(filePathTmp);
141
157
  let fileExports = {};
142
158
  try {
143
- fileExports = await import_(filePathTmp);
159
+ fileExports = await executeFile(filePathTmp, filePath);
160
+ }
161
+ finally {
162
+ clean();
163
+ }
164
+ if (fileImportsTransformed && !isValueFile) {
165
+ assert(filePathRelativeToUserRootDir !== undefined);
166
+ const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
167
+ assertImportsAreReExported(fileImportsTransformed, fileExports, filePathToShowToUser2);
168
+ }
169
+ return fileExports;
170
+ }
171
+ async function executeFile(filePathToExecuteAbsoluteFilesystem, filePathSourceFile) {
172
+ let fileExports = {};
173
+ try {
174
+ fileExports = await import_(filePathToExecuteAbsoluteFilesystem);
144
175
  }
145
176
  catch (err) {
146
177
  triggerPrepareStackTrace(err);
147
- const errIntroMsg = getErrIntroMsg('execute', filePath);
178
+ const errIntroMsg = getErrIntroMsg('execute', filePathSourceFile);
148
179
  assert(isObject(err));
149
180
  execErrIntroMsg.set(err, errIntroMsg);
150
181
  throw err;
151
182
  }
152
- finally {
153
- clean();
154
- }
155
- // Return a plain JavaScript object
183
+ // Return a plain JavaScript object:
156
184
  // - import() returns `[Module: null prototype] { default: { onRenderClient: '...' }}`
157
- // - We don't need this special object
185
+ // - We don't need this special object.
158
186
  fileExports = { ...fileExports };
159
- if (fileImports && !isValueFile) {
160
- assert(filePathRelativeToUserRootDir !== undefined);
161
- const filePathToShowToUser = filePathRelativeToUserRootDir ?? filePathAbsoluteFilesystem;
162
- assertImportsAreReExported(fileImports, fileExports, filePathToShowToUser);
163
- }
164
- return { fileExports };
187
+ return fileExports;
165
188
  }
166
189
  const formatted = '_formatted';
167
190
  function getConfigBuildErrorFormatted(err) {
@@ -191,10 +214,10 @@ function getConfigExecutionErrorIntroMsg(err) {
191
214
  return errIntroMsg ?? null;
192
215
  }
193
216
  const tmpPrefix = `[build-`;
194
- function getFilePathTmp(filePath) {
195
- assertPosixPath(filePath);
196
- const dirname = path.posix.dirname(filePath);
197
- const filename = path.posix.basename(filePath);
217
+ function getFilePathTmp(filePathAbsoluteFilesystem) {
218
+ assertPosixPath(filePathAbsoluteFilesystem);
219
+ const dirname = path.posix.dirname(filePathAbsoluteFilesystem);
220
+ const filename = path.posix.basename(filePathAbsoluteFilesystem);
198
221
  // Syntax with semicolon `[build:${/*...*/}]` doesn't work on Windows: https://github.com/vikejs/vike/issues/800#issuecomment-1517329455
199
222
  const tag = `${tmpPrefix}${getRandomId(12)}]`;
200
223
  const filePathTmp = path.posix.join(dirname, `${tag}${filename}.mjs`);
@@ -205,29 +228,29 @@ function isTmpFile(filePath) {
205
228
  const fileName = path.posix.basename(filePath);
206
229
  return fileName.startsWith(tmpPrefix);
207
230
  }
208
- function assertImportsAreReExported(fileImports, fileExports, filePathToShowToUser) {
209
- assertExportsOfConfigFile(fileExports, filePathToShowToUser);
210
- const exportedStrings = getExportedStrings(fileExports.default);
231
+ function assertImportsAreReExported(fileImportsTransformed, fileExports, filePathToShowToUser2) {
232
+ const fileExport = getConfigFileExport(fileExports, filePathToShowToUser2);
233
+ const exportedStrings = getExportedStrings(fileExport);
211
234
  Object.values(exportedStrings).forEach((exportVal) => {
212
235
  if (typeof exportVal !== 'string')
213
236
  return;
214
237
  if (!isImportData(exportVal))
215
238
  return;
216
239
  const importString = exportVal;
217
- fileImports.forEach((fileImport) => {
240
+ fileImportsTransformed.forEach((fileImport) => {
218
241
  if (fileImport.importString === importString) {
219
242
  fileImport.isReExported = true;
220
243
  }
221
244
  });
222
245
  });
223
- const fileImportsUnused = fileImports.filter((fi) => !fi.isReExported);
224
- if (fileImportsUnused.length === 0)
246
+ const fileImportsTransformedUnused = fileImportsTransformed.filter((fi) => !fi.isReExported);
247
+ if (fileImportsTransformedUnused.length === 0)
225
248
  return;
226
- const importStatements = unique(fileImportsUnused.map((fi) => fi.importStatementCode));
227
- const importNamesUnused = fileImportsUnused.map((fi) => pc.cyan(fi.importLocalName)).join(', ');
228
- const singular = fileImportsUnused.length === 1;
229
- assertWarning(fileImportsUnused.length === 0, [
230
- `${filePathToShowToUser} imports the following:`,
249
+ const importStatements = unique(fileImportsTransformedUnused.map((fi) => fi.importStatementCode));
250
+ const importNamesUnused = fileImportsTransformedUnused.map((fi) => pc.cyan(fi.importLocalName)).join(', ');
251
+ const singular = fileImportsTransformedUnused.length === 1;
252
+ assertWarning(fileImportsTransformedUnused.length === 0, [
253
+ `${filePathToShowToUser2} imports the following:`,
231
254
  ...importStatements.map((s) => pc.cyan(` ${s}`)),
232
255
  `But the import${singular ? '' : 's'} ${importNamesUnused} ${singular ? "isn't" : "aren't"} re-exported at ${pc.cyan('export default { ... }')} and therefore ${singular ? 'has' : 'have'} no effect, see explanation at https://vike.dev/header-file`
233
256
  ].join('\n'), { onlyOnce: true });
@@ -249,6 +272,7 @@ function getExportedStrings(obj) {
249
272
  return exportedStrings;
250
273
  }
251
274
  function isHeaderFile(filePath) {
275
+ assertPosixPath(filePath);
252
276
  const basenameParts = path.posix.basename(filePath).split('.');
253
277
  return basenameParts.includes('h');
254
278
  }
@@ -269,10 +293,19 @@ function triggerPrepareStackTrace(err) {
269
293
  }
270
294
  }
271
295
  function getErrIntroMsg(operation, filePath) {
296
+ const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
272
297
  const msg = [
298
+ // prettier ignore
273
299
  pc.red(`Failed to ${operation}`),
274
- pc.bold(pc.red(filePath.filePathToShowToUser)),
300
+ pc.bold(pc.red(filePathToShowToUser2)),
275
301
  pc.red(`because:`)
276
302
  ].join(' ');
277
303
  return msg;
278
304
  }
305
+ /** `filePath.filePathToShowToUser` may show the import path of a package, use `filePathToShowToUser2` instead always show a file path instead. */
306
+ function getFilePathToShowToUser2(filePath) {
307
+ const { filePathAbsoluteFilesystem, filePathRelativeToUserRootDir } = filePath;
308
+ const filePathToShowToUser2 = filePathRelativeToUserRootDir || filePathAbsoluteFilesystem;
309
+ assert(filePathToShowToUser2);
310
+ return filePathToShowToUser2;
311
+ }
@@ -2,9 +2,22 @@ export { getVikeConfig };
2
2
  export { reloadVikeConfig };
3
3
  export { vikeConfigDependencies };
4
4
  export { isVikeConfigFile };
5
- import type { PageConfigGlobalBuildTime, PageConfigBuildTime } from '../../../../../shared/page-configs/PageConfig.js';
5
+ export type { InterfaceValueFile };
6
+ import type { PageConfigGlobalBuildTime, PageConfigBuildTime, FilePathResolved } from '../../../../../shared/page-configs/PageConfig.js';
6
7
  import type { ExtensionResolved } from '../../../../../shared/ConfigVike.js';
7
8
  import type { ResolvedConfig } from 'vite';
9
+ type InterfaceFileCommons = {
10
+ filePath: FilePathResolved;
11
+ fileExportsByConfigName: Record<ConfigName, {
12
+ configValue?: unknown;
13
+ }>;
14
+ };
15
+ type InterfaceValueFile = InterfaceFileCommons & {
16
+ isConfigFile: false;
17
+ isValueFile: true;
18
+ configName: string;
19
+ };
20
+ type ConfigName = string;
8
21
  type VikeConfig = {
9
22
  pageConfigs: PageConfigBuildTime[];
10
23
  pageConfigGlobal: PageConfigGlobalBuildTime;