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 +};