fix(compartment-mapper): change how parse-cjs.js treats exports to align with behavior of cjs in Node.js; make execute synchronous
MODIFY
packages/compartment-mapper/src/parse-cjs.js
packages/compartment-mapper/src/parse-cjs.js
@@ -25,35 +25,42 @@ export const parseCjs = async (
* @param {Compartment} compartment
* @param {Record<string, string>} resolvedImports
*/
- const execute = async (moduleExports, compartment, resolvedImports) => {
+ const execute = (moduleExports, compartment, resolvedImports) => {
const functor = compartment.evaluate(
`(function (require, exports, module, __filename, __dirname) { ${source} //*/\n})\n//# sourceURL=${location}`,
);
-
+ let exportsReferenceCopy = moduleExports;
const module = freeze({
get exports() {
- return moduleExports;
+ return exportsReferenceCopy;
},
set exports(value) {
- moduleExports.default = value;
+ exportsReferenceCopy = value;
},
});
const require = freeze((/** @type {string} */ importSpecifier) => {
const namespace = compartment.importNow(resolvedImports[importSpecifier]);
if (namespace.default !== undefined) {
+ if (Object.keys(namespace).length > 1) {
+ return { ...namespace.default, ...namespace }; // this resembles Node's behavior more closely
+ } else {
return namespace.default;
}
+ }
return namespace;
});
functor(
require,
- moduleExports,
+ exportsReferenceCopy,
module,
location, // __filename
new URL('./', location).toString(), // __dirname
);
+ if (exportsReferenceCopy !== moduleExports) {
+ moduleExports.default = exportsReferenceCopy;
+ }
};
return {

fix(compartment-mapper): change how parse-cjs.js treats exports to align with behavior of cjs in Node.js; make execute synchronous