Skip to content

Commit 077c8e4

Browse files
ckerrcodebytere
authored andcommitted
update fix_expose_the_built-in_electron_module_via_the_esm_loader.patch
Xref: nodejs/node#50096 Xref: nodejs/node#50314 in lib/internal/modules/esm/load.js, update the code that checks for `format === 'electron'`. I'd like 👀 on this Xref: nodejs/node#49657 add braces in lib/internal/modules/esm/translators.js to sync with upstream
1 parent 3494aa6 commit 077c8e4

1 file changed

Lines changed: 28 additions & 31 deletions

File tree

patches/node/fix_expose_the_built-in_electron_module_via_the_esm_loader.patch

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ Subject: fix: expose the built-in electron module via the ESM loader
66
This allows usage of `import { app } from 'electron'` and `import('electron')` natively in the browser + non-sandboxed renderer
77

88
diff --git a/lib/internal/modules/esm/get_format.js b/lib/internal/modules/esm/get_format.js
9-
index 4ac9c011d153f4cb39cb7d4f46de0f8e65f70a56..f8462b69368aa7987e1e8ac8cd73e4ff2d921c51 100644
9+
index 1931688e85..afccc24392 100644
1010
--- a/lib/internal/modules/esm/get_format.js
1111
+++ b/lib/internal/modules/esm/get_format.js
12-
@@ -27,6 +27,7 @@ const protocolHandlers = {
12+
@@ -30,6 +30,7 @@ const protocolHandlers = {
1313
'http:': getHttpProtocolModuleFormat,
1414
'https:': getHttpProtocolModuleFormat,
1515
'node:'() { return 'builtin'; },
@@ -18,28 +18,27 @@ index 4ac9c011d153f4cb39cb7d4f46de0f8e65f70a56..f8462b69368aa7987e1e8ac8cd73e4ff
1818

1919
/**
2020
diff --git a/lib/internal/modules/esm/load.js b/lib/internal/modules/esm/load.js
21-
index d064296d11c463616111d28a32b7ad3f6a72bebd..14d5236d0ead946acf9a2f63bf979e7193bfbf56 100644
21+
index 6f9b73abd8..f108b567a9 100644
2222
--- a/lib/internal/modules/esm/load.js
2323
+++ b/lib/internal/modules/esm/load.js
24-
@@ -123,7 +123,8 @@ async function defaultLoad(url, context = kEmptyObject) {
24+
@@ -140,7 +140,7 @@ async function defaultLoad(url, context = kEmptyObject) {
25+
// Now that we have the source for the module, run `defaultGetFormat` again in case we detect ESM syntax.
26+
format ??= await defaultGetFormat(urlInstance, contextToPass);
2527

26-
if (
27-
format === 'builtin' ||
28-
- format === 'commonjs'
29-
+ format === 'commonjs' ||
30-
+ format === 'electron'
31-
) {
32-
source = null;
33-
} else if (source == null) {
34-
@@ -201,6 +202,7 @@ function throwIfUnsupportedURLScheme(parsed, experimentalNetworkImports) {
28+
- if (format === 'commonjs' && contextToPass !== context) {
29+
+ if ((format === 'commonjs' || format === 'electron') && contextToPass !== context) {
30+
// For backward compatibility reasons, we need to discard the source in
31+
// order for the CJS loader to re-fetch it.
32+
source = null;
33+
@@ -221,6 +221,7 @@ function throwIfUnsupportedURLScheme(parsed, experimentalNetworkImports) {
3534
protocol !== 'file:' &&
3635
protocol !== 'data:' &&
3736
protocol !== 'node:' &&
3837
+ protocol !== 'electron:' &&
3938
(
4039
!experimentalNetworkImports ||
4140
(
42-
@@ -209,7 +211,7 @@ function throwIfUnsupportedURLScheme(parsed, experimentalNetworkImports) {
41+
@@ -229,7 +230,7 @@ function throwIfUnsupportedURLScheme(parsed, experimentalNetworkImports) {
4342
)
4443
)
4544
) {
@@ -49,19 +48,19 @@ index d064296d11c463616111d28a32b7ad3f6a72bebd..14d5236d0ead946acf9a2f63bf979e71
4948
ArrayPrototypePush(schemes, 'https', 'http');
5049
}
5150
diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js
52-
index acb5ddca8af3d2495ce27d6426b28e6ff73a8f33..acfcc1f1f92548117ded857fa8d4dd140e07ab2b 100644
51+
index 58e7df07ca..2d077cc182 100644
5352
--- a/lib/internal/modules/esm/resolve.js
5453
+++ b/lib/internal/modules/esm/resolve.js
55-
@@ -725,6 +725,8 @@ function parsePackageName(specifier, base) {
56-
return { packageName, packageSubpath, isScoped };
54+
@@ -731,6 +731,8 @@ function packageImportsResolve(name, base, conditions) {
55+
throw importNotDefined(name, packageJSONUrl, base);
5756
}
5857

5958
+const electronTypes = ['electron', 'electron/main', 'electron/common', 'electron/renderer'];
6059
+
6160
/**
62-
* @param {string} specifier
63-
* @param {string | URL | undefined} base
64-
@@ -736,6 +738,11 @@ function packageResolve(specifier, base, conditions) {
61+
* Returns the package type for a given URL.
62+
* @param {URL} url - The URL to get the package type for.
63+
@@ -791,6 +793,11 @@ function packageResolve(specifier, base, conditions) {
6564
return new url("https://github.com/electron/electron/commit/'node:'%20+%20specifier");
6665
}
6766

@@ -74,10 +73,10 @@ index acb5ddca8af3d2495ce27d6426b28e6ff73a8f33..acfcc1f1f92548117ded857fa8d4dd14
7473
parsePackageName(specifier, base);
7574

7675
diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js
77-
index b143cd0ad34d0e039db3e7493be00d923cc68b2d..178f362b2869173d51b2e97a8d35a16ab4ed4962 100644
76+
index 7a62615cfe..bda102b266 100644
7877
--- a/lib/internal/modules/esm/translators.js
7978
+++ b/lib/internal/modules/esm/translators.js
80-
@@ -220,7 +220,7 @@ function createCJSModuleWrap(url, source, isMain, loadCJS = loadCJSModule) {
79+
@@ -280,7 +280,7 @@ function createCJSModuleWrap(url, source, isMain, loadCJS = loadCJSModule) {
8180

8281
const { exportNames, module } = cjsPreparseModuleExports(filename, source);
8382
cjsCache.set(url, module);
@@ -86,20 +85,18 @@ index b143cd0ad34d0e039db3e7493be00d923cc68b2d..178f362b2869173d51b2e97a8d35a16a
8685
[...exportNames] : ['default', ...exportNames];
8786

8887
if (isMain) {
89-
@@ -241,9 +241,9 @@ function createCJSModuleWrap(url, source, isMain, loadCJS = loadCJSModule) {
90-
} else {
88+
@@ -302,8 +302,8 @@ function createCJSModuleWrap(url, source, isMain, loadCJS = loadCJSModule) {
9189
({ exports } = module);
9290
}
93-
- for (const exportName of exportNames) {
91+
for (const exportName of exportNames) {
9492
- if (!ObjectPrototypeHasOwnProperty(exports, exportName) ||
95-
- exportName === 'default')
96-
+ for (const exportName of namesWithDefault) {
93+
- exportName === 'default') {
9794
+ if (exportName === 'default' ||
98-
+ !ObjectPrototypeHasOwnProperty(exports, exportName))
95+
+ !ObjectPrototypeHasOwnProperty(exports, exportName)) {
9996
continue;
97+
}
10098
// We might trigger a getter -> dont fail.
101-
let value;
102-
@@ -267,6 +267,10 @@ translators.set('require-commonjs', (url, source, isMain) => {
99+
@@ -328,6 +328,10 @@ translators.set('require-commonjs', (url, source, isMain) => {
103100
return createCJSModuleWrap(url, source);
104101
});
105102

@@ -111,7 +108,7 @@ index b143cd0ad34d0e039db3e7493be00d923cc68b2d..178f362b2869173d51b2e97a8d35a16a
111108
// or as the initial entry point when the ESM loader handles a CommonJS entry.
112109
translators.set('commonjs', async function commonjsStrategy(url, source,
113110
diff --git a/lib/internal/url.js b/lib/internal/url.js
114-
index 8d5926e8fcb9df031e37698588075a34a80aea6c..6df0b68c8d35445d5f02475678484a6d5157762e 100644
111+
index a58f96aee7..ae618cdeac 100644
115112
--- a/lib/internal/url.js
116113
+++ b/lib/internal/url.js
117114
@@ -1389,6 +1389,8 @@ function fileURLToPath(path) {

0 commit comments

Comments
 (0)