Skip to content

fix(commonjs): handle global shorthand property correctly#1958

Open
kaigritun wants to merge 1 commit intorollup:masterfrom
kaigritun:fix/commonjs-global-shorthand
Open

fix(commonjs): handle global shorthand property correctly#1958
kaigritun wants to merge 1 commit intorollup:masterfrom
kaigritun:fix/commonjs-global-shorthand

Conversation

@kaigritun
Copy link

Problem

When global is used as a shorthand property in an object literal (e.g., {global}), the commonjs plugin replaces global with commonjsHelpers.commonjsGlobal, resulting in invalid syntax {commonjsHelpers.commonjsGlobal}.

This causes a parse error like:

Unexpected token \`.\`. Expected ... , *, (, [, :, , ?, = or an identifier

Root Cause

As identified in rollup/rollup#6242 by @TrickyPi, the CommonJS plugin transforms {global} into {commonjsHelpers.commonjsGlobal}, which is invalid syntax because you can't have a dotted expression as a shorthand property.

Solution

This fix follows the same pattern already used for the require shorthand case (see existing shorthand-require test): when detecting a shorthand property, prepend global: to convert it to a full property before replacing the value.

The transformation now correctly produces:

  • Before: {global}{commonjsHelpers.commonjsGlobal} (invalid syntax)
  • After: {global}{global: commonjsHelpers.commonjsGlobal} (valid syntax)

Changes

  1. Added shorthand property handling for the global identifier in transform-commonjs.js
  2. Added a test case in test/fixtures/function/shorthand-global/

Related Issues

Fixes rollup/rollup#6242

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
@kaigritun kaigritun requested a review from shellscape as a code owner February 8, 2026 23:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot build with cssstyle package (issue with object shorthand?)

1 participant