vike 0.4.159 → 0.4.160

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 (59) 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 +57 -278
  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/shared/page-configs/assertPlusFileExport.js +44 -0
  16. package/dist/cjs/shared/page-configs/serialize/parseConfigValuesImported.js +2 -2
  17. package/dist/cjs/shared/route/abort.js +1 -1
  18. package/dist/cjs/utils/assert.js +5 -6
  19. package/dist/cjs/utils/assertIsNotProductionRuntime.js +35 -17
  20. package/dist/cjs/utils/nodeEnv.js +11 -1
  21. package/dist/cjs/utils/projectInfo.js +2 -4
  22. package/dist/esm/node/plugin/index.js +21 -2
  23. package/dist/esm/node/plugin/plugins/devConfig/index.js +2 -2
  24. package/dist/esm/node/plugin/plugins/importBuild/index.js +2 -2
  25. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getConfigFileExport.d.ts +2 -0
  26. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getConfigFileExport.js +12 -0
  27. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/configDefinitionsBuiltIn.d.ts +6 -1
  28. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/loadFileConfigEnv.d.ts +21 -0
  29. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/loadFileConfigEnv.js +123 -0
  30. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/resolveImportPath.d.ts +12 -0
  31. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/resolveImportPath.js +133 -0
  32. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/{replaceImportStatements.d.ts → getVikeConfig/transformImports.d.ts} +5 -5
  33. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/{replaceImportStatements.js → getVikeConfig/transformImports.js} +43 -12
  34. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/{transpileAndExecuteFile.d.ts → getVikeConfig/transpileAndExecuteFile.d.ts} +3 -3
  35. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/{transpileAndExecuteFile.js → getVikeConfig/transpileAndExecuteFile.js} +110 -77
  36. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.d.ts +14 -1
  37. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.js +54 -275
  38. package/dist/esm/node/plugin/plugins/previewConfig.js +2 -2
  39. package/dist/esm/node/plugin/shared/getHttpRequestAsyncStore.js +1 -1
  40. package/dist/esm/node/plugin/shared/loggerNotProd/log.js +3 -3
  41. package/dist/esm/node/plugin/shared/loggerNotProd.js +1 -1
  42. package/dist/esm/node/runtime/globalContext.js +5 -4
  43. package/dist/esm/shared/page-configs/PageConfig.d.ts +4 -0
  44. package/dist/esm/shared/page-configs/assertPlusFileExport.d.ts +2 -0
  45. package/dist/esm/shared/page-configs/assertPlusFileExport.js +38 -0
  46. package/dist/esm/shared/page-configs/serialize/parseConfigValuesImported.js +2 -2
  47. package/dist/esm/shared/route/abort.js +2 -2
  48. package/dist/esm/utils/assert.js +5 -6
  49. package/dist/esm/utils/assertIsNotProductionRuntime.d.ts +8 -6
  50. package/dist/esm/utils/assertIsNotProductionRuntime.js +35 -17
  51. package/dist/esm/utils/debug.d.ts +1 -1
  52. package/dist/esm/utils/nodeEnv.d.ts +2 -0
  53. package/dist/esm/utils/nodeEnv.js +10 -0
  54. package/dist/esm/utils/projectInfo.d.ts +2 -8
  55. package/dist/esm/utils/projectInfo.js +2 -4
  56. package/package.json +1 -1
  57. package/dist/cjs/shared/page-configs/assertExports.js +0 -67
  58. package/dist/esm/shared/page-configs/assertExports.d.ts +0 -6
  59. package/dist/esm/shared/page-configs/assertExports.js +0 -61
@@ -9,58 +9,74 @@ 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 transformImports_js_1 = require("./transformImports.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, isValueFile, userRootDir, isConfigOfExtension = false) {
19
+ if (isConfigOfExtension) {
20
+ const fileExports = await executeFile(filePath.filePathAbsoluteFilesystem, filePath);
21
+ if (isHeaderFile(filePath.filePathAbsoluteFilesystem)) {
22
+ const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
23
+ (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 });
24
+ }
25
+ return { fileExports };
26
+ }
27
+ else {
28
+ const { code, fileImportsTransformed } = await transpileFile(filePath, isValueFile, userRootDir);
29
+ const fileExports = await executeTranspiledFile(filePath, code, fileImportsTransformed, isValueFile);
30
+ return { fileExports };
31
+ }
22
32
  }
