Skip to content

Commit 5c8af6b

Browse files
committed
vfs: revert non-VFS changes in cjs/loader.js and esm/resolve.js
Revert changes to cjs/loader.js (extensionless file format detection) and esm/resolve.js (refactoring, bug fix, circular dep changes) that are not needed for VFS. Restore lazy loading of defaultResolve and defaultLoadSync in esm/loader.js to avoid snapshot build failure since the reverted resolve.js has top-level getOptionValue calls.
1 parent 37e576c commit 5c8af6b

9 files changed

Lines changed: 27 additions & 54 deletions

File tree

β€Žlib/internal/modules/cjs/loader.jsβ€Ž

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,15 +1931,6 @@ Module._extensions['.js'] = function(module, filename) {
19311931
} else {
19321932
format = 'typescript';
19331933
}
1934-
} else if (path.extname(filename) === '') {
1935-
// Extensionless files skip the .js suffix check above. When type is explicit,
1936-
// follow it so ESM syntax surfaces as SyntaxError for commonjs instead of
1937-
// silently delegating to ESM.
1938-
pkg = packageJsonReader.getNearestParentPackageJSON(filename);
1939-
const typeFromPjson = pkg?.data?.type;
1940-
if (typeFromPjson === 'commonjs' || typeFromPjson === 'module') {
1941-
format = typeFromPjson;
1942-
}
19431934
}
19441935
const { source, format: loadedFormat } = loadSource(module, filename, format);
19451936
// Function require shouldn't be used in ES modules when require(esm) is disabled.

β€Žlib/internal/modules/esm/loader.jsβ€Ž

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ function newLoadCache() {
9999
}
100100

101101
const { translators } = require('internal/modules/esm/translators');
102-
const { defaultResolve } = require('internal/modules/esm/resolve');
103-
const { defaultLoadSync, throwUnknownModuleFormat } = require('internal/modules/esm/load');
102+
103+
let defaultResolve, defaultLoadSync;
104104

