Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle __proto__ exports name in CJS/AMD/UMD #16015

Merged
merged 4 commits into from Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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__);
});