vike 0.4.228-commit-84954ae → 0.4.228-commit-0e9e0f2

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 (36) hide show
  1. package/dist/cjs/client/shared/createGetGlobalContext.js +8 -1
  2. package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/resolvePointerImport.js +1 -1
  3. package/dist/cjs/node/runtime/globalContext.js +11 -1
  4. package/dist/cjs/node/runtime/renderPage/preparePageContextForUserConsumptionServerSide.js +1 -1
  5. package/dist/cjs/shared/assertPageContextProvidedByUser.js +1 -1
  6. package/dist/cjs/shared/createGlobalContextShared.js +6 -0
  7. package/dist/cjs/shared/createPageContextShared.js +1 -1
  8. package/dist/cjs/utils/PROJECT_VERSION.js +1 -1
  9. package/dist/cjs/utils/objectAssign.js +1 -1
  10. package/dist/cjs/utils/requireResolve.js +85 -63
  11. package/dist/esm/client/client-routing-runtime/createPageContext.d.ts +5 -2
  12. package/dist/esm/client/client-routing-runtime/globalContext.d.ts +6 -2
  13. package/dist/esm/client/server-routing-runtime/createPageContextClientSide.d.ts +4 -1
  14. package/dist/esm/client/server-routing-runtime/globalContext.d.ts +3 -0
  15. package/dist/esm/client/shared/createGetGlobalContext.d.ts +9 -1
  16. package/dist/esm/client/shared/createGetGlobalContext.js +8 -1
  17. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/resolvePointerImport.js +1 -1
  18. package/dist/esm/node/prerender/runPrerender.d.ts +16 -2
  19. package/dist/esm/node/runtime/globalContext.d.ts +32 -2
  20. package/dist/esm/node/runtime/globalContext.js +11 -1
  21. package/dist/esm/node/runtime/renderPage/createPageContextServerSide.d.ts +8 -1
  22. package/dist/esm/node/runtime/renderPage/preparePageContextForUserConsumptionServerSide.js +1 -1
  23. package/dist/esm/node/runtime/renderPage/renderPageAlreadyRouted.d.ts +16 -2
  24. package/dist/esm/node/runtime/renderPage.d.ts +8 -1
  25. package/dist/esm/shared/assertPageContextProvidedByUser.js +1 -1
  26. package/dist/esm/shared/createGlobalContextShared.d.ts +13 -1
  27. package/dist/esm/shared/createGlobalContextShared.js +6 -0
  28. package/dist/esm/shared/createPageContextShared.d.ts +1 -1
  29. package/dist/esm/shared/createPageContextShared.js +1 -1
  30. package/dist/esm/shared/types.d.ts +8 -2
  31. package/dist/esm/utils/PROJECT_VERSION.d.ts +1 -1
  32. package/dist/esm/utils/PROJECT_VERSION.js +1 -1
  33. package/dist/esm/utils/objectAssign.js +1 -1
  34. package/dist/esm/utils/requireResolve.d.ts +2 -2
  35. package/dist/esm/utils/requireResolve.js +85 -63
  36. package/package.json +3 -3
@@ -11,93 +11,117 @@ import { scriptFileExtensionList } from './isScriptFile.js';
11
11
  import { createRequire } from 'node:module';
12
12
  import path from 'node:path';
13
13
  import { assertIsImportPathNpmPackage, isImportPathNpmPackageOrPathAlias } from './parseNpmPackage.js';
14
+ import { isNotNullish } from './isNullish.js';
14
15
  // @ts-ignore import.meta.url is shimmed at dist/cjs by dist-cjs-fixup.js.
15
16
  const importMetaUrl = import.meta.url;
17
+ assertPosixPath(importMetaUrl);
16
18
  assertIsNotBrowser();
17
19
  assertIsNotProductionRuntime();
