fix(vite): support commonjs module.exports assignment (#3247)

This commit is contained in:
Marvin Hagemeister
2025-08-25 08:48:34 +02:00
committed by GitHub
parent 9975d1698f
commit bf25d10a84
2 changed files with 67 additions and 28 deletions
@@ -14,12 +14,17 @@ function runTest(options: { input: string; expected: string }) {
}
const INIT = `var exports = {},
module = {
exports
};`;
module = {};
Object.defineProperty(module, "exports", {
get() {
return exports;
},
set(value) {
exports = value;
}
});`;
const DEFAULT_EXPORT =
`const _default = module.exports.default ?? exports.default ?? module.exports;`;
const DEFAULT_EXPORT = `const _default = exports.default ?? exports;`;
const DEFAULT_EXPORT_END = `export default _default;`;
Deno.test("commonjs - module.exports default", () => {
@@ -451,3 +456,18 @@ Object.assign(_default, _ns);
${DEFAULT_EXPORT_END}`,
});
});
Deno.test("commonjs - assign module.exports", () => {
runTest({
input: `module.exports = { foo: 1 };`,
expected: `${INIT}
module.exports = {
foo: 1
};
var _foo = exports.foo;
export { _foo as foo };
${DEFAULT_EXPORT}
_default.foo = _foo;
${DEFAULT_EXPORT_END}`,
});
});