vike 0.4.171-commit-3b2cb88 → 0.4.171-commit-2876124

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.
@@ -50,7 +50,8 @@ function envVarsPlugin() {
50
50
  (0, utils_js_1.assertUsage)(false, errMsg);
51
51
  }
52
52
  else {
53
- // Only a warning for faster development DX (e.g. when user toggles `ssr: boolean` or `onBeforeRenderIsomorph: boolean`)
53
+ // - Only a warning for faster development DX (e.g. when user toggles `ssr: boolean` or `onBeforeRenderIsomorph: boolean`).
54
+ // - But only showing a warning can be confusing: https://github.com/vikejs/vike/issues/1641
54
55
  (0, utils_js_1.assertWarning)(false, errMsg, { onlyOnce: true });
55
56
  }
56
57
  }
@@ -3,53 +3,95 @@ 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.getHint = exports.isKnownError = exports.isCjsEsmError = exports.logErrorHint = void 0;
6
+ exports.getErrorHint = exports.logErrorHint = void 0;
7
7
  const utils_js_1 = require("../utils.js");
8
8
  const picocolors_1 = __importDefault(require("@brillout/picocolors"));
9
- const knownErrors = [
9
+ const hintDefault = 'The error could be a CJS/ESM issue, see https://vike.dev/broken-npm-package';
10
+ const hintLinkPrefix = 'To fix this error, see ';
11
+ const errorsMisc = [
10
12
  {
11
13
  errMsg: 'jsxDEV is not a function',
12
- link: 'https://github.com/vikejs/vike/issues/1469#issuecomment-1919518096'
13
- },
14
- {
15
- errMsg: 'Element type is invalid: expected a string (for built-in components) or a class/function (for composite components)',
16
- link: 'https://vike.dev/broken-npm-package#react-invalid-component'
14
+ link: 'https://github.com/vikejs/vike/issues/1469#issuecomment-1919518096',
15
+ shouldMentionNodeModules: false
17
16
  },
18
17
  {
19
18
  // ```
20
19
  // Error [RollupError]: Could not resolve "../dist/client/assets.json" from "renderer/+onRenderHtml.tsx"
21
20
  // ```
22
21
  errMsg: 'assets.json',
23
- link: 'https://vike.dev/getGlobalContext'
22
+ link: 'https://vike.dev/getGlobalContext',
23
+ shouldMentionNodeModules: false
24
+ }
25
+ ];
26
+ const errorsReact = [
27
+ {
28
+ errMsg: 'Element type is invalid: expected a string (for built-in components) or a class/function (for composite components)',
29
+ link: 'https://vike.dev/broken-npm-package#react-invalid-component',
30
+ // The stack trace can be user-land while the import is coming from node_modules
31
+ shouldMentionNodeModules: false
24
32
  },
25
33
  {
34
+ // React's "Invalid hook call.", see https://github.com/vikejs/vike/discussions/1637#discussioncomment-9424712
35
+ errMsg: "Cannot read properties of null (reading 'useContext')"
36
+ }
37
+ ];
38
+ const errorsCjsEsm_withPreciseLink = [
39
+ {
40
+ // `SyntaxError: Named export '${exportName}' not found. The requested module '${packageName}' is a CommonJS module, which may not support all module.exports as named exports.`
26
41
  errMsg: /Named export.*not found/i,
27
- link: 'https://vike.dev/broken-npm-package#named-export-not-found'
42
+ link: 'https://vike.dev/broken-npm-package#named-export-not-found',
43
+ // It seems that this always points to an npm package import.
44
+ shouldMentionNodeModules: false
28
45
  }
29
46
  ];
47
+ const errorsCjsEsm = [
48
+ { errMsg: 'ERR_UNSUPPORTED_DIR_IMPORT' },
49
+ { errMsg: 'ERR_REQUIRE_ESM' },
50
+ { errMsg: 'Must use import' },
51
+ { errMsg: /Cannot find \S+ '(\S+)' imported from (\S+)/ },
52
+ { errMsg: 'ERR_UNKNOWN_FILE_EXTENSION' },
53
+ { errMsg: /Unknown file extension "\S+" for (\S+)/ },
54
+ // `SyntaxError: Cannot use import statement outside a module`.
55
+ {
56
+ errMsg: 'Cannot use import statement',
57
+ // Since user code is always ESM, this error must always originate from an npm package.
58
+ shouldMentionNodeModules: false
59
+ },
60
+ { errMsg: 'is not exported' },
61
+ { errMsg: 'Cannot read properties of undefined' },
62
+ { errMsg: '.default is not' },
63
+ // Using CJS inside ESM modules.
64
+ { errMsg: 'require is not a function' },
65
+ { errMsg: 'exports is not defined' },
66
+ { errMsg: 'module is not defined' },
67
+ { errMsg: 'window is not defined' },
68
+ { errMsg: 'not defined in ES' },
69
+ { errMsg: "Unexpected token 'export'" }
70
+ ];
30
71
  function logErrorHint(error) {
31
72
  /* Collect errors for ./logErrorHint.spec.ts
32
73
  collectError(error)
33
74
  //*/
34
- const hint = getHint(error);
75
+ const hint = getErrorHint(error);
35
76
  if (hint)
36
77
  logHint(hint);
37
78
  }
38
79
  exports.logErrorHint = logErrorHint;
39
- function getHint(error) {
80
+ function getErrorHint(error) {
40
81
  {
41
- const link = isKnownError(error);
42
- if (link)
43
- return `To fix this error, see ${link}`;
44
- }
45
- const res = isCjsEsmError(error);
46
- if (res) {
47
- const hint = 'The error seems to be a CJS/ESM issue, see https://vike.dev/broken-npm-package';
48
- return hint;
82
+ const knownErr = isKnownError(error);
83
+ if (knownErr) {
84
+ if (knownErr.link) {
85
+ return hintLinkPrefix + knownErr.link;
86
+ }
87
+ else {
88
+ return hintDefault;
89
+ }
90
+ }
49
91
  }
50
92
  return null;
51
93
  }
52
- exports.getHint = getHint;
94
+ exports.getErrorHint = getErrorHint;
53
95
  function logHint(hint) {
54
96
  hint = (0, utils_js_1.formatHintLog)(hint);
55
97
  hint = picocolors_1.default.bold(hint);
@@ -57,268 +99,42 @@ function logHint(hint) {
57
99
  }
58
100
  function isKnownError(error) {
59
101
  const anywhere = getAnywhere(error);
60
- const knownErr = knownErrors.find((knownErorr) => {
61
- return includes(anywhere, knownErorr.errMsg);
102
+ const knownErr = [
103
+ //
104
+ ...errorsMisc,
105
+ ...errorsReact,
106
+ ...errorsCjsEsm_withPreciseLink,
107
+ ...errorsCjsEsm
108
+ ].find((knownErorr) => {
109
+ if (!includesLowercase(anywhere, knownErorr.errMsg))
110
+ return false;
111
+ if (knownErorr.shouldMentionNodeModules !== false && !includesLowercase(anywhere, 'node_modules'))
112
+ return false;
113
+ return true;
62
114
  });
63
115
  if (!knownErr)
64
116
  return false;
65
- return knownErr.link;
66
- }
67
- exports.isKnownError = isKnownError;
68
- // `false` -> noop
69
- // `true` -> generic message
70
- // `'some-npm-package'` -> add some-npm-package to `ssr.noExternal`
71
- function isCjsEsmError(error) {
72
- const res = check(error);
73
- if (res === true || res === false)
74
- return res;
75
- const packageNames = normalizeRes(res);
76
- if (packageNames === false)
77
- return packageNames;
78
- packageNames.forEach((packageName) => {
79
- (0, utils_js_1.assert)(!['vite', 'vike'].includes(packageName));
80
- });
81
- // We don't use this anymore: we could return `true` instead. Shall we remove returning a list of npm packages?
82
- return packageNames;
83
- }
84
- exports.isCjsEsmError = isCjsEsmError;
85
- function normalizeRes(res) {
86
- let packageNames = (0, utils_js_1.isArray)(res) ? res : [res];
87
- packageNames = (0, utils_js_1.unique)(packageNames.filter(utils_js_1.isNotNullish).filter((packageName) => packageName !== '@brillout/import'));
88
- if (packageNames.length === 0)
89
- return false;
90
- return packageNames;
117
+ return knownErr;
91
118
  }
92
- function check(error) {
93
- const message = getErrMessage(error);
94
- const anywhere = getAnywhere(error);
95
- const packageName_stack1 = getPackageName_stack1(error);
96
- const packageName_stack2 = getPackageName_stack2(error);
97
- const isRelatedToNodeModules = !!packageName_stack1 || !!packageName_stack2 || includesNodeModules(message);
98
- /*
99
- const relatedNpmPackages = normalizeArray([
100
- packageName_stack1 || null,
101
- packageName_stack2 || null,
102
- (message && extractFromNodeModulesPath(message)) || null
103
- ])
104
- */
105
- // ERR_UNSUPPORTED_DIR_IMPORT
106
- {
107
- const packageName = parseNodeModulesPathMessage('ERR_UNSUPPORTED_DIR_IMPORT', anywhere);
108
- if (packageName)
109
- return packageName;
110
- }
111
- // ERR_UNKNOWN_FILE_EXTENSION
112
- {
113
- const packageName = parseUnkownFileExtensionMessage(anywhere);
114
- if (packageName)
115
- return packageName;
116
- }
117
- {
118
- const packageName = parseNodeModulesPathMessage('ERR_UNKNOWN_FILE_EXTENSION', anywhere);
119
- if (packageName)
120
- return packageName;
119
+ function includesLowercase(str, substr) {
120
+ if (substr instanceof RegExp) {
121
+ let { flags } = substr;
122
+ if (!flags.includes('i'))
123
+ flags += 'i';
124
+ const regex = new RegExp(substr.source, flags);
125
+ return regex.test(str);
121
126
  }
122
- {
123
- const packageName = parseNodeModulesPathMessage('is not exported', anywhere);
124
- if (packageName)
125
- return packageName;
126
- }
127
- // Using CJS inside ESM modules.
128
- if (includes(anywhere, 'require is not a function') ||
129
- includes(anywhere, 'exports is not defined') ||
130
- includes(anywhere, 'module is not defined') ||
131
- includes(anywhere, 'window is not defined') ||
132
- includes(anywhere, 'not defined in ES')) {
133
- if (packageName_stack1)
134
- return packageName_stack1;
135
- }
136
- if (includes(anywhere, "Unexpected token 'export'")) {
137
- if (packageName_stack2)
138
- return packageName_stack2;
139
- if (packageName_stack1)
140
- return packageName_stack1;
141
- }
142
- // ERR_REQUIRE_ESM
143
- if (includes(anywhere, 'ERR_REQUIRE_ESM')) {
144
- /* The issue is the importer, not the importee.
145
- if (relatedNpmPackages) return relatedNpmPackages
146
- */
147
- {
148
- if (packageName_stack1)
149
- return packageName_stack1;
150
- }
151
- if (isRelatedToNodeModules)
152
- return true;
127
+ if (typeof substr === 'string') {
128
+ return str.toLowerCase().includes(substr.toLowerCase());
153
129
  }
154
- /* The following two wrongfully match user land errors
155
- {
156
- const packageNames = parseNodeModulesPath('ERR_REQUIRE_ESM', anywhere)
157
- if (packageNames) return packageNames
158
- }
159
- {
160
- const packageNames = parseNodeModulesPath('Must use import', anywhere)
161
- if (packageNames) return packageNames
162
- }
163
- */
164
- // `SyntaxError: Named export '${exportName}' not found. The requested module '${packageName}' is a CommonJS module, which may not support all module.exports as named exports.`
165
- {
166
- const packageName = parseImportFrom(anywhere);
167
- if (packageName)
168
- return packageName;
169
- }
170
- if (includes(anywhere, 'Cannot read properties of undefined')) {
171
- if (isRelatedToNodeModules) {
172
- /* We return true because relatedNpmPackages points to the importer but the problematic npm package is the importee
173
- if (relatedNpmPackages) return relatedNpmPackages
174
- */
175
- return true;
176
- }
177
- }
178
- // ERR_MODULE_NOT_FOUND
179
- {
180
- const packageNames = parseCannotFindMessage(anywhere);
181
- if (packageNames)
182
- return packageNames;
183
- }
184
- if (
185
- // `SyntaxError: Cannot use import statement outside a module`.
186
- // Since user code is always ESM, this error must always originate from an npm package.
187
- includes(anywhere, 'Cannot use import statement') ||
188
- // `SyntaxError: Named export '${exportName}' not found. The requested module '${packageName}' is a CommonJS module, which may not support all module.exports as named exports.`
189
- // It seems that this always points to an npm package import.
190
- /Named export.*not found/i.test(anywhere)) {
191
- /* We return true even if fromNodeModules is false because the errors always relate to npm packages.
192
- if (fromNodeModules) return true
193
- */
194
- return true;
195
- }
196
- return false;
197
- }
198
- function parseCannotFindMessage(str) {
199
- const match = /Cannot find \S+ '(\S+)' imported from (\S+)/.exec(str);
200
- if (!match)
201
- return false;
202
- // const packageNameCannotFind = extractFromPath(match[1]!)
203
- const packageNameFrom = extractFromPath(match[2]);
204
- return normalizeArray([
205
- // packageNameCannotFind,
206
- packageNameFrom
207
- ]);
208
- }
209
- function parseUnkownFileExtensionMessage(str) {
210
- const match = /Unknown file extension "\S+" for (\S+)/.exec(str);
211
- if (!match)
212
- return false;
213
- const filePath = match[1];
214
- const packageName = extractFromPath(filePath);
215
- return packageName;
216
- }
217
- function parseImportFrom(str) {
218
- const match = /\bimport\b.*?\bfrom\b\s*?"(.+?)"/.exec(str);
219
- if (!match)
220
- return false;
221
- const importPath = match[1];
222
- const packageName = extractFromPath(importPath);
223
- return packageName;
224
- }
225
- function parseNodeModulesPathMessage(begin, str) {
226
- str = str.replaceAll('\\', '/');
227
- const regex = new RegExp(`${begin}.*(node_modules\\/\\S+)`);
228
- const match = regex.exec(str);
229
- if (!match)
230
- return false;
231
- const importPath = match[1];
232
- return extractFromNodeModulesPath(importPath);
233
- }
234
- function getPackageName_stack1(err) {
235
- const errStack = getErrStack(err);
236
- if (!errStack)
237
- return false;
238
- const firstLineStackTrace = errStack.split('\n').filter((line) => line.startsWith(' at '))[0];
239
- if (!firstLineStackTrace)
240
- return false;
241
- return extractFromNodeModulesPath(firstLineStackTrace);
242
- }
243
- /** See https://github.com/brillout/repro_node-syntax-error#nodejs-behavior */
244
- function getPackageName_stack2(err) {
245
- const errStack = getErrStack(err);
246
- if (!errStack)
247
- return false;
248
- const firstLine = errStack.trim().split('\n')[0];
249
- return extractFromNodeModulesPath(firstLine);
250
- }
251
- function extractFromPath(filePath) {
252
- (0, utils_js_1.assert)(filePath);
253
- filePath = clean(filePath);
254
- filePath = filePath.replaceAll('\\', '/');
255
- let packageName;
256
- if (!filePath.includes('node_modules/')) {
257
- packageName = filePath;
258
- if (packageName.startsWith('/'))
259
- return null;
260
- if (packageName.startsWith('.'))
261
- return null;
262
- }
263
- else {
264
- packageName = filePath.split('node_modules/').pop();
265
- // This assert is fairly risk, we should eventually remove it
266
- (0, utils_js_1.assert)(!packageName.startsWith('.'));
267
- // This assert is fairly risk, we should eventually remove it
268
- (0, utils_js_1.assert)(!packageName.startsWith('/'));
269
- }
270
- if (!packageName)
271
- return null;
272
- packageName = packageName.split('/').slice(0, 2).join('/');
273
- if (!packageName.startsWith('@')) {
274
- packageName = packageName.split('/')[0];
275
- }
276
- packageName = clean(packageName);
277
- return packageName;
278
- }
279
- function clean(packageName) {
280
- const b = ['"', "'", '(', ')'];
281
- if (b.includes(packageName[0])) {
282
- packageName = packageName.slice(1);
283
- }
284
- if (b.includes(packageName[packageName.length - 1])) {
285
- packageName = packageName.slice(0, -1);
286
- }
287
- return packageName;
130
+ (0, utils_js_1.assert)(false);
288
131
  }
289
- function extractFromNodeModulesPath(str) {
290
- if (!includesNodeModules(str))
291
- return false;
292
- const packageName = extractFromPath(str);
293
- (0, utils_js_1.assert)(packageName);
294
- return packageName;
295
- }
296
- function includes(str1, str2) {
297
- if (!str1)
298
- return false;
299
- if (str2 instanceof RegExp) {
300
- return str2.test(str1.toLowerCase());
301
- }
302
- if (typeof str2 === 'string') {
303
- return str1.toLowerCase().includes(str2.toLowerCase());
304
- }
305
- return false;
306
- }
307
- function includesNodeModules(str) {
308
- if (!str)
309
- return false;
310
- str = str.replaceAll('\\', '/');
311
- if (!str.includes('node_modules/'))
312
- return false;
313
- if (str.includes('node_modules/vite/'))
314
- return false;
315
- return true;
316
- }
317
- function normalizeArray(arr) {
318
- const arrNormalized = arr.filter(utils_js_1.isNotNullish);
319
- if (arrNormalized.length === 0)
320
- return null;
321
- return arrNormalized;
132
+ function getAnywhere(error) {
133
+ const code = getErrCode(error);
134
+ const message = getErrMessage(error);
135
+ const stack = getErrStack(error);
136
+ const anywhere = [code, message, stack].filter(Boolean).join('\n');
137
+ return anywhere;
322
138
  }
323
139
  function getErrMessage(err) {
324
140
  if (!(0, utils_js_1.isObject)(err))
@@ -347,13 +163,6 @@ function getErrStack(err) {
347
163
  return null;
348
164
  return err.stack;
349
165
  }
350
- function getAnywhere(error) {
351
- const code = getErrCode(error);
352
- const message = getErrMessage(error);
353
- const stack = getErrStack(error);
354
- const anywhere = [code, message, stack].filter(Boolean).join('\n');
355
- return anywhere;
356
- }
357
166
  function collectError(err) {
358
167
  console.log([
359
168
  '{',
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PROJECT_VERSION = exports.projectInfo = void 0;
4
- const PROJECT_VERSION = '0.4.171-commit-3b2cb88';
4
+ const PROJECT_VERSION = '0.4.171-commit-2876124';
5
5
  exports.PROJECT_VERSION = PROJECT_VERSION;
6
6
  const projectInfo = {
7
7
  projectName: 'Vike',
@@ -50,7 +50,8 @@ function envVarsPlugin() {
50
50
  assertUsage(false, errMsg);
51
51
  }
52
52
  else {
53
- // Only a warning for faster development DX (e.g. when user toggles `ssr: boolean` or `onBeforeRenderIsomorph: boolean`)
53
+ // - Only a warning for faster development DX (e.g. when user toggles `ssr: boolean` or `onBeforeRenderIsomorph: boolean`).
54
+ // - But only showing a warning can be confusing: https://github.com/vikejs/vike/issues/1641
54
55
  assertWarning(false, errMsg, { onlyOnce: true });
55
56
  }
56
57
  }
@@ -1,8 +1,4 @@
1
1
  export { logErrorHint };
2
- export { isCjsEsmError };
3
- export { isKnownError };
4
- export { getHint };
2
+ export { getErrorHint };
5
3
  declare function logErrorHint(error: unknown): void;
6
- declare function getHint(error: unknown): null | string;
7
- declare function isKnownError(error: unknown): false | string;
8
- declare function isCjsEsmError(error: unknown): boolean | string[];
4
+ declare function getErrorHint(error: unknown): null | string;
@@ -1,49 +1,89 @@
1
1
  export { logErrorHint };
2
- // For ./logErrorHint/*.spec.ts
3
- export { isCjsEsmError };
4
- export { isKnownError };
5
- export { getHint };
6
- import { assert, formatHintLog, isArray, isNotNullish, isObject, unique } from '../utils.js';
2
+ // For ./logErrorHint/getErrorHint.spec.ts
3
+ export { getErrorHint };
4
+ import { assert, formatHintLog, isObject } from '../utils.js';
7
5
  import pc from '@brillout/picocolors';
8
- const knownErrors = [
6
+ const hintDefault = 'The error could be a CJS/ESM issue, see https://vike.dev/broken-npm-package';
7
+ const hintLinkPrefix = 'To fix this error, see ';
8
+ const errorsMisc = [
9
9
  {
10
10
  errMsg: 'jsxDEV is not a function',
11
- link: 'https://github.com/vikejs/vike/issues/1469#issuecomment-1919518096'
12
- },
13
- {
14
- errMsg: 'Element type is invalid: expected a string (for built-in components) or a class/function (for composite components)',
15
- link: 'https://vike.dev/broken-npm-package#react-invalid-component'
11
+ link: 'https://github.com/vikejs/vike/issues/1469#issuecomment-1919518096',
12
+ shouldMentionNodeModules: false
16
13
  },
17
14
  {
18
15
  // ```
19
16
  // Error [RollupError]: Could not resolve "../dist/client/assets.json" from "renderer/+onRenderHtml.tsx"
20
17
  // ```
21
18
  errMsg: 'assets.json',
22
- link: 'https://vike.dev/getGlobalContext'
19
+ link: 'https://vike.dev/getGlobalContext',
20
+ shouldMentionNodeModules: false
21
+ }
22
+ ];
23
+ const errorsReact = [
24
+ {
25
+ errMsg: 'Element type is invalid: expected a string (for built-in components) or a class/function (for composite components)',
26
+ link: 'https://vike.dev/broken-npm-package#react-invalid-component',
27
+ // The stack trace can be user-land while the import is coming from node_modules
28
+ shouldMentionNodeModules: false
23
29
  },
24
30
  {
31
+ // React's "Invalid hook call.", see https://github.com/vikejs/vike/discussions/1637#discussioncomment-9424712
32
+ errMsg: "Cannot read properties of null (reading 'useContext')"
33
+ }
34
+ ];
35
+ const errorsCjsEsm_withPreciseLink = [
36
+ {
37
+ // `SyntaxError: Named export '${exportName}' not found. The requested module '${packageName}' is a CommonJS module, which may not support all module.exports as named exports.`
25
38
  errMsg: /Named export.*not found/i,
26
- link: 'https://vike.dev/broken-npm-package#named-export-not-found'
39
+ link: 'https://vike.dev/broken-npm-package#named-export-not-found',
40
+ // It seems that this always points to an npm package import.
41
+ shouldMentionNodeModules: false
27
42
  }
28
43
  ];
44
+ const errorsCjsEsm = [
45
+ { errMsg: 'ERR_UNSUPPORTED_DIR_IMPORT' },
46
+ { errMsg: 'ERR_REQUIRE_ESM' },
47
+ { errMsg: 'Must use import' },
48
+ { errMsg: /Cannot find \S+ '(\S+)' imported from (\S+)/ },
49
+ { errMsg: 'ERR_UNKNOWN_FILE_EXTENSION' },
50
+ { errMsg: /Unknown file extension "\S+" for (\S+)/ },
51
+ // `SyntaxError: Cannot use import statement outside a module`.
52
+ {
53
+ errMsg: 'Cannot use import statement',
54
+ // Since user code is always ESM, this error must always originate from an npm package.
55
+ shouldMentionNodeModules: false
56
+ },
57
+ { errMsg: 'is not exported' },
58
+ { errMsg: 'Cannot read properties of undefined' },
59
+ { errMsg: '.default is not' },
60
+ // Using CJS inside ESM modules.
61
+ { errMsg: 'require is not a function' },
62
+ { errMsg: 'exports is not defined' },
63
+ { errMsg: 'module is not defined' },
64
+ { errMsg: 'window is not defined' },
65
+ { errMsg: 'not defined in ES' },
66
+ { errMsg: "Unexpected token 'export'" }
67
+ ];
29
68
  function logErrorHint(error) {
30
69
  /* Collect errors for ./logErrorHint.spec.ts
31
70
  collectError(error)
32
71
  //*/
33
- const hint = getHint(error);
72
+ const hint = getErrorHint(error);
34
73
  if (hint)
35
74
  logHint(hint);
36
75
  }
37
- function getHint(error) {
76
+ function getErrorHint(error) {
38
77
  {
39
- const link = isKnownError(error);
40
- if (link)
41
- return `To fix this error, see ${link}`;
42
- }
43
- const res = isCjsEsmError(error);
44
- if (res) {
45
- const hint = 'The error seems to be a CJS/ESM issue, see https://vike.dev/broken-npm-package';
46
- return hint;
78
+ const knownErr = isKnownError(error);
79
+ if (knownErr) {
80
+ if (knownErr.link) {
81
+ return hintLinkPrefix + knownErr.link;
82
+ }
83
+ else {
84
+ return hintDefault;
85
+ }
86
+ }
47
87
  }
48
88
  return null;
49
89
  }
@@ -54,266 +94,42 @@ function logHint(hint) {
54
94
  }
55
95
  function isKnownError(error) {
56
96
  const anywhere = getAnywhere(error);
57
- const knownErr = knownErrors.find((knownErorr) => {
58
- return includes(anywhere, knownErorr.errMsg);
97
+ const knownErr = [
98
+ //
99
+ ...errorsMisc,
100
+ ...errorsReact,
101
+ ...errorsCjsEsm_withPreciseLink,
102
+ ...errorsCjsEsm
103
+ ].find((knownErorr) => {
104
+ if (!includesLowercase(anywhere, knownErorr.errMsg))
105
+ return false;
106
+ if (knownErorr.shouldMentionNodeModules !== false && !includesLowercase(anywhere, 'node_modules'))
107
+ return false;
108
+ return true;
59
109
  });
60
110
  if (!knownErr)
61
111
  return false;
62
- return knownErr.link;
63
- }
64
- // `false` -> noop
65
- // `true` -> generic message
66
- // `'some-npm-package'` -> add some-npm-package to `ssr.noExternal`
67
- function isCjsEsmError(error) {
68
- const res = check(error);
69
- if (res === true || res === false)
70
- return res;
71
- const packageNames = normalizeRes(res);
72
- if (packageNames === false)
73
- return packageNames;
74
- packageNames.forEach((packageName) => {
75
- assert(!['vite', 'vike'].includes(packageName));
76
- });
77
- // We don't use this anymore: we could return `true` instead. Shall we remove returning a list of npm packages?
78
- return packageNames;
79
- }
80
- function normalizeRes(res) {
81
- let packageNames = isArray(res) ? res : [res];
82
- packageNames = unique(packageNames.filter(isNotNullish).filter((packageName) => packageName !== '@brillout/import'));
83
- if (packageNames.length === 0)
84
- return false;
85
- return packageNames;
112
+ return knownErr;
86
113
  }
87
- function check(error) {
88
- const message = getErrMessage(error);
89
- const anywhere = getAnywhere(error);
90
- const packageName_stack1 = getPackageName_stack1(error);
91
- const packageName_stack2 = getPackageName_stack2(error);
92
- const isRelatedToNodeModules = !!packageName_stack1 || !!packageName_stack2 || includesNodeModules(message);
93
- /*
94
- const relatedNpmPackages = normalizeArray([
95
- packageName_stack1 || null,
96
- packageName_stack2 || null,
97
- (message && extractFromNodeModulesPath(message)) || null
98
- ])
99
- */
100
- // ERR_UNSUPPORTED_DIR_IMPORT
101
- {
102
- const packageName = parseNodeModulesPathMessage('ERR_UNSUPPORTED_DIR_IMPORT', anywhere);
103
- if (packageName)
104
- return packageName;
105
- }
106
- // ERR_UNKNOWN_FILE_EXTENSION
107
- {
108
- const packageName = parseUnkownFileExtensionMessage(anywhere);
109
- if (packageName)
110
- return packageName;
111
- }
112
- {
113
- const packageName = parseNodeModulesPathMessage('ERR_UNKNOWN_FILE_EXTENSION', anywhere);
114
- if (packageName)
115
- return packageName;
114
+ function includesLowercase(str, substr) {
115
+ if (substr instanceof RegExp) {
116
+ let { flags } = substr;
117
+ if (!flags.includes('i'))
118
+ flags += 'i';
119
+ const regex = new RegExp(substr.source, flags);
120
+ return regex.test(str);
116
121
  }
117
- {
118
- const packageName = parseNodeModulesPathMessage('is not exported', anywhere);
119
- if (packageName)
120
- return packageName;
121
- }
122
- // Using CJS inside ESM modules.
123
- if (includes(anywhere, 'require is not a function') ||
124
- includes(anywhere, 'exports is not defined') ||
125
- includes(anywhere, 'module is not defined') ||
126
- includes(anywhere, 'window is not defined') ||
127
- includes(anywhere, 'not defined in ES')) {
128
- if (packageName_stack1)
129
- return packageName_stack1;
130
- }
131
- if (includes(anywhere, "Unexpected token 'export'")) {
132
- if (packageName_stack2)
133
- return packageName_stack2;
134
- if (packageName_stack1)
135
- return packageName_stack1;
136
- }
137
- // ERR_REQUIRE_ESM
138
- if (includes(anywhere, 'ERR_REQUIRE_ESM')) {
139
- /* The issue is the importer, not the importee.
140
- if (relatedNpmPackages) return relatedNpmPackages
141
- */
142
- {
143
- if (packageName_stack1)
144
- return packageName_stack1;
145
- }
146
- if (isRelatedToNodeModules)
147
- return true;
122
+ if (typeof substr === 'string') {
123
+ return str.toLowerCase().includes(substr.toLowerCase());
148
124
  }
149
- /* The following two wrongfully match user land errors
150
- {
151
- const packageNames = parseNodeModulesPath('ERR_REQUIRE_ESM', anywhere)
152
- if (packageNames) return packageNames
153
- }
154
- {
155
- const packageNames = parseNodeModulesPath('Must use import', anywhere)
156
- if (packageNames) return packageNames
157
- }
158
- */
159
- // `SyntaxError: Named export '${exportName}' not found. The requested module '${packageName}' is a CommonJS module, which may not support all module.exports as named exports.`
160
- {
161
- const packageName = parseImportFrom(anywhere);
162
- if (packageName)
163
- return packageName;
164
- }
165
- if (includes(anywhere, 'Cannot read properties of undefined')) {
166
- if (isRelatedToNodeModules) {
167
- /* We return true because relatedNpmPackages points to the importer but the problematic npm package is the importee
168
- if (relatedNpmPackages) return relatedNpmPackages
169
- */
170
- return true;
171
- }
172
- }
173
- // ERR_MODULE_NOT_FOUND
174
- {
175
- const packageNames = parseCannotFindMessage(anywhere);
176
- if (packageNames)
177
- return packageNames;
178
- }
179
- if (
180
- // `SyntaxError: Cannot use import statement outside a module`.
181
- // Since user code is always ESM, this error must always originate from an npm package.
182
- includes(anywhere, 'Cannot use import statement') ||
183
- // `SyntaxError: Named export '${exportName}' not found. The requested module '${packageName}' is a CommonJS module, which may not support all module.exports as named exports.`
184
- // It seems that this always points to an npm package import.
185
- /Named export.*not found/i.test(anywhere)) {
186
- /* We return true even if fromNodeModules is false because the errors always relate to npm packages.
187
- if (fromNodeModules) return true
188
- */
189
- return true;
190
- }
191
- return false;
192
- }
193
- function parseCannotFindMessage(str) {
194
- const match = /Cannot find \S+ '(\S+)' imported from (\S+)/.exec(str);
195
- if (!match)
196
- return false;
197
- // const packageNameCannotFind = extractFromPath(match[1]!)
198
- const packageNameFrom = extractFromPath(match[2]);
199
- return normalizeArray([
200
- // packageNameCannotFind,
201
- packageNameFrom
202
- ]);
203
- }
204
- function parseUnkownFileExtensionMessage(str) {
205
- const match = /Unknown file extension "\S+" for (\S+)/.exec(str);
206
- if (!match)
207
- return false;
208
- const filePath = match[1];
209
- const packageName = extractFromPath(filePath);
210
- return packageName;
211
- }
212
- function parseImportFrom(str) {
213
- const match = /\bimport\b.*?\bfrom\b\s*?"(.+?)"/.exec(str);
214
- if (!match)
215
- return false;
216
- const importPath = match[1];
217
- const packageName = extractFromPath(importPath);
218
- return packageName;
219
- }
220
- function parseNodeModulesPathMessage(begin, str) {
221
- str = str.replaceAll('\\', '/');
222
- const regex = new RegExp(`${begin}.*(node_modules\\/\\S+)`);
223
- const match = regex.exec(str);
224
- if (!match)
225
- return false;
226
- const importPath = match[1];
227
- return extractFromNodeModulesPath(importPath);
228
- }
229
- function getPackageName_stack1(err) {
230
- const errStack = getErrStack(err);
231
- if (!errStack)
232
- return false;
233
- const firstLineStackTrace = errStack.split('\n').filter((line) => line.startsWith(' at '))[0];
234
- if (!firstLineStackTrace)
235
- return false;
236
- return extractFromNodeModulesPath(firstLineStackTrace);
237
- }
238
- /** See https://github.com/brillout/repro_node-syntax-error#nodejs-behavior */
239
- function getPackageName_stack2(err) {
240
- const errStack = getErrStack(err);
241
- if (!errStack)
242
- return false;
243
- const firstLine = errStack.trim().split('\n')[0];
244
- return extractFromNodeModulesPath(firstLine);
245
- }
246
- function extractFromPath(filePath) {
247
- assert(filePath);
248
- filePath = clean(filePath);
249
- filePath = filePath.replaceAll('\\', '/');
250
- let packageName;
251
- if (!filePath.includes('node_modules/')) {
252
- packageName = filePath;
253
- if (packageName.startsWith('/'))
254
- return null;
255
- if (packageName.startsWith('.'))
256
- return null;
257
- }
258
- else {
259
- packageName = filePath.split('node_modules/').pop();
260
- // This assert is fairly risk, we should eventually remove it
261
- assert(!packageName.startsWith('.'));
262
- // This assert is fairly risk, we should eventually remove it
263
- assert(!packageName.startsWith('/'));
264
- }
265
- if (!packageName)
266
- return null;
267
- packageName = packageName.split('/').slice(0, 2).join('/');
268
- if (!packageName.startsWith('@')) {
269
- packageName = packageName.split('/')[0];
270
- }
271
- packageName = clean(packageName);
272
- return packageName;
273
- }
274
- function clean(packageName) {
275
- const b = ['"', "'", '(', ')'];
276
- if (b.includes(packageName[0])) {
277
- packageName = packageName.slice(1);
278
- }
279
- if (b.includes(packageName[packageName.length - 1])) {
280
- packageName = packageName.slice(0, -1);
281
- }
282
- return packageName;
125
+ assert(false);
283
126
  }
284
- function extractFromNodeModulesPath(str) {
285
- if (!includesNodeModules(str))
286
- return false;
287
- const packageName = extractFromPath(str);
288
- assert(packageName);
289
- return packageName;
290
- }
291
- function includes(str1, str2) {
292
- if (!str1)
293
- return false;
294
- if (str2 instanceof RegExp) {
295
- return str2.test(str1.toLowerCase());
296
- }
297
- if (typeof str2 === 'string') {
298
- return str1.toLowerCase().includes(str2.toLowerCase());
299
- }
300
- return false;
301
- }
302
- function includesNodeModules(str) {
303
- if (!str)
304
- return false;
305
- str = str.replaceAll('\\', '/');
306
- if (!str.includes('node_modules/'))
307
- return false;
308
- if (str.includes('node_modules/vite/'))
309
- return false;
310
- return true;
311
- }
312
- function normalizeArray(arr) {
313
- const arrNormalized = arr.filter(isNotNullish);
314
- if (arrNormalized.length === 0)
315
- return null;
316
- return arrNormalized;
127
+ function getAnywhere(error) {
128
+ const code = getErrCode(error);
129
+ const message = getErrMessage(error);
130
+ const stack = getErrStack(error);
131
+ const anywhere = [code, message, stack].filter(Boolean).join('\n');
132
+ return anywhere;
317
133
  }
318
134
  function getErrMessage(err) {
319
135
  if (!isObject(err))
@@ -342,13 +158,6 @@ function getErrStack(err) {
342
158
  return null;
343
159
  return err.stack;
344
160
  }
345
- function getAnywhere(error) {
346
- const code = getErrCode(error);
347
- const message = getErrMessage(error);
348
- const stack = getErrStack(error);
349
- const anywhere = [code, message, stack].filter(Boolean).join('\n');
350
- return anywhere;
351
- }
352
161
  function collectError(err) {
353
162
  console.log([
354
163
  '{',
@@ -1,7 +1,7 @@
1
1
  export { projectInfo };
2
2
  export { PROJECT_VERSION };
3
- declare const PROJECT_VERSION: "0.4.171-commit-3b2cb88";
3
+ declare const PROJECT_VERSION: "0.4.171-commit-2876124";
4
4
  declare const projectInfo: {
5
5
  projectName: "Vike";
6
- projectVersion: "0.4.171-commit-3b2cb88";
6
+ projectVersion: "0.4.171-commit-2876124";
7
7
  };
@@ -1,6 +1,6 @@
1
1
  export { projectInfo };
2
2
  export { PROJECT_VERSION };
3
- const PROJECT_VERSION = '0.4.171-commit-3b2cb88';
3
+ const PROJECT_VERSION = '0.4.171-commit-2876124';
4
4
  const projectInfo = {
5
5
  projectName: 'Vike',
6
6
  projectVersion: PROJECT_VERSION
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vike",
3
- "version": "0.4.171-commit-3b2cb88",
3
+ "version": "0.4.171-commit-2876124",
4
4
  "scripts": {
5
5
  "dev": "tsc --watch",
6
6
  "build": "rimraf dist/ && pnpm run build:esm && pnpm run build:cjs",