From aab9cc1dbe5a5f3128e5653ed678345c07f7b8be Mon Sep 17 00:00:00 2001 From: Kai Gritun Date: Sun, 8 Feb 2026 18:04:53 -0500 Subject: [PATCH] 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 --- packages/commonjs/src/transform-commonjs.js | 6 ++++++ .../test/fixtures/function/shorthand-global/_config.js | 3 +++ .../test/fixtures/function/shorthand-global/main.js | 7 +++++++ 3 files changed, 16 insertions(+) create mode 100644 packages/commonjs/test/fixtures/function/shorthand-global/_config.js create mode 100644 packages/commonjs/test/fixtures/function/shorthand-global/main.js diff --git a/packages/commonjs/src/transform-commonjs.js b/packages/commonjs/src/transform-commonjs.js index 80edc3e82..2d6ebdee0 100644 --- a/packages/commonjs/src/transform-commonjs.js +++ b/packages/commonjs/src/transform-commonjs.js @@ -322,6 +322,12 @@ export default async function transformCommonjs( case 'global': uses.global = true; if (!ignoreGlobal) { + if (isShorthandProperty(parent)) { + // as key and value are the same object, isReference regards + // both as references, so we need to skip now + skippedNodes.add(parent.value); + magicString.prependRight(node.start, 'global: '); + } replacedGlobal.push(node); } return; diff --git a/packages/commonjs/test/fixtures/function/shorthand-global/_config.js b/packages/commonjs/test/fixtures/function/shorthand-global/_config.js new file mode 100644 index 000000000..ec3064264 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/shorthand-global/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'correctly replaces shorthand `global` property in object' +}; diff --git a/packages/commonjs/test/fixtures/function/shorthand-global/main.js b/packages/commonjs/test/fixtures/function/shorthand-global/main.js new file mode 100644 index 000000000..691dbd199 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/shorthand-global/main.js @@ -0,0 +1,7 @@ +const HOST = { + global +}; + +module.exports = { + HOST +};