Skip to content

Commit

Permalink
fix: handle __proto__ exports name in CJS/AMD/UMD (#16015)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicol貌 Ribaudo <hello@nicr.dev>
  • Loading branch information
magic-akari and nicolo-ribaudo committed Oct 13, 2023
1 parent a345a10 commit 418dd21
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 2 deletions.
10 changes: 8 additions & 2 deletions packages/babel-helper-module-transforms/src/index.ts
Expand Up @@ -557,6 +557,7 @@ function buildExportInitializationStatements(
const InitTemplate = {
computed: template.expression`EXPORTS["NAME"] = VALUE`,
default: template.expression`EXPORTS.NAME = VALUE`,
define: template.expression`Object.defineProperty(EXPORTS, "NAME", { enumerable:true, value: void 0, writable: true })["NAME"] = VALUE`,
};

function buildInitStatement(
Expand All @@ -572,11 +573,16 @@ function buildInitStatement(
NAME: exportName,
VALUE: acc,
};

if (exportName === "__proto__") {
return InitTemplate.define(params);
}

if (stringSpecifiers.has(exportName)) {
return InitTemplate.computed(params);
} else {
return InitTemplate.default(params);
}

return InitTemplate.default(params);
}, initExpr),
);
}
@@ -0,0 +1,7 @@
export const __proto__ = null;
export const a = 1;
export const _ = 2;

import { __proto__ as p } from "./input.js";

console.log(p);
@@ -0,0 +1,16 @@
define(["exports", "./input.js"], function (_exports, _input) {
"use strict";

Object.defineProperty(_exports, "__esModule", {
value: true
});
_exports.a = Object.defineProperty(_exports, "__proto__", {
enumerable: true,
value: void 0,
writable: true
})["__proto__"] = _exports._ = void 0;
const __proto__ = _exports.__proto__ = null;
const a = _exports.a = 1;
const _ = _exports._ = 2;
console.log(_input.__proto__);
});
@@ -0,0 +1 @@
export { __proto__ } from "xyz";
@@ -0,0 +1,12 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "__proto__", {
enumerable: true,
get: function () {
return _xyz.__proto__;
}
});
var _xyz = require("xyz");
@@ -0,0 +1,7 @@
export const __proto__ = null;
export const a = 1;
export const _ = 2;

import { __proto__ as p } from "./input.js";

console.log(p);
@@ -0,0 +1,15 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.a = Object.defineProperty(exports, "__proto__", {
enumerable: true,
value: void 0,
writable: true
})["__proto__"] = exports._ = void 0;
var _input = require("./input.js");
const __proto__ = exports.__proto__ = null;
const a = exports.a = 1;
const _ = exports._ = 2;
console.log(_input.__proto__);
@@ -0,0 +1,7 @@
export const __proto__ = null;
export const a = 1;
export const _ = 2;

import { __proto__ as p } from "./input.js";

console.log(p);
@@ -0,0 +1,28 @@
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["exports", "./input.js"], factory);
} else if (typeof exports !== "undefined") {
factory(exports, require("./input.js"));
} else {
var mod = {
exports: {}
};
factory(mod.exports, global.input);
global.input = mod.exports;
}
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _input) {
"use strict";

Object.defineProperty(_exports, "__esModule", {
value: true
});
_exports.a = Object.defineProperty(_exports, "__proto__", {
enumerable: true,
value: void 0,
writable: true
})["__proto__"] = _exports._ = void 0;
const __proto__ = _exports.__proto__ = null;
const a = _exports.a = 1;
const _ = _exports._ = 2;
console.log(_input.__proto__);
});

0 comments on commit 418dd21

Please sign in to comment.