23
33
  exports.transpileAndExecuteFile = transpileAndExecuteFile;
24
34
  async function transpileFile(filePath, isValueFile, userRootDir) {
25
35
  const { filePathAbsoluteFilesystem } = filePath;
36
+ const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
26
37
  (0, utils_js_1.assertPosixPath)(filePathAbsoluteFilesystem);
27
38
  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);
39
+ const importsAreTransformed = !isValueFile;
40
+ let code = await transpileWithEsbuild(filePath, userRootDir, importsAreTransformed, isValueFile);
41
+ let fileImportsTransformed = null;
42
+ if (importsAreTransformed) {
43
+ const res = transformImports_(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
+ if (isValueFile) {
52
+ (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 });
53
+ }
54
+ else {
55
+ (0, utils_js_1.assert)(false);
56
+ }
57
+ }
47
58
  }
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) {
59
+ return { code, fileImportsTransformed };
60
+ }
61
+ function transformImports_(codeOriginal, filePath) {
62
+ const { filePathAbsoluteFilesystem } = filePath;
63
+ const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
64
+ // Replace import statements with import strings
65
+ const res = (0, transformImports_js_1.transformImports)(codeOriginal, filePathToShowToUser2);
66
+ if (res.noTransformation) {
52
67
  return null;
53
68
  }
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`, {
69
+ const { code, fileImportsTransformed } = res;
70
+ if (!isHeaderFile(filePathAbsoluteFilesystem)) {
71
+ const filePathCorrect = appendHeaderFileExtension(filePathToShowToUser2);
72
+ (0, utils_js_1.assertWarning)(false, `Rename ${filePathToShowToUser2} to ${filePathCorrect}, see https://vike.dev/header-file`, {
58
73
  onlyOnce: true
59
74
  });
60
75
  }
61
- return { code, fileImports };
76
+ return { code, fileImportsTransformed };
62
77
  }
