Skip to content

Commit

Permalink
fix(compartment-mapper): top-level "this" is "module.exports" (#2620)
Browse files Browse the repository at this point in the history
For CJS compat, the top-level `this` must be equal to `module.exports`
(which must be equal to `exports`).

This implementation changes the CJS parsers to call the wrapper
`functor` using the context of `module.exports`.

Fixed an unrelated "dynamic require" test so it does not use the CJS compat fixtures.
  • Loading branch information
boneskull authored Nov 5, 2024
1 parent 28999e8 commit a923f4e
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 8 deletions.
10 changes: 9 additions & 1 deletion packages/compartment-mapper/src/parse-cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,15 @@ export const parseCjs = (
readPowers,
});

functor(require, moduleExports, module, filename, dirname);
// In CommonJS, the top-level `this` is the `module.exports` object.
functor.call(
moduleExports,
require,
moduleExports,
module,
filename,
dirname,
);

afterExecute();
};
Expand Down
10 changes: 9 additions & 1 deletion packages/compartment-mapper/src/parse-pre-cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ export const parsePreCjs = (
readPowers,
});

functor(require, moduleExports, module, filename, dirname);
// In CommonJS, the top-level `this` is the `module.exports` object.
functor.call(
moduleExports,
require,
moduleExports,
module,
filename,
dirname,
);

afterExecute();
};
Expand Down
4 changes: 2 additions & 2 deletions packages/compartment-mapper/test/dynamic-require.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ test('sync module transforms work with dynamic require support', async t => {

test('sync module transforms work without dynamic require support', async t => {
const fixture = new URL(
'fixtures-cjs-compat/node_modules/app/index.js',
'fixtures-dynamic/node_modules/static-app/index.js',
import.meta.url,
).toString();

Expand All @@ -413,5 +413,5 @@ test('sync module transforms work without dynamic require support', async t => {
syncModuleTransforms,
});

t.true(transformCount === 29);
t.is(transformCount, 2);
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Generated by [AVA](https://avajs.dev).
at compartmentImportNow (file://.../ses/src/compartment.js:…)␊
at Compartment.importNow (file://.../ses/src/compartment.js:…)␊
at require (file://.../compartment-mapper/src/parse-cjs-shared-export-wrapper.js:…)␊
at eval (eval at <anonymous> (eval at makeEvaluate (file://.../ses/src/make-evaluate.js:…)), <anonymous>:…)␊
at Proxy.eval (eval at <anonymous> (eval at makeEvaluate (file://.../ses/src/make-evaluate.js:…)), <anonymous>:…)␊
at Object.execute (file://.../compartment-mapper/src/parse-pre-cjs.js:…)␊
at execute (file://.../ses/src/module-instance.js:…)␊
at compartmentImportNow (file://.../ses/src/compartment.js:…)␊
Expand All @@ -128,7 +128,7 @@ Generated by [AVA](https://avajs.dev).
at compartmentImportNow (file://.../ses/src/compartment.js:…)␊
at Compartment.importNow (file://.../ses/src/compartment.js:…)␊
at require (file://.../compartment-mapper/src/parse-cjs-shared-export-wrapper.js:…)␊
at eval (eval at <anonymous> (eval at makeEvaluate (file://.../ses/src/make-evaluate.js:…)), <anonymous>:…)␊
at Proxy.eval (eval at <anonymous> (eval at makeEvaluate (file://.../ses/src/make-evaluate.js:…)), <anonymous>:…)␊
at Object.execute (file://.../compartment-mapper/src/parse-pre-cjs.js:…)␊
at execute (file://.../ses/src/module-instance.js:…)␊
at compartmentImportNow (file://.../ses/src/compartment.js:…)␊
Expand All @@ -144,7 +144,7 @@ Generated by [AVA](https://avajs.dev).
at compartmentImportNow (file://.../ses/src/compartment.js:…)␊
at Compartment.importNow (file://.../ses/src/compartment.js:…)␊
at require (file://.../compartment-mapper/src/parse-cjs-shared-export-wrapper.js:…)␊
at eval (eval at <anonymous> (eval at makeEvaluate (file://.../ses/src/make-evaluate.js:…)), <anonymous>:…)␊
at Proxy.eval (eval at <anonymous> (eval at makeEvaluate (file://.../ses/src/make-evaluate.js:…)), <anonymous>:…)␊
at Object.execute (file://.../compartment-mapper/src/parse-pre-cjs.js:…)␊
at execute (file://.../ses/src/module-instance.js:…)␊
at compartmentImportNow (file://.../ses/src/compartment.js:…)␊
Expand All @@ -160,7 +160,7 @@ Generated by [AVA](https://avajs.dev).
at compartmentImportNow (file://.../ses/src/compartment.js:…)␊
at Compartment.importNow (file://.../ses/src/compartment.js:…)␊
at require (file://.../compartment-mapper/src/parse-cjs-shared-export-wrapper.js:…)␊
at eval (eval at <anonymous> (eval at makeEvaluate (file://.../ses/src/make-evaluate.js:…)), <anonymous>:…)␊
at Proxy.eval (eval at <anonymous> (eval at makeEvaluate (file://.../ses/src/make-evaluate.js:…)), <anonymous>:…)␊
at Object.execute (file://.../compartment-mapper/src/parse-pre-cjs.js:…)␊
at execute (file://.../ses/src/module-instance.js:…)␊
at compartmentImportNow (file://.../ses/src/compartment.js:…)␊
Expand Down
Binary file not shown.

0 comments on commit a923f4e

Please sign in to comment.