Visitar URL original
fix(patch): implement package maps in the ESM resolver · electron/electron@dcd1b96 · GitHub
Skip to content

Commit dcd1b96

Browse files
fix(patch): implement package maps in the ESM resolver
Upstream rewrote packageResolve() to branch on hasPackageMap() and added join to the path imports, which shifted the context around the electron: specifier shortcut and the legacyMainResolve imports. Ref: nodejs/node#62239
1 parent 8882847 commit dcd1b96

2 files changed

Lines changed: 22 additions & 21 deletions

File tree

‎patches/node/fix_allow_passing_fileexists_fn_to_legacymainresolve.patch‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,24 @@ We can fix this by allowing the C++ implementation of legacyMainResolve to use
1111
a fileExists function that does take Asar into account.
1212

1313
diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js
14-
index 185793fa305edbeab299f743e0018f34b2aaa655..b44eab420fe1bbec7ce34bde2ca4974b9d24a6dd 100644
14+
index beb3452ea134e4b408f8a3335a6cc65e97c8055a..a1c743cde5063a6ca504e8d2c32e9f2e13802cd9 100644
1515
--- a/lib/internal/modules/esm/resolve.js
1616
+++ b/lib/internal/modules/esm/resolve.js
1717
@@ -28,11 +28,10 @@ const { BuiltinModule } = require('internal/bootstrap/realm');
1818
const fs = require('fs');
1919
const { getOptionValue } = require('internal/options');
2020
// Do not eagerly grab .manifest, it may be in TDZ
21-
-const { sep, posix: { relative: relativePosixPath }, resolve } = require('path');
21+
-const { sep, posix: { relative: relativePosixPath }, resolve, join } = require('path');
2222
-const { URL, pathToFileURL, fileURLToPath, isURL, URLParse } = require('internal/url');
23-
+const { sep, posix: { relative: relativePosixPath }, toNamespacedPath, resolve } = require('path');
23+
+const { sep, posix: { relative: relativePosixPath }, toNamespacedPath, resolve, join } = require('path');
2424
+const { URL, pathToFileURL, fileURLToPath, isURL, URLParse, toPathIfFileURL } = require('internal/url');
2525
const { getCWDURL, setOwnProperty } = require('internal/util');
2626
const { canParse: URLCanParse } = internalBinding('url');
2727
-const { legacyMainResolve: FSLegacyMainResolve } = internalBinding('fs');
2828
const {
2929
ERR_INPUT_TYPE_NOT_ALLOWED,
3030
ERR_INVALID_ARG_TYPE,
31-
@@ -180,6 +179,11 @@ const legacyMainResolveExtensionsIndexes = {
31+
@@ -181,6 +180,11 @@ const legacyMainResolveExtensionsIndexes = {
3232
kResolvedByPackageAndNode: 9,
3333
};
3434

@@ -40,7 +40,7 @@ index 185793fa305edbeab299f743e0018f34b2aaa655..b44eab420fe1bbec7ce34bde2ca4974b
4040
/**
4141
* Legacy CommonJS main resolution:
4242
* 1. let M = pkg_url + (json main field)
43-
@@ -198,7 +202,7 @@ function legacyMainResolve(packageJSONUrl, packageConfig, base) {
43+
@@ -199,7 +203,7 @@ function legacyMainResolve(packageJSONUrl, packageConfig, base) {
4444

4545
const baseStringified = isURL(base) ? base.href : base;
4646

@@ -50,10 +50,10 @@ index 185793fa305edbeab299f743e0018f34b2aaa655..b44eab420fe1bbec7ce34bde2ca4974b
5050
const maybeMain = resolvedOption <= legacyMainResolveExtensionsIndexes.kResolvedByMainIndexNode ?
5151
packageConfig.main || './' : '';
5252
diff --git a/src/node_file.cc b/src/node_file.cc
53-
index c46adc83d8542ae28c84b41821204b4608b1f34f..d828e7e78f9b41e053c306b01a71ad3ad847aaf6 100644
53+
index 126913185e0cd3e0e78cb2fb92177c9a8d9de044..5be1b553e29dbcc78c4bee1b778587ede6bc60f8 100644
5454
--- a/src/node_file.cc
5555
+++ b/src/node_file.cc
56-
@@ -3648,13 +3648,25 @@ static void CpSyncCopyDir(const FunctionCallbackInfo<Value>& args) {
56+
@@ -3877,13 +3877,25 @@ static void CpSyncCopyDir(const FunctionCallbackInfo<Value>& args) {
5757
}
5858

5959
BindingData::FilePathIsFileReturnType BindingData::FilePathIsFile(
@@ -80,7 +80,7 @@ index c46adc83d8542ae28c84b41821204b4608b1f34f..d828e7e78f9b41e053c306b01a71ad3a
8080
uv_fs_t req;
8181

8282
int rc = uv_fs_stat(env->event_loop(), &req, file_path.c_str(), nullptr);
83-
@@ -3712,6 +3724,11 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
83+
@@ -3941,6 +3953,11 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
8484
std::optional<std::string> initial_file_path;
8585
std::string file_path;
8686

@@ -92,7 +92,7 @@ index c46adc83d8542ae28c84b41821204b4608b1f34f..d828e7e78f9b41e053c306b01a71ad3a
9292
if (args.Length() >= 2 && args[1]->IsString()) {
9393
auto package_config_main = Utf8Value(isolate, args[1]).ToString();
9494

95-
@@ -3732,7 +3749,7 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
95+
@@ -3961,7 +3978,7 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
9696
BufferValue buff_file_path(isolate, local_file_path);
9797
ToNamespacedPath(env, &buff_file_path);
9898

@@ -101,7 +101,7 @@ index c46adc83d8542ae28c84b41821204b4608b1f34f..d828e7e78f9b41e053c306b01a71ad3a
101101
case BindingData::FilePathIsFileReturnType::kIsFile:
102102
return args.GetReturnValue().Set(i);
103103
case BindingData::FilePathIsFileReturnType::kIsNotFile:
104-
@@ -3769,7 +3786,7 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
104+
@@ -3998,7 +4015,7 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
105105
BufferValue buff_file_path(isolate, local_file_path);
106106
ToNamespacedPath(env, &buff_file_path);
107107

@@ -111,10 +111,10 @@ index c46adc83d8542ae28c84b41821204b4608b1f34f..d828e7e78f9b41e053c306b01a71ad3a
111111
return args.GetReturnValue().Set(i);
112112
case BindingData::FilePathIsFileReturnType::kIsNotFile:
113113
diff --git a/src/node_file.h b/src/node_file.h
114-
index c276a873f8516a5c7c6efcf8a400e07f78f92620..fe314d2a2c0fa9e2b71e12ae83b915c4314986a0 100644
114+
index 17f3b4203c8eddc8d050243d9cc26ba2ee61ff86..f2ae912dbe28f6165befb07f34c1dbf2430afce6 100644
115115
--- a/src/node_file.h
116116
+++ b/src/node_file.h
117-
@@ -102,7 +102,8 @@ class BindingData : public SnapshotableObject {
117+
@@ -101,7 +101,8 @@ class BindingData : public SnapshotableObject {
118118
InternalFieldInfo* internal_field_info_ = nullptr;
119119

120120
static FilePathIsFileReturnType FilePathIsFile(Environment* env,

‎patches/node/fix_expose_the_built-in_electron_module_via_the_esm_loader.patch‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ index 94879761553e02746853eecf316d88da61ab4277..080d917129990f3593ece63ebcf65037
7676
}
7777
}
7878
diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js
79-
index b96999d80b6aaad4767b563bdbd0f9e2591d37e3..117f844efb7c6ac6f7bd9c1b0fd244443fb841f7 100644
79+
index e34e6f58538fc57791d8bc73bcc38cc6b9072d24..91cf127534088ad8c3b9b5effc828bf5c8e95110 100644
8080
--- a/lib/internal/modules/esm/loader.js
8181
+++ b/lib/internal/modules/esm/loader.js
82-
@@ -398,7 +398,7 @@ class ModuleLoader {
82+
@@ -402,7 +402,7 @@ class ModuleLoader {
8383
assert(wrap instanceof ModuleWrap, `Translator used for require(${url}) should not be async`);
8484

8585
const cjsModule = wrap[imported_cjs_symbol];
@@ -89,20 +89,21 @@ index b96999d80b6aaad4767b563bdbd0f9e2591d37e3..117f844efb7c6ac6f7bd9c1b0fd24444
8989
if (cjsModule?.[kIsExecuting]) {
9090
const parentFilename = urlToFilename(parentURL);
9191
diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js
92-
index b028f44f01388605e114130c138568f0bceea76d..185793fa305edbeab299f743e0018f34b2aaa655 100644
92+
index f6c8078228419eaca8fbed5a9d43d8b56cacffb0..beb3452ea134e4b408f8a3335a6cc65e97c8055a 100644
9393
--- a/lib/internal/modules/esm/resolve.js
9494
+++ b/lib/internal/modules/esm/resolve.js
95-
@@ -751,6 +751,9 @@ function packageImportsResolve(name, base, conditions) {
95+
@@ -752,6 +752,10 @@ function packageImportsResolve(name, base, conditions) {
9696
throw importNotDefined(name, packageJSONUrl, base);
9797
}
9898

9999
+const electronTypes = [
100100
+ 'electron', 'electron/main', 'electron/common', 'electron/renderer', 'electron/utility'
101101
+];
102-
102+
+
103103
/**
104104
* Resolves a package specifier to a URL.
105-
@@ -765,6 +768,11 @@ function packageResolve(specifier, base, conditions) {
105+
* @param {string} specifier - The package specifier to resolve.
106+
@@ -765,6 +769,11 @@ function packageResolve(specifier, base, conditions) {
106107
return new URL('node:' + specifier);
107108
}
108109

@@ -111,11 +112,11 @@ index b028f44f01388605e114130c138568f0bceea76d..185793fa305edbeab299f743e0018f34
111112
+ return new URL('electron:electron');
112113
+ }
113114
+
114-
const { packageJSONUrl, packageJSONPath, packageSubpath } = packageJsonReader.getPackageJSONURL(specifier, base);
115+
let packageJSONUrl, packageJSONPath, packageSubpath;
115116

116-
const packageConfig = packageJsonReader.read(packageJSONPath, { __proto__: null, specifier, base, isESM: true });
117+
if (hasPackageMap()) {
117118
diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js
118-
index 7fb265c01b8d06f15fb24d9096fabf74e02c2354..f3c3399b6891dd6766e6ecf8c892aa95d91c6ec3 100644
119+
index 54e15417addea96cdb6d70fd9e2b65caa0f1ae34..f54a8ca32a601e886b0cb09f0a9361a0c9c184ee 100644
119120
--- a/lib/internal/modules/esm/translators.js
120121
+++ b/lib/internal/modules/esm/translators.js
121122
@@ -217,7 +217,9 @@ function createCJSModuleWrap(url, translateContext, parentURL) {

0 commit comments

Comments
 (0)