vike 0.4.158 → 0.4.159-commit-71760c7

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 (48) 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.js +14 -15
  6. package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/{replaceImportStatements.js → transformImportStatements.js} +23 -8
  7. package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/transpileAndExecuteFile.js +44 -34
  8. package/dist/cjs/node/plugin/plugins/previewConfig.js +1 -1
  9. package/dist/cjs/node/plugin/shared/loggerNotProd/log.js +3 -3
  10. package/dist/cjs/node/runtime/globalContext.js +4 -3
  11. package/dist/cjs/shared/page-configs/assertPlusFileExport.js +44 -0
  12. package/dist/cjs/shared/page-configs/serialize/parseConfigValuesImported.js +2 -2
  13. package/dist/cjs/shared/route/abort.js +1 -1
  14. package/dist/cjs/utils/assert.js +5 -6
  15. package/dist/cjs/utils/assertIsNotProductionRuntime.js +35 -17
  16. package/dist/cjs/utils/nodeEnv.js +11 -1
  17. package/dist/cjs/utils/projectInfo.js +2 -4
  18. package/dist/esm/node/plugin/index.js +21 -2
  19. package/dist/esm/node/plugin/plugins/devConfig/index.js +2 -2
  20. package/dist/esm/node/plugin/plugins/importBuild/index.js +2 -2
  21. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getConfigFileExport.d.ts +2 -0
  22. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getConfigFileExport.js +12 -0
  23. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/configDefinitionsBuiltIn.d.ts +6 -1
  24. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.js +12 -13
  25. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/{replaceImportStatements.d.ts → transformImportStatements.d.ts} +7 -5
  26. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/{replaceImportStatements.js → transformImportStatements.js} +23 -7
  27. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/transpileAndExecuteFile.js +43 -33
  28. package/dist/esm/node/plugin/plugins/previewConfig.js +2 -2
  29. package/dist/esm/node/plugin/shared/loggerNotProd/log.js +3 -3
  30. package/dist/esm/node/runtime/globalContext.js +5 -4
  31. package/dist/esm/shared/page-configs/PageConfig.d.ts +4 -0
  32. package/dist/esm/shared/page-configs/assertPlusFileExport.d.ts +2 -0
  33. package/dist/esm/shared/page-configs/assertPlusFileExport.js +38 -0
  34. package/dist/esm/shared/page-configs/serialize/parseConfigValuesImported.js +2 -2
  35. package/dist/esm/shared/route/abort.js +2 -2
  36. package/dist/esm/shared/types.d.ts +55 -28
  37. package/dist/esm/utils/assert.js +5 -6
  38. package/dist/esm/utils/assertIsNotProductionRuntime.d.ts +8 -6
  39. package/dist/esm/utils/assertIsNotProductionRuntime.js +35 -17
  40. package/dist/esm/utils/debug.d.ts +1 -1
  41. package/dist/esm/utils/nodeEnv.d.ts +2 -0
  42. package/dist/esm/utils/nodeEnv.js +10 -0
  43. package/dist/esm/utils/projectInfo.d.ts +2 -8
  44. package/dist/esm/utils/projectInfo.js +2 -4
  45. package/package.json +1 -1
  46. package/dist/cjs/shared/page-configs/assertExports.js +0 -67
  47. package/dist/esm/shared/page-configs/assertExports.d.ts +0 -6
  48. package/dist/esm/shared/page-configs/assertExports.js +0 -61
@@ -28,7 +28,8 @@ const baseUrls_js_1 = require("./plugins/baseUrls.js");
28
28
  const envVars_js_1 = require("./plugins/envVars.js");
29
29
  const picocolors_1 = __importDefault(require("@brillout/picocolors"));
30
30
  const fileEnv_js_1 = require("./plugins/fileEnv.js");
31
- (0, utils_js_2.markEnvAsVite)();
31
+ assertNodeEnv();
32
+ (0, utils_js_2.markEnvAsVikePluginLoaded)();
32
33
  // Return as `any` to avoid Plugin type mismatches when there are multiple Vite versions installed
33
34
  function plugin(vikeConfig) {
34
35
  const plugins = [
@@ -70,3 +71,21 @@ Object.defineProperty(plugin, 'apply', {
70
71
  (0, utils_js_2.assertUsage)(false, `Add ${picocolors_1.default.cyan('vike()')} instead of ${picocolors_1.default.cyan('vike')} to vite.config.js#plugins (i.e. call the function and add the return value instead of adding the function itself)`, { showStackTrace: true });
71
72
  }
72
73
  });
74
+ function assertNodeEnv() {
75
+ const nodeEnv = (0, utils_js_2.getNodeEnv)();
76
+ if (nodeEnv === 'test')
77
+ return;
78
+ // We should change this to be a warning if it blocks users (e.g. if a bad-citizen tool sets a wrong process.env.NODE_ENV value).
79
+ (0, utils_js_2.assertUsage)(
80
+ /* We can enable this assertion after Vike's CLI is implemented and using Vite's CLI is deprecated (we can then check whether the context is a `$ vike build`).
81
+ isNodeEnvDev() || isVikeCliBuild(),
82
+ /*/
83
+ (0, utils_js_2.isNodeEnvDev)() || true,
84
+ ///*/
85
+ [
86
+ picocolors_1.default.cyan(`process.env.NODE_ENV === ${JSON.stringify(nodeEnv)}`),
87
+ '(which Vike interprets as a non-development environment https://vike.dev/NODE_ENV)',
88
+ 'while the vike/plugin module is loaded.',
89
+ utils_js_2.vikeVitePluginLoadedInProductionError
90
+ ].join(' '));
91
+ }
@@ -66,7 +66,7 @@ function devConfig() {
66
66
  }
67
67
  },
