Skip to content

Commit 8e33f8c

Browse files
naugturkriskowal
authored andcommitted
feat(compartment-mapper): a simple version of allowing exports override via define semantics with little new consequences
1 parent 433bfbb commit 8e33f8c

File tree

1 file changed

+2
-37
lines changed

1 file changed

+2
-37
lines changed

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

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

138-
let finalExports = originalExports;
139-
140-
// This might need some work on fidelity of the define semantics, but I feel bad about making it even more lines
141-
const moduleHandler = {
142-
get(target, prop) {
143-
if (prop === 'exports') {
144-
return finalExports;
145-
}
146-
return target[prop];
147-
},
148-
set(target, prop, value) {
149-
if (prop === 'exports') {
150-
finalExports = value;
151-
}
152-
return true;
153-
},
154-
defineProperty(target, prop, descriptor) {
155-
if (prop === 'exports') {
156-
if (descriptor.configurable === false) {
157-
// For non-configurable properties, we must define it on the target because ECMAScript spec says so
158-
defineProperty(target, prop, descriptor);
159-
}
160-
if (has(descriptor, 'value')) {
161-
finalExports = descriptor.value;
162-
return true;
163-
}
164-
if (has(descriptor, 'get')) {
165-
finalExports = descriptor.get();
166-
return true;
167-
}
168-
}
169-
// Seems like there are no reasonable other fields anyone would define https://github.com/search?q=%22Object.defineProperty%28module%2C%22&type=code
170-
return false;
171-
},
172-
};
173-
174-
const module = new Proxy({}, moduleHandler);
138+
const module = { exports: originalExports };
175139

176140
/** @param {string} importSpecifier */
177141
const require = importSpecifier => {
@@ -219,6 +183,7 @@ export const wrap = ({
219183
freeze(require);
220184

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

0 commit comments

Comments
 (0)