18
- function requireResolve_(importPath, importerFile, options) {
19
- assertPosixPath(importerFile);
20
+ // - We still can't use import.meta.resolve() as of 23.1.0 (November 2024) because `parent` argument requires an experimental flag.
21
+ // - https://stackoverflow.com/questions/54977743/do-require-resolve-for-es-modules#comment139581675_62272600
22
+ // - Passing context to createRequire(context) isn't equivalent to passing it to the `paths` argument of require.resolve()
23
+ // - https://github.com/brillout/require-test
24
+ // - In practice, I guess it doesn't make a difference? It just seems to be small Node.js weirdness.
25
+ // - The argument createRequire(argument) seems to be overriden by the `paths` argument require.resolve()
26
+ // - For example, passing an empty array to `paths` kills the argument passed to `createRequire()`
27
+ // - Thus, when `paths` is defined, then the context needs to be passed to both createRequire() as well as the `paths` argument of require.resolve()
28
+ function requireResolve_(importPath, importerFilePath, { userRootDir, doNotHandleFileExtension } = {}) {
20
29
  assertPosixPath(importPath);
21
- assertPosixPath(importMetaUrl);
22
- assert(path.posix.basename(importerFile).includes('.'), { cwd: importerFile });
23
- const importerPath = addFilePrefix(importerFile);
24
- const require_ = createRequire(importerPath);
25
- if (!options?.doNotHandleFileExtension) {
26
- addFileExtensionsToRequireResolve(require_);
27
- importPath = removeFileExtention(importPath);
30
+ const contexts = importerFilePath
31
+ ? [importerFilePath]
32
+ : [userRootDir ? getFakeImporterFile(userRootDir) : importMetaUrl];
33
+ addExtraContextForNpmPackageImport(contexts, { importPath, userRootDir });
34
+ let importPathResolvedFilePath;
35
+ let failure;
36
+ for (const context of contexts) {
37
+ assertPosixPath(context);
38
+ const require_ = createRequire(ensureFilePrefix(context));
39
+ if (!doNotHandleFileExtension) {
40
+ addFileExtensionsToRequireResolve(require_);
41
+ importPath = removeFileExtention(importPath);
42
+ }
43
+ try {
44
+ importPathResolvedFilePath = require_.resolve(importPath);
45
+ }
46
+ catch (err) {
47
+ /* DEBUG
48
+ console.log('err', err)
49
+ console.log('importPath', importPath)
50
+ console.log('importerFilePath', importerFilePath)
51
+ console.log('context', context)
52
+ console.log('importMetaUrl', importMetaUrl)
53
+ console.log('paths', paths)
54
+ //*/
55
+ failure ?? (failure = { err });
56
+ }
57
+ if (importPathResolvedFilePath)
58
+ break;
28
59
  }
29
- const paths = !options?.paths
30
- ? undefined
31
- : [
32
- // Seems like `importerPath` gets overriden by the `paths` argument, so we add it to `paths`. (For example, passing an empty array to `paths` kills the argument passed to `createRequire()`.)
33
- toDirPath(importerFile),
34
- ...(options?.paths || [])
35
- ];
36
- let importedFile;
37
- try {
38
- // We still can't use import.meta.resolve() as of 23.1.0 (November 2024) because `parent` argument requires an experimental flag.
39
- // - https://stackoverflow.com/questions/54977743/do-require-resolve-for-es-modules#comment139581675_62272600
40
- importedFile = require_.resolve(importPath, { paths });
60
+ if (!importPathResolvedFilePath) {
61
+ assert(failure);
62
+ return { importPathResolvedFilePath: undefined, err: failure.err, hasFailed: true };
41
63
  }
42
- catch (err) {
43
- return { importedFile: undefined, err, hasFailed: true };
64
+ else {
65
+ assert(importPathResolvedFilePath);
66
+ importPathResolvedFilePath = toPosixPath(importPathResolvedFilePath);
67
+ return { importPathResolvedFilePath, err: undefined, hasFailed: false };
44
68
  }
45
- importedFile = toPosixPath(importedFile);
46
- return { importedFile, err: undefined, hasFailed: false };
47
69
  }
48
- function requireResolveOptional({ importPath, importerFile, userRootDir }) {
49
- const paths = getExtraPathsForNpmPackageImport({ importPath, userRootDir });
50
- const res = requireResolve_(importPath, importerFile, { paths });
70
+ function requireResolveOptional({ importPath, importerFilePath, userRootDir }) {
71
+ const res = requireResolve_(importPath, importerFilePath, { userRootDir });
51
72
  if (res.hasFailed)
52
73
  return null;
53
- return res.importedFile;
74
+ return res.importPathResolvedFilePath;
54
75
  }
55
76
  function requireResolveOptionalDir({ importPath, importerDir, userRootDir }) {
56
- const importerFile = getFakeFilePath(importerDir);
57
- const paths = getExtraPathsForNpmPackageImport({ importPath, userRootDir });
58
- const res = requireResolve_(importPath, importerFile, { paths });
77
+ const importerFilePath = getFakeImporterFile(importerDir);
78
+ const res = requireResolve_(importPath, importerFilePath, { userRootDir });
59
79
  if (res.hasFailed)
60
80
  return null;
61
- return res.importedFile;
81
+ return res.importPathResolvedFilePath;
62
82
  }
63
83
  function requireResolveNpmPackage({ importPathNpmPackage, userRootDir }) {
64
84
  assertIsImportPathNpmPackage(importPathNpmPackage);
65
- const importerFile = getFakeFilePath(userRootDir);
66
- const paths = getExtraPathsForNpmPackageImport({ importPath: importPathNpmPackage, userRootDir });
67
- const res = requireResolve_(importPathNpmPackage, importerFile, { paths });
85
+ const importerFilePath = getFakeImporterFile(userRootDir);
86
+ const res = requireResolve_(importPathNpmPackage, importerFilePath, { userRootDir });
68
87
  if (res.hasFailed)
69
88
  throw res.err;
70
- return res.importedFile;
89
+ return res.importPathResolvedFilePath;
71
90
  }
72
91
  function requireResolveVikeDistFile(vikeDistFile) {
73
92
  const vikeNodeModulesRoot = getVikeNodeModulesRoot();
74
93
  assertPosixPath(vikeNodeModulesRoot);
75
94
  assertPosixPath(vikeDistFile);
76
- const importedFile = path.posix.join(vikeNodeModulesRoot, vikeDistFile);
95
+ const importPathResolvedFilePath = path.posix.join(vikeNodeModulesRoot, vikeDistFile);
77
96
  // Double check
78
97
  {
79
- const res = requireResolve_(importedFile,
80
- // Passing some dummy context as the context isn't needed since importerFile is already resolved and absolute
81
- importMetaUrl, { doNotHandleFileExtension: true });
98
+ const res = requireResolve_(importPathResolvedFilePath,
99
+ // No context needed: importPathResolvedFilePath is already resolved and absolute
100
+ null, { doNotHandleFileExtension: true });
82
101
  if (res.hasFailed)
83
102
  throw res.err;
84
- assert(res.importedFile === importedFile);
103
+ assert(res.importPathResolvedFilePath === importPathResolvedFilePath);
85
104
  }
86
- return importedFile;
105
+ return importPathResolvedFilePath;
87
106
  }
88
- function getExtraPathsForNpmPackageImport({ importPath, userRootDir }) {
89
- if (
90
- // Ideally we'd set `paths` only for npm packages, but unfortunately we cannot always disambiguate between npm package imports and path aliases.
91
- !isImportPathNpmPackageOrPathAlias(importPath)) {
92
- return undefined;
93
- }
94
- const paths = [
107
+ function addExtraContextForNpmPackageImport(contexts, { importPath, userRootDir }) {
108
+ // We should add extra context only for npm packages, but unfortunately we cannot always disambiguate between npm package imports and path aliases.
109
+ if (!isImportPathNpmPackageOrPathAlias(importPath))
110
+ return;
111
+ const userRootDirFakeFile = userRootDir && getFakeImporterFile(userRootDir);
112
+ [
95
113
  // Workaround for monorepo resolve issue: https://github.com/vikejs/vike-react/pull/161/commits/dbaa6643e78015ac2797c237552800fef29b72a7
96
- userRootDir,
97
- // I can't think of a use case where this would be needed, but let's add it to be extra safe
98
- toDirPath(importMetaUrl)
99
- ];
100
- return paths;
114
+ userRootDirFakeFile,
115
+ // I can't think of a use case where this would be needed, but let's add one extra last chance to sucessfully resolve some complex monorepo setups
116
+ importMetaUrl
117
+ ]
118
+ .filter(isNotNullish)
119
+ .forEach((context) => {
120
+ const alreadyHasContext = contexts.includes(context) || contexts.includes(ensureFilePrefix(context));
121
+ if (alreadyHasContext)
122
+ return;
123
+ contexts.push(context);
124
+ });
101
125
  }
102
126
  function removeFileExtention(importPath) {
103
127
  // Skip for Bun: https://github.com/vikejs/vike/issues/2204
@@ -132,16 +156,14 @@ function getVikeNodeModulesRoot() {
132
156
  const vikeNodeModulesRoot = path.posix.join(removeFilePrefix(importMetaUrl), '../../../../');
133
157
  return vikeNodeModulesRoot;
134
158
  }
135
- function toDirPath(filePath) {
136
- return path.posix.dirname(removeFilePrefix(filePath));
137
- }
138
- function getFakeFilePath(dirPath) {
159
+ function getFakeImporterFile(dirPath) {
139
160
  assertPosixPath(dirPath);
140
- assert(!dirPath.startsWith('file')); // The file:// prefix is bogus with path.join
141
- const filePath = path.posix.join(dirPath, 'fakeFileForNodeResolve.js');
142
- return filePath;
161
+ assert(!dirPath.startsWith('file')); // The file:// prefix is bogus when used with path.posix.join()
162
+ const importerFilePath = path.posix.join(dirPath, 'fakeFileForNodeResolve.js');
163
+ return importerFilePath;
143
164
  }
144
- function addFilePrefix(filePath) {
165
+ function ensureFilePrefix(filePath) {
166
+ assertPosixPath(filePath);
145
167
  const filePrefix = getFilePrefix();
146
168
  if (!filePath.startsWith(filePrefix)) {
147
169
  assert(!filePath.startsWith('file'));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vike",
3
- "version": "0.4.228-commit-84954ae",
3
+ "version": "0.4.228-commit-0e9e0f2",
4
4
  "repository": "https://github.com/vikejs/vike",
5
5
  "exports": {
6
6
  "./server": {
@@ -123,7 +123,7 @@
123
123
  "@brillout/json-serializer": "^0.5.15",
124
124
  "@brillout/picocolors": "^1.0.26",
125
125
  "@brillout/require-shim": "^0.1.2",
126
- "@brillout/vite-plugin-server-entry": "^0.7.5",
126
+ "@brillout/vite-plugin-server-entry": "^0.7.8",
127
127
  "acorn": "^8.0.0",
128
128
  "cac": "^6.0.0",
129
129
  "es-module-lexer": "^1.0.0",
@@ -256,7 +256,7 @@
256
256
  "react-streaming": "^0.3.47",
257
257
  "rimraf": "^5.0.5",
258
258
  "typescript": "^5.8.3",
259
- "vite": "^6.2.5"
259
+ "vite": "^6.3.2"
260
260
  },
261
261
  "scripts": {
262
262
  "dev": "tsc --watch",