Skip to content

Commit 81e0c45

Browse files
committed
feat(compartment-mapper): support treating top-level return in CJS as a way to export from module
1 parent 5d350ad commit 81e0c45

File tree

5 files changed

+14
-6
lines changed

5 files changed

+14
-6
lines changed

packages/compartment-mapper/src/parse-cjs-shared-export-wrapper.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,9 @@ export const wrap = ({
184184

185185
freeze(require);
186186

187-
const afterExecute = () => {
188-
const finalExports = module.exports; // in case it's a getter, only call it once
187+
const afterExecute = returnValue => {
188+
const finalExports =
189+
returnValue !== undefined ? returnValue : module.exports; // in case it's a getter, only call it once
189190
const exportsHaveBeenOverwritten = finalExports !== originalExports;
190191
// Promotes keys from redefined module.export to top level namespace for import *
191192
// Note: We could do it less consistently but closer to how node does it if we iterated over exports detected by

packages/compartment-mapper/src/parse-cjs.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export const parseCjs = (
5555
});
5656

5757
// In CommonJS, the top-level `this` is the `module.exports` object.
58-
functor.call(
58+
const returnValue = functor.call(
5959
moduleExports,
6060
require,
6161
moduleExports,
@@ -64,7 +64,7 @@ export const parseCjs = (
6464
dirname,
6565
);
6666

67-
afterExecute();
67+
afterExecute(returnValue);
6868
};
6969

7070
return {

packages/compartment-mapper/src/parse-pre-cjs.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export const parsePreCjs = (
4848
});
4949

5050
// In CommonJS, the top-level `this` is the `module.exports` object.
51-
functor.call(
51+
const returnValue = functor.call(
5252
moduleExports,
5353
require,
5454
moduleExports,
@@ -57,7 +57,7 @@ export const parsePreCjs = (
5757
dirname,
5858
);
5959

60-
afterExecute();
60+
afterExecute(returnValue);
6161
};
6262

6363
return {

packages/compartment-mapper/test/fixtures-cjs-compat/node_modules/app/index.js

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compartment-mapper/test/fixtures-cjs-compat/node_modules/exports-shenanigans/exports-return.js

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)