68
68
  configureServer() {
69
- (0, utils_js_1.markEnvAsDev)();
69
+ (0, utils_js_1.markEnvAsViteDev)();
70
70
  }
71
71
  },
72
72
  {
@@ -37,7 +37,7 @@ function importBuild() {
37
37
  getImporterCode: () => {
38
38
  return getEntryCode(config, configVike);
39
39
  },
40
- libraryName: utils_js_1.projectInfo.projectName
40
+ libraryName: 'Vike'
41
41
  })
42
42
  ];
43
43
  }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getConfigFileExport = void 0;
7
+ const picocolors_1 = __importDefault(require("@brillout/picocolors"));
8
+ const assertPlusFileExport_js_1 = require("../../../../../shared/page-configs/assertPlusFileExport.js");
9
+ const utils_js_1 = require("../../../utils.js");
10
+ function getConfigFileExport(fileExports, filePathToShowToUser) {
11
+ (0, assertPlusFileExport_js_1.assertPlusFileExport)(fileExports, filePathToShowToUser, 'config');
12
+ const fileExport = fileExports.default || fileExports.config;
13
+ (0, utils_js_1.assert)('default' in fileExports !== 'config' in fileExports);
14
+ const exportName = picocolors_1.default.cyan('default' in fileExports ? 'export default' : 'export { config }');
15
+ (0, utils_js_1.assertUsage)((0, utils_js_1.isObject)(fileExport), `The ${exportName} of ${filePathToShowToUser} should be an object (but it's ${picocolors_1.default.cyan(`typeof exportedValue === ${JSON.stringify(typeof fileExport)}`)} instead)`);
16
+ return fileExport;
17
+ }
18
+ exports.getConfigFileExport = getConfigFileExport;
@@ -9,17 +9,18 @@ const path_1 = __importDefault(require("path"));
9
9
  const configDefinitionsBuiltIn_js_1 = require("./getVikeConfig/configDefinitionsBuiltIn.js");
10
10
  const filesystemRouting_js_1 = require("./getVikeConfig/filesystemRouting.js");
11
11
  const transpileAndExecuteFile_js_1 = require("./transpileAndExecuteFile.js");
12
- const replaceImportStatements_js_1 = require("./replaceImportStatements.js");
12
+ const transformImportStatements_js_1 = require("./transformImportStatements.js");
13
13
  const isConfigInvalid_js_1 = require("../../../../runtime/renderPage/isConfigInvalid.js");
14
14
  const globalContext_js_1 = require("../../../../runtime/globalContext.js");
15
15
  const loggerNotProd_js_1 = require("../../../shared/loggerNotProd.js");
16
16
  const removeSuperfluousViteLog_js_1 = require("../../../shared/loggerVite/removeSuperfluousViteLog.js");
17
17
  const picocolors_1 = __importDefault(require("@brillout/picocolors"));
18
18
  const helpers_js_1 = require("../../../../../shared/page-configs/helpers.js");
19
- const assertExports_js_1 = require("../../../../../shared/page-configs/assertExports.js");
19
+ const assertPlusFileExport_js_1 = require("../../../../../shared/page-configs/assertPlusFileExport.js");
20
20
  const getConfigVike_js_1 = require("../../../../shared/getConfigVike.js");
21
21
  const getConfigValuesSerialized_js_1 = require("./getConfigValuesSerialized.js");
22
22
  const crawlPlusFiles_js_1 = require("./getVikeConfig/crawlPlusFiles.js");
23
+ const getConfigFileExport_js_1 = require("./getConfigFileExport.js");
23
24
  (0, utils_js_1.assertIsNotProductionRuntime)();
24
25
  let devServerIsCorrupt = false;
25
26
  let wasConfigInvalid = null;
