Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/commonjs/src/ast-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
10 changes: 8 additions & 2 deletions packages/commonjs/src/transform-commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
description: 'correctly replaces shorthand `global` property in object'
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const HOST = {
global
};

module.exports = {
HOST
};
36 changes: 36 additions & 0 deletions packages/commonjs/test/snapshots/function.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file modified packages/commonjs/test/snapshots/function.js.snap
Binary file not shown.
Loading