diff --git a/packages/commonjs/src/ast-utils.js b/packages/commonjs/src/ast-utils.js index 042ef8f2e..3367482ca 100644 --- a/packages/commonjs/src/ast-utils.js +++ b/packages/commonjs/src/ast-utils.js @@ -113,6 +113,10 @@ export function isShorthandProperty(parent) { return parent && parent.type === 'Property' && parent.shorthand; } +export function isPropertyDefinitionKey(parent, node) { + return parent && parent.type === 'PropertyDefinition' && parent.key === node; +} + export function hasDefineEsmProperty(node) { return node.properties.some((property) => { if ( diff --git a/packages/commonjs/src/transform-commonjs.js b/packages/commonjs/src/transform-commonjs.js index 80edc3e82..2bc22ae88 100644 --- a/packages/commonjs/src/transform-commonjs.js +++ b/packages/commonjs/src/transform-commonjs.js @@ -13,7 +13,8 @@ import { isReference, isShorthandProperty, isTruthy, - KEY_COMPILED_ESM + KEY_COMPILED_ESM, + isPropertyDefinitionKey } from './ast-utils'; import { COMMONJS_REQUIRE_EXPORT, CREATE_COMMONJS_REQUIRE_EXPORT } from './dynamic-modules'; import { rewriteExportsAndGetExportsBlock, wrapCode } from './generate-exports'; @@ -295,7 +296,7 @@ export default async function transformCommonjs( if ( !isReference(node, parent) || scope.contains(name) || - (parent.type === 'PropertyDefinition' && parent.key === node) + isPropertyDefinitionKey(parent, node) ) return; switch (name) { @@ -322,6 +323,10 @@ export default async function transformCommonjs( case 'global': uses.global = true; if (!ignoreGlobal) { + if (isShorthandProperty(parent)) { + skippedNodes.add(parent.value); + magicString.prependRight(node.start, 'global: '); + } replacedGlobal.push(node); } return; @@ -442,6 +447,7 @@ export default async function transformCommonjs( for (const node of replacedGlobal) { magicString.overwrite(node.start, node.end, `${helpersName}.commonjsGlobal`, { + contentOnly: true, storeName: true }); } 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..ed7513577 --- /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..e68ac4a5e --- /dev/null +++ b/packages/commonjs/test/fixtures/function/shorthand-global/main.js @@ -0,0 +1,7 @@ +const HOST = { + global +}; + +module.exports = { + HOST +}; diff --git a/packages/commonjs/test/snapshots/function.js.md b/packages/commonjs/test/snapshots/function.js.md index 494b74a1b..b33c2e368 100644 --- a/packages/commonjs/test/snapshots/function.js.md +++ b/packages/commonjs/test/snapshots/function.js.md @@ -8609,6 +8609,42 @@ Generated by [AVA](https://avajs.dev). `, } +## shorthand-global + +> Snapshot 1 + + { + 'main.js': `'use strict';␊ + ␊ + var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊ + ␊ + function getDefaultExportFromCjs (x) {␊ + return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊ + }␊ + ␊ + var main$1;␊ + var hasRequiredMain;␊ + ␊ + function requireMain () {␊ + if (hasRequiredMain) return main$1;␊ + hasRequiredMain = 1;␊ + const HOST = {␊ + global: commonjsGlobal␊ + };␊ + ␊ + main$1 = {␊ + HOST␊ + };␊ + return main$1;␊ + }␊ + ␊ + var mainExports = requireMain();␊ + var main = /*@__PURE__*/getDefaultExportFromCjs(mainExports);␊ + ␊ + module.exports = main;␊ + `, + } + ## shorthand-require > Snapshot 1 diff --git a/packages/commonjs/test/snapshots/function.js.snap b/packages/commonjs/test/snapshots/function.js.snap index c05cb5899..b7dce85a6 100644 Binary files a/packages/commonjs/test/snapshots/function.js.snap and b/packages/commonjs/test/snapshots/function.js.snap differ