@@ -142,7 +143,7 @@ function getConfigDefinitionOptional(configDefinitions, configName) {
142
143
  async function loadValueFile(interfaceValueFile, configName, userRootDir) {
143
144
  const { fileExports } = await (0, transpileAndExecuteFile_js_1.transpileAndExecuteFile)(interfaceValueFile.filePath, true, userRootDir);
144
145
  const { filePathToShowToUser } = interfaceValueFile.filePath;
145
- (0, assertExports_js_1.assertExportsOfValueFile)(fileExports, filePathToShowToUser, configName);
146
+ (0, assertPlusFileExport_js_1.assertPlusFileExport)(fileExports, filePathToShowToUser, configName);
146
147
  Object.entries(fileExports).forEach(([exportName, configValue]) => {
147
148
  const configName_ = exportName === 'default' ? configName : exportName;
148
149
  interfaceValueFile.configMap[configName_] = { configValue };
@@ -174,9 +175,8 @@ function getInterfaceFileFromConfigFile(configFile, isConfigExtend) {
174
175
  isConfigExtend,
175
176
  extendsFilePaths
176
177
  };
177
- const { filePathToShowToUser } = filePath;
178
- (0, assertExports_js_1.assertExportsOfConfigFile)(fileExports, filePathToShowToUser);
179
- Object.entries(fileExports.default).forEach(([configName, configValue]) => {
178
+ const fileExport = (0, getConfigFileExport_js_1.getConfigFileExport)(fileExports, filePath.filePathToShowToUser);
179
+ Object.entries(fileExport).forEach(([configName, configValue]) => {
180
180
  interfaceFile.configMap[configName] = { configValue };
181
181
  });
182
182
  return interfaceFile;
@@ -582,7 +582,7 @@ function isDefiningPageConfig(configName) {
582
582
  function resolveImport(configValue, importerFilePath, userRootDir, configEnv, configName) {
583
583
  if (typeof configValue !== 'string')
584
584
  return null;
585
- const importData = (0, replaceImportStatements_js_1.parseImportData)(configValue);
585
+ const importData = (0, transformImportStatements_js_1.parseImportData)(configValue);
586
586
  if (!importData)
587
587
  return null;
588
588
  const { importPath, exportName } = importData;
@@ -925,24 +925,23 @@ function assertExtendsImportPath(importPath, filePath, configFilePath) {
925
925
  }
926
926
  function getExtendsImportData(configFileExports, configFilePath) {
927
927
  const { filePathToShowToUser } = configFilePath;
928
- (0, assertExports_js_1.assertExportsOfConfigFile)(configFileExports, filePathToShowToUser);
929
- const defaultExports = configFileExports.default;
928
+ const configFileExport = (0, getConfigFileExport_js_1.getConfigFileExport)(configFileExports, filePathToShowToUser);
930
929
  const wrongUsage = `${filePathToShowToUser} sets the config ${picocolors_1.default.cyan('extends')} to an invalid value, see https://vike.dev/extends`;
931
930
  let extendList;
932
- if (!('extends' in defaultExports)) {
931
+ if (!('extends' in configFileExport)) {
933
932
  return [];
934
933
  }
935
- else if ((0, utils_js_1.hasProp)(defaultExports, 'extends', 'string')) {
936
- extendList = [defaultExports.extends];
934
+ else if ((0, utils_js_1.hasProp)(configFileExport, 'extends', 'string')) {
935
+ extendList = [configFileExport.extends];
937
936
  }
938
- else if ((0, utils_js_1.hasProp)(defaultExports, 'extends', 'string[]')) {
939
- extendList = defaultExports.extends;
937
+ else if ((0, utils_js_1.hasProp)(configFileExport, 'extends', 'string[]')) {
938
+ extendList = configFileExport.extends;
940
939
  }
941
940
  else {
942
941
  (0, utils_js_1.assertUsage)(false, wrongUsage);
943
942
  }
944
943
  const extendsImportData = extendList.map((importDataSerialized) => {
945
- const importData = (0, replaceImportStatements_js_1.parseImportData)(importDataSerialized);
944
+ const importData = (0, transformImportStatements_js_1.parseImportData)(importDataSerialized);
946
945
  (0, utils_js_1.assertUsage)(importData, wrongUsage);
947
946
  return importData;
948
947
  });
@@ -3,23 +3,30 @@ 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.isVikeRealImport = exports.isImportData = exports.parseImportData = exports.transformImportStatements = 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
11
  const utils_js_1 = require("../../../utils.js");
10
12
  const picocolors_1 = __importDefault(require("@brillout/picocolors"));
11
- function replaceImportStatements(code, filePathToShowToUser) {
13
+ function transformImportStatements(code, filePathToShowToUser2) {
12
14
  const spliceOperations = [];
13
- const fileImports = [];
15
+ const fileImportsTransformed = [];
16
+ // Performance trick
17
+ if (!code.includes('import'))
18
+ return { noTransformation: true };
14
19
  const imports = getImports(code);
15
20
  if (imports.length === 0)
16
- return { noImportStatement: true };
21
+ return { noTransformation: true };
17
22
  imports.forEach((node) => {
18
23
  if (node.type !== 'ImportDeclaration')
19
24
  return;
20
25
  const importPath = node.source.value;
21
26
  (0, utils_js_1.assert)(typeof importPath === 'string');
22
27
  const { start, end } = node;
28
+ if (isVikeRealImport(code, end))
29
+ return;
23
30
  const importStatementCode = code.slice(start, end);
24
31
  // No variable imported
25
32
  if (node.specifiers.length === 0) {
@@ -32,7 +39,7 @@ function replaceImportStatements(code, filePathToShowToUser) {
32
39
  quote = picocolors_1.default.bold(picocolors_1.default.red(quote));
33
40
  }
34
41
  const errMsg = [
35
- `As explained in https://vike.dev/header-file the following import in ${filePathToShowToUser} has no effect:`,
42
+ `As explained in https://vike.dev/header-file the following import in ${filePathToShowToUser2} has no effect:`,
36
43
  quote
37
44
  ].join('\n');
38
45
  if (!isWarning) {
@@ -60,7 +67,7 @@ function replaceImportStatements(code, filePathToShowToUser) {
60
67
  })();
61
68
  const importString = serializeImportData({ importPath, exportName, importStringWasGenerated: true });
62
69
  replacement += `const ${importLocalName} = '${importString}';`;
63
- fileImports.push({
70
+ fileImportsTransformed.push({
64
71
  importStatementCode,
65
72
  importString,
66
73
  importLocalName
@@ -73,9 +80,9 @@ function replaceImportStatements(code, filePathToShowToUser) {
73
80
  });
74
81
  });
75
82
  const codeMod = spliceMany(code, spliceOperations);
76
- return { code: codeMod, fileImports, noImportStatement: false };
83
+ return { code: codeMod, fileImportsTransformed, noTransformation: false };
77
84
  }
78
- exports.replaceImportStatements = replaceImportStatements;
85
+ exports.transformImportStatements = transformImportStatements;
79
86
  function getImports(code) {
80
87
  const { body } = (0, acorn_1.parse)(code, {
81
88
  ecmaVersion: 'latest',
@@ -152,3 +159,11 @@ function indent(str) {
152
159
  .map((s) => ` ${s}`)
153
160
  .join('\n');
154
161
  }
162
+ function isVikeRealImport(code, posStart) {
163
+ let posEnd = code.indexOf('\n', posStart);
164
+ if (posEnd === -1)
165
+ posEnd = code.length;
166
+ const lineEnd = code.slice(posStart, posEnd);
167
+ return lineEnd.includes('@vike-real-import');
168
+ }
169
+ exports.isVikeRealImport = isVikeRealImport;
@@ -10,14 +10,14 @@ const path_1 = __importDefault(require("path"));
10
10
  const picocolors_1 = __importDefault(require("@brillout/picocolors"));
11
11
  const import_1 = require("@brillout/import");
12
12
  const utils_js_1 = require("../../../utils.js");
13
- const replaceImportStatements_js_1 = require("./replaceImportStatements.js");
13
+ const transformImportStatements_js_1 = require("./transformImportStatements.js");
14
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
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);
19
+ const { code, fileImportsTransformed } = await transpileFile(filePath, isValueFile, userRootDir);
20
+ const { fileExports } = await executeFile(filePath, code, fileImportsTransformed, isValueFile);
21
21
  return { fileExports };
22
22
  }
23
23
  exports.transpileAndExecuteFile = transpileAndExecuteFile;
@@ -26,39 +26,40 @@ async function transpileFile(filePath, isValueFile, userRootDir) {
26
26
  (0, utils_js_1.assertPosixPath)(filePathAbsoluteFilesystem);
27
27
  getVikeConfig_js_1.vikeConfigDependencies.add(filePathAbsoluteFilesystem);
28
28
  let code = await transpileWithEsbuild(filePath, isValueFile, userRootDir);
29
- let fileImports = null;
29
+ let fileImportsTransformed = null;
30
30
  {
31
- const res = transpileImports(code, filePath, isValueFile);
31
+ const res = transformImports(code, filePath, isValueFile);
32
32
  if (res) {
33
33
  code = res.code;
34
- fileImports = res.fileImports;
34
+ fileImportsTransformed = res.fileImportsTransformed;
35
35
  }
36
36
  }
37
- return { code, fileImports };
37
+ return { code, fileImportsTransformed };
38
38
  }
39
- function transpileImports(codeOriginal, filePath, isValueFile) {
39
+ function transformImports(codeOriginal, filePath, isValueFile) {
40
40
  // Do we need to remove the imports?
41
- const { filePathAbsoluteFilesystem, filePathToShowToUser } = filePath;
41
+ const { filePathAbsoluteFilesystem } = filePath;
42
+ const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
42
43
  (0, utils_js_1.assertPosixPath)(filePathAbsoluteFilesystem);
43
44
  const isHeader = isHeaderFile(filePathAbsoluteFilesystem);
44
45
  const isPageConfigFile = !isValueFile;
45
46
  if (!isHeader && !isPageConfigFile) {
46
47
  return null;
47
48
  }
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
+ (0, utils_js_1.assertWarning)(isPageConfigFile, `${filePathToShowToUser2} 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
50
  // Remove the imports
50
- const res = (0, replaceImportStatements_js_1.replaceImportStatements)(codeOriginal, filePathToShowToUser);
51
- if (res.noImportStatement) {
51
+ const res = (0, transformImportStatements_js_1.transformImportStatements)(codeOriginal, filePathToShowToUser2);
52
+ if (res.noTransformation) {
52
53
  return null;
53
54
  }
54
- const { code, fileImports } = res;
55
+ const { code, fileImportsTransformed } = res;
55
56
  if (!isHeader) {
56
- const filePathCorrect = appendHeaderFileExtension(filePathToShowToUser);
57
- (0, utils_js_1.assertWarning)(false, `Rename ${filePathToShowToUser} to ${filePathCorrect}, see https://vike.dev/header-file`, {
57
+ const filePathCorrect = appendHeaderFileExtension(filePathToShowToUser2);
58
+ (0, utils_js_1.assertWarning)(false, `Rename ${filePathToShowToUser2} to ${filePathCorrect}, see https://vike.dev/header-file`, {
58
59
  onlyOnce: true
59
60
  });
60
61
  }
61
- return { code, fileImports };
62
+ return { code, fileImportsTransformed };
62
63
  }
63
64
  async function transpileWithEsbuild(filePath, bundle, userRootDir) {
64
65
  const entryFilePath = filePath.filePathAbsoluteFilesystem;
@@ -134,7 +135,7 @@ async function transpileWithEsbuild(filePath, bundle, userRootDir) {
134
135
  (0, utils_js_1.assert)(typeof code === 'string');
135
136
  return code;
136
137
  }
137
- async function executeFile(filePath, code, fileImports, isValueFile) {
138
+ async function executeFile(filePath, code, fileImportsTransformed, isValueFile) {
138
139
  const { filePathAbsoluteFilesystem, filePathRelativeToUserRootDir } = filePath;
139
140
  // Alternative to using a temporary file: https://github.com/vitejs/vite/pull/13269
140
141
  // - But seems to break source maps, so I don't think it's worth it
@@ -159,10 +160,10 @@ async function executeFile(filePath, code, fileImports, isValueFile) {
159
160
  // - import() returns `[Module: null prototype] { default: { onRenderClient: '...' }}`
160
161
  // - We don't need this special object
161
162
  fileExports = { ...fileExports };
162
- if (fileImports && !isValueFile) {
163
+ if (fileImportsTransformed && !isValueFile) {
163
164
  (0, utils_js_1.assert)(filePathRelativeToUserRootDir !== undefined);
164
- const filePathToShowToUser = filePathRelativeToUserRootDir ?? filePathAbsoluteFilesystem;
165
- assertImportsAreReExported(fileImports, fileExports, filePathToShowToUser);
165
+ const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
166
+ assertImportsAreReExported(fileImportsTransformed, fileExports, filePathToShowToUser2);
166
167
  }
167
168
  return { fileExports };
168
169
  }
@@ -211,29 +212,29 @@ function isTmpFile(filePath) {
211
212
  return fileName.startsWith(tmpPrefix);
212
213
  }
213
214
  exports.isTmpFile = isTmpFile;
214
- function assertImportsAreReExported(fileImports, fileExports, filePathToShowToUser) {
215
- (0, assertExports_js_1.assertExportsOfConfigFile)(fileExports, filePathToShowToUser);
216
- const exportedStrings = getExportedStrings(fileExports.default);
215
+ function assertImportsAreReExported(fileImportsTransformed, fileExports, filePathToShowToUser2) {
216
+ const fileExport = (0, getConfigFileExport_js_1.getConfigFileExport)(fileExports, filePathToShowToUser2);
217
+ const exportedStrings = getExportedStrings(fileExport);
217
218
  Object.values(exportedStrings).forEach((exportVal) => {
218
219
  if (typeof exportVal !== 'string')
219
220
  return;
220
- if (!(0, replaceImportStatements_js_1.isImportData)(exportVal))
221
+ if (!(0, transformImportStatements_js_1.isImportData)(exportVal))
221
222
  return;
222
223
  const importString = exportVal;
223
- fileImports.forEach((fileImport) => {
224
+ fileImportsTransformed.forEach((fileImport) => {
224
225
  if (fileImport.importString === importString) {
225
226
  fileImport.isReExported = true;
226
227
  }
227
228
  });
228
229
  });
229
- const fileImportsUnused = fileImports.filter((fi) => !fi.isReExported);
230
- if (fileImportsUnused.length === 0)
230
+ const fileImportsTransformedUnused = fileImportsTransformed.filter((fi) => !fi.isReExported);
231
+ if (fileImportsTransformedUnused.length === 0)
231
232
  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:`,
233
+ const importStatements = (0, utils_js_1.unique)(fileImportsTransformedUnused.map((fi) => fi.importStatementCode));
234
+ const importNamesUnused = fileImportsTransformedUnused.map((fi) => picocolors_1.default.cyan(fi.importLocalName)).join(', ');
235
+ const singular = fileImportsTransformedUnused.length === 1;
236
+ (0, utils_js_1.assertWarning)(fileImportsTransformedUnused.length === 0, [
237
+ `${filePathToShowToUser2} imports the following:`,
237
238
  ...importStatements.map((s) => picocolors_1.default.cyan(` ${s}`)),
238
239
  `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
240
  ].join('\n'), { onlyOnce: true });
@@ -275,10 +276,19 @@ function triggerPrepareStackTrace(err) {
275
276
  }
276
277
  }
277
278
  function getErrIntroMsg(operation, filePath) {
279
+ const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
278
280
  const msg = [
281
+ // prettier ignore
279
282
  picocolors_1.default.red(`Failed to ${operation}`),
280
- picocolors_1.default.bold(picocolors_1.default.red(filePath.filePathToShowToUser)),
283
+ picocolors_1.default.bold(picocolors_1.default.red(filePathToShowToUser2)),
281
284
  picocolors_1.default.red(`because:`)
282
285
  ].join(' ');
283
286
  return msg;
284
287
  }
288
+ /** `filePath.filePathToShowToUser` may show the import path of a package, use `filePathToShowToUser2` instead always show a file path instead. */
289
+ function getFilePathToShowToUser2(filePath) {
290
+ const { filePathAbsoluteFilesystem, filePathRelativeToUserRootDir } = filePath;
291
+ const filePathToShowToUser2 = filePathRelativeToUserRootDir || filePathAbsoluteFilesystem;
292
+ (0, utils_js_1.assert)(filePathToShowToUser2);
293
+ return filePathToShowToUser2;
294
+ }
@@ -33,7 +33,7 @@ function previewConfig() {
33
33
  - This ugly hack to set appType for preview won't be need once https://github.com/vitejs/vite/pull/14855 is merged.
34
34
  config.appType = 'mpa'
35
35
  */
36
- (0, utils_js_1.markEnvAsPreview)();
36
+ (0, utils_js_1.markEnvAsVitePreview)();
37
37
  return () => {
38
38
  assertDist();
39
39
  /* We don't use this condition (we wrongfully always use the SSR middleware) because of the regression introduced by https://github.com/vitejs/vite/pull/14756 which stops servering .html files when `appType: 'custom'`.
@@ -22,10 +22,10 @@ exports.logWithVikeTag = logWithVikeTag;
22
22
  function getProjectTag(showVikeVersion) {
23
23
  let projectTag;
24
24
  if (showVikeVersion) {
25
- projectTag = `[${utils_js_1.projectInfo.npmPackageName}@${utils_js_1.projectInfo.projectVersion}]`;
25
+ projectTag = `[vike@${utils_js_1.projectInfo.projectVersion}]`;
26
26
  }
27
27
  else {
28
- projectTag = `[${utils_js_1.projectInfo.npmPackageName}]`;
28
+ projectTag = `[vike]`;
29
29
  }
30
30
  return projectTag;
31
31
  }
@@ -89,7 +89,7 @@ function prependTags(msg, projectTag, category, logType) {
89
89
  return picocolors_1.default.yellow(s);
90
90
  if (projectTag === '[vite]')
91
91
  return picocolors_1.default.bold(picocolors_1.default.cyan(s));
92
- if (projectTag.startsWith(`[${utils_js_1.projectInfo.npmPackageName}`))
92
+ if (projectTag.startsWith(`[vike`))
93
93
  return picocolors_1.default.bold(picocolors_1.default.cyan(s));
94
94
  (0, utils_js_1.assert)(false);
95
95
  };
@@ -157,7 +157,8 @@ function assertNodeEnv(hasViteDevServer) {
157
157
  const nodeEnv = (0, utils_js_1.getNodeEnv)();
158
158
  if (nodeEnv === null || nodeEnv === 'test')
159
159
  return;
160
- const isDevNodeEnv = [undefined, '', 'dev', 'development'].includes(nodeEnv);
161
- // calling Vite's createServer() is enough for hasViteDevServer to be true, even without actually adding Vite's development middleware to the server: https://github.com/vikejs/vike/issues/792#issuecomment-1516830759
162
- (0, utils_js_1.assertWarning)(hasViteDevServer === isDevNodeEnv, `Vite's development server was${hasViteDevServer ? '' : "n't"} instantiated while the environment is set to be a ${isDevNodeEnv ? 'development' : 'production'} environment by ${picocolors_1.default.cyan(`process.env.NODE_ENV === ${JSON.stringify(nodeEnv)}`)} which is contradictory, see https://vike.dev/NODE_ENV`, { onlyOnce: true });
160
+ const isDevNodeEnv = (0, utils_js_1.isNodeEnvDev)();
161
+ // - Calling Vite's createServer() is enough for hasViteDevServer to be true, even without actually adding Vite's development middleware to the server: https://github.com/vikejs/vike/issues/792#issuecomment-1516830759
162
+ // - We should change this to be a warning if it blocks users (e.g. if a bad-citizen tool sets a wrong process.env.NODE_ENV value).
163
+ (0, utils_js_1.assertUsage)(hasViteDevServer === isDevNodeEnv, `Vite's development server was${hasViteDevServer ? '' : "n't"} instantiated while the environment is set to be a ${isDevNodeEnv ? 'development' : 'production'} environment by ${picocolors_1.default.cyan(`process.env.NODE_ENV === ${JSON.stringify(nodeEnv)}`)} which is contradictory, see https://vike.dev/NODE_ENV`);
163
164
  }
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.assertPlusFileExport = void 0;
7
+ const utils_js_1 = require("../utils.js");
8
+ const picocolors_1 = __importDefault(require("@brillout/picocolors"));
9
+ const EXPORTS_IGNORE = [
10
+ // vite-plugin-solid adds `export { $$registrations }`
11
+ '$$registrations',
12
+ // @vitejs/plugin-vue adds `export { _rerender_only }`
13
+ '_rerender_only'
14
+ ];
15
+ // Tolerate `export { frontmatter }` in .mdx files
16
+ const TOLERATE_SIDE_EXPORTS = ['.md', '.mdx'];
17
+ function assertPlusFileExport(fileExports, filePathToShowToUser, configName) {
18
+ const exportsAll = Object.keys(fileExports).filter((exportName) => !EXPORTS_IGNORE.includes(exportName));
19
+ const exportsInvalid = exportsAll.filter((e) => e !== 'default' && e !== configName);
20
+ if (exportsInvalid.length === 0) {
21
+ if (exportsAll.length === 1) {
22
+ return;
23
+ }
24
+ const exportDefault = picocolors_1.default.cyan('export default');
25
+ const exportNamed = picocolors_1.default.cyan(`export { ${configName} }`);
26
+ if (exportsAll.length === 0) {
27
+ (0, utils_js_1.assertUsage)(false, `${filePathToShowToUser} doesn't export any value, but it should have a ${exportNamed} or ${exportDefault}`);
28
+ }
29
+ else {
30
+ (0, utils_js_1.assert)(exportsAll.length === 2); // because `exportsInvalid.length === 0`
31
+ (0, utils_js_1.assertWarning)(false, `${filePathToShowToUser} remove ${exportNamed} or ${exportDefault}`, {
32
+ onlyOnce: true
33
+ });
34
+ }
35
+ }
36
+ else {
37
+ if (TOLERATE_SIDE_EXPORTS.some((ext) => filePathToShowToUser.endsWith(ext)))
38
+ return;
39
+ exportsInvalid.forEach((exportInvalid) => {
40
+ (0, utils_js_1.assertWarning)(false, `${filePathToShowToUser} should have only one export: move ${picocolors_1.default.cyan(`export { ${exportInvalid} }`)} to its own +${exportsInvalid}.js file`, { onlyOnce: true });
41
+ });
42
+ }
43
+ }
44
+ exports.assertPlusFileExport = assertPlusFileExport;
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.parseConfigValuesImported = void 0;
4
4
  const utils_js_1 = require("../../utils.js");
5
- const assertExports_js_1 = require("../assertExports.js");
5
+ const assertPlusFileExport_js_1 = require("../assertPlusFileExport.js");
6
6
  function parseConfigValuesImported(configValuesImported) {
7
7
  const configValues = {};
8
8
  const addConfigValue = (configName, value, importPath, exportName) => {
@@ -23,7 +23,7 @@ function parseConfigValuesImported(configValuesImported) {
23
23
  if (configValueLoaded.isValueFile) {
24
24
  const { exportValues, importPath, configName } = configValueLoaded;
25
25
  if (configName !== 'client') {
26
- (0, assertExports_js_1.assertExportsOfValueFile)(exportValues, importPath, configName);
26
+ (0, assertPlusFileExport_js_1.assertPlusFileExport)(exportValues, importPath, configName);
27
27
  }
28
28
  Object.entries(exportValues).forEach(([exportName, exportValue]) => {
29
29
  const isSideExport = exportName !== 'default'; // .md files may have "side-exports" such as `export { frontmatter }`
@@ -136,7 +136,7 @@ function logAbortErrorHandled(err, isProduction, pageContext) {
136
136
  exports.logAbortErrorHandled = logAbortErrorHandled;
137
137
  function assertStatusCode(statusCode, expected, caller) {
138
138
  const expectedEnglish = (0, utils_js_1.joinEnglish)(expected.map((s) => s.toString()), 'or');
139
- (0, utils_js_1.assertWarning)(expected.includes(statusCode), `Unepexected status code ${statusCode} passed to ${caller}(), we recommend ${expectedEnglish} instead. (Or reach out at ${utils_js_1.projectInfo.githubRepository}/issues/1008 if you believe ${statusCode} should be added.)`, { onlyOnce: true });
139
+ (0, utils_js_1.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 });
140
140
  }
141
141
  function getPageContextFromAllRewrites(pageContextsFromRewrite) {
142
142
  assertNoInfiniteLoop(pageContextsFromRewrite);
@@ -22,8 +22,8 @@ const globalObject = (0, getGlobalObject_js_1.getGlobalObject)('utils/assert.ts'
22
22
  },
23
23
  showStackTraceList: new WeakSet()
24
24
  });
25
- const projectTag = `[${projectInfo_js_1.projectInfo.npmPackageName}]`;
26
- const projectTagWithVersion = `[${projectInfo_js_1.projectInfo.npmPackageName}@${projectInfo_js_1.projectInfo.projectVersion}]`;
25
+ const projectTag = `[vike]`;
26
+ const projectTagWithVersion = `[vike@${projectInfo_js_1.projectInfo.projectVersion}]`;
27
27
  const numberOfStackTraceLinesToRemove = 2;
28
28
  function assert(condition, debugInfo) {
29
29
  if (condition)
@@ -33,12 +33,11 @@ function assert(condition, debugInfo) {
33
33
  return null;
34
34
  }
35
35
  const debugInfoSerialized = typeof debugInfo === 'string' ? debugInfo : JSON.stringify(debugInfo);
36
- return picocolors_1.default.dim(`Debug info (for ${projectInfo_js_1.projectInfo.projectName} maintainers; you can ignore this): ${debugInfoSerialized}`);
36
+ return picocolors_1.default.dim(`Debug info (for Vike maintainers; you can ignore this): ${debugInfoSerialized}`);
37
37
  })();
38
- const link = `${projectInfo_js_1.projectInfo.githubRepository}/issues/new`;
39
38
  let errMsg = [
40
- `You stumbled upon a bug in ${projectInfo_js_1.projectInfo.projectName}'s source code.`,
41
- `Go to ${picocolors_1.default.blue(link)} and copy-paste this error. A maintainer will fix the bug (usually under 24 hours).`,
39
+ `You stumbled upon a Vike bug.`,
40
+ `Go to ${picocolors_1.default.blue('https://github.com/vikejs/vike/issues/new')} and copy-paste this error. A maintainer will fix the bug (usually under 24 hours).`,
42
41
  debugStr
43
42
  ]
44
43
  .filter(Boolean)
@@ -1,44 +1,62 @@
1
1
  "use strict";
2
2
  // Mechanism to ensure code isn't loaded by production runtime
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.assertEnv = exports.markEnvAsVite = exports.markEnvAsPreview = exports.markEnvAsDev = exports.assertIsNotProductionRuntime = void 0;
4
+ exports.vikeVitePluginLoadedInProductionError = exports.assertEnv = exports.markEnvAsVikePluginLoaded = exports.markEnvAsVitePreview = exports.markEnvAsViteDev = exports.assertIsNotProductionRuntime = void 0;
5
5
  const assert_js_1 = require("./assert.js");
6
6
  const assertIsNotBrowser_js_1 = require("./assertIsNotBrowser.js");
7
+ const debug_js_1 = require("./debug.js");
7
8
  const getGlobalObject_js_1 = require("./getGlobalObject.js");
8
9
  const isVitest_js_1 = require("./isVitest.js");
9
10
  (0, assertIsNotBrowser_js_1.assertIsNotBrowser)();
11
+ const debug = (0, debug_js_1.createDebugger)('vike:setup');
12
+ const vikeVitePluginLoadedInProductionError = `Loading Vike's Vite plugin (the vike/plugin module) is prohibited in production.`;
13
+ exports.vikeVitePluginLoadedInProductionError = vikeVitePluginLoadedInProductionError;
10
14
  const env = (0, getGlobalObject_js_1.getGlobalObject)('utils/assertIsNotProductionRuntime.ts', {});
11
- // Called by *.ts that want to ensure that they aren't loaded by the server runtime in production
15
+ // Called by Vike modules that want to ensure that they aren't loaded by the server runtime in production
12
16
  function assertIsNotProductionRuntime() {
13
- env.shouldBeVite = true;
17
+ if (debug.isEnabled)
18
+ debug('assertIsNotProductionRuntime()', new Error().stack);
19
+ env.shouldNotBeProduction = true;
14
20
  }
15
21
  exports.assertIsNotProductionRuntime = assertIsNotProductionRuntime;
16
22
  // Called by Vite hook configureServer()
17
- function markEnvAsDev() {
18
- env.isDev = true;
23
+ function markEnvAsViteDev() {
24
+ if (debug.isEnabled)
25
+ debug('markEnvAsViteDev()', new Error().stack);
26
+ env.isViteDev = true;
19
27
  }
20
- exports.markEnvAsDev = markEnvAsDev;
28
+ exports.markEnvAsViteDev = markEnvAsViteDev;
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
- exports.markEnvAsPreview = markEnvAsPreview;
35
+ exports.markEnvAsVitePreview = markEnvAsVitePreview;
26
36
  // Called by ../node/plugin/index.ts
27
- function markEnvAsVite() {
28
- env.isVite = true;
37
+ function markEnvAsVikePluginLoaded() {
38
+ if (debug.isEnabled)
39
+ debug('markEnvAsVikePluginLoaded()', new Error().stack);
40
+ env.isVikePluginLoaded = true;
29
41
  }
30
- exports.markEnvAsVite = markEnvAsVite;
42
+ exports.markEnvAsVikePluginLoaded = markEnvAsVikePluginLoaded;
31
43
  // Called by ../node/runtime/index.ts
32
44
  function assertEnv() {
45
+ if (debug.isEnabled)
46
+ debug('assertEnv()', new Error().stack);
33
47
  if ((0, isVitest_js_1.isVitest)())
34
48
  return;
35
- if (env.isDev || env.isPreview) {
36
- (0, assert_js_1.assert)(env.isVite);
37
- (0, assert_js_1.assert)(env.shouldBeVite);
49
+ const isProduction = !env.isViteDev && !env.isVitePreview;
50
+ if (isProduction) {
51
+ // 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.)
52
+ (0, assert_js_1.assertUsage)(!env.isVikePluginLoaded, vikeVitePluginLoadedInProductionError);
53
+ // This assert() is the main goal of this file: it ensures assertIsNotProductionRuntime()
54
+ (0, assert_js_1.assert)(!env.shouldNotBeProduction);
38
55
  }
39
56
  else {
40
- (0, assert_js_1.assert)(!env.isVite);
41
- (0, assert_js_1.assert)(!env.shouldBeVite);
57
+ // This assert() is obious and boring
58
+ (0, assert_js_1.assert)(env.shouldNotBeProduction);
59
+ (0, assert_js_1.assert)(env.isVikePluginLoaded);
42
60
  }
43
61
  }
44
62
  exports.assertEnv = assertEnv;