Skip to content

Commit

Permalink
feat: Export __proto__ outside the forEach
Browse files Browse the repository at this point in the history
  • Loading branch information
TrickyPi committed Feb 6, 2024
1 parent 28457fa commit e417abe
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
16 changes: 11 additions & 5 deletions src/finalisers/shared/getExportBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ export function getExportBlock(
for (const specifier of reexports) {
if (specifier.reexported === '*') {
if (exportBlock) exportBlock += n;
if (!specifier.needsLiveBinding) {
const protoString = "'__proto__'";
exportBlock +=
`Object.prototype.hasOwnProperty.call(${name},${_}${protoString})${_}&&${n}` +
`${t}!Object.prototype.hasOwnProperty.call(exports,${_}${protoString})${_}&&${n}` +
`${t}Object.defineProperty(exports,${_}${protoString},${_}{${n}` +
`${t}${t}enumerable:${_}true,${n}` +
`${t}${t}value:${_}${name}[${protoString}]${n}` +
`${t}});${n}${n}`;
}
const copyPropertyIfNecessary = `{${n}${t}if${_}(k${_}!==${_}'default'${_}&&${_}!Object.prototype.hasOwnProperty.call(exports,${_}k))${_}${getDefineProperty(
name,
specifier.needsLiveBinding,
Expand Down Expand Up @@ -251,9 +261,5 @@ const getDefineProperty = (
`${t}${t}get:${_}${left}${name}[k]${right}${n}${t}})`
);
}
return (
`k${_}===${_}'__proto__'${_}?${_}Object.defineProperty(exports,${_}k,${_}{${n}` +
`${t}${t}enumerable:${_}true,${n}` +
`${t}${t}value:${_}${name}[k]${n}${t}})${_}:${_}exports[k]${_}=${_}${name}[k]`
);
return `exports[k]${_}=${_}${name}[k]`;
};
6 changes: 4 additions & 2 deletions test/form/samples/cjs-transpiler-re-exports/_expected/cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

var foo_js = require('./foo.js');

!Object.prototype.hasOwnProperty.call(exports, '__proto__') &&
Object.prototype.hasOwnProperty.call(foo_js, '__proto__') &&


Object.prototype.hasOwnProperty.call(foo_js, '__proto__') &&
!Object.prototype.hasOwnProperty.call(exports, '__proto__') &&
Object.defineProperty(exports, '__proto__', {
enumerable: true,
value: foo_js['__proto__']
Expand Down

0 comments on commit e417abe

Please sign in to comment.