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