105105
/**
106106
* Generate message about potential race condition caused by requiring a cached module that has started
@@ -694,6 +694,7 @@ class ModuleLoader {
694694
return cachedResult;
695695
}
696696

697+
defaultResolve ??= require('internal/modules/esm/resolve').defaultResolve;
697698
const result = defaultResolve(specifier, context);
698699
this.#resolveCache.set(requestKey, parentURL, result);
699700
return result;
@@ -770,6 +771,7 @@ class ModuleLoader {
770771
if (this.#asyncLoaderHooks?.loadSync) {
771772
return this.#asyncLoaderHooks.loadSync(url, context);
772773
}
774+
defaultLoadSync ??= require('internal/modules/esm/load').defaultLoadSync;
773775
return defaultLoadSync(url, context);
774776
}
775777

@@ -795,7 +797,7 @@ class ModuleLoader {
795797

796798
validateLoadResult(url, format) {
797799
if (format == null) {
798-
throwUnknownModuleFormat(url, format);
800+
require('internal/modules/esm/load').throwUnknownModuleFormat(url, format);
799801
}
800802
}
801803

β€Žlib/internal/modules/esm/resolve.jsβ€Ž

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const {
99
ObjectPrototypeHasOwnProperty,
1010
RegExpPrototypeExec,
1111
RegExpPrototypeSymbolReplace,
12+
SafeMap,
1213
SafeSet,
1314
String,
1415
StringPrototypeEndsWith,
@@ -22,12 +23,15 @@ const {
2223
encodeURIComponent,
2324
} = primordials;
2425
const assert = require('internal/assert');
26+
const internalFS = require('internal/fs/utils');
2527
const { BuiltinModule } = require('internal/bootstrap/realm');
26-
const { toRealPath } = require('internal/modules/helpers');
27-
const internalFsBinding = internalBinding('fs');
28+
const { realpathSync } = require('fs');
2829
const { getOptionValue } = require('internal/options');
2930
// Do not eagerly grab .manifest, it may be in TDZ
3031
const { sep, posix: { relative: relativePosixPath }, resolve } = require('path');
32+
const preserveSymlinks = getOptionValue('--preserve-symlinks');
33+
const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
34+
const inputTypeFlag = getOptionValue('--input-type');
3135
const { URL, pathToFileURL, fileURLToPath, isURL, URLParse } = require('internal/url');
3236
const { getCWDURL, setOwnProperty } = require('internal/util');
3337
const { canParse: URLCanParse } = internalBinding('url');
@@ -45,9 +49,11 @@ const {
4549
ERR_UNSUPPORTED_DIR_IMPORT,
4650
ERR_UNSUPPORTED_RESOLVE_REQUEST,
4751
} = require('internal/errors').codes;
48-
const { defaultGetFormatWithoutErrors } = require('internal/modules/esm/get_format');
52+
53+
const { Module: CJSModule } = require('internal/modules/cjs/loader');
4954
const { getConditionsSet } = require('internal/modules/esm/utils');
5055
const packageJsonReader = require('internal/modules/package_json_reader');
56+
const internalFsBinding = internalBinding('fs');
5157

5258
/**
5359
* @typedef {import('internal/modules/esm/package_config.js').PackageConfig} PackageConfig
@@ -147,6 +153,8 @@ function emitLegacyIndexDeprecation(url, path, pkgPath, base, main) {
147153
}
148154
}
149155

156+
const realpathCache = new SafeMap();
157+
150158
const legacyMainResolveExtensions = [
151159
'',
152160
'.js',
@@ -241,7 +249,7 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
241249
}
242250

243251
const stats = internalFsBinding.internalModuleStat(
244-
StringPrototypeEndsWith(path, '/') ? StringPrototypeSlice(path, -1) : path,
252+
StringPrototypeEndsWith(internalFsBinding, path, '/') ? StringPrototypeSlice(path, -1) : path,
245253
);
246254

247255
// Check for stats.isDirectory()
@@ -269,7 +277,9 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
269277
}
270278

271279
if (!preserveSymlinks) {
272-
const real = toRealPath(path);
280+
const real = realpathSync(path, {
281+
[internalFS.realpathCacheKey]: realpathCache,
282+
});
273283
const { search, hash } = resolved;
274284
resolved =
275285
pathToFileURL(real + (StringPrototypeEndsWith(path, sep) ? '/' : ''));
@@ -863,7 +873,6 @@ function moduleResolve(specifier, base, conditions, preserveSymlinks) {
863873
*/
864874
function resolveAsCommonJS(specifier, parentURL) {
865875
try {
866-
const { Module: CJSModule } = require('internal/modules/cjs/loader');
867876
const parent = fileURLToPath(parentURL);
868877
const tmpModule = new CJSModule(parent, null);
869878
tmpModule.paths = CJSModule._nodeModulePaths(parent);
@@ -973,7 +982,7 @@ function defaultResolve(specifier, context = {}) {
973982
// input, to avoid user confusion over how expansive the effect of the
974983
// flag should be (i.e. entry point only, package scope surrounding the
975984
// entry point, etc.).
976-
if (getOptionValue('--input-type')) { throw new ERR_INPUT_TYPE_NOT_ALLOWED(); }
985+
if (inputTypeFlag) { throw new ERR_INPUT_TYPE_NOT_ALLOWED(); }
977986
}
978987

979988
conditions = getConditionsSet(conditions);
@@ -983,7 +992,7 @@ function defaultResolve(specifier, context = {}) {
983992
specifier,
984993
parentURL,
985994
conditions,
986-
isMain ? getOptionValue('--preserve-symlinks-main') : getOptionValue('--preserve-symlinks'),
995+
isMain ? preserveSymlinksMain : preserveSymlinks,
987996
);
988997
} catch (error) {
989998
// Try to give the user a hint of what would have been the
@@ -1037,3 +1046,8 @@ module.exports = {
10371046
packageResolve,
10381047
throwIfInvalidParentURL,
10391048
};
1049+
1050+
// cycle
1051+
const {
1052+
defaultGetFormatWithoutErrors,
1053+
} = require('internal/modules/esm/get_format');

β€Žtest/es-module/test-extensionless-esm-type-commonjs.jsβ€Ž

Lines changed: 0 additions & 19 deletions
This file was deleted.

β€Žtest/fixtures/es-modules/extensionless-esm-commonjs/package.jsonβ€Ž

Lines changed: 0 additions & 3 deletions
This file was deleted.

β€Žtest/fixtures/es-modules/extensionless-esm-commonjs/scriptβ€Ž

Lines changed: 0 additions & 4 deletions
This file was deleted.

β€Žtest/fixtures/es-modules/extensionless-esm-module/package.jsonβ€Ž

Lines changed: 0 additions & 3 deletions
This file was deleted.

β€Žtest/fixtures/es-modules/extensionless-esm-module/scriptβ€Ž

Lines changed: 0 additions & 4 deletions
This file was deleted.

β€Žtest/parallel/test-bootstrap-modules.jsβ€Ž

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ if (isMainThread) {
131131
'NativeModule internal/modules/esm/assert',
132132
'NativeModule internal/modules/esm/loader',
133133
'NativeModule internal/modules/esm/load',
134-
'NativeModule internal/modules/esm/resolve',
135134
'NativeModule internal/modules/esm/translators',
136135
'NativeModule url',
137136
].forEach(expected.beforePreExec.add.bind(expected.beforePreExec));

0 commit comments

Comments
Β (0)