63
- async function transpileWithEsbuild(filePath, bundle, userRootDir) {
78
+ async function transpileWithEsbuild(filePath, userRootDir, importsAreTransformed, isValueFile) {
79
+ const isConfigFile = !isValueFile;
64
80
  const entryFilePath = filePath.filePathAbsoluteFilesystem;
65
81
  const entryFileDir = path_1.default.posix.dirname(entryFilePath);
66
82
  const options = {
@@ -76,22 +92,26 @@ async function transpileWithEsbuild(filePath, bundle, userRootDir) {
76
92
  'NEVER_EMITTED.js'),
77
93
  logLevel: 'silent',
78
94
  format: 'esm',
79
- bundle,
95
+ absWorkingDir: userRootDir,
96
+ // Disable tree-shaking to avoid dead-code elimination, so that unused imports aren't removed.
97
+ // Esbuild still sometimes removes unused imports because of TypeScript: https://github.com/evanw/esbuild/issues/3034
98
+ treeShaking: false,
80
99
  minify: false,
81
- metafile: bundle,
82
- absWorkingDir: userRootDir
100
+ metafile: isConfigFile,
101
+ // We cannot bundle imports that are meant to be transformed
102
+ bundle: !importsAreTransformed
83
103
  };
84
- if (bundle) {
85
- options.bundle = true;
104
+ // Track dependencies
105
+ if (isConfigFile) {
86
106
  options.packages = 'external';
87
107
  options.plugins = [
88
108
  {
89
- name: 'vike:import-hook',
109
+ name: 'vike:dependency-tracker',
90
110
  setup(b) {
91
111
  b.onLoad({ filter: /./ }, (args) => {
112
+ // We collect the dependency `args.path` in case the bulid fails (upon build error => error is thrown => no metafile)
92
113
  let { path } = args;
93
114
  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
115
  getVikeConfig_js_1.vikeConfigDependencies.add(path);
96
116
  return undefined;
97
117
  });
@@ -108,11 +128,6 @@ async function transpileWithEsbuild(filePath, bundle, userRootDir) {
108
128
  }
109
129
  ];
110
130
  }
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
131
  let result;
117
132
  try {
118
133
  result = await (0, esbuild_1.build)(options);
@@ -121,7 +136,8 @@ async function transpileWithEsbuild(filePath, bundle, userRootDir) {
121
136
  await formatBuildErr(err, filePath);
122
137
  throw err;
123
138
  }
124
- if (bundle) {
139
+ // Track dependencies
140
+ if (isConfigFile) {
125
141
  (0, utils_js_1.assert)(result.metafile);
126
142
  Object.keys(result.metafile.inputs).forEach((filePathRelative) => {
127
143
  filePathRelative = (0, utils_js_1.toPosixPath)(filePathRelative);
@@ -134,7 +150,7 @@ async function transpileWithEsbuild(filePath, bundle, userRootDir) {
134
150
  (0, utils_js_1.assert)(typeof code === 'string');
135
151
  return code;
136
152
  }
137
- async function executeFile(filePath, code, fileImports, isValueFile) {
153
+ async function executeTranspiledFile(filePath, code, fileImportsTransformed, isValueFile) {
138
154
  const { filePathAbsoluteFilesystem, filePathRelativeToUserRootDir } = filePath;
139
155
  // Alternative to using a temporary file: https://github.com/vitejs/vite/pull/13269
140
156
  // - But seems to break source maps, so I don't think it's worth it
@@ -143,28 +159,35 @@ async function executeFile(filePath, code, fileImports, isValueFile) {
143
159
  const clean = () => fs_1.default.unlinkSync(filePathTmp);
144
160
  let fileExports = {};
145
161
  try {
146
- fileExports = await (0, import_1.import_)(filePathTmp);
162
+ fileExports = await executeFile(filePathTmp, filePath);
163
+ }
164
+ finally {
165
+ clean();
166
+ }
167
+ if (fileImportsTransformed && !isValueFile) {
168
+ (0, utils_js_1.assert)(filePathRelativeToUserRootDir !== undefined);
169
+ const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
170
+ assertImportsAreReExported(fileImportsTransformed, fileExports, filePathToShowToUser2);
171
+ }
172
+ return fileExports;
173
+ }
174
+ async function executeFile(filePathToExecuteAbsoluteFilesystem, filePathSourceFile) {
175
+ let fileExports = {};
176
+ try {
177
+ fileExports = await (0, import_1.import_)(filePathToExecuteAbsoluteFilesystem);
147
178
  }
148
179
  catch (err) {
149
180
  triggerPrepareStackTrace(err);
150
- const errIntroMsg = getErrIntroMsg('execute', filePath);
181
+ const errIntroMsg = getErrIntroMsg('execute', filePathSourceFile);
151
182
  (0, utils_js_1.assert)((0, utils_js_1.isObject)(err));
152
183
  execErrIntroMsg.set(err, errIntroMsg);
153
184
  throw err;
154
185
  }
155
- finally {
156
- clean();
157
- }
158
- // Return a plain JavaScript object
186
+ // Return a plain JavaScript object:
159
187
  // - import() returns `[Module: null prototype] { default: { onRenderClient: '...' }}`
160
- // - We don't need this special object
188
+ // - We don't need this special object.
161
189
  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 };
190
+ return fileExports;
168
191
  }
169
192
  const formatted = '_formatted';
170
193
  function getConfigBuildErrorFormatted(err) {
@@ -196,10 +219,10 @@ function getConfigExecutionErrorIntroMsg(err) {
196
219
  }
197
220
  exports.getConfigExecutionErrorIntroMsg = getConfigExecutionErrorIntroMsg;
198
221
  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);
222
+ function getFilePathTmp(filePathAbsoluteFilesystem) {
223
+ (0, utils_js_1.assertPosixPath)(filePathAbsoluteFilesystem);
224
+ const dirname = path_1.default.posix.dirname(filePathAbsoluteFilesystem);
225
+ const filename = path_1.default.posix.basename(filePathAbsoluteFilesystem);
203
226
  // Syntax with semicolon `[build:${/*...*/}]` doesn't work on Windows: https://github.com/vikejs/vike/issues/800#issuecomment-1517329455
204
227
  const tag = `${tmpPrefix}${(0, utils_js_1.getRandomId)(12)}]`;
205
228
  const filePathTmp = path_1.default.posix.join(dirname, `${tag}${filename}.mjs`);
@@ -211,29 +234,29 @@ function isTmpFile(filePath) {
211
234
  return fileName.startsWith(tmpPrefix);
212
235
  }
213
236
  exports.isTmpFile = isTmpFile;
214
- function assertImportsAreReExported(fileImports, fileExports, filePathToShowToUser) {
215
- (0, assertExports_js_1.assertExportsOfConfigFile)(fileExports, filePathToShowToUser);
216
- const exportedStrings = getExportedStrings(fileExports.default);
237
+ function assertImportsAreReExported(fileImportsTransformed, fileExports, filePathToShowToUser2) {
238
+ const fileExport = (0, getConfigFileExport_js_1.getConfigFileExport)(fileExports, filePathToShowToUser2);
239
+ const exportedStrings = getExportedStrings(fileExport);
217
240
  Object.values(exportedStrings).forEach((exportVal) => {
218
241
  if (typeof exportVal !== 'string')
219
242
  return;
220
- if (!(0, replaceImportStatements_js_1.isImportData)(exportVal))
243
+ if (!(0, transformImports_js_1.isImportData)(exportVal))
221
244
  return;
222
245
  const importString = exportVal;
223
- fileImports.forEach((fileImport) => {
246
+ fileImportsTransformed.forEach((fileImport) => {
224
247
  if (fileImport.importString === importString) {
225
248
  fileImport.isReExported = true;
226
249
  }
227
250
  });
228
251
  });
229
- const fileImportsUnused = fileImports.filter((fi) => !fi.isReExported);
230
- if (fileImportsUnused.length === 0)
252
+ const fileImportsTransformedUnused = fileImportsTransformed.filter((fi) => !fi.isReExported);
253
+ if (fileImportsTransformedUnused.length === 0)
231
254
  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:`,
255
+ const importStatements = (0, utils_js_1.unique)(fileImportsTransformedUnused.map((fi) => fi.importStatementCode));
256
+ const importNamesUnused = fileImportsTransformedUnused.map((fi) => picocolors_1.default.cyan(fi.importLocalName)).join(', ');
257
+ const singular = fileImportsTransformedUnused.length === 1;
258
+ (0, utils_js_1.assertWarning)(fileImportsTransformedUnused.length === 0, [
259
+ `${filePathToShowToUser2} imports the following:`,
237
260
  ...importStatements.map((s) => picocolors_1.default.cyan(` ${s}`)),
238
261
  `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
262
  ].join('\n'), { onlyOnce: true });
@@ -255,6 +278,7 @@ function getExportedStrings(obj) {
255
278
  return exportedStrings;
256
279
  }
257
280
  function isHeaderFile(filePath) {
281
+ (0, utils_js_1.assertPosixPath)(filePath);
258
282
  const basenameParts = path_1.default.posix.basename(filePath).split('.');
259
283
  return basenameParts.includes('h');
260
284
  }
@@ -275,10 +299,19 @@ function triggerPrepareStackTrace(err) {
275
299
  }
276
300
  }
277
301
  function getErrIntroMsg(operation, filePath) {
302
+ const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
278
303
  const msg = [
304
+ // prettier ignore
279
305
  picocolors_1.default.red(`Failed to ${operation}`),
280
- picocolors_1.default.bold(picocolors_1.default.red(filePath.filePathToShowToUser)),
306
+ picocolors_1.default.bold(picocolors_1.default.red(filePathToShowToUser2)),
281
307
  picocolors_1.default.red(`because:`)
282
308
  ].join(' ');
283
309
  return msg;
284
310
  }
311
+ /** `filePath.filePathToShowToUser` may show the import path of a package, use `filePathToShowToUser2` instead always show a file path instead. */
312
+ function getFilePathToShowToUser2(filePath) {
313
+ const { filePathAbsoluteFilesystem, filePathRelativeToUserRootDir } = filePath;
314
+ const filePathToShowToUser2 = filePathRelativeToUserRootDir || filePathAbsoluteFilesystem;
315
+ (0, utils_js_1.assert)(filePathToShowToUser2);
316
+ return filePathToShowToUser2;
317
+ }