Skip to content

Commit aab9cc1

Browse files
author
Kai Gritun
committed
fix(commonjs): handle global shorthand property correctly
When `global` is used as a shorthand property in an object literal (e.g., `{global}`), the plugin was replacing `global` with `commonjsHelpers.commonjsGlobal`, resulting in invalid syntax `{commonjsHelpers.commonjsGlobal}`. This fix follows the same pattern used for the `require` shorthand case: when detecting a shorthand property, prepend `global: ` to convert it to a full property before replacing the value. Fixes rollup/rollup#6242
1 parent c8e78c8 commit aab9cc1

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

packages/commonjs/src/transform-commonjs.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,12 @@ export default async function transformCommonjs(
322322
case 'global':
323323
uses.global = true;
324324
if (!ignoreGlobal) {
325+
if (isShorthandProperty(parent)) {
326+
// as key and value are the same object, isReference regards
327+
// both as references, so we need to skip now
328+
skippedNodes.add(parent.value);
329+
magicString.prependRight(node.start, 'global: ');
330+
}
325331
replacedGlobal.push(node);
326332
}
327333
return;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
description: 'correctly replaces shorthand `global` property in object'
3+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const HOST = {
2+
global
3+
};
4+
5+
module.exports = {
6+
HOST
7+
};

0 commit comments

Comments
 (0)