Skip to content

Commit 0ae2128

Browse files
committed
esm: fix module.exports export on CJS modules
nodejs/node#57366
1 parent afb8a5b commit 0ae2128

File tree

1 file changed

+21
-32
lines changed

1 file changed

+21
-32
lines changed

patches/node/fix_expose_the_built-in_electron_module_via_the_esm_loader.patch

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ 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 9519f947b8dfdc69808839948c9cb8434a0acf0e..23ce72d479f638c33edffcea7c35f5da6cab7cae 100644
9+
index 01584983c16..6afbbca6c95 100644
1010
--- a/lib/internal/modules/esm/get_format.js
1111
+++ b/lib/internal/modules/esm/get_format.js
1212
@@ -26,6 +26,7 @@ const protocolHandlers = {
@@ -18,10 +18,10 @@ index 9519f947b8dfdc69808839948c9cb8434a0acf0e..23ce72d479f638c33edffcea7c35f5da
1818

1919
/**
2020
diff --git a/lib/internal/modules/esm/load.js b/lib/internal/modules/esm/load.js
21-
index 307c35980551470b65128bebf5efe94df4e56892..3676a9852bcd42de0a3a380de117de58035f757b 100644
21+
index 8414d303c07..e8a2326e158 100644
2222
--- a/lib/internal/modules/esm/load.js
2323
+++ b/lib/internal/modules/esm/load.js
24-
@@ -81,7 +81,7 @@ function defaultLoad(url, context = kEmptyObject) {
24+
@@ -77,7 +77,7 @@ function defaultLoad(url, context = kEmptyObject) {
2525

2626
throwIfUnsupportedURLScheme(urlInstance);
2727

@@ -30,7 +30,7 @@ index 307c35980551470b65128bebf5efe94df4e56892..3676a9852bcd42de0a3a380de117de58
3030
source = null;
3131
format ??= 'builtin';
3232
} else if (format === 'addon') {
33-
@@ -97,7 +97,7 @@ function defaultLoad(url, context = kEmptyObject) {
33+
@@ -93,7 +93,7 @@ function defaultLoad(url, context = kEmptyObject) {
3434
// Now that we have the source for the module, run `defaultGetFormat` to detect its format.
3535
format = defaultGetFormat(urlInstance, context);
3636

@@ -39,7 +39,7 @@ index 307c35980551470b65128bebf5efe94df4e56892..3676a9852bcd42de0a3a380de117de58
3939
// For backward compatibility reasons, we need to discard the source in
4040
// order for the CJS loader to re-fetch it.
4141
source = null;
42-
@@ -145,7 +145,7 @@ function defaultLoadSync(url, context = kEmptyObject) {
42+
@@ -141,7 +141,7 @@ function defaultLoadSync(url, context = kEmptyObject) {
4343

4444
throwIfUnsupportedURLScheme(urlInstance, false);
4545

@@ -48,7 +48,7 @@ index 307c35980551470b65128bebf5efe94df4e56892..3676a9852bcd42de0a3a380de117de58
4848
source = null;
4949
} else if (source == null) {
5050
({ responseURL, source } = getSourceSync(urlInstance, context));
51-
@@ -178,12 +178,13 @@ function throwIfUnsupportedURLScheme(parsed) {
51+
@@ -174,12 +174,13 @@ function throwIfUnsupportedURLScheme(parsed) {
5252
protocol !== 'file:' &&
5353
protocol !== 'data:' &&
5454
protocol !== 'node:' &&
@@ -64,10 +64,10 @@ index 307c35980551470b65128bebf5efe94df4e56892..3676a9852bcd42de0a3a380de117de58
6464
}
6565
}
6666
diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js
67-
index 78985575beb3df7722ba90968e8f085574b5afdf..e032c016efe227c26364e81804ad183cd2c0d17f 100644
67+
index 300da51afe6..4c7e2dc8c00 100644
6868
--- a/lib/internal/modules/esm/loader.js
6969
+++ b/lib/internal/modules/esm/loader.js
70-
@@ -504,7 +504,7 @@ class ModuleLoader {
70+
@@ -522,7 +522,7 @@ class ModuleLoader {
7171
}
7272

7373
const cjsModule = wrap[imported_cjs_symbol];
@@ -77,10 +77,10 @@ index 78985575beb3df7722ba90968e8f085574b5afdf..e032c016efe227c26364e81804ad183c
7777
// Check if the ESM initiating import CJS is being required by the same CJS module.
7878
if (cjsModule?.[kIsExecuting]) {
7979
diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js
80-
index 859b6bfedac4bbee2df054f9ebca7cbaaed45f18..5aa946f66c71beff0b7a43c30638ab28a1a5dfc0 100644
80+
index c27ee4c6612..5f03cf14e94 100644
8181
--- a/lib/internal/modules/esm/resolve.js
8282
+++ b/lib/internal/modules/esm/resolve.js
83-
@@ -750,6 +750,9 @@ function packageImportsResolve(name, base, conditions) {
83+
@@ -751,6 +751,9 @@ function packageImportsResolve(name, base, conditions) {
8484
throw importNotDefined(name, packageJSONUrl, base);
8585
}
8686

@@ -90,7 +90,7 @@ index 859b6bfedac4bbee2df054f9ebca7cbaaed45f18..5aa946f66c71beff0b7a43c30638ab28
9090

9191
/**
9292
* Resolves a package specifier to a URL.
93-
@@ -764,6 +767,11 @@ function packageResolve(specifier, base, conditions) {
93+
@@ -765,6 +768,11 @@ function packageResolve(specifier, base, conditions) {
9494
return new URL('node:' + specifier);
9595
}
9696

@@ -103,30 +103,19 @@ index 859b6bfedac4bbee2df054f9ebca7cbaaed45f18..5aa946f66c71beff0b7a43c30638ab28
103103

104104
const packageConfig = packageJsonReader.read(packageJSONPath, { __proto__: null, specifier, base, isESM: true });
105105
diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js
106-
index 757f093becd112002f3422302f4c29bb464f1a6c..c8cea2117080930105b33e4e50586a2c88ef7352 100644
106+
index 147d96bda80..e50e91a7710 100644
107107
--- a/lib/internal/modules/esm/translators.js
108108
+++ b/lib/internal/modules/esm/translators.js
109-
@@ -188,7 +188,7 @@ function createCJSModuleWrap(url, source, isMain, format, loadCJS = loadCJSModul
110-
109+
@@ -215,7 +215,7 @@ function createCJSModuleWrap(url, source, isMain, format, loadCJS = loadCJSModul
111110
const { exportNames, module } = cjsPreparseModuleExports(filename, source, format);
112111
cjsCache.set(url, module);
113-
- const namesWithDefault = exportNames.has('default') ?
114-
+ const namesWithDefault = filename === 'electron' ? ['default', ...Object.keys(module.exports)] : exportNames.has('default') ?
115-
[...exportNames] : ['default', ...exportNames];
116112

117-
if (isMain) {
118-
@@ -210,8 +210,8 @@ function createCJSModuleWrap(url, source, isMain, format, loadCJS = loadCJSModul
119-
({ exports } = module);
120-
}
121-
for (const exportName of exportNames) {
122-
- if (!ObjectPrototypeHasOwnProperty(exports, exportName) ||
123-
- exportName === 'default') {
124-
+ if (exportName === 'default' ||
125-
+ !ObjectPrototypeHasOwnProperty(exports, exportName)) {
126-
continue;
127-
}
128-
// We might trigger a getter -> dont fail.
129-
@@ -286,6 +286,10 @@ translators.set('require-commonjs', (url, source, isMain) => {
113+
- const wrapperNames = [...exportNames];
114+
+ const wrapperNames = filename === 'electron' ? [...Object.keys(module.exports)] : [...exportNames];
115+
if (!exportNames.has('default')) {
116+
ArrayPrototypePush(wrapperNames, 'default');
117+
}
118+
@@ -319,6 +319,10 @@ translators.set('require-commonjs', (url, source, isMain) => {
130119
return createCJSModuleWrap(url, source, isMain, 'commonjs');
131120
});
132121

@@ -138,10 +127,10 @@ index 757f093becd112002f3422302f4c29bb464f1a6c..c8cea2117080930105b33e4e50586a2c
138127
// This translator function must be sync, as `require` is sync.
139128
translators.set('require-commonjs-typescript', (url, source, isMain) => {
140129
diff --git a/lib/internal/url.js b/lib/internal/url.js
141-
index d0c04be7c6ebc352d5958a987f3a4ba538e0d23a..00f9f3b73ed84c04ae712f6057b68737bd416333 100644
130+
index a1473fdac8a..0a87d997856 100644
142131
--- a/lib/internal/url.js
143132
+++ b/lib/internal/url.js
144-
@@ -1605,6 +1605,8 @@ function fileURLToPath(path, options = kEmptyObject) {
133+
@@ -1609,6 +1609,8 @@ function fileURLToPath(path, options = kEmptyObject) {
145134
path = new URL(path);
146135
else if (!isURL(path))
147136
throw new ERR_INVALID_ARG_TYPE('path', ['string', 'URL'], path);

0 commit comments

Comments
 (0)