diff --git a/packages/commonjs/src/transform-commonjs.js b/packages/commonjs/src/transform-commonjs.js index 543336cd9..4601094e9 100644 --- a/packages/commonjs/src/transform-commonjs.js +++ b/packages/commonjs/src/transform-commonjs.js @@ -291,13 +291,16 @@ export default async function transformCommonjs( return; case 'Identifier': { const { name } = node; - if (!isReference(node, parent) || scope.contains(name)) return; - const isClassBodyPropertyKey = - parent.type === 'PropertyDefinition' && parent.key === node; + if ( + !isReference(node, parent) || + scope.contains(name) || + (parent.type === 'PropertyDefinition' && parent.key === node) + ) + return; switch (name) { case 'require': uses.require = true; - if (isNodeRequirePropertyAccess(parent) || isClassBodyPropertyKey) { + if (isNodeRequirePropertyAccess(parent)) { return; } if (!ignoreDynamicRequires) { @@ -318,14 +321,10 @@ export default async function transformCommonjs( case 'global': uses.global = true; if (!ignoreGlobal) { - if (isClassBodyPropertyKey && !parent.value) { - magicString.prependLeft(node.start, 'global = '); - } replacedGlobal.push(node); } return; case 'define': - if (isClassBodyPropertyKey) return; magicString.overwrite(node.start, node.end, 'undefined', { storeName: true }); diff --git a/packages/commonjs/test/snapshots/function.js.md b/packages/commonjs/test/snapshots/function.js.md index 5e6c0d462..2c31aab43 100644 --- a/packages/commonjs/test/snapshots/function.js.md +++ b/packages/commonjs/test/snapshots/function.js.md @@ -250,14 +250,12 @@ Generated by [AVA](https://avajs.dev). ␊ Object.defineProperty(exports, '__esModule', { value: true });␊ ␊ - var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊ - ␊ var main = {};␊ ␊ class Rollup {␊ define;␊ require;␊ - global = commonjsGlobal;␊ + global;␊ }␊ ␊ var Rollup_1 = main.Rollup = Rollup;␊ diff --git a/packages/commonjs/test/snapshots/function.js.snap b/packages/commonjs/test/snapshots/function.js.snap index 5361b8c84..97a1e03b0 100644 Binary files a/packages/commonjs/test/snapshots/function.js.snap and b/packages/commonjs/test/snapshots/function.js.snap differ