Skip to content

Commit

Permalink
fix(compartment-mapper): Support ESM imports defined property from CJ…
Browse files Browse the repository at this point in the history
…S from set property of CJS
  • Loading branch information
kriskowal committed Jul 17, 2024
1 parent c7a80fd commit bfd51ee
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ export const wrap = ({
}
};

const redefined = new Set();

const originalExports = new Proxy(moduleEnvironmentRecord.default, {
set(_target, prop, value) {
promoteToNamedExport(prop, value);
Expand All @@ -121,6 +123,9 @@ export const wrap = ({
// descriptor.get in the trap will cause an error.
// Object.defineProperty is used instead of Reflect.defineProperty for better error messages.
defineProperty(target, prop, descriptor);
// Make a note to propagate the value from the getter to the module
// environment record when the module finishes executing.
redefined.add(prop);
return true;
},
});
Expand Down Expand Up @@ -176,8 +181,13 @@ export const wrap = ({
if (exportsHaveBeenOverwritten) {
moduleEnvironmentRecord.default = finalExports;
for (const prop of keys(moduleEnvironmentRecord.default || {})) {
if (prop !== 'default')
if (prop !== 'default') {
moduleEnvironmentRecord[prop] = moduleEnvironmentRecord.default[prop];
}
}
} else {
for (const prop of redefined) {
moduleEnvironmentRecord[prop] = moduleEnvironmentRecord.default[prop];
}
}
};
Expand Down
23 changes: 23 additions & 0 deletions packages/compartment-mapper/test/esm-imports-cjs-define.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* eslint-disable no-underscore-dangle */
// import "./ses-lockdown.js";
import 'ses';
import test from 'ava';

import { scaffold } from './scaffold.js';

const fixture = new URL(
'fixtures-esm-imports-cjs-define/0.mjs',
import.meta.url,
).toString();

const assertFixture = t => t.pass();

const fixtureAssertionCount = 1;

scaffold(
'fixtures-esm-imports-cjs-define',
test,
fixture,
assertFixture,
fixtureAssertionCount,
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { meaning } from './1.cjs';
if (meaning !== 42) {
throw new Error('Fail');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const universe = require('./2.cjs');
Object.defineProperty(exports, '__esModule', { value: true });
Object.defineProperty(exports, 'meaning', {
enumerable: true,
get() {
return universe.meaning;
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Object.defineProperty(exports, '__esModule', { value: true });
exports.meaning = 42;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "app",
"version": "1.0.0",
"main": "./0.mjs",
"type": "commonjs",
"scripts": {
"preinstall": "echo DO NOT INSTALL TEST FIXTURES; exit -1"
}
}

0 comments on commit bfd51ee

Please sign in to comment.