Skip to content

Commit 52a6bf4

Browse files
authored
feat(compartment-mapper): CommonJS - Allow overwriting exports field with define semantics (#2731)
2 parents 01529a5 + 59e1f68 commit 52a6bf4

File tree

4 files changed

+14
-10
lines changed

4 files changed

+14
-10
lines changed

packages/compartment-mapper/NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ User-visible changes to `@endo/compartment-mapper`:
22

33
# Next release
44

5+
- Accommodates CommonJS modules that use `defineProperty` on `exports`.
6+
57
- Divides the role of `makeBundle` into `makeScript` and `makeFunctor`.
68
The new `makeScript` replaces `makeBundle` without breaking changes,
79
producing a JavaScript string that is suitable as a `<script>` tag in a web

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

+2-10
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,7 @@ export const wrap = ({
135135
},
136136
});
137137

138-
let finalExports = originalExports;
139-
140-
const module = freeze({
141-
get exports() {
142-
return finalExports;
143-
},
144-
set exports(value) {
145-
finalExports = value;
146-
},
147-
});
138+
const module = { exports: originalExports };
148139

149140
/** @param {string} importSpecifier */
150141
const require = importSpecifier => {
@@ -192,6 +183,7 @@ export const wrap = ({
192183
freeze(require);
193184

194185
const afterExecute = () => {
186+
const finalExports = module.exports; // in case it's a getter, only call it once
195187
const exportsHaveBeenOverwritten = finalExports !== originalExports;
196188
// Promotes keys from redefined module.export to top level namespace for import *
197189
// Note: We could do it less consistently but closer to how node does it if we iterated over exports detected by

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/define-exports.js

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

0 commit comments

Comments
 (0)